forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_http_client.c
2270 lines (2028 loc) · 71 KB
/
swoole_http_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Fang <[email protected]> |
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#include "swoole_http_client.h"
typedef struct
{
zval *onConnect;
zval *onError;
zval *onClose;
zval *onMessage;
zval *onResponse;
zval _object;
zval _request_body;
zval _request_header;
zval _request_upload_files;
zval _download_file;
zval _cookies;
zval _onConnect;
zval _onError;
zval _onClose;
zval _onMessage;
zval *cookies;
zval *request_header;
zval *request_body;
zval *request_upload_files;
zval *download_file;
off_t download_offset;
char *request_method;
int callback_index;
uint8_t error_flag;
uint8_t shutdown;
#ifdef SW_COROUTINE
zend_bool defer; //0 normal 1 wait for receive
zend_bool defer_result;//0
zend_bool defer_chunk_status;// 0 1 now use rango http->complete
zend_bool send_yield;
http_client_defer_state defer_status;
long cid;
/**
* for websocket
*/
swLinkedList *message_queue;
#endif
} http_client_property;
typedef struct
{
swClient *cli;
char *host;
size_t host_len;
long port;
#ifdef SW_USE_OPENSSL
uint8_t ssl;
#endif
double connect_timeout;
double timeout;
char* uri;
size_t uri_len;
swTimer_node *timer;
char *tmp_header_field_name;
int tmp_header_field_name_len;
#ifdef SW_HAVE_ZLIB
z_stream gzip_stream;
swString *gzip_buffer;
#endif
/**
* download page
*/
int file_fd;
swoole_http_parser parser;
zval _object;
zval *object;
swString *body;
uint8_t state; //0 wait 1 ready 2 busy
uint8_t keep_alive; //0 no 1 keep
uint8_t upgrade; //if upgrade successfully
uint8_t gzip;
uint8_t chunked; //Transfer-Encoding: chunked
uint8_t completed;
uint8_t websocket_mask;
uint8_t download; //save http response to file
uint8_t header_completed;
int8_t method;
} http_client;
swString *http_client_buffer;
static void http_client_onReceive(swClient *cli, char *data, uint32_t length);
static void http_client_onConnect(swClient *cli);
static void http_client_onClose(swClient *cli);
static void http_client_onError(swClient *cli);
static void http_client_onRequestTimeout(swTimer *timer, swTimer_node *tnode);
static void http_client_onResponseException();
static int http_client_onMessage(swConnection *conn, char *data, uint32_t length);
static int http_client_send_http_request(zval *zobject);
static int http_client_execute(zval *zobject, char *uri, size_t uri_len, zval *callback);
#ifdef SW_HAVE_ZLIB
BEGIN_EXTERN_C()
static void http_init_gzip_stream(http_client*);
extern int http_response_uncompress(z_stream *stream, swString *buffer, char *body, int length);
END_EXTERN_C()
#endif
static void http_client_clear_response_properties(zval *zobject);
static int http_client_parser_on_header_field(swoole_http_parser *parser, const char *at, size_t length);
static int http_client_parser_on_header_value(swoole_http_parser *parser, const char *at, size_t length);
static int http_client_parser_on_body(swoole_http_parser *parser, const char *at, size_t length);
static int http_client_parser_on_headers_complete(swoole_http_parser *parser);
static int http_client_parser_on_message_complete(swoole_http_parser *parser);
static http_client* http_client_create(zval *zobject);
static void http_client_clear(http_client *http);
static int http_client_check_keep(http_client *http);
static void http_client_reset(http_client *http);
static uint8_t http_client_free(zval *zobject);
static const swoole_http_parser_settings http_parser_settings =
{
NULL,
NULL,
NULL,
NULL,
NULL,
http_client_parser_on_header_field,
http_client_parser_on_header_value,
http_client_parser_on_headers_complete,
http_client_parser_on_body,
http_client_parser_on_message_complete
};
static zend_class_entry swoole_http_client_ce;
static zend_class_entry *swoole_http_client_ce_ptr;
static zend_object_handlers swoole_http_client_handlers;
static PHP_METHOD(swoole_http_client, __construct);
static PHP_METHOD(swoole_http_client, __destruct);
static PHP_METHOD(swoole_http_client, set);
static PHP_METHOD(swoole_http_client, setMethod);
static PHP_METHOD(swoole_http_client, setHeaders);
static PHP_METHOD(swoole_http_client, setCookies);
static PHP_METHOD(swoole_http_client, setData);
static PHP_METHOD(swoole_http_client, addFile);
static PHP_METHOD(swoole_http_client, execute);
static PHP_METHOD(swoole_http_client, push);
static PHP_METHOD(swoole_http_client, isConnected);
static PHP_METHOD(swoole_http_client, close);
static PHP_METHOD(swoole_http_client, on);
static PHP_METHOD(swoole_http_client, get);
static PHP_METHOD(swoole_http_client, post);
static PHP_METHOD(swoole_http_client, upgrade);
static PHP_METHOD(swoole_http_client, download);
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_construct, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, ssl)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_set, 0, 0, 1)
ZEND_ARG_ARRAY_INFO(0, settings, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_setMethod, 0, 0, 1)
ZEND_ARG_INFO(0, method)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_setHeaders, 0, 0, 1)
ZEND_ARG_ARRAY_INFO(0, headers, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_setCookies, 0, 0, 1)
ZEND_ARG_ARRAY_INFO(0, cookies, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_setData, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_addFile, 0, 0, 2)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, offset)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_execute, 0, 0, 2)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_get, 0, 0, 2)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_post, 0, 0, 3)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_download, 0, 0, 3)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, file)
ZEND_ARG_INFO(0, callback)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_upgrade, 0, 0, 2)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_push, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, opcode)
ZEND_ARG_INFO(0, finish)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_client_on, 0, 0, 2)
ZEND_ARG_INFO(0, event_name)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
static const zend_function_entry swoole_http_client_methods[] =
{
PHP_ME(swoole_http_client, __construct, arginfo_swoole_http_client_construct, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, __destruct, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, set, arginfo_swoole_http_client_set, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, setMethod, arginfo_swoole_http_client_setMethod, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, setHeaders, arginfo_swoole_http_client_setHeaders, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, setCookies, arginfo_swoole_http_client_setCookies, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, setData, arginfo_swoole_http_client_setData, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, addFile, arginfo_swoole_http_client_addFile, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, execute, arginfo_swoole_http_client_execute, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, push, arginfo_swoole_http_client_push, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, get, arginfo_swoole_http_client_get, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, post, arginfo_swoole_http_client_post, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, upgrade, arginfo_swoole_http_client_upgrade, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, download, arginfo_swoole_http_client_download, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, isConnected, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, close, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, on, arginfo_swoole_http_client_on, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static void http_client_clear_response_properties(zval *zobject)
{
zval *zattr;
zend_class_entry *ce = Z_OBJCE_P(zobject);
zend_update_property_long(ce, zobject, ZEND_STRL("errCode"), 0);
zend_update_property_string(ce, zobject, ZEND_STRL("errMsg"), "");
zend_update_property_long(ce, zobject, ZEND_STRL("statusCode"), 0);
zattr = sw_zend_read_property(ce, zobject, ZEND_STRL("headers"), 1);
if (Z_TYPE_P(zattr) == IS_ARRAY)
{
zend_hash_clean(Z_ARRVAL_P(zattr));
}
zattr = sw_zend_read_property(ce, zobject, ZEND_STRL("set_cookie_headers"), 1);
if (Z_TYPE_P(zattr) == IS_ARRAY)
{
zend_hash_clean(Z_ARRVAL_P(zattr));
}
zend_update_property_string(ce, zobject, ZEND_STRL("body"), "");
}
static int http_client_execute(zval *zobject, char *uri, size_t uri_len, zval *callback)
{
if (uri_len <= 0)
{
swoole_php_fatal_error(E_WARNING, "path is empty.");
return SW_ERR;
}
char *func_name = NULL;
if (!sw_zend_is_callable(callback, 0, &func_name))
{
swoole_php_fatal_error(E_WARNING, "function '%s' is not callable", func_name);
efree(func_name);
return SW_ERR;
}
efree(func_name);
http_client_property *hcc = swoole_get_property(zobject, 0);
// when new request, clear all properties about the last response
http_client_clear_response_properties(zobject);
hcc->error_flag = 0;
http_client *http = swoole_get_object(zobject);
//http is not null when keeping alive
if (http)
{
//http not ready
if (http->state != HTTP_CLIENT_STATE_READY)
{
//swWarn("fd=%d, state=%d, active=%d, keep_alive=%d", http->cli->socket->fd, http->state, http->cli->socket->active, http->keep_alive);
swoole_php_fatal_error(E_WARNING, "Operation now in progress phase %d.", http->state);
return SW_ERR;
}
else if (!http->cli->socket->active)
{
swoole_php_fatal_error(E_WARNING, "connection#%d is closed.", http->cli->socket->fd);
return SW_ERR;
}
}
else
{
php_swoole_check_reactor();
http = http_client_create(zobject);
}
if (http == NULL)
{
return SW_ERR;
}
if (http->body == NULL)
{
http->body = swString_new(SW_HTTP_RESPONSE_INIT_SIZE);
if (http->body == NULL)
{
swoole_php_fatal_error(E_ERROR, "[1] swString_new(%d) failed.", SW_HTTP_RESPONSE_INIT_SIZE);
return SW_ERR;
}
}
else
{
swString_clear(http->body);
}
if (http->uri)
{
efree(http->uri);
}
http->uri = estrdup(uri);
http->uri_len = uri_len;
if (callback == NULL || ZVAL_IS_NULL(callback))
{
swoole_php_fatal_error(E_WARNING, "response callback is not set.");
}
Z_TRY_ADDREF_P(callback);
hcc->onResponse = sw_zval_dup(callback);
/**
* download response body
*/
if (hcc->download_file)
{
int fd = open(Z_STRVAL_P(hcc->download_file), O_CREAT | O_WRONLY, 0664);
if (fd < 0)
{
swSysError("open(%s, O_CREAT | O_WRONLY) failed.", Z_STRVAL_P(hcc->download_file));
return SW_ERR;
}
if (hcc->download_offset == 0)
{
if (ftruncate(fd, 0) < 0)
{
swSysError("ftruncate(%s) failed.", Z_STRVAL_P(hcc->download_file));
close(fd);
return SW_ERR;
}
}
else
{
if (lseek(fd, hcc->download_offset, SEEK_SET) < 0)
{
swSysError("fseek(%s, %jd) failed.", Z_STRVAL_P(hcc->download_file), (intmax_t) hcc->download_offset);
close(fd);
return SW_ERR;
}
}
http->download = 1;
http->file_fd = fd;
hcc->download_file = NULL;
}
//if connection exists
if (http->cli)
{
return http_client_send_http_request(zobject) < 0 ? SW_ERR : SW_OK;
}
swClient *cli = php_swoole_client_new(zobject, http->host, http->host_len, http->port);
if (cli == NULL)
{
return SW_ERR;
}
http->cli = cli;
zval *ztmp;
HashTable *vht;
zval *zset = sw_zend_read_property(swoole_http_client_ce_ptr, zobject, ZEND_STRL("setting"), 1);
if (zset && ZVAL_IS_ARRAY(zset))
{
vht = Z_ARRVAL_P(zset);
/**
* timeout
*/
if (php_swoole_array_get_value(vht, "timeout", ztmp))
{
convert_to_double(ztmp);
http->timeout = (double) Z_DVAL_P(ztmp);
}
/**
* keep_alive
*/
if (php_swoole_array_get_value(vht, "keep_alive", ztmp))
{
convert_to_boolean(ztmp);
http->keep_alive = Z_BVAL_P(ztmp);
}
/**
* websocket mask
*/
if (php_swoole_array_get_value(vht, "websocket_mask", ztmp))
{
convert_to_boolean(ztmp);
http->websocket_mask = (int) Z_BVAL_P(ztmp);
}
//client settings
php_swoole_client_check_setting(http->cli, zset);
if (http->cli->http_proxy)
{
zval *zsend_header = sw_zend_read_property(swoole_http_client_ce_ptr, zobject, ZEND_STRL("requestHeaders"), 1);
if (zsend_header == NULL || Z_TYPE_P(zsend_header) != IS_ARRAY)
{
swoole_php_fatal_error (E_WARNING, "http proxy must set Host");
return SW_ERR;
}
zval *value;
if (!(value = zend_hash_str_find(Z_ARRVAL_P(zsend_header), ZEND_STRL("Host"))) ||
Z_TYPE_P(value) != IS_STRING || Z_STRLEN_P(value) < 1)
{
swoole_php_fatal_error(E_WARNING, "http proxy must set Host");
return SW_ERR;
}
if (http->cli->http_proxy->password)
{
char _buf1[128];
char _buf2[256];
int _n1 = snprintf(_buf1, sizeof(_buf1), "%*s:%*s", http->cli->http_proxy->l_user,
http->cli->http_proxy->user, http->cli->http_proxy->l_password,
http->cli->http_proxy->password);
zend_string *str = php_base64_encode((const unsigned char *) _buf1, _n1);
int _n2 = snprintf(_buf2, sizeof(_buf2), "Basic %*s", (int)str->len, str->val);
zend_string_free(str);
add_assoc_stringl_ex(zsend_header, ZEND_STRL("Proxy-Authorization"), _buf2, _n2);
}
}
}
if (cli->socket->active == 1)
{
swoole_php_fatal_error(E_WARNING, "swoole_http_client is already connected.");
return SW_ERR;
}
cli->object = zobject;
sw_copy_to_stack(cli->object, hcc->_object);
Z_TRY_ADDREF_P(zobject);
cli->open_eof_check = 0;
cli->open_length_check = 0;
cli->reactor_fdtype = PHP_SWOOLE_FD_STREAM_CLIENT;
cli->onReceive = http_client_onReceive;
cli->onConnect = http_client_onConnect;
cli->onClose = http_client_onClose;
cli->onError = http_client_onError;
return cli->connect(cli, http->host, http->port, http->timeout, 0);
}
void swoole_http_client_init(int module_number)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_http_client, "Swoole\\Http\\Client", "swoole_http_client", NULL, swoole_http_client_methods);
SWOOLE_SET_CLASS_SERIALIZABLE(swoole_http_client, zend_class_serialize_deny, zend_class_unserialize_deny);
SWOOLE_SET_CLASS_CLONEABLE(swoole_http_client, zend_class_clone_deny);
SWOOLE_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_http_client, zend_class_unset_property_deny);
zend_declare_property_long(swoole_http_client_ce_ptr, ZEND_STRL("type"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_http_client_ce_ptr, ZEND_STRL("errCode"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_string(swoole_http_client_ce_ptr, ZEND_STRL("errMsg"), "", ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_http_client_ce_ptr, ZEND_STRL("statusCode"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("host"), ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_http_client_ce_ptr, ZEND_STRL("port"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_bool(swoole_http_client_ce_ptr, ZEND_STRL("ssl"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("requestMethod"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("requestHeaders"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("requestBody"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("uploadFiles"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("set_cookie_headers"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("downloadFile"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("headers"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("cookies"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("body"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("onConnect"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("onError"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("onMessage"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_http_client_ce_ptr, ZEND_STRL("onClose"), ZEND_ACC_PUBLIC);
http_client_buffer = swString_new(SW_HTTP_RESPONSE_INIT_SIZE);
if (!http_client_buffer)
{
swoole_php_fatal_error(E_ERROR, "[1] swString_new(%d) failed.", SW_HTTP_RESPONSE_INIT_SIZE);
}
#ifdef SW_HAVE_ZLIB
swoole_zlib_buffer = swString_new(SW_HTTP_RESPONSE_INIT_SIZE);
if (!swoole_zlib_buffer)
{
swoole_php_fatal_error(E_ERROR, "[2] swString_new(%d) failed.", SW_HTTP_RESPONSE_INIT_SIZE);
}
#endif
}
static void http_client_execute_callback(zval *zobject, enum php_swoole_client_callback_type type)
{
zval *callback = NULL;
zval *retval = NULL;
zval args[1];
http_client_property *hcc = swoole_get_property(zobject, 0);
if (!hcc)
{
return;
}
char *callback_name;
switch(type)
{
case SW_CLIENT_CB_onConnect:
callback = hcc->onConnect;
callback_name = "onConnect";
break;
case SW_CLIENT_CB_onError:
callback = hcc->onError;
callback_name = "onError";
break;
case SW_CLIENT_CB_onClose:
callback = hcc->onClose;
callback_name = "onClose";
break;
default:
return;
}
//request is not completed
if (hcc->onResponse && (type == SW_CLIENT_CB_onError || type == SW_CLIENT_CB_onClose))
{
int error_code;
if (type == SW_CLIENT_CB_onError)
{
error_code = HTTP_CLIENT_ESTATUS_CONNECT_FAILED;
}
else if (hcc->error_flag & HTTP_CLIENT_EFLAG_TIMEOUT)
{
error_code = HTTP_CLIENT_ESTATUS_REQUEST_TIMEOUT;
}
else
{
error_code = HTTP_CLIENT_ESTATUS_SERVER_RESET;
}
zend_update_property_long(swoole_http_client_ce_ptr, zobject, ZEND_STRL("statusCode"), error_code);
zend_update_property_string(swoole_http_client_ce_ptr, zobject, ZEND_STRL("body"), "");
http_client_onResponseException(zobject);
}
//callback function is not set
if (!callback || ZVAL_IS_NULL(callback))
{
return;
}
args[0] = *zobject;
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 1, args, 0, NULL) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_http_client->%s handler error.", callback_name);
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
//free the callback return value
if (retval)
{
zval_ptr_dtor(retval);
}
}
/**
* @zobject: swoole_http_client object
*/
static void http_client_onClose(swClient *cli)
{
zval *zobject = cli->object;
http_client *http = swoole_get_object(zobject);
if (http && http->state == HTTP_CLIENT_STATE_WAIT_CLOSE)
{
http_client_parser_on_message_complete(&http->parser);
http_client_property *hcc = swoole_get_property(zobject, 0);
http_client_onResponseException(zobject);
sw_zval_free(hcc->onResponse);
hcc->onResponse = NULL;
}
http_client_free(zobject);
http_client_execute_callback(zobject, SW_CLIENT_CB_onClose);
zval_ptr_dtor(zobject);
}
static int http_client_onMessage(swConnection *conn, char *data, uint32_t length)
{
swClient *cli = conn->object;
zval *zobject = cli->object;
zval args[2];
zval *retval = NULL;
zval *zframe;
SW_MAKE_STD_ZVAL(zframe);
php_swoole_websocket_frame_unpack(cli->buffer, zframe);
args[0] = *zobject;
args[1] = *zframe;
http_client_property *hcc = swoole_get_property(zobject, 0);
zval *zcallback = hcc->onMessage;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL) == FAILURE)
{
swoole_php_fatal_error(E_ERROR, "swoole_http_client->onMessage: onClose handler error");
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
//free the callback return value
if (retval)
{
zval_ptr_dtor(retval);
}
zval_ptr_dtor(zframe);
return SW_OK;
}
/**
* @zobject: swoole_http_client object
*/
static void http_client_onError(swClient *cli)
{
zval *zobject = cli->object;
zend_update_property_long(swoole_http_client_ce_ptr, zobject, ZEND_STRL("errCode"), SwooleG.error);
http_client_free(zobject);
http_client_execute_callback(zobject, SW_CLIENT_CB_onError);
zval_ptr_dtor(zobject);
}
static void http_client_onRequestTimeout(swTimer *timer, swTimer_node *tnode)
{
swClient *cli = (swClient *) tnode->data;
zval *zobject = (zval *) cli->object;
zend_update_property_long(swoole_http_client_ce_ptr, zobject, ZEND_STRL("errCode"), ETIMEDOUT);
zend_update_property_long(swoole_http_client_ce_ptr, zobject, ZEND_STRL("statusCode"), HTTP_CLIENT_ESTATUS_REQUEST_TIMEOUT);
http_client_property *hcc = swoole_get_property(zobject, 0);
if (!hcc)
{
return;
}
hcc->error_flag |= HTTP_CLIENT_EFLAG_TIMEOUT;
if (cli->buffer && cli->buffer->length > 0) // received something bug not complete
{
zval *zheaders = sw_zend_read_property_array(swoole_http_client_ce_ptr, zobject, ZEND_STRL("requestHeaders"), 1);
zval *v;
if (php_swoole_array_get_value(Z_ARRVAL_P(zheaders), "Connection", v))
{
convert_to_string(v);
if (strcmp(Z_STRVAL_P(v), "Upgrade") == 0) // is upgrade
{
hcc->error_flag |= HTTP_CLIENT_EFLAG_UPGRADE;
}
}
}
zval *retval = NULL;
sw_zend_call_method_with_0_params(&zobject, swoole_http_client_ce_ptr, NULL, "close", &retval);
if (retval)
{
zval_ptr_dtor(retval);
}
}
static void http_client_onResponseException(zval *zobject)
{
zval args[1];
zval *retval = NULL;
http_client_property *hcc = swoole_get_property(zobject, 0);
if (!hcc)
{
return;
}
if (!hcc->onResponse)
{
return;
}
hcc->shutdown = 1;
zval *zcallback = hcc->onResponse;
args[0] = *zobject;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 1, args, 0, NULL) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onResponse handler error");
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
if (retval)
{
zval_ptr_dtor(retval);
}
}
static void http_client_onReceive(swClient *cli, char *data, uint32_t length)
{
zval *zobject = cli->object;
http_client *http = swoole_get_object(zobject);
if (!http->cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_http_client.");
return;
}
if (http->header_completed == 0)
{
swString *buffer = cli->buffer;
buffer->length += length;
//HTTP/1.1 200 OK
if (buffer->length < 16)
{
return;
}
//No header
if (swoole_strnpos(buffer->str + buffer->offset, buffer->length - buffer->offset, ZEND_STRL("\r\n\r\n")) < 0)
{
if (buffer->length == buffer->size)
{
swSysError("Wrong http response.");
cli->close(cli);
return;
}
buffer->offset = buffer->length - 4 <= 0 ? 0 : buffer->length - 4;
return;
}
else
{
http->header_completed = 1;
data = buffer->str;
length = buffer->length;
swString_clear(buffer);
}
}
long parsed_n = swoole_http_parser_execute(&http->parser, &http_parser_settings, data, length);
if (parsed_n < 0)
{
swSysError("Parsing http over socket[%d] failed.", cli->socket->fd);
cli->close(cli);
return;
}
//not complete
if (!http->completed)
{
return;
}
swConnection *conn = cli->socket; // get connection pointer first because it's on Reactor so that it always be safe
zval *retval = NULL;
http_client_property *hcc = swoole_get_property(zobject, 0);
zval *zcallback = hcc->onResponse;
if (zcallback == NULL || ZVAL_IS_NULL(zcallback))
{
swoole_php_fatal_error(E_WARNING, "swoole_http_client object have not receive callback.");
return;
}
/**
* TODO: Sec-WebSocket-Accept check
*/
if (http->upgrade)
{
cli->open_length_check = 1;
cli->protocol.get_package_length = swWebSocket_get_package_length;
cli->protocol.onPackage = http_client_onMessage;
cli->protocol.package_length_size = SW_WEBSOCKET_HEADER_LEN + SW_WEBSOCKET_MASK_LEN + sizeof(uint64_t);
http->state = HTTP_CLIENT_STATE_UPGRADE;
//data frame
if (length > parsed_n + 3)
{
cli->buffer->length = length - parsed_n - 1;
memmove(cli->buffer->str, data + parsed_n + 1, cli->buffer->length);
}
else
{
swString_clear(cli->buffer);
}
}
http_client_clear(http);
http_client_reset(http);
hcc->onResponse = NULL;
zval args[1];
args[0] = *zobject;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 1, args, 0, NULL) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onReactorCallback handler error");
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
if (retval)
{
zval_ptr_dtor(retval);
}
sw_zval_free(zcallback);
// maybe close in callback, check it
if (conn->active == 0)
{
return;
}
if (http->upgrade && cli->buffer->length > 0)
{
cli->socket->skip_recv = 1;
swProtocol_recv_check_length(&cli->protocol, cli->socket, cli->buffer);
return;
}
http_client_check_keep(http);
}
static void http_client_onConnect(swClient *cli)
{
zval *zobject = cli->object;
http_client *http = swoole_get_object(zobject);
if (!http->cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_http_client.");
return;
}
http_client_execute_callback(zobject, SW_CLIENT_CB_onConnect);
//send http request on write
http_client_send_http_request(zobject);
}
static int http_client_send_http_request(zval *zobject)
{
int ret;
http_client *http = swoole_get_object(zobject);
if (!http->cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_http_client.");
return SW_ERR;
}
if (!http->cli->socket && http->cli->socket->active == 0)
{
swoole_php_error(E_WARNING, "server is not connected.");
return SW_ERR;
}
if (http->state != HTTP_CLIENT_STATE_READY)
{
swoole_php_error(E_WARNING, "http client is not ready.");
return SW_ERR;
}
http->state = HTTP_CLIENT_STATE_BUSY;
//clear errno
SwooleG.error = 0;
http_client_property *hcc = swoole_get_property(zobject, 0);
zval *zpost_data = sw_zend_read_property(swoole_http_client_ce_ptr, zobject, ZEND_STRL("requestBody"), 1);
zval *zsend_header = sw_zend_read_property(swoole_http_client_ce_ptr, zobject, ZEND_STRL("requestHeaders"), 1);
uint32_t header_flag = 0x0;
//POST
if (zpost_data && !ZVAL_IS_NULL(zpost_data))
{
if (hcc->request_method == NULL)
{
hcc->request_method = "POST";
}
}
//GET
else
{
if (hcc->request_method == NULL)
{
hcc->request_method = "GET";
}
}
http->method = swHttp_get_method(hcc->request_method, strlen(hcc->request_method) + 1);
char *key;
uint32_t keylen;
int keytype;
zval *value = NULL;
swString_clear(http_client_buffer);
swString_append_ptr(http_client_buffer, hcc->request_method, strlen(hcc->request_method));
hcc->request_method = NULL;
swString_append_ptr(http_client_buffer, ZEND_STRL(" "));
#ifdef SW_USE_OPENSSL
if (http->cli->http_proxy && !http->cli->open_ssl)
#else
if (http->cli->http_proxy)
#endif
{
value = zend_hash_str_find(Z_ARRVAL_P(zsend_header), ZEND_STRL("Host")); //checked before
char *pre = "http://";
int len = http->uri_len + Z_STRLEN_P(value) + strlen(pre) + 10;
void *addr = emalloc(http->uri_len + Z_STRLEN_P(value) + strlen(pre) + 10);
http->uri_len = snprintf(addr, len, "%s%s:%ld%s", pre, Z_STRVAL_P(value), http->port, http->uri);
efree(http->uri);
http->uri = addr;
}
swString_append_ptr (http_client_buffer, http->uri, http->uri_len);
swString_append_ptr(http_client_buffer, ZEND_STRL(" HTTP/1.1\r\n"));
if (zsend_header && Z_TYPE_P(zsend_header) == IS_ARRAY)
{
// As much as possible to ensure that Host is the first header.
// See: http://tools.ietf.org/html/rfc7230#section-5.4
if ((value = zend_hash_str_find(Z_ARRVAL_P(zsend_header), ZEND_STRL("Host"))) || (value = zend_hash_str_find(Z_ARRVAL_P(zsend_header), ZEND_STRL("host"))))
{
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Host"), Z_STRVAL_P(value), Z_STRLEN_P(value));
}
else
{
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Host"), http->host, http->host_len);
}
SW_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(zsend_header), key, keylen, keytype, value)
if (HASH_KEY_IS_STRING != keytype)
{
continue;
}