forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
swoole_http.c
2179 lines (1903 loc) · 66.1 KB
/
swoole_http.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"
#include <ext/standard/url.h>
#include <ext/standard/sha1.h>
#include <ext/standard/php_var.h>
#include <ext/standard/php_string.h>
#include <ext/standard/php_math.h>
#include <ext/date/php_date.h>
#include <ext/standard/md5.h>
#include <main/php_variables.h>
#include "websocket.h"
#include "Connection.h"
#include "base64.h"
#ifdef SW_HAVE_ZLIB
#include <zlib.h>
#endif
static swArray *http_client_array;
static uint8_t http_merge_global_flag = 0;
static uint8_t http_merge_request_flag = 0;
swString *swoole_http_buffer;
swString *swoole_http_form_data_buffer;
#ifdef SW_HAVE_ZLIB
swString *swoole_zlib_buffer;
#endif
enum http_response_flag
{
HTTP_RESPONSE_SERVER = 1u << 1,
HTTP_RESPONSE_CONNECTION = 1u << 2,
HTTP_RESPONSE_CONTENT_LENGTH = 1u << 3,
HTTP_RESPONSE_DATE = 1u << 4,
HTTP_RESPONSE_CONTENT_TYPE = 1u << 5,
};
enum http_global_flag
{
HTTP_GLOBAL_GET = 1u << 1,
HTTP_GLOBAL_POST = 1u << 2,
HTTP_GLOBAL_COOKIE = 1u << 3,
HTTP_GLOBAL_REQUEST = 1u << 4,
HTTP_GLOBAL_SERVER = 1u << 5,
HTTP_GLOBAL_FILES = 1u << 6,
};
enum http_upload_errno
{
HTTP_UPLOAD_ERR_OK = 0,
HTTP_UPLOAD_ERR_INI_SIZE,
HTTP_UPLOAD_ERR_FORM_SIZE,
HTTP_UPLOAD_ERR_PARTIAL,
HTTP_UPLOAD_ERR_NO_FILE,
HTTP_UPLOAD_ERR_NO_TMP_DIR = 6,
HTTP_UPLOAD_ERR_CANT_WRITE,
};
enum http_callback_type
{
HTTP_CALLBACK_onRequest = 0,
HTTP_CALLBACK_onHandShake = 1,
};
zend_class_entry swoole_http_server_ce;
zend_class_entry *swoole_http_server_class_entry_ptr;
zend_class_entry swoole_http_response_ce;
zend_class_entry *swoole_http_response_class_entry_ptr;
zend_class_entry swoole_http_request_ce;
zend_class_entry *swoole_http_request_class_entry_ptr;
static zval* php_sw_http_server_callbacks[2];
static int http_onReceive(swServer *serv, swEventData *req);
static void http_onClose(swServer *serv, int fd, int from_id);
static int http_request_on_path(php_http_parser *parser, const char *at, size_t length);
static int http_request_on_query_string(php_http_parser *parser, const char *at, size_t length);
static int http_request_on_body(php_http_parser *parser, const char *at, size_t length);
static int http_request_on_header_field(php_http_parser *parser, const char *at, size_t length);
static int http_request_on_header_value(php_http_parser *parser, const char *at, size_t length);
static int http_request_on_headers_complete(php_http_parser *parser);
static int http_request_message_complete(php_http_parser *parser);
static int multipart_body_on_header_field(multipart_parser* p, const char *at, size_t length);
static int multipart_body_on_header_value(multipart_parser* p, const char *at, size_t length);
static int multipart_body_on_data(multipart_parser* p, const char *at, size_t length);
static int multipart_body_on_header_complete(multipart_parser* p);
static int multipart_body_on_data_end(multipart_parser* p);
static int multipart_body_end(multipart_parser* p);
static int http_request_new(swoole_http_client* c TSRMLS_DC);
static void http_global_merge(zval *val, zval *zrequest, int type);
static void http_global_clear(TSRMLS_D);
static swoole_http_client* http_get_client(zval *object, int check_end TSRMLS_DC);
static void http_build_header(swoole_http_client *client, zval *object, swString *response, int body_length TSRMLS_DC);
static void http_parse_cookie(zval *array, const char *at, size_t length);
static int http_trim_double_quote(zval **value, char **ptr);
#ifdef SW_HAVE_ZLIB
static int http_response_compress(swString *body, int level);
#endif
#if PHP_MAJOR_VERSION >= 7
#define http_alloc_zval(client,object,val) val = &client->object##_stack.val; client->object.val = val
#else
#define http_alloc_zval(client,object,val) MAKE_STD_ZVAL(val); client->object.val = val
#endif
#define http_merge_php_global(v,r,t) if (http_merge_global_flag > 0) http_global_merge(v,r,t)
static PHP_METHOD(swoole_http_server, on);
static PHP_METHOD(swoole_http_server, start);
static PHP_METHOD(swoole_http_server, setglobal);
static PHP_METHOD(swoole_http_request, rawcontent);
static PHP_METHOD(swoole_http_response, write);
static PHP_METHOD(swoole_http_response, end);
static PHP_METHOD(swoole_http_response, cookie);
static PHP_METHOD(swoole_http_response, rawcookie);
static PHP_METHOD(swoole_http_response, header);
static PHP_METHOD(swoole_http_response, gzip);
static PHP_METHOD(swoole_http_response, status);
static sw_inline char* http_get_method_name(int method)
{
switch (method)
{
case PHP_HTTP_GET:
return "GET";
case PHP_HTTP_POST:
return "POST";
case PHP_HTTP_HEAD:
return "HEAD";
case PHP_HTTP_PUT:
return "PUT";
case PHP_HTTP_DELETE:
return "DELETE";
case PHP_HTTP_PATCH:
return "PATCH";
case PHP_HTTP_CONNECT:
return "CONNECT";
case PHP_HTTP_OPTIONS:
return "OPTIONS";
case PHP_HTTP_TRACE:
return "TRACE";
case PHP_HTTP_COPY:
return "COPY";
case PHP_HTTP_LOCK:
return "LOCK";
case PHP_HTTP_MKCOL:
return "MKCOL";
case PHP_HTTP_MOVE:
return "MOVE";
case PHP_HTTP_PROPFIND:
return "PROPFIND";
case PHP_HTTP_PROPPATCH:
return "PROPPATCH";
case PHP_HTTP_UNLOCK:
return "UNLOCK";
/* subversion */
case PHP_HTTP_REPORT:
return "REPORT";
case PHP_HTTP_MKACTIVITY:
return "MKACTIVITY";
case PHP_HTTP_CHECKOUT:
return "CHECKOUT";
case PHP_HTTP_MERGE:
return "MERGE";
/* upnp */
case PHP_HTTP_MSEARCH:
return "MSEARCH";
case PHP_HTTP_NOTIFY:
return "NOTIFY";
case PHP_HTTP_SUBSCRIBE:
return "SUBSCRIBE";
case PHP_HTTP_UNSUBSCRIBE:
return "UNSUBSCRIBE";
case PHP_HTTP_NOT_IMPLEMENTED:
return "IMPLEMENTED";
default:
return NULL;
}
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_http_server_on, 0, 0, 2)
ZEND_ARG_INFO(0, ha_name)
ZEND_ARG_INFO(0, cb)
ZEND_END_ARG_INFO()
static const php_http_parser_settings http_parser_settings =
{
NULL,
http_request_on_path,
http_request_on_query_string,
NULL,
NULL,
http_request_on_header_field,
http_request_on_header_value,
http_request_on_headers_complete,
http_request_on_body,
http_request_message_complete
};
static const multipart_parser_settings mt_parser_settings =
{
multipart_body_on_header_field,
multipart_body_on_header_value,
multipart_body_on_data,
NULL,
multipart_body_on_header_complete,
multipart_body_on_data_end,
multipart_body_end
};
const zend_function_entry swoole_http_server_methods[] =
{
PHP_ME(swoole_http_server, on, arginfo_swoole_http_server_on, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_server, setglobal, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_server, start, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
const zend_function_entry swoole_http_request_methods[] =
{
PHP_ME(swoole_http_request, rawcontent, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
const zend_function_entry swoole_http_response_methods[] =
{
PHP_ME(swoole_http_response, cookie, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_response, rawcookie, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_response, status, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_response, gzip, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_response, header, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_response, write, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_http_response, end, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static int http_request_on_path(php_http_parser *parser, const char *at, size_t length)
{
swoole_http_client *client = parser->data;
client->request.path = estrndup(at, length);
client->request.path_len = length;
return 0;
}
static void http_global_clear(TSRMLS_D)
{
sw_zend_hash_del(&EG(symbol_table), "_GET", sizeof("_GET"));
sw_zend_hash_del(&EG(symbol_table), "_POST", sizeof("_POST"));
sw_zend_hash_del(&EG(symbol_table), "_COOKIE", sizeof("_COOKIE"));
sw_zend_hash_del(&EG(symbol_table), "_REQUEST", sizeof("_REQUEST"));
sw_zend_hash_del(&EG(symbol_table), "_SERVER", sizeof("_SERVER"));
}
static void http_global_merge(zval *val, zval *zrequest, int type)
{
zval *_request;
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
if (type == HTTP_GLOBAL_SERVER)
{
zval *php_global_server;
SW_MAKE_STD_ZVAL(php_global_server);
array_init(php_global_server);
char *key;
char _php_key[128];
int keytype;
uint32_t keylen;
ulong idx;
zval *value;
zval *server = sw_zend_read_property(swoole_http_request_class_entry_ptr, zrequest, ZEND_STRL("server"), 1 TSRMLS_CC);
if (server || !ZVAL_IS_NULL(server))
{
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(server), value)
keytype = sw_zend_hash_get_current_key(Z_ARRVAL_P(server), &key, &keylen, &idx);
if (HASH_KEY_IS_STRING != keytype)
{
continue;
}
strncpy(_php_key, key, sizeof(_php_key));
php_strtoupper(_php_key, keylen);
convert_to_string(value);
sw_add_assoc_stringl_ex(php_global_server, _php_key, keylen, Z_STRVAL_P(value), Z_STRLEN_P(value), 1);
SW_HASHTABLE_FOREACH_END();
}
zval *header = sw_zend_read_property(swoole_http_request_class_entry_ptr, zrequest, ZEND_STRL("header"), 1 TSRMLS_CC);
if (header || !ZVAL_IS_NULL(header))
{
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(header), value)
keytype = sw_zend_hash_get_current_key(Z_ARRVAL_P(header), &key, &keylen, &idx);
if (HASH_KEY_IS_STRING != keytype)
{
continue;
}
int i;
//replace '-' to '_'
for (i = 0; i < keylen; i++)
{
if (key[i] == '-')
{
key[i] = '_';
}
}
keylen = snprintf(_php_key, sizeof(_php_key), "HTTP_%s", key) + 1;
php_strtoupper(_php_key, keylen);
convert_to_string(value);
sw_add_assoc_stringl_ex(php_global_server, _php_key, keylen, Z_STRVAL_P(value), Z_STRLEN_P(value), 1);
SW_HASHTABLE_FOREACH_END();
}
ZEND_SET_SYMBOL(&EG(symbol_table), "_SERVER", php_global_server);
return;
}
switch (type)
{
case HTTP_GLOBAL_GET:
ZEND_SET_SYMBOL(&EG(symbol_table), "_GET", val);
break;
case HTTP_GLOBAL_POST:
ZEND_SET_SYMBOL(&EG(symbol_table), "_POST", val);
break;
case HTTP_GLOBAL_COOKIE:
ZEND_SET_SYMBOL(&EG(symbol_table), "_COOKIE", val);
break;
case HTTP_GLOBAL_REQUEST:
if (!http_merge_request_flag)
{
return;
}
_request = sw_zend_read_property(swoole_http_request_class_entry_ptr, zrequest, ZEND_STRL("request"), 1 TSRMLS_CC);
if (_request && !(ZVAL_IS_NULL(_request)))
{
ZEND_SET_SYMBOL(&EG(symbol_table), "_REQUEST", _request);
}
return;
case HTTP_GLOBAL_FILES:
ZEND_SET_SYMBOL(&EG(symbol_table), "_FILES", val);
return;
default:
swWarn("unknow global type [%d]", type);
return;
}
if (http_merge_request_flag & type)
{
_request = sw_zend_read_property(swoole_http_request_class_entry_ptr, zrequest, ZEND_STRL("request"), 1 TSRMLS_CC);
if (!_request || ZVAL_IS_NULL(_request))
{
_request = val;
}
else
{
sw_zend_hash_copy(Z_ARRVAL_P(_request), Z_ARRVAL_P(val), NULL, NULL, sizeof(zval));
}
zend_update_property(swoole_http_request_class_entry_ptr, zrequest, ZEND_STRL("request"), _request TSRMLS_CC);
}
}
static int http_request_on_query_string(php_http_parser *parser, const char *at, size_t length)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swoole_http_client *client = parser->data;
//no need free, will free by treat_data
char *query = estrndup(at, length);
sw_add_assoc_stringl_ex(client->request.zserver, ZEND_STRS("query_string"), query, length, 1);
zval *zget;
http_alloc_zval(client, request, zget);
array_init(zget);
zend_update_property(swoole_http_request_class_entry_ptr, client->request.zrequest_object, ZEND_STRL("get"), zget TSRMLS_CC);
//parse url params
sapi_module.treat_data(PARSE_STRING, query, zget TSRMLS_CC);
//merge php global variable
http_merge_php_global(zget, client->request.zrequest_object, HTTP_GLOBAL_GET);
return 0;
}
static int http_request_on_header_field(php_http_parser *parser, const char *at, size_t length)
{
swoole_http_client *client = parser->data;
if (client->current_header_name_allocated)
{
efree(client->current_header_name);
client->current_header_name_allocated = 0;
}
client->current_header_name = (char *)at;
client->current_header_name_len = length;
return 0;
}
static void http_parse_cookie(zval *array, const char *at, size_t length)
{
char keybuf[SW_HTTP_COOKIE_KEYLEN];
char *value;
char *_c = (char *) at;
int klen = 0;
int vlen = 0;
int state = 0;
int i = 0, j = 0;
while (_c < at + length)
{
if (state == 0 && *_c == '=')
{
klen = i - j + 1;
memcpy(keybuf, at + j, klen - 1);
keybuf[klen - 1] = 0;
j = i + 1;
state = 1;
}
else if (state == 1 && *_c == ';')
{
vlen = i - j;
value = estrndup((char * ) at + j, vlen);
vlen = php_url_decode(value, vlen);
sw_add_assoc_stringl_ex(array, keybuf, klen, value, vlen, 1);
j = i + 2;
state = 0;
}
_c++;
i++;
}
if (j < length)
{
vlen = i - j;
keybuf[klen - 1] = 0;
value = estrndup((char * ) at + j, vlen);
vlen = php_url_decode(value, vlen);
sw_add_assoc_stringl_ex(array, keybuf, klen, value, vlen, 1);
}
}
static int http_trim_double_quote(zval **value, char **ptr)
{
int len = Z_STRLEN_PP(value);
*ptr = Z_STRVAL_PP(value);
//ltrim('"')
if ((*ptr)[0] == '"')
{
(*ptr)++;
len--;
}
//rtrim('"')
if ((*ptr)[len - 1] == '"')
{
len--;
}
return len;
}
static int http_request_on_header_value(php_http_parser *parser, const char *at, size_t length)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swoole_http_client *client = parser->data;
char *header_name = zend_str_tolower_dup(client->current_header_name, client->current_header_name_len);
if (strncasecmp(header_name, "cookie", client->current_header_name_len) == 0)
{
zval *zcookie;
http_alloc_zval(client, request, zcookie);
array_init(zcookie);
zend_update_property(swoole_http_request_class_entry_ptr, client->request.zrequest_object, ZEND_STRL("cookie"), zcookie TSRMLS_CC);
http_parse_cookie(zcookie, at, length);
http_merge_php_global(zcookie, client->request.zrequest_object, HTTP_GLOBAL_COOKIE);
}
else if (strncasecmp(header_name, ZEND_STRL("upgrade")) == 0 && strncasecmp(at, ZEND_STRL("websocket")) == 0)
{
swConnection *conn = swWorker_get_connection(SwooleG.serv, client->fd);
if (!conn)
{
swWarn("connection[%d] is closed.", client->fd);
return SW_ERR;
}
conn->websocket_status = WEBSOCKET_STATUS_CONNECTION;
zval *header = client->request.zheader;
sw_add_assoc_stringl_ex(header, header_name, client->current_header_name_len + 1, (char *) at, length, 1);
}
else if ((parser->method == PHP_HTTP_POST || parser->method == PHP_HTTP_PUT || parser->method == PHP_HTTP_PATCH)
&& memcmp(header_name, ZEND_STRL("content-type")) == 0
&& strncasecmp(at, ZEND_STRL("application/x-www-form-urlencoded")) == 0)
{
client->request.post_form_urlencoded = 1;
zval *header = client->request.zheader;
sw_add_assoc_stringl_ex(header, header_name, client->current_header_name_len + 1, (char *) at, length, 1);
}
else if (parser->method == PHP_HTTP_POST && memcmp(header_name, ZEND_STRL("content-type")) == 0
&& strncasecmp(at, ZEND_STRL("multipart/form-data")) == 0)
{
int boundary_len = length - strlen("multipart/form-data; boundary=");
multipart_parser *p = multipart_parser_init(at + length - boundary_len, boundary_len, &mt_parser_settings);
client->mt_parser = p;
p->data = client;
zval *header = client->request.zheader;
sw_add_assoc_stringl_ex(header, header_name, client->current_header_name_len + 1, (char *) at, length, 1);
}
else
{
zval *header = client->request.zheader;
sw_add_assoc_stringl_ex(header, header_name, client->current_header_name_len + 1, (char *) at, length, 1);
}
if (client->current_header_name_allocated)
{
efree(client->current_header_name);
client->current_header_name_allocated = 0;
}
efree(header_name);
return 0;
}
static int http_request_on_headers_complete(php_http_parser *parser)
{
swoole_http_client *client = parser->data;
if (client->current_header_name_allocated)
{
efree(client->current_header_name);
client->current_header_name_allocated = 0;
}
client->current_header_name = NULL;
return 0;
}
static int multipart_body_on_header_field(multipart_parser* p, const char *at, size_t length)
{
swoole_http_client *client = (swoole_http_client *) p->data;
return http_request_on_header_field(&client->parser, at, length);
}
static int multipart_body_on_header_value(multipart_parser* p, const char *at, size_t length)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swoole_http_client *client = (swoole_http_client*) p->data;
zval *zfiles = client->request.zfiles;
if (!zfiles)
{
http_alloc_zval(client, request, zfiles);
array_init(zfiles);
zend_update_property(swoole_http_request_class_entry_ptr, client->request.zrequest_object, ZEND_STRL("files"), zfiles TSRMLS_CC);
}
char *headername = zend_str_tolower_dup(client->current_header_name, client->current_header_name_len);
if (strncasecmp(headername, ZEND_STRL("content-disposition")) == 0)
{
//not form data
if (swoole_strnpos((char *) at, length, ZEND_STRL("form-data;")) < 0)
{
return SW_OK;
}
zval *tmp_array;
SW_MAKE_STD_ZVAL(tmp_array);
array_init(tmp_array);
http_parse_cookie(tmp_array, (char *) at + sizeof("form-data;"), length - sizeof("form-data;"));
zval *form_name;
if (sw_zend_hash_find(Z_ARRVAL_P(tmp_array), ZEND_STRS("name"), (void **) &form_name) == FAILURE)
{
return SW_OK;
}
char *str;
int len = http_trim_double_quote(&form_name, &str);
zval *filename;
//POST form data
if (sw_zend_hash_find(Z_ARRVAL_P(tmp_array), ZEND_STRS("filename"), (void **) &filename) == FAILURE)
{
client->current_form_data_name = estrndup(str, len);
client->current_form_data_name_len = len;
}
//upload file
else
{
client->current_input_name = estrndup(str, len);
zval *multipart_header;
SW_MAKE_STD_ZVAL(multipart_header);
array_init(multipart_header);
add_assoc_zval(zfiles, client->current_input_name, multipart_header);
sw_add_assoc_string(multipart_header, "name", "", 1);
sw_add_assoc_string(multipart_header, "type", "", 1);
sw_add_assoc_string(multipart_header, "tmp_name", "", 1);
add_assoc_long(multipart_header, "error", HTTP_UPLOAD_ERR_OK);
add_assoc_long(multipart_header, "size", 0);
len = http_trim_double_quote(&filename, &str);
sw_add_assoc_stringl(multipart_header, "name", str, len, 1);
}
sw_zval_ptr_dtor(&tmp_array);
}
if (strncasecmp(headername, ZEND_STRL("content-type")) == 0)
{
zval *multipart_header;
sw_zend_hash_find(Z_ARRVAL_P(zfiles), client->current_input_name, strlen(client->current_input_name) + 1, (void **) &multipart_header);
sw_add_assoc_stringl(multipart_header, "type", (char * ) at, length, 1);
}
if (client->current_header_name_allocated)
{
efree(client->current_header_name);
client->current_header_name_allocated = 0;
}
efree(headername);
return 0;
}
static int multipart_body_on_data(multipart_parser* p, const char *at, size_t length)
{
swoole_http_client *client = (swoole_http_client *) p->data;
if (client->current_form_data_name)
{
swString_append_ptr(swoole_http_form_data_buffer, (char*) at, length);
return 0;
}
if (p->fp == NULL)
{
return 0;
}
int n = fwrite(at, sizeof(char), length, (FILE *) p->fp);
if (n != length)
{
swoole_http_client *client = (swoole_http_client*) p->data;
zval *files = client->request.zfiles;
zval *multipart_header;
sw_zend_hash_find(Z_ARRVAL_P(files), client->current_input_name, strlen(client->current_input_name) + 1, (void **) &multipart_header);
add_assoc_long(multipart_header, "error", HTTP_UPLOAD_ERR_CANT_WRITE);
fclose((FILE *) p->fp);
p->fp = NULL;
swWarn("write upload file failed. Error %s[%d]", strerror(errno), errno);
}
return 0;
}
void get_random_file_name(char *des, const char *src)
{
unsigned char digest[16] = {0};
char buf[19] = {0};
sprintf(buf, "%s%d", src, swoole_system_random(0,9999));
PHP_MD5_CTX ctx;
PHP_MD5Init(&ctx);
PHP_MD5Update(&ctx, buf, strlen(buf));
PHP_MD5Final(digest, &ctx);
make_digest_ex(des, digest, 16);
}
static int multipart_body_on_header_complete(multipart_parser* p)
{
swoole_http_client *client = (swoole_http_client *) p->data;
if (!client->current_input_name)
{
return 0;
}
zval *files = client->request.zfiles;
zval *multipart_header;
if (sw_zend_hash_find(Z_ARRVAL_P(files), client->current_input_name, strlen(client->current_input_name) + 1, (void **) &multipart_header) == FAILURE)
{
return 0;
}
zval *zerr;
sw_zend_hash_find(Z_ARRVAL_P(multipart_header), ZEND_STRS("error"), (void **) &zerr);
if (Z_LVAL_P(zerr) != HTTP_UPLOAD_ERR_OK)
{
return 0;
}
char file_path[sizeof(SW_HTTP_UPLOAD_TMP_FILE)];
memcpy(file_path, SW_HTTP_UPLOAD_TMP_FILE, sizeof(SW_HTTP_UPLOAD_TMP_FILE));
int tmpfile = swoole_tmpfile(file_path);
FILE *fp = fdopen(tmpfile, "wb+");
if (fp < 0)
{
add_assoc_long(multipart_header, "error", HTTP_UPLOAD_ERR_NO_TMP_DIR);
swWarn("fopen(%s) failed. Error %s[%d]", file_path, strerror(errno), errno);
return 0;
}
p->fp = fp;
sw_add_assoc_string(multipart_header, "tmp_name", file_path, 1);
return 0;
}
static int multipart_body_on_data_end(multipart_parser* p)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swoole_http_client *client = (swoole_http_client *) p->data;
if (client->current_form_data_name)
{
zval *zpost =sw_zend_read_property(swoole_http_request_class_entry_ptr, client->request.zrequest_object, ZEND_STRL("post"), 1 TSRMLS_CC);
if (ZVAL_IS_NULL(zpost))
{
http_alloc_zval(client, request, zpost);
array_init(zpost);
zend_update_property(swoole_http_request_class_entry_ptr, client->request.zrequest_object, ZEND_STRL("post"), zpost TSRMLS_CC);
}
sw_add_assoc_stringl_ex(zpost, client->current_form_data_name, client->current_form_data_name_len + 1,
swoole_http_form_data_buffer->str, swoole_http_form_data_buffer->length, 1);
efree(client->current_form_data_name);
client->current_form_data_name = NULL;
client->current_form_data_name_len = 0;
swString_clear(swoole_http_form_data_buffer);
return 0;
}
if (!client->current_input_name)
{
return 0;
}
zval *files = client->request.zfiles;
if (ZVAL_IS_NULL(files))
{
return 0;
}
zval *multipart_header;
sw_zend_hash_find(Z_ARRVAL_P(files), client->current_input_name, strlen(client->current_input_name) + 1, (void **) &multipart_header);
if (p->fp != NULL)
{
long size = swoole_file_get_size((FILE*) p->fp);
add_assoc_long(multipart_header, "size", size);
fclose((FILE *)p->fp);
p->fp = NULL;
}
efree(client->current_input_name);
return 0;
}
static int multipart_body_end(multipart_parser* p)
{
swoole_http_client *client = (swoole_http_client *) p->data;
zval *files = client->request.zfiles;
http_merge_php_global(files, client->request.zrequest_object, HTTP_GLOBAL_FILES);
return 0;
}
static int http_request_on_body(php_http_parser *parser, const char *at, size_t length)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swoole_http_client *client = parser->data;
char *body = estrndup(at, length);
if (SwooleG.serv->http_parse_post && client->request.post_form_urlencoded)
{
zval *post;
SW_MAKE_STD_ZVAL(post);
array_init(post);
zend_update_property(swoole_http_request_class_entry_ptr, client->request.zrequest_object, ZEND_STRL("post"), post TSRMLS_CC);
sapi_module.treat_data(PARSE_STRING, body, post TSRMLS_CC);
http_merge_php_global(post, client->request.zrequest_object, HTTP_GLOBAL_POST);
}
else
{
client->request.post_content = body;
client->request.post_length = length;
}
if (client->mt_parser != NULL)
{
multipart_parser *multipart_parser = client->mt_parser;
size_t n = multipart_parser_execute(multipart_parser, body, length);
if (n != length)
{
swoole_php_fatal_error(E_ERROR, "fail to parse multipart body");
}
}
return 0;
}
static int http_request_message_complete(php_http_parser *parser)
{
swoole_http_client *client = parser->data;
client->request.version = parser->http_major * 100 + parser->http_minor;
const char *vpath = client->request.path, *end = vpath + client->request.path_len, *p = end;
client->request.ext = end;
client->request.ext_len = 0;
while (p > vpath)
{
--p;
if (*p == '.')
{
++p;
client->request.ext = p;
client->request.ext_len = end - p;
break;
}
}
client->request_read = 1;
if (client->mt_parser)
{
multipart_parser_free(client->mt_parser);
client->mt_parser = NULL;
}
return 0;
}
static void http_onClose(swServer *serv, int fd, int from_id)
{
swConnection *conn = swWorker_get_connection(SwooleG.serv, fd);
if (!conn)
{
swWarn("connection[%d] is closed.", fd);
return;
}
swoole_http_client *client = swArray_fetch(http_client_array, conn->fd);
if (client)
{
if (client->request.zrequest_object && !client->end)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swoole_http_request_free(client TSRMLS_CC);
}
}
if (php_sw_callback[SW_SERVER_CB_onClose] != NULL)
{
php_swoole_onClose(serv, fd, from_id);
}
}
static int http_onReceive(swServer *serv, swEventData *req)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
int fd = req->info.fd;
swConnection *conn = swWorker_get_connection(SwooleG.serv, fd);
if (!conn)
{
swWarn("connection[%d] is closed.", fd);
return SW_ERR;
}
if (conn->websocket_status == WEBSOCKET_STATUS_FRAME) //websocket callback
{
return swoole_websocket_onMessage(req);
}
swoole_http_client *client = swArray_alloc(http_client_array, conn->fd);
if (!client)
{
return SW_OK;
}
client->fd = fd;
php_http_parser *parser = &client->parser;
/**
* create request and response object
*/
http_request_new(client TSRMLS_CC);
zval *zserver = client->request.zserver;
parser->data = client;
php_http_parser_init(parser, PHP_HTTP_REQUEST);
zval *zdata;
SW_MAKE_STD_ZVAL(zdata);
zdata = php_swoole_get_recv_data(zdata, req TSRMLS_CC);
swTrace("httpRequest %d bytes:\n---------------------------------------\n%s\n", Z_STRLEN_P(zdata), Z_STRVAL_P(zdata));
long n = php_http_parser_execute(parser, &http_parser_settings, Z_STRVAL_P(zdata), Z_STRLEN_P(zdata));
sw_zval_ptr_dtor(&zdata);
if (n < 0)
{
swWarn("php_http_parser_execute failed.");
if (conn->websocket_status == WEBSOCKET_STATUS_CONNECTION)
{
return SwooleG.serv->factory.end(&SwooleG.serv->factory, fd);
}
}
else
{
zval *retval;
zval **args[2];
zval *zreques_object = client->request.zrequest_object;
char *method_name = http_get_method_name(parser->method);
sw_add_assoc_string(zserver, "request_method", method_name, 1);
sw_add_assoc_stringl(zserver, "request_uri", client->request.path, client->request.path_len, 1);
sw_add_assoc_stringl(zserver, "path_info", client->request.path, client->request.path_len, 1);
add_assoc_long_ex(zserver, ZEND_STRS("request_time"), SwooleGS->now);
swConnection *conn = swWorker_get_connection(SwooleG.serv, fd);
if (!conn)
{
swWarn("connection[%d] is closed.", fd);
return SW_ERR;
}
add_assoc_long(client->request.zserver, "server_port", swConnection_get_port(&SwooleG.serv->connection_list[conn->from_fd]));
add_assoc_long(client->request.zserver, "remote_port", swConnection_get_port(conn));
sw_add_assoc_string(zserver, "remote_addr", swConnection_get_ip(conn), 1);
if (client->request.version == 101)
{
sw_add_assoc_string(zserver, "server_protocol", "HTTP/1.1", 1);
}
else
{
sw_add_assoc_string(zserver, "server_protocol", "HTTP/1.0", 1);
}
sw_add_assoc_string(zserver, "server_software", SW_HTTP_SERVER_SOFTWARE, 1);