add brotli encoding
[m6w6/ext-http] / src / php_http_message_body.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #include "ext/standard/php_lcg.h"
16
17 #define BOUNDARY_OPEN(body) \
18 do {\
19 size_t size = php_http_message_body_size(body); \
20 if (size) { \
21 php_stream_truncate_set_size(php_http_message_body_stream(body), size - lenof("--" PHP_HTTP_CRLF)); \
22 php_http_message_body_append(body, ZEND_STRL(PHP_HTTP_CRLF)); \
23 } else { \
24 php_http_message_body_appendf(body, "--%s" PHP_HTTP_CRLF, php_http_message_body_boundary(body)); \
25 } \
26 } while(0)
27
28 #define BOUNDARY_CLOSE(body) \
29 php_http_message_body_appendf(body, PHP_HTTP_CRLF "--%s--" PHP_HTTP_CRLF, php_http_message_body_boundary(body))
30
31 static ZEND_RESULT_CODE add_recursive_fields(php_http_message_body_t *body, const char *name, HashTable *fields);
32 static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const char *name, HashTable *files);
33
34 php_http_message_body_t *php_http_message_body_init(php_http_message_body_t **body_ptr, php_stream *stream)
35 {
36 php_http_message_body_t *body;
37
38 if (body_ptr && *body_ptr) {
39 body = *body_ptr;
40 php_http_message_body_addref(body);
41 return body;
42 }
43
44 body = ecalloc(1, sizeof(php_http_message_body_t));
45 body->refcount = 1;
46
47 if (stream) {
48 body->res = stream->res;
49 GC_ADDREF(body->res);
50 } else {
51 body->res = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff)->res;
52 }
53 php_stream_auto_cleanup(php_http_message_body_stream(body));
54
55 if (body_ptr) {
56 *body_ptr = body;
57 }
58
59 return body;
60 }
61
62 unsigned php_http_message_body_addref(php_http_message_body_t *body)
63 {
64 return ++body->refcount;
65 }
66
67 php_http_message_body_t *php_http_message_body_copy(php_http_message_body_t *from, php_http_message_body_t *to)
68 {
69 if (from) {
70 if (to) {
71 php_stream_truncate_set_size(php_http_message_body_stream(to), 0);
72 } else {
73 to = php_http_message_body_init(NULL, NULL);
74 }
75 php_http_message_body_to_stream(from, php_http_message_body_stream(to), 0, 0);
76
77 if (to->boundary) {
78 efree(to->boundary);
79 }
80 if (from->boundary) {
81 to->boundary = estrdup(from->boundary);
82 }
83 } else {
84 to = NULL;
85 }
86 return to;
87 }
88
89 void php_http_message_body_free(php_http_message_body_t **body_ptr)
90 {
91 if (*body_ptr) {
92 php_http_message_body_t *body = *body_ptr;
93 if (!--body->refcount) {
94 zend_list_delete(body->res);
95 body->res = NULL;
96 PTR_FREE(body->boundary);
97 efree(body);
98 }
99 *body_ptr = NULL;
100 }
101 }
102
103 const php_stream_statbuf *php_http_message_body_stat(php_http_message_body_t *body)
104 {
105 php_stream_stat(php_http_message_body_stream(body), &body->ssb);
106 return &body->ssb;
107 }
108
109 const char *php_http_message_body_boundary(php_http_message_body_t *body)
110 {
111 if (!body->boundary) {
112 union { double dbl; int num[2]; } data;
113
114 data.dbl = php_combined_lcg();
115 spprintf(&body->boundary, 0, "%x.%x", data.num[0], data.num[1]);
116 }
117 return body->boundary;
118 }
119
120 char *php_http_message_body_etag(php_http_message_body_t *body)
121 {
122 php_http_etag_t *etag;
123 php_stream *s = php_http_message_body_stream(body);
124
125 /* real file or temp buffer ? */
126 if (s->ops != &php_stream_temp_ops && s->ops != &php_stream_memory_ops) {
127 php_stream_stat(php_http_message_body_stream(body), &body->ssb);
128
129 if (body->ssb.sb.st_mtime) {
130 char *etag;
131
132 spprintf(&etag, 0, "%lx-%lx-%lx", body->ssb.sb.st_ino, body->ssb.sb.st_mtime, body->ssb.sb.st_size);
133 return etag;
134 }
135 }
136
137 /* content based */
138 if ((etag = php_http_etag_init(PHP_HTTP_G->env.etag_mode))) {
139 php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_etag_update, etag, 0, 0);
140 return php_http_etag_finish(etag);
141 }
142
143 return NULL;
144 }
145
146 zend_string *php_http_message_body_to_string(php_http_message_body_t *body, off_t offset, size_t forlen)
147 {
148 php_stream *s = php_http_message_body_stream(body);
149
150 php_stream_seek(s, offset, SEEK_SET);
151 if (!forlen) {
152 forlen = -1;
153 }
154 return php_stream_copy_to_mem(s, forlen, 0);
155 }
156
157 ZEND_RESULT_CODE php_http_message_body_to_stream(php_http_message_body_t *body, php_stream *dst, off_t offset, size_t forlen)
158 {
159 php_stream *s = php_http_message_body_stream(body);
160
161 php_stream_seek(s, offset, SEEK_SET);
162
163 if (!forlen) {
164 forlen = -1;
165 }
166 return php_stream_copy_to_stream_ex(s, dst, forlen, NULL);
167 }
168
169 ZEND_RESULT_CODE php_http_message_body_to_callback(php_http_message_body_t *body, php_http_pass_callback_t cb, void *cb_arg, off_t offset, size_t forlen)
170 {
171 php_stream *s = php_http_message_body_stream(body);
172 char *buf = emalloc(0x1000);
173
174 php_stream_seek(s, offset, SEEK_SET);
175
176 if (!forlen) {
177 forlen = -1;
178 }
179 while (!php_stream_eof(s)) {
180 size_t read = php_stream_read(s, buf, MIN(forlen, 0x1000));
181
182 if (read) {
183 if (-1 == cb(cb_arg, buf, read)) {
184 return FAILURE;
185 }
186 }
187
188 if (read < MIN(forlen, sizeof(buf))) {
189 break;
190 }
191
192 if (forlen && !(forlen -= read)) {
193 break;
194 }
195 }
196 efree(buf);
197
198 return SUCCESS;
199 }
200
201 size_t php_http_message_body_append(php_http_message_body_t *body, const char *buf, size_t len)
202 {
203 php_stream *s;
204 size_t written;
205
206 if (!(s = php_http_message_body_stream(body))) {
207 return -1;
208 }
209
210 if (s->ops->seek) {
211 php_stream_seek(s, 0, SEEK_END);
212 }
213
214 written = php_stream_write(s, buf, len);
215
216 if (written != len) {
217 php_error_docref(NULL, E_WARNING, "Failed to append %zu bytes to body; wrote %zu", len, written);
218 }
219
220 return len;
221 }
222
223 size_t php_http_message_body_appendf(php_http_message_body_t *body, const char *fmt, ...)
224 {
225 va_list argv;
226 char *print_str;
227 size_t print_len;
228
229 va_start(argv, fmt);
230 print_len = vspprintf(&print_str, 0, fmt, argv);
231 va_end(argv);
232
233 print_len = php_http_message_body_append(body, print_str, print_len);
234 efree(print_str);
235
236 return print_len;
237 }
238
239 ZEND_RESULT_CODE php_http_message_body_add_form(php_http_message_body_t *body, HashTable *fields, HashTable *files)
240 {
241 if (fields) {
242 if (SUCCESS != add_recursive_fields(body, NULL, fields)) {
243 return FAILURE;
244 }
245 }
246 if (files) {
247 if (SUCCESS != add_recursive_files(body, NULL, files)) {
248 return FAILURE;
249 }
250 }
251
252 return SUCCESS;
253 }
254
255 void php_http_message_body_add_part(php_http_message_body_t *body, php_http_message_t *part)
256 {
257 BOUNDARY_OPEN(body);
258 php_http_message_to_callback(part, (php_http_pass_callback_t) php_http_message_body_append, body);
259 BOUNDARY_CLOSE(body);
260 }
261
262
263 ZEND_RESULT_CODE php_http_message_body_add_form_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len)
264 {
265 zend_string *safe_name = zend_string_init(name, strlen(name), 0);
266
267 safe_name = php_addslashes(safe_name, 1);
268
269 BOUNDARY_OPEN(body);
270 php_http_message_body_appendf(
271 body,
272 "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
273 "" PHP_HTTP_CRLF,
274 safe_name->val
275 );
276 php_http_message_body_append(body, value_str, value_len);
277 BOUNDARY_CLOSE(body);
278
279 zend_string_release(safe_name);
280 return SUCCESS;
281 }
282
283 ZEND_RESULT_CODE php_http_message_body_add_form_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *path, php_stream *in)
284 {
285 size_t path_len = strlen(path);
286 char *path_dup = estrndup(path, path_len);
287 zend_string *base_name, *safe_name = zend_string_init(name, strlen(name), 0);
288
289 safe_name = php_addslashes(safe_name, 1);
290 base_name = php_basename(path_dup, path_len, NULL, 0);
291
292 BOUNDARY_OPEN(body);
293 php_http_message_body_appendf(
294 body,
295 "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
296 "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
297 "Content-Type: %s" PHP_HTTP_CRLF
298 PHP_HTTP_CRLF,
299 safe_name->val, base_name->val, ctype
300 );
301 php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL);
302 BOUNDARY_CLOSE(body);
303
304 zend_string_release(safe_name);
305 zend_string_release(base_name);
306 efree(path_dup);
307
308 return SUCCESS;
309 }
310
311 static inline char *format_key(php_http_arrkey_t *key, const char *prefix) {
312 char *new_key = NULL;
313
314 if (prefix && *prefix) {
315 if (key->key) {
316 spprintf(&new_key, 0, "%s[%s]", prefix, key->key->val);
317 } else {
318 spprintf(&new_key, 0, "%s[%lu]", prefix, key->h);
319 }
320 } else if (key->key) {
321 new_key = estrdup(key->key->val);
322 } else {
323 new_key = estrdup("");
324 }
325
326 return new_key;
327 }
328
329 static ZEND_RESULT_CODE add_recursive_field_value(php_http_message_body_t *body, const char *name, zval *value)
330 {
331 zend_string *zs = zval_get_string(value);
332 ZEND_RESULT_CODE rc = php_http_message_body_add_form_field(body, name, zs->val, zs->len);
333 zend_string_release(zs);
334 return rc;
335 }
336
337 static ZEND_RESULT_CODE add_recursive_fields(php_http_message_body_t *body, const char *name, HashTable *fields)
338 {
339 zval *val;
340 php_http_arrkey_t key;
341
342 if (!HT_IS_RECURSIVE(fields)) {
343 HT_PROTECT_RECURSION(fields);
344 ZEND_HASH_FOREACH_KEY_VAL_IND(fields, key.h, key.key, val)
345 {
346 char *str = format_key(&key, name);
347
348 if (Z_TYPE_P(val) != IS_ARRAY && Z_TYPE_P(val) != IS_OBJECT) {
349 if (SUCCESS != add_recursive_field_value(body, str, val)) {
350 efree(str);
351 HT_UNPROTECT_RECURSION(fields);
352 return FAILURE;
353 }
354 } else if (SUCCESS != add_recursive_fields(body, str, HASH_OF(val))) {
355 efree(str);
356 HT_UNPROTECT_RECURSION(fields);
357 return FAILURE;
358 }
359 efree(str);
360 }
361 ZEND_HASH_FOREACH_END();
362 HT_UNPROTECT_RECURSION(fields);
363 }
364
365 return SUCCESS;
366 }
367
368 static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const char *name, HashTable *files)
369 {
370 zval *zdata = NULL, *zfile, *zname, *ztype;
371
372 /* single entry */
373 if (!(zname = zend_hash_str_find(files, ZEND_STRL("name")))
374 || !(ztype = zend_hash_str_find(files, ZEND_STRL("type")))
375 || !(zfile = zend_hash_str_find(files, ZEND_STRL("file")))
376 ) {
377 zval *val;
378 php_http_arrkey_t key;
379
380 if (!HT_IS_RECURSIVE(files)) {
381 HT_PROTECT_RECURSION(files);
382 ZEND_HASH_FOREACH_KEY_VAL_IND(files, key.h, key.key, val)
383 {
384 if (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT) {
385 char *str = format_key(&key, name);
386
387 if (SUCCESS != add_recursive_files(body, str, HASH_OF(val))) {
388 efree(str);
389 HT_UNPROTECT_RECURSION(files);
390 return FAILURE;
391 }
392 efree(str);
393 }
394 }
395 ZEND_HASH_FOREACH_END();
396 HT_UNPROTECT_RECURSION(files);
397 }
398 return SUCCESS;
399 } else {
400 /* stream entry */
401 php_stream *stream;
402 zend_string *zfc = zval_get_string(zfile);
403
404 if ((zdata = zend_hash_str_find(files, ZEND_STRL("data")))) {
405 if (Z_TYPE_P(zdata) == IS_RESOURCE) {
406 php_stream_from_zval_no_verify(stream, zdata);
407 } else {
408 zend_string *tmp = zval_get_string(zdata);
409
410 stream = php_stream_memory_open(TEMP_STREAM_READONLY, tmp->val, tmp->len);
411 zend_string_release(tmp);
412 }
413 } else {
414 stream = php_stream_open_wrapper(zfc->val, "r", REPORT_ERRORS|USE_PATH, NULL);
415 }
416
417 if (!stream) {
418 zend_string_release(zfc);
419 return FAILURE;
420 } else {
421 zend_string *znc = zval_get_string(zname), *ztc = zval_get_string(ztype);
422 php_http_arrkey_t arrkey = {0, znc};
423 char *key = format_key(&arrkey, name);
424 ZEND_RESULT_CODE ret = php_http_message_body_add_form_file(body, key, ztc->val, zfc->val, stream);
425
426 efree(key);
427 zend_string_release(znc);
428 zend_string_release(ztc);
429 zend_string_release(zfc);
430 if (!zdata || Z_TYPE_P(zdata) != IS_RESOURCE) {
431 php_stream_close(stream);
432 }
433 return ret;
434 }
435
436 }
437 }
438
439 struct splitbody_arg {
440 php_http_buffer_t buf;
441 php_http_message_parser_t *parser;
442 char *boundary_str;
443 size_t boundary_len;
444 size_t consumed;
445 };
446
447 static size_t splitbody(void *opaque, char *buf, size_t len)
448 {
449 struct splitbody_arg *arg = opaque;
450 const char *boundary = NULL;
451 size_t consumed = 0;
452 int first_boundary;
453
454 do {
455 first_boundary = !(consumed || arg->consumed);
456
457 if ((boundary = php_http_locate_str(buf, len, arg->boundary_str + first_boundary, arg->boundary_len - first_boundary))) {
458 size_t real_boundary_len = arg->boundary_len - 1, cut;
459 const char *real_boundary = boundary + !first_boundary;
460 int eol_len = 0;
461
462 if (buf + len <= real_boundary + real_boundary_len) {
463 /* if we just have enough data for the boundary, it's just a byte too less */
464 arg->consumed += consumed;
465 return consumed;
466 }
467
468 if (!first_boundary) {
469 /* this is not the first boundary, read rest of this message */
470 php_http_buffer_append(&arg->buf, buf, real_boundary - buf);
471 php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
472 }
473
474 /* move after the boundary */
475 cut = real_boundary - buf + real_boundary_len;
476 buf += cut;
477 len -= cut;
478 consumed += cut;
479
480 if (buf == php_http_locate_bin_eol(buf, len, &eol_len)) {
481 /* skip CRLF */
482 buf += eol_len;
483 len -= eol_len;
484 consumed += eol_len;
485
486 if (!first_boundary) {
487 /* advance messages */
488 php_http_message_t *msg;
489
490 msg = php_http_message_init(NULL, 0, NULL);
491 msg->parent = arg->parser->message;
492 arg->parser->message = msg;
493 }
494 } else {
495 /* is this the last boundary? */
496 if (*buf == '-') {
497 /* ignore the rest */
498 consumed += len;
499 len = 0;
500 } else {
501 /* let this be garbage */
502 php_error_docref(NULL, E_WARNING, "Malformed multipart boundary at pos %zu", consumed);
503 return -1;
504 }
505 }
506 }
507 } while (boundary && len);
508
509 /* let there be room for the next boundary */
510 if (len > arg->boundary_len) {
511 consumed += len - arg->boundary_len;
512 php_http_buffer_append(&arg->buf, buf, len - arg->boundary_len);
513 php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
514 }
515
516 arg->consumed += consumed;
517 return consumed;
518 }
519
520 php_http_message_t *php_http_message_body_split(php_http_message_body_t *body, const char *boundary)
521 {
522 php_stream *s = php_http_message_body_stream(body);
523 php_http_buffer_t *tmp = NULL;
524 php_http_message_t *msg = NULL;
525 struct splitbody_arg arg;
526
527 php_http_buffer_init(&arg.buf);
528 arg.parser = php_http_message_parser_init(NULL);
529 arg.boundary_len = spprintf(&arg.boundary_str, 0, "\n--%s", boundary);
530 arg.consumed = 0;
531
532 php_stream_rewind(s);
533 while (!php_stream_eof(s)) {
534 php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg);
535 }
536
537 msg = arg.parser->message;
538 arg.parser->message = NULL;
539
540 php_http_buffer_free(&tmp);
541 php_http_message_parser_free(&arg.parser);
542 php_http_buffer_dtor(&arg.buf);
543 PTR_FREE(arg.boundary_str);
544
545 return msg;
546 }
547
548 static zend_class_entry *php_http_message_body_class_entry;
549 zend_class_entry *php_http_get_message_body_class_entry(void)
550 {
551 return php_http_message_body_class_entry;
552 }
553
554 static zend_object_handlers php_http_message_body_object_handlers;
555
556 zend_object *php_http_message_body_object_new(zend_class_entry *ce)
557 {
558 return &php_http_message_body_object_new_ex(ce, NULL)->zo;
559 }
560
561 php_http_message_body_object_t *php_http_message_body_object_new_ex(zend_class_entry *ce, php_http_message_body_t *body)
562 {
563 php_http_message_body_object_t *o;
564
565 o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
566 zend_object_std_init(&o->zo, php_http_message_body_class_entry);
567 object_properties_init(&o->zo, ce);
568
569 o->gc = emalloc(sizeof(zval));
570
571 if (body) {
572 o->body = body;
573 }
574
575 o->zo.handlers = &php_http_message_body_object_handlers;
576
577 return o;
578 }
579
580 zend_object *php_http_message_body_object_clone(zval *object)
581 {
582 php_http_message_body_object_t *new_obj;
583 php_http_message_body_object_t *old_obj = PHP_HTTP_OBJ(NULL, object);
584 php_http_message_body_t *body = php_http_message_body_copy(old_obj->body, NULL);
585
586 new_obj = php_http_message_body_object_new_ex(old_obj->zo.ce, body);
587 zend_objects_clone_members(&new_obj->zo, &old_obj->zo);
588
589 return &new_obj->zo;
590 }
591
592 static HashTable *php_http_message_body_object_get_gc(zval *object, zval **table, int *n)
593 {
594 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, object);
595 HashTable *props = Z_OBJPROP_P(object);
596 uint32_t count = zend_hash_num_elements(props);
597
598 obj->gc = erealloc(obj->gc, (1 + count) * sizeof(zval));
599
600 if (php_http_message_body_stream(obj->body)) {
601 *n = 1;
602 php_stream_to_zval(php_http_message_body_stream(obj->body), obj->gc);
603 } else {
604 *n = 0;
605 }
606
607 if (count) {
608 zval *val;
609
610 ZEND_HASH_FOREACH_VAL(props, val)
611 {
612 ZVAL_COPY_VALUE(&obj->gc[(*n)++], val);
613 }
614 ZEND_HASH_FOREACH_END();
615 }
616 *table = obj->gc;
617
618 return NULL;
619 }
620
621 void php_http_message_body_object_free(zend_object *object)
622 {
623 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(object, NULL);
624
625 PTR_FREE(obj->gc);
626 php_http_message_body_free(&obj->body);
627 zend_object_std_dtor(object);
628 }
629
630 #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \
631 do { \
632 if (!obj->body) { \
633 obj->body = php_http_message_body_init(NULL, NULL); \
634 php_stream_to_zval(php_http_message_body_stream(obj->body), obj->gc); \
635 } \
636 } while(0)
637
638 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___construct, 0, 0, 0)
639 ZEND_ARG_INFO(0, stream)
640 ZEND_END_ARG_INFO();
641 PHP_METHOD(HttpMessageBody, __construct)
642 {
643 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
644 zval *zstream = NULL;
645 php_stream *stream;
646
647 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream), invalid_arg, return);
648
649 if (zstream) {
650 php_http_expect(php_stream_from_zval_no_verify(stream, zstream), unexpected_val, return);
651
652 if (obj->body) {
653 php_http_message_body_free(&obj->body);
654 }
655 obj->body = php_http_message_body_init(NULL, stream);
656 php_stream_to_zval(stream, obj->gc);
657 }
658 }
659
660 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___toString, 0, 0, 0)
661 ZEND_END_ARG_INFO();
662 PHP_METHOD(HttpMessageBody, __toString)
663 {
664 if (SUCCESS == zend_parse_parameters_none()) {
665 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
666 zend_string *zs;
667
668 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
669
670 zs = php_http_message_body_to_string(obj->body, 0, 0);
671 if (zs) {
672 RETURN_STR(zs);
673 }
674 }
675 RETURN_EMPTY_STRING();
676 }
677
678 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_unserialize, 0, 0, 1)
679 ZEND_ARG_INFO(0, serialized)
680 ZEND_END_ARG_INFO();
681 PHP_METHOD(HttpMessageBody, unserialize)
682 {
683 char *us_str;
684 size_t us_len;
685
686 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &us_str, &us_len)) {
687 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
688 php_stream *s = php_stream_memory_open(0, us_str, us_len);
689
690 obj->body = php_http_message_body_init(NULL, s);
691 php_stream_to_zval(s, obj->gc);
692 }
693 }
694
695 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toStream, 0, 0, 1)
696 ZEND_ARG_INFO(0, stream)
697 ZEND_ARG_INFO(0, offset)
698 ZEND_ARG_INFO(0, maxlen)
699 ZEND_END_ARG_INFO();
700 PHP_METHOD(HttpMessageBody, toStream)
701 {
702 zval *zstream;
703 zend_long offset = 0, forlen = 0;
704
705 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &zstream, &offset, &forlen)) {
706 php_stream *stream;
707 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
708
709 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
710
711 php_stream_from_zval(stream, zstream);
712 php_http_message_body_to_stream(obj->body, stream, offset, forlen);
713 RETURN_ZVAL(getThis(), 1, 0);
714 }
715 }
716
717 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toCallback, 0, 0, 1)
718 ZEND_ARG_INFO(0, callback)
719 ZEND_ARG_INFO(0, offset)
720 ZEND_ARG_INFO(0, maxlen)
721 ZEND_END_ARG_INFO();
722 PHP_METHOD(HttpMessageBody, toCallback)
723 {
724 php_http_pass_fcall_arg_t fcd;
725 zend_long offset = 0, forlen = 0;
726
727 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) {
728 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
729
730 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
731
732 ZVAL_COPY(&fcd.fcz, getThis());
733 php_http_message_body_to_callback(obj->body, php_http_pass_fcall_callback, &fcd, offset, forlen);
734 zend_fcall_info_args_clear(&fcd.fci, 1);
735 zval_ptr_dtor(&fcd.fcz);
736 RETURN_ZVAL(getThis(), 1, 0);
737 }
738 }
739
740 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getResource, 0, 0, 0)
741 ZEND_END_ARG_INFO();
742 PHP_METHOD(HttpMessageBody, getResource)
743 {
744 if (SUCCESS == zend_parse_parameters_none()) {
745 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
746
747 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
748
749 php_stream_to_zval(php_http_message_body_stream(obj->body), return_value);
750 Z_ADDREF_P(return_value);
751 }
752 }
753
754 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getBoundary, 0, 0, 0)
755 ZEND_END_ARG_INFO();
756 PHP_METHOD(HttpMessageBody, getBoundary)
757 {
758 if (SUCCESS == zend_parse_parameters_none()) {
759 php_http_message_body_object_t * obj = PHP_HTTP_OBJ(NULL, getThis());
760
761 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
762
763 if (obj->body->boundary) {
764 RETURN_STRING(obj->body->boundary);
765 }
766 }
767 }
768
769 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_append, 0, 0, 1)
770 ZEND_ARG_INFO(0, string)
771 ZEND_END_ARG_INFO();
772 PHP_METHOD(HttpMessageBody, append)
773 {
774 char *str;
775 size_t len;
776 php_http_message_body_object_t *obj;
777
778 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &len), invalid_arg, return);
779
780 obj = PHP_HTTP_OBJ(NULL, getThis());
781 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
782
783 php_http_expect(len == php_http_message_body_append(obj->body, str, len), runtime, return);
784
785 RETURN_ZVAL(getThis(), 1, 0);
786 }
787
788 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addForm, 0, 0, 0)
789 ZEND_ARG_ARRAY_INFO(0, fields, 1)
790 ZEND_ARG_ARRAY_INFO(0, files, 1)
791 ZEND_END_ARG_INFO();
792 PHP_METHOD(HttpMessageBody, addForm)
793 {
794 HashTable *fields = NULL, *files = NULL;
795 php_http_message_body_object_t *obj;
796
797 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|h!h!", &fields, &files), invalid_arg, return);
798
799 obj = PHP_HTTP_OBJ(NULL, getThis());
800 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
801
802 php_http_expect(SUCCESS == php_http_message_body_add_form(obj->body, fields, files), runtime, return);
803
804 RETURN_ZVAL(getThis(), 1, 0);
805 }
806
807 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart, 0, 0, 1)
808 ZEND_ARG_OBJ_INFO(0, message, http\\Message, 0)
809 ZEND_END_ARG_INFO();
810 PHP_METHOD(HttpMessageBody, addPart)
811 {
812 zval *zobj;
813 php_http_message_body_object_t *obj;
814 php_http_message_object_t *mobj;
815 zend_error_handling zeh;
816
817 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zobj, php_http_message_get_class_entry()), invalid_arg, return);
818
819 obj = PHP_HTTP_OBJ(NULL, getThis());
820 mobj = PHP_HTTP_OBJ(NULL, zobj);
821
822 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
823
824 zend_replace_error_handling(EH_THROW, php_http_get_exception_runtime_class_entry(), &zeh);
825 php_http_message_body_add_part(obj->body, mobj->message);
826 zend_restore_error_handling(&zeh);
827
828 if (!EG(exception)) {
829 RETURN_ZVAL(getThis(), 1, 0);
830 }
831 }
832
833 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_etag, 0, 0, 0)
834 ZEND_END_ARG_INFO();
835 PHP_METHOD(HttpMessageBody, etag)
836 {
837 if (SUCCESS == zend_parse_parameters_none()) {
838 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
839 char *etag;
840
841 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
842
843 if ((etag = php_http_message_body_etag(obj->body))) {
844 RETURN_STR(php_http_cs2zs(etag, strlen(etag)));
845 } else {
846 RETURN_FALSE;
847 }
848 }
849 }
850
851 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_stat, 0, 0, 0)
852 ZEND_ARG_INFO(0, field)
853 ZEND_END_ARG_INFO();
854 PHP_METHOD(HttpMessageBody, stat)
855 {
856 char *field_str = NULL;
857 size_t field_len = 0;
858
859 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &field_str, &field_len)) {
860 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
861 const php_stream_statbuf *sb;
862
863 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
864
865 if ((sb = php_http_message_body_stat(obj->body))) {
866 if (field_str && field_len) {
867 switch (*field_str) {
868 case 's':
869 case 'S':
870 RETURN_LONG(sb->sb.st_size);
871 break;
872 case 'a':
873 case 'A':
874 RETURN_LONG(sb->sb.st_atime);
875 break;
876 case 'm':
877 case 'M':
878 RETURN_LONG(sb->sb.st_mtime);
879 break;
880 case 'c':
881 case 'C':
882 RETURN_LONG(sb->sb.st_ctime);
883 break;
884 default:
885 php_error_docref(NULL, E_WARNING, "Unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str);
886 break;
887 }
888 } else {
889 object_init(return_value);
890 add_property_long_ex(return_value, ZEND_STRL("size"), sb->sb.st_size);
891 add_property_long_ex(return_value, ZEND_STRL("atime"), sb->sb.st_atime);
892 add_property_long_ex(return_value, ZEND_STRL("mtime"), sb->sb.st_mtime);
893 add_property_long_ex(return_value, ZEND_STRL("ctime"), sb->sb.st_ctime);
894 }
895 }
896 }
897 }
898
899 static zend_function_entry php_http_message_body_methods[] = {
900 PHP_ME(HttpMessageBody, __construct, ai_HttpMessageBody___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
901 PHP_ME(HttpMessageBody, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
902 PHP_MALIAS(HttpMessageBody, toString, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
903 PHP_MALIAS(HttpMessageBody, serialize, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
904 PHP_ME(HttpMessageBody, unserialize, ai_HttpMessageBody_unserialize, ZEND_ACC_PUBLIC)
905 PHP_ME(HttpMessageBody, toStream, ai_HttpMessageBody_toStream, ZEND_ACC_PUBLIC)
906 PHP_ME(HttpMessageBody, toCallback, ai_HttpMessageBody_toCallback, ZEND_ACC_PUBLIC)
907 PHP_ME(HttpMessageBody, getResource, ai_HttpMessageBody_getResource, ZEND_ACC_PUBLIC)
908 PHP_ME(HttpMessageBody, getBoundary, ai_HttpMessageBody_getBoundary, ZEND_ACC_PUBLIC)
909 PHP_ME(HttpMessageBody, append, ai_HttpMessageBody_append, ZEND_ACC_PUBLIC)
910 PHP_ME(HttpMessageBody, addForm, ai_HttpMessageBody_addForm, ZEND_ACC_PUBLIC)
911 PHP_ME(HttpMessageBody, addPart, ai_HttpMessageBody_addPart, ZEND_ACC_PUBLIC)
912 PHP_ME(HttpMessageBody, etag, ai_HttpMessageBody_etag, ZEND_ACC_PUBLIC)
913 PHP_ME(HttpMessageBody, stat, ai_HttpMessageBody_stat, ZEND_ACC_PUBLIC)
914 EMPTY_FUNCTION_ENTRY
915 };
916
917 PHP_MINIT_FUNCTION(http_message_body)
918 {
919 zend_class_entry ce = {0};
920
921 INIT_NS_CLASS_ENTRY(ce, "http\\Message", "Body", php_http_message_body_methods);
922 php_http_message_body_class_entry = zend_register_internal_class(&ce);
923 php_http_message_body_class_entry->create_object = php_http_message_body_object_new;
924 memcpy(&php_http_message_body_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
925 php_http_message_body_object_handlers.offset = XtOffsetOf(php_http_message_body_object_t, zo);
926 php_http_message_body_object_handlers.clone_obj = php_http_message_body_object_clone;
927 php_http_message_body_object_handlers.free_obj = php_http_message_body_object_free;
928 php_http_message_body_object_handlers.get_gc = php_http_message_body_object_get_gc;
929 zend_class_implements(php_http_message_body_class_entry, 1, zend_ce_serializable);
930
931 return SUCCESS;
932 }
933
934 /*
935 * Local variables:
936 * tab-width: 4
937 * c-basic-offset: 4
938 * End:
939 * vim600: noet sw=4 ts=4 fdm=marker
940 * vim<600: noet sw=4 ts=4
941 */