forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_http_client.c
1413 lines (1241 loc) · 44.4 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 |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Fang <coooold@live.com> |
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#include "websocket.h"
#include "thirdparty/php_http_parser.h"
#include "ext/standard/basic_functions.h"
#include "ext/standard/php_http.h"
#include "ext/standard/base64.h"
#ifdef SW_HAVE_ZLIB
#include <zlib.h>
#endif
enum http_client_state
{
HTTP_CLIENT_STATE_WAIT,
HTTP_CLIENT_STATE_READY,
HTTP_CLIENT_STATE_BUSY,
//WebSocket
HTTP_CLIENT_STATE_UPGRADE,
};
typedef struct
{
zval *onConnect;
zval *onError;
zval *onClose;
zval *onMessage;
zval *onResponse;
#if PHP_MAJOR_VERSION >= 7
zval _object;
zval _request_body;
zval _request_header;
zval _cookies;
zval _onResponse;
zval _onConnect;
zval _onError;
zval _onClose;
zval _onMessage;
#endif
zval *cookies;
zval *request_header;
zval *request_body;
char *request_method;
int callback_index;
} http_client_property;
typedef struct
{
swClient *cli;
char *host;
zend_size_t host_len;
long port;
double timeout;
char* uri;
zend_size_t uri_len;
char *tmp_header_field_name;
zend_size_t tmp_header_field_name_len;
php_http_parser parser;
swString *buffer;
swString *body;
uint8_t state; //0 wait 1 ready 2 busy
uint8_t keep_alive; //0 no 1 keep
uint8_t upgrade;
uint8_t gzip;
} http_client;
extern swString *swoole_zlib_buffer;
static swString *http_client_buffer;
static int http_client_parser_on_header_field(php_http_parser *parser, const char *at, size_t length);
static int http_client_parser_on_header_value(php_http_parser *parser, const char *at, size_t length);
static int http_client_parser_on_body(php_http_parser *parser, const char *at, size_t length);
static int http_client_parser_on_message_complete(php_http_parser *parser);
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 int http_client_send_http_request(zval *zobject TSRMLS_DC);
static http_client* http_client_create(zval *object TSRMLS_DC);
static void http_client_free(zval *object TSRMLS_DC);
static int http_client_execute(zval *zobject, char *uri, zend_size_t uri_len, zval *callback TSRMLS_DC);
static sw_inline void http_client_swString_append_headers(swString* swStr, char* key, zend_size_t key_len, char* data, zend_size_t data_len)
{
swString_append_ptr(swStr, key, key_len);
swString_append_ptr(swStr, ZEND_STRL(": "));
swString_append_ptr(swStr, data, data_len);
swString_append_ptr(swStr, ZEND_STRL("\r\n"));
}
static sw_inline void http_client_create_token(int length, char *buf)
{
char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"§$%&/()=[]{}";
int i;
assert(length < 1024);
for (i = 0; i < length; i++)
{
buf[i] = characters[rand() % sizeof(characters) - 1];
}
buf[length] = '\0';
}
static const php_http_parser_settings http_parser_settings =
{
NULL,
NULL,
NULL,
NULL,
NULL,
http_client_parser_on_header_field,
http_client_parser_on_header_value,
NULL,
http_client_parser_on_body,
http_client_parser_on_message_complete
};
zend_class_entry swoole_http_client_ce;
zend_class_entry *swoole_http_client_class_entry_ptr;
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, 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 const zend_function_entry swoole_http_client_methods[] =
{
PHP_ME(swoole_http_client, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_http_client, __destruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_http_client, set, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, setMethod, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, setHeaders, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, setCookies, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, setData, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, execute, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, push, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, get, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, post, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, upgrade, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, isConnected, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, close, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_client, on, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static int http_client_execute(zval *zobject, char *uri, zend_size_t uri_len, zval *callback TSRMLS_DC)
{
if (uri_len <= 0)
{
swoole_php_fatal_error(E_WARNING, "path is empty.");
return SW_ERR;
}
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
{
http = http_client_create(zobject TSRMLS_CC);
}
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.");
}
http_client_property *hcc = swoole_get_property(zobject, 0);
hcc->onResponse = callback;
sw_copy_to_stack(hcc->onResponse, hcc->_onResponse);
sw_zval_add_ref(&hcc->onResponse);
//if connection exists
if (http->cli)
{
http_client_send_http_request(zobject TSRMLS_CC);
return 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;
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);
sw_zval_add_ref(&zobject);
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 TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_http_client_ce, "swoole_http_client", "Swoole\\Http\\Client", swoole_http_client_methods);
swoole_http_client_class_entry_ptr = zend_register_internal_class(&swoole_http_client_ce TSRMLS_CC);
zend_declare_property_long(swoole_http_client_class_entry_ptr, SW_STRL("errCode")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_long(swoole_http_client_class_entry_ptr, SW_STRL("sock")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
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(2048);
if (!swoole_zlib_buffer)
{
swoole_php_fatal_error(E_ERROR, "[2] swString_new(%d) failed.", SW_HTTP_RESPONSE_INIT_SIZE);
}
#endif
}
static sw_inline void http_client_execute_callback(zval *zobject, enum php_swoole_client_callback_type type)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
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;
}
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 TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_http_client->%s handler error.", callback_name);
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
//free the callback return value
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
/**
* @zobject: swoole_http_client object
*/
static void http_client_onClose(swClient *cli)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
zval *zobject = cli->object;
if (!cli->released)
{
http_client_free(zobject TSRMLS_CC);
}
http_client_execute_callback(zobject, SW_CLIENT_CB_onClose);
sw_zval_ptr_dtor(&zobject);
}
/**
* @zobject: swoole_http_client object
*/
static void http_client_onError(swClient *cli)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
zval *zobject = cli->object;
zend_update_property_long(swoole_http_client_class_entry_ptr, zobject, ZEND_STRL("errCode"), SwooleG.error TSRMLS_CC);
if (!cli->released)
{
http_client_free(zobject TSRMLS_CC);
}
http_client_execute_callback(zobject, SW_CLIENT_CB_onError);
sw_zval_ptr_dtor(&zobject);
}
static void http_client_onReceive(swClient *cli, char *data, uint32_t length)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
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->state == HTTP_CLIENT_STATE_UPGRADE)
{
swString *buffer = http->buffer;
if (swString_append_ptr(buffer, data, length) < 0)
{
cli->close(cli);
return;
}
if (cli->socket->recv_wait)
{
recv_wait:
if (buffer->offset == buffer->length)
{
zval **args[2];
zval *retval;
zval *zframe = php_swoole_websocket_unpack(buffer TSRMLS_CC);
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 TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_ERROR, "swoole_http_client->onMessage: onClose handler error");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
//free the callback return value
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&zframe);
cli->socket->recv_wait = 0;
swString_clear(buffer);
}
}
else
{
int package_length = swWebSocket_get_package_length(NULL, cli->socket, data, length);
//invalid package, close connection.
if (package_length < 0)
{
cli->close(cli);
return;
}
//no length
else if (package_length == 0)
{
return;
}
//get length success
else
{
if (buffer->size < package_length)
{
if (swString_extend(buffer, package_length) < 0)
{
return;
}
}
buffer->offset = package_length;
cli->socket->recv_wait = 1;
goto recv_wait;
}
}
}
else
{
long parsed_n = php_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);
}
}
}
static void http_client_onConnect(swClient *cli)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
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 TSRMLS_CC);
}
#if PHP_MAJOR_VERSION < 7
static inline char* sw_http_build_query(zval *data, zend_size_t *length, smart_str *formstr TSRMLS_DC)
{
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 3
if (php_url_encode_hash_ex(HASH_OF(data), formstr, NULL, 0, NULL, 0, NULL, 0, NULL, NULL TSRMLS_CC) == FAILURE)
#else
if (php_url_encode_hash_ex(HASH_OF(data), formstr, NULL, 0, NULL, 0, NULL, 0, NULL, NULL, (int) PHP_QUERY_RFC1738 TSRMLS_CC) == FAILURE)
#endif
{
if (formstr->c)
{
smart_str_free(formstr);
}
return NULL;
}
if (!formstr->c)
{
return NULL;
}
smart_str_0(formstr);
*length = formstr->len;
return formstr->c;
}
#else
static inline char* sw_http_build_query(zval *data, zend_size_t *length, smart_str *formstr TSRMLS_DC)
{
if (php_url_encode_hash_ex(HASH_OF(data), formstr, NULL, 0, NULL, 0, NULL, 0, NULL, NULL, (int) PHP_QUERY_RFC1738) == FAILURE)
{
if (formstr->s)
{
smart_str_free(formstr);
}
return NULL;
}
if (!formstr->s)
{
return NULL;
}
smart_str_0(formstr);
*length = formstr->s->len;
return formstr->s->val;
}
#endif
static int http_client_send_http_request(zval *zobject TSRMLS_DC)
{
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 *post_data = hcc->request_body;
zval *send_header = hcc->request_header;
//POST
if (post_data)
{
if (hcc->request_method == NULL)
{
hcc->request_method = "POST";
}
}
//GET
else
{
if (hcc->request_method == NULL)
{
hcc->request_method = "GET";
}
}
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(" "));
swString_append_ptr(http_client_buffer, http->uri, http->uri_len);
swString_append_ptr(http_client_buffer, ZEND_STRL(" HTTP/1.1\r\n"));
char *key;
uint32_t keylen;
int keytype;
zval *value;
if (send_header && Z_TYPE_P(send_header) == IS_ARRAY)
{
if (sw_zend_hash_find(Z_ARRVAL_P(send_header), ZEND_STRS("Connection"), (void **) &value) == FAILURE)
{
if (http->keep_alive)
{
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Connection"), ZEND_STRL("keep-alive"));
}
else
{
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Connection"), ZEND_STRL("closed"));
}
}
if (sw_zend_hash_find(Z_ARRVAL_P(send_header), ZEND_STRS("Host"), (void **) &value) == FAILURE)
{
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Host"), http->host, http->host_len);
}
#ifdef SW_HAVE_ZLIB
if (sw_zend_hash_find(Z_ARRVAL_P(send_header), ZEND_STRS("Accept-Encoding"), (void **) &value) == FAILURE)
{
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Accept-Encoding"), ZEND_STRL("gzip"));
}
#endif
SW_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(send_header), key, keylen, keytype, value)
if (HASH_KEY_IS_STRING != keytype)
{
continue;
}
convert_to_string(value);
http_client_swString_append_headers(http_client_buffer, key, keylen, Z_STRVAL_P(value), Z_STRLEN_P(value));
SW_HASHTABLE_FOREACH_END();
}
else
{
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Connection"), ZEND_STRL("keep-alive"));
http->keep_alive = 1;
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Host"), http->host, http->host_len);
#ifdef SW_HAVE_ZLIB
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Accept-Encoding"), ZEND_STRL("gzip"));
#endif
}
if (hcc->cookies && Z_TYPE_P(hcc->cookies) == IS_ARRAY)
{
swString_append_ptr(http_client_buffer, ZEND_STRL("Cookie: "));
int n_cookie = Z_ARRVAL_P(hcc->cookies)->nNumOfElements;
int i = 0;
char *encoded_value;
SW_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(hcc->cookies), key, keylen, keytype, value)
i ++;
if (HASH_KEY_IS_STRING != keytype)
{
continue;
}
convert_to_string(value);
swString_append_ptr(http_client_buffer, key, keylen);
swString_append_ptr(http_client_buffer, "=", 1);
int encoded_value_len;
encoded_value = sw_php_url_encode( Z_STRVAL_P(value), Z_STRLEN_P(value), &encoded_value_len);
if (encoded_value)
{
swString_append_ptr(http_client_buffer, encoded_value, encoded_value_len);
efree(encoded_value);
}
if (i < n_cookie)
{
swString_append_ptr(http_client_buffer, "; ", 2);
}
SW_HASHTABLE_FOREACH_END();
swString_append_ptr(http_client_buffer, ZEND_STRL("\r\n"));
}
if (post_data)
{
char content_length_str[32];
int n;
if (Z_TYPE_P(post_data) == IS_ARRAY)
{
zend_size_t len;
http_client_swString_append_headers(http_client_buffer, ZEND_STRL("Content-Type"), ZEND_STRL("application/x-www-form-urlencoded"));
smart_str formstr_s = { 0 };
char *formstr = sw_http_build_query(post_data, &len, &formstr_s TSRMLS_CC);
if (formstr == NULL)
{
swoole_php_error(E_WARNING, "http_build_query failed.");
return SW_ERR;
}
n = snprintf(content_length_str, sizeof(content_length_str), "Content-Length: %d\r\n\r\n", len);
swString_append_ptr(http_client_buffer, content_length_str, n);
swString_append_ptr(http_client_buffer, formstr, len);
smart_str_free(&formstr_s);
}
else
{
n = snprintf(content_length_str, sizeof(content_length_str), "Content-Length: %d\r\n\r\n", Z_STRLEN_P(post_data));
swString_append_ptr(http_client_buffer, content_length_str, n);
swString_append_ptr(http_client_buffer, Z_STRVAL_P(post_data), Z_STRLEN_P(post_data));
}
zend_update_property_null(swoole_http_client_class_entry_ptr, zobject, ZEND_STRL("requestBody") TSRMLS_CC);
hcc->request_body = NULL;
}
else
{
swString_append_ptr(http_client_buffer, ZEND_STRL("\r\n"));
}
swTrace("[%d]: %s\n", http_client_buffer->length, http_client_buffer->str);
int ret = http->cli->send(http->cli, http_client_buffer->str, http_client_buffer->length, 0);
if (ret < 0)
{
SwooleG.error = errno;
swoole_php_sys_error(E_WARNING, "send(%d) %d bytes failed.", http->cli->socket->fd, (int )http_client_buffer->length);
zend_update_property_long(swoole_http_client_class_entry_ptr, zobject, SW_STRL("errCode")-1, SwooleG.error TSRMLS_CC);
}
return ret;
}
static void http_client_free(zval *object TSRMLS_DC)
{
http_client *http = swoole_get_object(object);
if (!http)
{
return;
}
if (http->uri)
{
efree(http->uri);
}
if (http->body)
{
swString_free(http->body);
}
if (http->buffer)
{
swString_free(http->buffer);
}
swClient *cli = http->cli;
if (cli)
{
php_swoole_client_free(object, cli TSRMLS_CC);
http->cli = NULL;
}
efree(http);
}
static http_client* http_client_create(zval *object TSRMLS_DC)
{
zval *ztmp;
http_client *http;
HashTable *vht;
http = (http_client*) emalloc(sizeof(http_client));
bzero(http, sizeof(http_client));
swoole_set_object(object, http);
php_http_parser_init(&http->parser, PHP_HTTP_RESPONSE);
http->parser.data = http;
ztmp = sw_zend_read_property(swoole_http_client_class_entry_ptr, object, ZEND_STRL("host"), 0 TSRMLS_CC);
http->host = Z_STRVAL_P(ztmp);
http->host_len = Z_STRLEN_P(ztmp);
ztmp = sw_zend_read_property(swoole_http_client_class_entry_ptr, object, ZEND_STRL("port"), 0 TSRMLS_CC);
convert_to_long(ztmp);
http->port = Z_LVAL_P(ztmp);
http->timeout = SW_CLIENT_DEFAULT_TIMEOUT;
http->keep_alive = 0;
zval *zset = sw_zend_read_property(swoole_http_client_class_entry_ptr, object, ZEND_STRL("setting"), 1 TSRMLS_CC);
if (zset && !ZVAL_IS_NULL(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 = (int) Z_LVAL_P(ztmp);
}
}
http->state = HTTP_CLIENT_STATE_READY;
return http;
}
static PHP_METHOD(swoole_http_client, __construct)
{
char *host;
zend_size_t host_len;
long port = 80;
zend_bool ssl = SW_FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lb", &host, &host_len, &port, &ssl) == FAILURE)
{
return;
}
if (host_len <= 0)
{
swoole_php_fatal_error(E_ERROR, "host is empty.");
RETURN_FALSE;
}
zend_update_property_stringl(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("host"), host, host_len TSRMLS_CC);
zend_update_property_long(swoole_http_client_class_entry_ptr,
getThis(), ZEND_STRL("port"), port TSRMLS_CC);
php_swoole_check_reactor();
//init
swoole_set_object(getThis(), NULL);
zval *headers;
SW_MAKE_STD_ZVAL(headers);
array_init(headers);
zend_update_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("headers"), headers TSRMLS_CC);
sw_zval_ptr_dtor(&headers);
http_client_property *hcc;
hcc = (http_client_property*) emalloc(sizeof(http_client_property));
bzero(hcc, sizeof(http_client_property));
swoole_set_property(getThis(), 0, hcc);
int flags = SW_SOCK_TCP | SW_FLAG_ASYNC;
if (ssl)
{
flags |= SW_SOCK_SSL;
}
zend_update_property_long(swoole_client_class_entry_ptr, getThis(), ZEND_STRL("type"), flags TSRMLS_CC);
RETURN_TRUE;
}
static PHP_METHOD(swoole_http_client, __destruct)
{
http_client *http = swoole_get_object(getThis());
if (http)
{
zval *zobject = getThis();
zval *retval;
sw_zend_call_method_with_0_params(&zobject, swoole_http_client_class_entry_ptr, NULL, "close", &retval);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
http_client_property *hcc = swoole_get_property(getThis(), 0);
efree(hcc);
swoole_set_property(getThis(), 0, NULL);
}
static PHP_METHOD(swoole_http_client, set)
{
zval *zset;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zset) == FAILURE)
{
return;
}
zend_update_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("setting"), zset TSRMLS_CC);
RETURN_TRUE;
}
static PHP_METHOD(swoole_http_client, setHeaders)
{
zval *headers;
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "z", &headers) == FAILURE)
{
return;
}
zend_update_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("requestHeaders"), headers TSRMLS_CC);
http_client_property *hcc = swoole_get_property(getThis(), 0);
hcc->request_header = sw_zend_read_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("requestHeaders"), 1 TSRMLS_CC);
sw_copy_to_stack(hcc->request_header, hcc->_request_header);
RETURN_TRUE;
}
static PHP_METHOD(swoole_http_client, setCookies)
{
zval *cookies;
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "z", &cookies) == FAILURE)
{
return;
}
zend_update_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("cookies"), cookies TSRMLS_CC);
http_client_property *hcc = swoole_get_property(getThis(), 0);
hcc->cookies = sw_zend_read_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("cookies"), 1 TSRMLS_CC);
sw_copy_to_stack(hcc->cookies, hcc->_cookies);
RETURN_TRUE;
}
static PHP_METHOD(swoole_http_client, setData)
{
zval *data;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &data) == FAILURE)
{
return;
}
zend_update_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("requestBody"), data TSRMLS_CC);
http_client_property *hcc = swoole_get_property(getThis(), 0);
hcc->request_body = sw_zend_read_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("requestBody"), 1 TSRMLS_CC);
sw_copy_to_stack(hcc->request_body, hcc->_request_body);
RETURN_TRUE;
}
static PHP_METHOD(swoole_http_client, setMethod)
{
zval *method;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &method) == FAILURE)
{
return;
}
convert_to_string(method);
zend_update_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("requestMethod"), method TSRMLS_CC);
http_client_property *hcc = swoole_get_property(getThis(), 0);
hcc->request_method = Z_STRVAL_P(method);
RETURN_TRUE;
}
static PHP_METHOD(swoole_http_client, isConnected)
{
http_client *http = swoole_get_object(getThis());
if (!http || !http->cli)
{
RETURN_FALSE;
}
if (!http->cli->socket)
{
RETURN_FALSE;
}
RETURN_BOOL(http->cli->socket->active);
}
static PHP_METHOD(swoole_http_client, close)
{
http_client *http = swoole_get_object(getThis());
swClient *cli = http->cli;
if (!cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_http_client.");
RETURN_FALSE;
}
if (!cli->socket)
{
swoole_php_error(E_WARNING, "not connected to the server");
RETURN_FALSE;
}
if (cli->socket->closed)
{
swoole_php_error(E_WARNING, "client socket is closed.");
RETURN_FALSE;
}
int ret = SW_OK;
if (!cli->keep || swConnection_error(SwooleG.error) == SW_CLOSE)
{
cli->released = 1;
ret = cli->close(cli);
http_client_free(getThis() TSRMLS_CC);
}
else