-
Notifications
You must be signed in to change notification settings - Fork 1
/
swoole_http_v2_client.c
1281 lines (1116 loc) · 40.1 KB
/
swoole_http_v2_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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#include "swoole_http.h"
#ifdef SW_USE_HTTP2
#include "swoole_http_v2_client.h"
static zend_class_entry swoole_http2_client_ce;
static zend_class_entry *swoole_http2_client_class_entry_ptr;
extern zend_class_entry *swoole_http2_response_class_entry_ptr;
swString *cookie_buffer = NULL;
enum
{
HTTP2_CLIENT_PROPERTY_INDEX = 3,
};
typedef struct
{
char *uri;
uint32_t uri_len;
uint32_t stream_id;
uint8_t type;
zval *callback;
zval *data;
zval _callback;
zval _data;
} http2_client_request;
static PHP_METHOD(swoole_http2_client, __construct);
static PHP_METHOD(swoole_http2_client, __destruct);
static PHP_METHOD(swoole_http2_client, onConnect);
static PHP_METHOD(swoole_http2_client, onError);
static PHP_METHOD(swoole_http2_client, onReceive);
static PHP_METHOD(swoole_http2_client, onClose);
static PHP_METHOD(swoole_http2_client, setHeaders);
static PHP_METHOD(swoole_http2_client, setCookies);
static PHP_METHOD(swoole_http2_client, get);
static PHP_METHOD(swoole_http2_client, post);
static PHP_METHOD(swoole_http2_client, openStream);
static PHP_METHOD(swoole_http2_client, push);
static PHP_METHOD(swoole_http2_client, closeStream);
static void http2_client_send_request(zval *zobject, http2_client_request *req TSRMLS_DC);
static void http2_client_send_stream_request(zval *zobject, http2_client_request *req TSRMLS_DC);
static void http2_client_send_all_requests(zval *zobject TSRMLS_DC);
static void http2_client_request_free(void *ptr);
static void http2_client_stream_free(void *ptr);
static const zend_function_entry swoole_http2_client_methods[] =
{
PHP_ME(swoole_http2_client, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_http2_client, __destruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_http2_client, setHeaders, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, setCookies, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, get, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, post, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, onConnect, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, onError, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, onReceive, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, onClose, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, openStream, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, push, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http2_client, closeStream, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void swoole_http2_client_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_http2_client_ce, "swoole_http2_client", "Swoole\\Http2\\Client", swoole_http2_client_methods);
swoole_http2_client_class_entry_ptr = sw_zend_register_internal_class_ex(&swoole_http2_client_ce, swoole_client_class_entry_ptr, "swoole_client" TSRMLS_CC);
SWOOLE_CLASS_ALIAS(swoole_http2_client, "Swoole\\Http2\\Client");
zend_declare_property_null(swoole_http2_client_class_entry_ptr, SW_STRL("requestHeaders")-1, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_http2_client_class_entry_ptr, SW_STRL("cookies")-1, ZEND_ACC_PUBLIC TSRMLS_CC);
if (cookie_buffer == NULL)
{
cookie_buffer = swString_new(8192);
}
}
static PHP_METHOD(swoole_http2_client, __construct)
{
char *host;
zend_size_t host_len;
long port = 80;
zend_bool ssl = SW_FALSE;
swoole_php_fatal_error(
E_DEPRECATED,
"Http2 async client will be removed in the next version, "
"please use \\Swoole\\Coroutine\\Http2\\Client instead."
);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lb", &host, &host_len, &port, &ssl) == FAILURE)
{
return;
}
if (host_len <= 0)
{
zend_throw_exception(swoole_exception_class_entry_ptr, "host is empty.", SW_ERROR_INVALID_PARAMS TSRMLS_CC);
RETURN_FALSE;
}
http2_client_property *hcc;
hcc = (http2_client_property*) emalloc(sizeof(http2_client_property));
bzero(hcc, sizeof(http2_client_property));
swoole_set_property(getThis(), HTTP2_CLIENT_PROPERTY_INDEX, hcc);
hcc->requests = swLinkedList_new(0, http2_client_request_free);
hcc->stream_requests = swLinkedList_new(0, http2_client_request_free);
hcc->streams = swHashMap_new(8, http2_client_stream_free);
hcc->stream_id = 1;
zval *ztype;
SW_MAKE_STD_ZVAL(ztype);
long type = SW_FLAG_ASYNC | SW_SOCK_TCP;
if (ssl)
{
type |= SW_SOCK_SSL;
hcc->ssl = 1;
}
ZVAL_LONG(ztype, type);
zval *zobject = getThis();
zval *retval = NULL;
sw_zend_call_method_with_1_params(&zobject, swoole_client_class_entry_ptr, NULL, "__construct", &retval, ztype);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&ztype);
hcc->host = estrndup(host, host_len);
hcc->host_len = host_len;
hcc->port = port;
}
static PHP_METHOD(swoole_http2_client, setHeaders)
{
zval *headers;
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "z", &headers) == FAILURE)
{
return;
}
zend_update_property(swoole_http2_client_class_entry_ptr, getThis(), ZEND_STRL("requestHeaders"), headers TSRMLS_CC);
}
static PHP_METHOD(swoole_http2_client, setCookies)
{
zval *cookies;
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "z", &cookies) == FAILURE)
{
return;
}
zend_update_property(swoole_http2_client_class_entry_ptr, getThis(), ZEND_STRL("cookies"), cookies TSRMLS_CC);
}
static int http2_client_build_header(zval *zobject, http2_client_request *req, char *buffer, int buffer_len TSRMLS_DC)
{
char *date_str = NULL;
int ret;
zval *zheader = sw_zend_read_property(swoole_http2_client_class_entry_ptr, zobject, ZEND_STRL("requestHeaders"), 1 TSRMLS_CC);
int index = 0;
int find_host = 0;
nghttp2_nv nv[1024];
http2_client_property *hcc = swoole_get_property(zobject, HTTP2_CLIENT_PROPERTY_INDEX);
if (req->type == HTTP_GET)
{
http2_add_header(&nv[index++], ZEND_STRL(":method"), ZEND_STRL("GET"));
}
else
{
http2_add_header(&nv[index++], ZEND_STRL(":method"), ZEND_STRL("POST"));
}
http2_add_header(&nv[index++], ZEND_STRL(":path"), req->uri, req->uri_len);
if (hcc->ssl)
{
http2_add_header(&nv[index++], ZEND_STRL(":scheme"), ZEND_STRL("https"));
}
else
{
http2_add_header(&nv[index++], ZEND_STRL(":scheme"), ZEND_STRL("http"));
}
//Host
index++;
if (zheader && !ZVAL_IS_NULL(zheader))
{
HashTable *ht = Z_ARRVAL_P(zheader);
zval *value = NULL;
char *key = NULL;
uint32_t keylen = 0;
int type;
SW_HASHTABLE_FOREACH_START2(ht, key, keylen, type, value)
{
if (!key)
{
break;
}
if (*key == ':')
{
continue;
}
if (strncasecmp("Host", key, keylen) == 0)
{
http2_add_header(&nv[HTTP2_CLIENT_HOST_HEADER_INDEX], ZEND_STRL(":authority"), Z_STRVAL_P(value), Z_STRLEN_P(value));
find_host = 1;
}
else
{
http2_add_header(&nv[index++], key, keylen, Z_STRVAL_P(value), Z_STRLEN_P(value));
}
}
SW_HASHTABLE_FOREACH_END();
(void)type;
}
if (!find_host)
{
http2_add_header(&nv[HTTP2_CLIENT_HOST_HEADER_INDEX], ZEND_STRL(":authority"), hcc->host, hcc->host_len);
}
zval *zcookie = sw_zend_read_property(swoole_http2_client_class_entry_ptr, zobject, ZEND_STRL("cookies"), 1 TSRMLS_CC);
//http cookies
if (zcookie && !ZVAL_IS_NULL(zcookie))
{
http2_add_cookie(nv, &index, zcookie TSRMLS_CC);
}
ssize_t rv;
size_t buflen;
size_t i;
size_t sum = 0;
#if 0
for (i = 0; i < index; ++i)
{
swTraceLog(SW_TRACE_HTTP2, "Header[%d]: "SW_ECHO_CYAN_BLUE"=%s", i, nv[i].name, nv[i].value);
}
#endif
nghttp2_hd_deflater *deflater;
ret = nghttp2_hd_deflate_new(&deflater, 4096);
if (ret != 0)
{
swoole_php_error(E_WARNING, "nghttp2_hd_deflate_init failed with error: %s\n", nghttp2_strerror(ret));
return SW_ERR;
}
for (i = 0; i < index; ++i)
{
sum += nv[i].namelen + nv[i].valuelen;
}
buflen = nghttp2_hd_deflate_bound(deflater, nv, index);
if (buflen > buffer_len)
{
swoole_php_error(E_WARNING, "header is too large.");
return SW_ERR;
}
rv = nghttp2_hd_deflate_hd(deflater, (uchar *) buffer, buflen, nv, index);
if (rv < 0)
{
swoole_php_error(E_WARNING, "nghttp2_hd_deflate_hd() failed with error: %s\n", nghttp2_strerror((int ) rv));
return SW_ERR;
}
for (i = 0; i < index; ++i)
{
efree(nv[i].name); // free lower header name copy
}
if (date_str)
{
efree(date_str);
}
nghttp2_hd_deflate_del(deflater);
return rv;
}
void http2_add_cookie(nghttp2_nv *nv, int *index, zval *cookies TSRMLS_DC)
{
char *key;
uint32_t keylen;
int keytype;
zval *value = NULL;
char *encoded_value;
uint32_t offest = 0;
swString_clear(cookie_buffer);
SW_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(cookies), key, keylen, keytype, value)
if (HASH_KEY_IS_STRING != keytype)
{
continue;
}
convert_to_string(value);
if (Z_STRLEN_P(value) == 0)
{
continue;
}
swString_append_ptr(cookie_buffer, key, keylen);
swString_append_ptr(cookie_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(cookie_buffer, encoded_value, encoded_value_len);
efree(encoded_value);
http2_add_header(&nv[(*index)++], ZEND_STRL("cookie"), cookie_buffer->str + offest, keylen + 1 + encoded_value_len);
offest += keylen + 1 + encoded_value_len;
}
SW_HASHTABLE_FOREACH_END();
}
int http2_client_parse_header(http2_client_property *hcc, http2_client_stream *stream , int flags, char *in, size_t inlen)
{
nghttp2_hd_inflater *inflater = hcc->inflater;
zval *zresponse = stream->response_object;
if (!inflater)
{
int ret = nghttp2_hd_inflate_new(&inflater);
if (ret != 0)
{
swoole_php_error(E_WARNING, "nghttp2_hd_inflate_init() failed, Error: %s[%d].", nghttp2_strerror(ret), ret);
return SW_ERR;
}
hcc->inflater = inflater;
}
if (flags & SW_HTTP2_FLAG_PRIORITY)
{
//int stream_deps = ntohl(*(int *) (in));
//uint8_t weight = in[4];
in += 5;
inlen -= 5;
}
zval *headers = sw_zend_read_property_array(swoole_http2_response_class_entry_ptr, zresponse, ZEND_STRL("headers"), 1 TSRMLS_CC);
zval *cookies = sw_zend_read_property_array(swoole_http2_response_class_entry_ptr, zresponse, ZEND_STRL("cookies"), 1 TSRMLS_CC);
zval *set_cookie_headers = sw_zend_read_property_array(swoole_http2_response_class_entry_ptr, zresponse, ZEND_STRL("set_cookie_headers"), 1 TSRMLS_CC);
ssize_t rv;
for (;;)
{
nghttp2_nv nv;
int inflate_flags = 0;
size_t proclen;
rv = nghttp2_hd_inflate_hd(inflater, &nv, &inflate_flags, (uchar *) in, inlen, 1);
if (rv < 0)
{
swoole_php_error(E_WARNING, "inflate failed, Error: %s[%zd].", nghttp2_strerror(rv), rv);
return SW_ERR;
}
proclen = (size_t) rv;
in += proclen;
inlen -= proclen;
//swTraceLog(SW_TRACE_HTTP2, "Header: %s[%d]: %s[%d]", nv.name, nv.namelen, nv.value, nv.valuelen);
if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT)
{
if (nv.name[0] == ':')
{
if (strncasecmp((char *) nv.name + 1, "status", nv.namelen -1) == 0)
{
zend_update_property_long(swoole_http2_response_class_entry_ptr, zresponse, ZEND_STRL("statusCode"), atoi((char *) nv.value) TSRMLS_CC);
continue;
}
}
#ifdef SW_HAVE_ZLIB
else if (strncasecmp((char *) nv.name, "content-encoding", nv.namelen) == 0 && strncasecmp((char *) nv.value, "gzip", nv.valuelen) == 0)
{
http2_client_init_gzip_stream(stream);
if (Z_OK != inflateInit2(&stream->gzip_stream, MAX_WBITS + 16))
{
swWarn("inflateInit2() failed.");
return SW_ERR;
}
}
#endif
else if (strncasecmp((char *) nv.name, "set-cookie", nv.namelen) == 0)
{
if (SW_OK != http_parse_set_cookies((char *) nv.value, nv.valuelen, cookies, set_cookie_headers))
{
return SW_ERR;
}
}
sw_add_assoc_stringl_ex(headers, (char *) nv.name, nv.namelen + 1, (char *) nv.value, nv.valuelen, 1);
}
if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL)
{
nghttp2_hd_inflate_end_headers(inflater);
break;
}
if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0)
{
break;
}
}
rv = nghttp2_hd_inflate_change_table_size(inflater, 4096);
if (rv != 0)
{
return rv;
}
return SW_OK;
}
/**
* Http2
*/
static int http2_client_onFrame(zval *zobject, zval *zdata TSRMLS_DC)
{
char *buf = Z_STRVAL_P(zdata);
int type = buf[3];
int flags = buf[4];
int stream_id = ntohl((*(int *) (buf + 5))) & 0x7fffffff;
uint32_t length = swHttp2_get_length(buf);
buf += SW_HTTP2_FRAME_HEADER_SIZE;
char frame[SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_FRAME_PING_PAYLOAD_SIZE];
http2_client_property *hcc = swoole_get_property(zobject, HTTP2_CLIENT_PROPERTY_INDEX);
swClient *cli = swoole_get_object(zobject);
uint16_t id;
uint32_t value;
swTraceLog(SW_TRACE_HTTP2, "["SW_ECHO_YELLOW"]\tflags=%d, stream_id=%d, length=%d", swHttp2_get_type(type), flags, stream_id, length);
if (type == SW_HTTP2_TYPE_SETTINGS)
{
if (flags & SW_HTTP2_FLAG_ACK)
{
return SW_OK;
}
while(length > 0)
{
id = ntohs(*(uint16_t *) (buf));
value = ntohl(*(uint32_t *) (buf + sizeof(uint16_t)));
switch (id)
{
case SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
hcc->max_concurrent_streams = value;
swTraceLog(SW_TRACE_HTTP2, "setting: max_concurrent_streams=%d.", value);
break;
case SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE:
hcc->send_window = value;
swTraceLog(SW_TRACE_HTTP2, "setting: init_window_size=%d.", value);
break;
case SW_HTTP2_SETTINGS_MAX_FRAME_SIZE:
hcc->max_frame_size = value;
swTraceLog(SW_TRACE_HTTP2, "setting: max_frame_size=%d.", value);
break;
case SW_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
hcc->max_header_list_size = value;
swTraceLog(SW_TRACE_HTTP2, "setting: max_header_list_size=%d.", value);
break;
default:
swWarn("unknown option[%d].", id);
break;
}
buf += sizeof(id) + sizeof(value);
length -= sizeof(id) + sizeof(value);
}
swHttp2_set_frame_header(frame, SW_HTTP2_TYPE_SETTINGS, 0, SW_HTTP2_FLAG_ACK, stream_id);
swTraceLog(SW_TRACE_HTTP2, "["SW_ECHO_GREEN", ACK, STREAM#%d]\t[length=%d]", swHttp2_get_type(SW_HTTP2_TYPE_SETTINGS), stream_id, length);
cli->send(cli, frame, SW_HTTP2_FRAME_HEADER_SIZE, 0);
return SW_OK;
}
else if (type == SW_HTTP2_TYPE_WINDOW_UPDATE)
{
hcc->send_window = ntohl(*(int *) buf);
swTraceLog(SW_TRACE_HTTP2, "update: window_size=%d.", hcc->send_window);
return SW_OK;
}
else if (type == SW_HTTP2_TYPE_PING)
{
swHttp2_set_frame_header(frame, SW_HTTP2_TYPE_PING, SW_HTTP2_FRAME_PING_PAYLOAD_SIZE, SW_HTTP2_FLAG_ACK, stream_id);
memcpy(frame + SW_HTTP2_FRAME_HEADER_SIZE, buf + SW_HTTP2_FRAME_HEADER_SIZE, SW_HTTP2_FRAME_PING_PAYLOAD_SIZE);
swTraceLog(SW_TRACE_HTTP2, "["SW_ECHO_GREEN", STREAM#%d]", swHttp2_get_type(SW_HTTP2_FRAME_PING_PAYLOAD_SIZE), stream_id);
cli->send(cli, frame, SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_FRAME_PING_PAYLOAD_SIZE, 0);
return SW_OK;
}
else if (type == SW_HTTP2_TYPE_GOAWAY)
{
int last_stream_id = htonl(*(int *) (buf));
buf += 4;
int error_code = htonl(*(int *) (buf));
swWarn("["SW_ECHO_RED"] last_stream_id=%d, error_code=%d.", "GOAWAY", last_stream_id, error_code);
zval* retval;
sw_zend_call_method_with_0_params(&zobject, swoole_client_class_entry_ptr, NULL, "close", &retval);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
return SW_OK;
}
http2_client_stream *stream = swHashMap_find_int(hcc->streams, stream_id);
// stream has closed
if (stream == NULL)
{
return SW_OK;
}
if (type == SW_HTTP2_TYPE_HEADERS)
{
http2_client_parse_header(hcc, stream, flags, buf, length);
}
else if (type == SW_HTTP2_TYPE_DATA)
{
if (!stream->buffer)
{
stream->buffer = swString_new(8192);
}
#ifdef SW_HAVE_ZLIB
if (stream->gzip)
{
if (http_response_uncompress(&stream->gzip_stream, stream->gzip_buffer, buf, length) == SW_ERR)
{
return -1;
}
swString_append_ptr(stream->buffer, stream->gzip_buffer->str, stream->gzip_buffer->length);
}
else
#endif
{
swString_append_ptr(stream->buffer, buf, length);
}
}
else
{
swWarn("unknown frame, type=%d, stream_id=%d, length=%d.", type, stream_id, length);
return SW_OK;
}
if ((type == SW_HTTP2_TYPE_DATA && stream->type == SW_HTTP2_STREAM_PIPELINE)
|| (stream->type == SW_HTTP2_STREAM_NORMAL && (flags & SW_HTTP2_FLAG_END_STREAM)))
{
zval *retval = NULL;
zval *zcallback = stream->callback;
zval *zresponse = stream->response_object;
if (stream->buffer)
{
zend_update_property_stringl(swoole_http2_response_class_entry_ptr, stream->response_object, ZEND_STRL("data"), stream->buffer->str, stream->buffer->length TSRMLS_CC);
}
zval **args[1];
args[0] = &zresponse;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_http2_client handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
if (stream->type == SW_HTTP2_STREAM_NORMAL)
{
swHashMap_del_int(hcc->streams, stream_id);
}
else
{
swString_clear(stream->buffer);
}
}
return SW_OK;
}
static void http2_client_request_free(void *ptr)
{
http2_client_request *req = ptr;
if (req->callback)
{
sw_zval_ptr_dtor(&req->callback);
}
if (req->data)
{
sw_zval_ptr_dtor(&req->data);
}
if (req->uri)
{
efree(req->uri);
}
efree(req);
}
static void http2_client_stream_free(void *ptr)
{
http2_client_stream *stream = ptr;
sw_zval_ptr_dtor(&stream->callback);
sw_zval_ptr_dtor(&stream->response_object);
if (stream->buffer)
{
swString_free(stream->buffer);
}
#ifdef SW_HAVE_ZLIB
if (stream->gzip)
{
inflateEnd(&stream->gzip_stream);
swString_free(stream->gzip_buffer);
}
#endif
efree(stream);
}
static void http2_client_set_callback(zval *zobject, const char *callback_name, const char *method_name TSRMLS_DC)
{
zval *retval = NULL;
zval *zcallback;
SW_MAKE_STD_ZVAL(zcallback);
array_init(zcallback);
zval *zname;
SW_MAKE_STD_ZVAL(zname);
zval *zmethod_name;
SW_MAKE_STD_ZVAL(zmethod_name);
SW_ZVAL_STRING(zname, callback_name, 1);
SW_ZVAL_STRING(zmethod_name, method_name, 1);
add_next_index_zval(zcallback, zobject);
add_next_index_zval(zcallback, zmethod_name);
sw_zend_call_method_with_2_params(&zobject, swoole_http2_client_class_entry_ptr, NULL, "on", &retval, zname, zcallback);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&zname);
sw_zval_ptr_dtor(&zcallback);
}
static void http2_client_send_all_requests(zval *zobject TSRMLS_DC)
{
http2_client_property *hcc = swoole_get_property(zobject, HTTP2_CLIENT_PROPERTY_INDEX);
swLinkedList *requests = hcc->requests;
swLinkedList_node *node = requests->head;
http2_client_request *request;
while(node)
{
request = node->data;
http2_client_send_request(zobject, request TSRMLS_CC);
node = node->next;
}
swLinkedList_free(requests);
hcc->requests = NULL;
requests = hcc->stream_requests;
node = requests->head;
while(node)
{
request = node->data;
http2_client_send_stream_request(zobject, request TSRMLS_CC);
node = node->next;
}
swLinkedList_free(requests);
hcc->stream_requests = NULL;
}
static void http2_client_send_stream_request(zval *zobject, http2_client_request *req TSRMLS_DC)
{
swClient *cli = swoole_get_object(zobject);
http2_client_property *hcc = swoole_get_property(zobject, HTTP2_CLIENT_PROPERTY_INDEX);
char buffer[8192];
/**
* create stream
*/
if (req->stream_id == 0)
{
/**
* send header
*/
int n = http2_client_build_header(zobject, req, buffer + SW_HTTP2_FRAME_HEADER_SIZE, sizeof(buffer) - SW_HTTP2_FRAME_HEADER_SIZE TSRMLS_CC);
if (n <= 0)
{
swWarn("http2_client_build_header() failed.");
return;
}
swHttp2_set_frame_header(buffer, SW_HTTP2_TYPE_HEADERS, n, SW_HTTP2_FLAG_END_HEADERS, hcc->stream_id);
http2_client_stream *stream = emalloc(sizeof(http2_client_stream));
memset(stream, 0, sizeof(http2_client_stream));
zval *response_object;
SW_MAKE_STD_ZVAL(response_object);
object_init_ex(response_object, swoole_http2_response_class_entry_ptr);
stream->stream_id = hcc->stream_id;
stream->response_object = response_object;
stream->callback = req->callback;
stream->type = SW_HTTP2_STREAM_PIPELINE;
sw_copy_to_stack(stream->callback, stream->_callback);
sw_zval_add_ref(&stream->callback);
sw_copy_to_stack(stream->response_object, stream->_response_object);
zend_update_property_long(swoole_http2_response_class_entry_ptr, response_object, ZEND_STRL("streamId"), stream->stream_id TSRMLS_CC);
swHashMap_add_int(hcc->streams, hcc->stream_id, stream);
swTraceLog(SW_TRACE_HTTP2, "["SW_ECHO_GREEN", STREAM#%d] length=%d", swHttp2_get_type(SW_HTTP2_TYPE_HEADERS), hcc->stream_id, n);
cli->send(cli, buffer, n + SW_HTTP2_FRAME_HEADER_SIZE, 0);
hcc->stream_id += 2;
return;
}
else
{
int stream_id = req->stream_id;
zval *post_data = req->data;
/**
* send body
*/
if (post_data)
{
if (Z_TYPE_P(post_data) == IS_ARRAY)
{
zend_size_t len;
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;
}
memset(buffer, 0, SW_HTTP2_FRAME_HEADER_SIZE);
swHttp2_set_frame_header(buffer, SW_HTTP2_TYPE_DATA, len, 0, stream_id);
swTraceLog(SW_TRACE_HTTP2, "["SW_ECHO_GREEN", END, STREAM#%d] length=%zu", swHttp2_get_type(SW_HTTP2_TYPE_DATA), stream_id, len);
cli->send(cli, buffer, SW_HTTP2_FRAME_HEADER_SIZE, 0);
cli->send(cli, formstr, len, 0);
smart_str_free(&formstr_s);
}
else
{
swHttp2_set_frame_header(buffer, SW_HTTP2_TYPE_DATA, Z_STRLEN_P(post_data), 0, stream_id);
swTraceLog(SW_TRACE_HTTP2, "["SW_ECHO_GREEN", END, STREAM#%d] length=%zu", swHttp2_get_type(SW_HTTP2_TYPE_DATA), stream_id, Z_STRLEN_P(post_data));
cli->send(cli, buffer, SW_HTTP2_FRAME_HEADER_SIZE, 0);
cli->send(cli, Z_STRVAL_P(post_data), Z_STRLEN_P(post_data), 0);
}
}
return;
}
}
static void http2_client_send_request(zval *zobject, http2_client_request *req TSRMLS_DC)
{
swClient *cli = swoole_get_object(zobject);
http2_client_property *hcc = swoole_get_property(zobject, HTTP2_CLIENT_PROPERTY_INDEX);
zval *post_data = req->data;
if (post_data)
{
zval *zheader = sw_zend_read_property_array(swoole_http2_client_class_entry_ptr, zobject, ZEND_STRL("requestHeaders"), 1 TSRMLS_CC);
if (Z_TYPE_P(post_data) == IS_ARRAY)
{
sw_add_assoc_stringl_ex(zheader, ZEND_STRS("content-type"), ZEND_STRL("application/x-www-form-urlencoded"), 1);
}
}
/**
* send header
*/
char buffer[8192];
int n = http2_client_build_header(zobject, req, buffer + SW_HTTP2_FRAME_HEADER_SIZE, sizeof(buffer) - SW_HTTP2_FRAME_HEADER_SIZE TSRMLS_CC);
if (n <= 0)
{
swWarn("http2_client_build_header() failed.");
return;
}
if (post_data == NULL)
{
swHttp2_set_frame_header(buffer, SW_HTTP2_TYPE_HEADERS, n, SW_HTTP2_FLAG_END_STREAM | SW_HTTP2_FLAG_END_HEADERS, hcc->stream_id);
}
else
{
swHttp2_set_frame_header(buffer, SW_HTTP2_TYPE_HEADERS, n, SW_HTTP2_FLAG_END_HEADERS, hcc->stream_id);
}
http2_client_stream *stream = emalloc(sizeof(http2_client_stream));
memset(stream, 0, sizeof(http2_client_stream));
zval *response_object;
SW_MAKE_STD_ZVAL(response_object);
object_init_ex(response_object, swoole_http2_response_class_entry_ptr);
stream->stream_id = hcc->stream_id;
stream->response_object = response_object;
stream->callback = req->callback;
stream->type = SW_HTTP2_STREAM_NORMAL;
sw_copy_to_stack(stream->callback, stream->_callback);
sw_zval_add_ref(&stream->callback);
sw_copy_to_stack(stream->response_object, stream->_response_object);
zend_update_property_long(swoole_http2_response_class_entry_ptr, response_object, ZEND_STRL("streamId"), stream->stream_id TSRMLS_CC);
swHashMap_add_int(hcc->streams, hcc->stream_id, stream);
swTraceLog(SW_TRACE_HTTP2, "["SW_ECHO_GREEN", STREAM#%d] length=%d", swHttp2_get_type(SW_HTTP2_TYPE_HEADERS), hcc->stream_id, n);
cli->send(cli, buffer, n + SW_HTTP2_FRAME_HEADER_SIZE, 0);
/**
* send body
*/
if (post_data)
{
if (Z_TYPE_P(post_data) == IS_ARRAY)
{
zend_size_t len;
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;
}
memset(buffer, 0, SW_HTTP2_FRAME_HEADER_SIZE);
swHttp2_set_frame_header(buffer, SW_HTTP2_TYPE_DATA, len, SW_HTTP2_FLAG_END_STREAM, hcc->stream_id);
swTraceLog(SW_TRACE_HTTP2, "["SW_ECHO_GREEN", END, STREAM#%d] length=%zu", swHttp2_get_type(SW_HTTP2_TYPE_DATA), hcc->stream_id, len);
cli->send(cli, buffer, SW_HTTP2_FRAME_HEADER_SIZE, 0);
cli->send(cli, formstr, len, 0);
smart_str_free(&formstr_s);
}
else
{
swHttp2_set_frame_header(buffer, SW_HTTP2_TYPE_DATA, Z_STRLEN_P(req->data), SW_HTTP2_FLAG_END_STREAM, hcc->stream_id);
swTraceLog(SW_TRACE_HTTP2, "["SW_ECHO_GREEN", END, STREAM#%d] length=%zu", swHttp2_get_type(SW_HTTP2_TYPE_DATA), hcc->stream_id, Z_STRLEN_P(req->data));
cli->send(cli, buffer, SW_HTTP2_FRAME_HEADER_SIZE, 0);
cli->send(cli, Z_STRVAL_P(post_data), Z_STRLEN_P(post_data), 0);
}
}
hcc->stream_id += 2;
return;
}
static void http2_client_connect(zval *zobject TSRMLS_DC)
{
http2_client_property *hcc = swoole_get_property(zobject, HTTP2_CLIENT_PROPERTY_INDEX);
zval *retval = NULL;
zval *zhost;
SW_MAKE_STD_ZVAL(zhost);
SW_ZVAL_STRINGL(zhost, hcc->host, hcc->host_len, 1);
zval *zport;
SW_MAKE_STD_ZVAL(zport);
ZVAL_LONG(zport, hcc->port);
http2_client_set_callback(zobject, "Connect", "onConnect" TSRMLS_CC);
http2_client_set_callback(zobject, "Receive", "onReceive" TSRMLS_CC);
if (!php_swoole_client_isset_callback(zobject, SW_CLIENT_CB_onClose TSRMLS_CC))
{
http2_client_set_callback(zobject, "Close", "onClose" TSRMLS_CC);
}
if (!php_swoole_client_isset_callback(zobject, SW_CLIENT_CB_onError TSRMLS_CC))
{
http2_client_set_callback(zobject, "Error", "onError" TSRMLS_CC);
}
sw_zend_call_method_with_2_params(&zobject, swoole_http2_client_class_entry_ptr, NULL, "connect", &retval, zhost, zport);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&zhost);
sw_zval_ptr_dtor(&zport);
swClient *cli = swoole_get_object(zobject);
cli->http2 = 1;
}
static PHP_METHOD(swoole_http2_client, get)
{
zval *uri;
zval *callback;
http2_client_property *hcc = swoole_get_property(getThis(), HTTP2_CLIENT_PROPERTY_INDEX);
swClient *cli = swoole_get_object(getThis());
if (!cli && hcc->connecting == 1)
{
swoole_php_error(E_WARNING, "The connection is closed.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &uri, &callback) == FAILURE)
{
return;
}
char *func_name = NULL;
if (!sw_zend_is_callable(callback, 0, &func_name TSRMLS_CC))
{
swoole_php_fatal_error(E_WARNING, "Function '%s' is not callable", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
if (Z_TYPE_P(uri) != IS_STRING)
{
swoole_php_fatal_error(E_WARNING, "uri is not string.");
RETURN_FALSE;
}
if (cli && cli->socket && cli->socket->active == 1)
{
http2_client_request _req;
_req.uri = Z_STRVAL_P(uri);
_req.uri_len = Z_STRLEN_P(uri);
_req.type = HTTP_GET;
_req.callback = callback;
_req.data = NULL;
http2_client_send_request(getThis(), &_req TSRMLS_CC);
}
else
{
swLinkedList *requests = hcc->requests;
http2_client_request *req = emalloc(sizeof(http2_client_request));
req->uri = estrndup(Z_STRVAL_P(uri), Z_STRLEN_P(uri));
req->uri_len = Z_STRLEN_P(uri);
req->type = HTTP_GET;
req->callback = callback;
req->data = NULL;
sw_copy_to_stack(req->callback, req->_callback);
sw_zval_add_ref(&req->callback);
swLinkedList_append(requests, req);
if (!hcc->connecting)
{
http2_client_connect(getThis() TSRMLS_CC);
hcc->connecting = 1;
}
}
RETURN_TRUE;
}
static PHP_METHOD(swoole_http2_client, post)
{
zval *uri;
zval *callback;
zval *data;
http2_client_property *hcc = swoole_get_property(getThis(), HTTP2_CLIENT_PROPERTY_INDEX);
swClient *cli = swoole_get_object(getThis());
if (!cli && hcc->connecting == 1)
{
swoole_php_error(E_WARNING, "The connection is closed.");