prepare v4.2.5
[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 == (size_t) -1 ? 0 : 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, *zstr_name = zend_string_init(name, strlen(name), 0);
266
267 #if PHP_VERSION_ID < 70300
268 safe_name = php_addslashes(zstr_name, 1);
269 #else
270 safe_name = php_addslashes(zstr_name);
271 zend_string_release_ex(zstr_name, 0);
272 #endif
273
274 BOUNDARY_OPEN(body);
275 php_http_message_body_appendf(
276 body,
277 "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
278 "" PHP_HTTP_CRLF,
279 safe_name->val
280 );
281 php_http_message_body_append(body, value_str, value_len);
282 BOUNDARY_CLOSE(body);
283
284 zend_string_release(safe_name);
285 return SUCCESS;
286 }
287
288 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)
289 {
290 size_t path_len = strlen(path);
291 char *path_dup = estrndup(path, path_len);
292 zend_string *base_name, *safe_name, *zstr_name = zend_string_init(name, strlen(name), 0);
293
294 #if PHP_VERSION_ID < 70300
295 safe_name = php_addslashes(zstr_name, 1);
296 #else
297 safe_name = php_addslashes(zstr_name);
298 zend_string_release_ex(zstr_name, 0);
299 #endif
300 base_name = php_basename(path_dup, path_len, NULL, 0);
301
302 BOUNDARY_OPEN(body);
303 php_http_message_body_appendf(
304 body,
305 "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
306 "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
307 "Content-Type: %s" PHP_HTTP_CRLF
308 PHP_HTTP_CRLF,
309 safe_name->val, base_name->val, ctype
310 );
311 php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL);
312 BOUNDARY_CLOSE(body);
313
314 zend_string_release(safe_name);
315 zend_string_release(base_name);
316 efree(path_dup);
317
318 return SUCCESS;
319 }
320
321 static inline char *format_key(php_http_arrkey_t *key, const char *prefix) {
322 char *new_key = NULL;
323
324 if (prefix && *prefix) {
325 if (key->key) {
326 spprintf(&new_key, 0, "%s[%s]", prefix, key->key->val);
327 } else {
328 spprintf(&new_key, 0, "%s[%lu]", prefix, key->h);
329 }
330 } else if (key->key) {
331 new_key = estrdup(key->key->val);
332 } else {
333 spprintf(&new_key, 0, "%lu", key->h);
334 }
335
336 return new_key;
337 }
338
339 static ZEND_RESULT_CODE add_recursive_field_value(php_http_message_body_t *body, const char *name, zval *value)
340 {
341 zend_string *zs = zval_get_string(value);
342 ZEND_RESULT_CODE rc = php_http_message_body_add_form_field(body, name, zs->val, zs->len);
343 zend_string_release(zs);
344 return rc;
345 }
346
347 static ZEND_RESULT_CODE add_recursive_fields(php_http_message_body_t *body, const char *name, HashTable *fields)
348 {
349 zval *val;
350 php_http_arrkey_t key;
351
352 if (!HT_IS_RECURSIVE(fields)) {
353 HT_PROTECT_RECURSION(fields);
354 ZEND_HASH_FOREACH_KEY_VAL_IND(fields, key.h, key.key, val)
355 {
356 char *str = format_key(&key, name);
357
358 if (Z_TYPE_P(val) != IS_ARRAY && Z_TYPE_P(val) != IS_OBJECT) {
359 if (SUCCESS != add_recursive_field_value(body, str, val)) {
360 efree(str);
361 HT_UNPROTECT_RECURSION(fields);
362 return FAILURE;
363 }
364 } else if (SUCCESS != add_recursive_fields(body, str, HASH_OF(val))) {
365 efree(str);
366 HT_UNPROTECT_RECURSION(fields);
367 return FAILURE;
368 }
369 efree(str);
370 }
371 ZEND_HASH_FOREACH_END();
372 HT_UNPROTECT_RECURSION(fields);
373 }
374
375 return SUCCESS;
376 }
377
378 static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const char *name, HashTable *files)
379 {
380 zval *zdata = NULL, *zfile, *zname, *ztype;
381
382 /* single entry */
383 if (!(zname = zend_hash_str_find(files, ZEND_STRL("name")))
384 || !(ztype = zend_hash_str_find(files, ZEND_STRL("type")))
385 || !(zfile = zend_hash_str_find(files, ZEND_STRL("file")))
386 ) {
387 zval *val;
388 php_http_arrkey_t key;
389
390 if (!HT_IS_RECURSIVE(files)) {
391 HT_PROTECT_RECURSION(files);
392 ZEND_HASH_FOREACH_KEY_VAL_IND(files, key.h, key.key, val)
393 {
394 if (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT) {
395 char *str = key.key ? format_key(&key, name) : NULL;
396 const char *prefix = str ? str : name;
397
398 if (SUCCESS != add_recursive_files(body, prefix, HASH_OF(val))) {
399 efree(str);
400 HT_UNPROTECT_RECURSION(files);
401 return FAILURE;
402 }
403 if (str) {
404 efree(str);
405 }
406 }
407 }
408 ZEND_HASH_FOREACH_END();
409 HT_UNPROTECT_RECURSION(files);
410 }
411 return SUCCESS;
412 } else {
413 /* stream entry */
414 php_stream *stream;
415 zend_string *zfc = zval_get_string(zfile);
416
417 if ((zdata = zend_hash_str_find(files, ZEND_STRL("data")))) {
418 if (Z_TYPE_P(zdata) == IS_RESOURCE) {
419 php_stream_from_zval_no_verify(stream, zdata);
420 } else {
421 zend_string *tmp = zval_get_string(zdata);
422
423 stream = php_http_mem_stream_open(TEMP_STREAM_READONLY, tmp);
424 zend_string_release(tmp);
425 }
426 } else {
427 stream = php_stream_open_wrapper(zfc->val, "r", REPORT_ERRORS|USE_PATH, NULL);
428 }
429
430 if (!stream) {
431 zend_string_release(zfc);
432 return FAILURE;
433 } else {
434 zend_string *znc = zval_get_string(zname), *ztc = zval_get_string(ztype);
435 php_http_arrkey_t arrkey = {0, znc, 0, 0};
436 char *key = format_key(&arrkey, name);
437 ZEND_RESULT_CODE ret = php_http_message_body_add_form_file(body, key, ztc->val, zfc->val, stream);
438
439 efree(key);
440 zend_string_release(znc);
441 zend_string_release(ztc);
442 zend_string_release(zfc);
443 if (!zdata || Z_TYPE_P(zdata) != IS_RESOURCE) {
444 php_stream_close(stream);
445 }
446 return ret;
447 }
448
449 }
450 }
451
452 struct splitbody_arg {
453 php_http_buffer_t buf;
454 php_http_message_parser_t *parser;
455 char *boundary_str;
456 size_t boundary_len;
457 size_t consumed;
458 };
459
460 static size_t splitbody(void *opaque, char *buf, size_t len)
461 {
462 struct splitbody_arg *arg = opaque;
463 const char *boundary = NULL;
464 size_t consumed = 0;
465 int first_boundary;
466
467 do {
468 first_boundary = !(consumed || arg->consumed);
469
470 if ((boundary = php_http_locate_str(buf, len, arg->boundary_str + first_boundary, arg->boundary_len - first_boundary))) {
471 size_t real_boundary_len = arg->boundary_len - 1, cut;
472 const char *real_boundary = boundary + !first_boundary;
473 int eol_len = 0;
474
475 if (buf + len <= real_boundary + real_boundary_len) {
476 /* if we just have enough data for the boundary, it's just a byte too less */
477 arg->consumed += consumed;
478 return consumed;
479 }
480
481 if (!first_boundary) {
482 /* this is not the first boundary, read rest of this message */
483 php_http_buffer_append(&arg->buf, buf, real_boundary - buf);
484 php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
485 }
486
487 /* move after the boundary */
488 cut = real_boundary - buf + real_boundary_len;
489 buf += cut;
490 len -= cut;
491 consumed += cut;
492
493 if (buf == php_http_locate_bin_eol(buf, len, &eol_len)) {
494 /* skip CRLF */
495 buf += eol_len;
496 len -= eol_len;
497 consumed += eol_len;
498
499 if (!first_boundary) {
500 /* advance messages */
501 php_http_message_t *msg;
502
503 msg = php_http_message_init(NULL, 0, NULL);
504 msg->parent = arg->parser->message;
505 arg->parser->message = msg;
506 }
507 } else {
508 /* is this the last boundary? */
509 if (*buf == '-') {
510 /* ignore the rest */
511 consumed += len;
512 len = 0;
513 } else {
514 /* let this be garbage */
515 php_error_docref(NULL, E_WARNING, "Malformed multipart boundary at pos %zu", consumed);
516 return -1;
517 }
518 }
519 }
520 } while (boundary && len);
521
522 /* let there be room for the next boundary */
523 if (len > arg->boundary_len) {
524 consumed += len - arg->boundary_len;
525 php_http_buffer_append(&arg->buf, buf, len - arg->boundary_len);
526 php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
527 }
528
529 arg->consumed += consumed;
530 return consumed;
531 }
532
533 php_http_message_t *php_http_message_body_split(php_http_message_body_t *body, const char *boundary)
534 {
535 php_stream *s = php_http_message_body_stream(body);
536 php_http_buffer_t *tmp = NULL;
537 php_http_message_t *msg = NULL;
538 struct splitbody_arg arg;
539
540 php_http_buffer_init(&arg.buf);
541 arg.parser = php_http_message_parser_init(NULL);
542 arg.boundary_len = spprintf(&arg.boundary_str, 0, "\n--%s", boundary);
543 arg.consumed = 0;
544
545 php_stream_rewind(s);
546 while (!php_stream_eof(s)) {
547 php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg);
548 }
549
550 msg = arg.parser->message;
551 arg.parser->message = NULL;
552
553 php_http_buffer_free(&tmp);
554 php_http_message_parser_free(&arg.parser);
555 php_http_buffer_dtor(&arg.buf);
556 PTR_FREE(arg.boundary_str);
557
558 return msg;
559 }
560
561 static zend_class_entry *php_http_message_body_class_entry;
562 zend_class_entry *php_http_get_message_body_class_entry(void)
563 {
564 return php_http_message_body_class_entry;
565 }
566
567 static zend_object_handlers php_http_message_body_object_handlers;
568
569 zend_object *php_http_message_body_object_new(zend_class_entry *ce)
570 {
571 return &php_http_message_body_object_new_ex(ce, NULL)->zo;
572 }
573
574 php_http_message_body_object_t *php_http_message_body_object_new_ex(zend_class_entry *ce, php_http_message_body_t *body)
575 {
576 php_http_message_body_object_t *o;
577
578 o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
579 zend_object_std_init(&o->zo, php_http_message_body_class_entry);
580 object_properties_init(&o->zo, ce);
581
582 o->gc = emalloc(sizeof(zval));
583
584 if (body) {
585 o->body = body;
586 }
587
588 o->zo.handlers = &php_http_message_body_object_handlers;
589
590 return o;
591 }
592
593 zend_object *php_http_message_body_object_clone(zend_object *object)
594 {
595 php_http_message_body_object_t *new_obj;
596 php_http_message_body_object_t *old_obj = PHP_HTTP_OBJ(object, NULL);
597 php_http_message_body_t *body = php_http_message_body_copy(old_obj->body, NULL);
598
599 new_obj = php_http_message_body_object_new_ex(old_obj->zo.ce, body);
600 zend_objects_clone_members(&new_obj->zo, &old_obj->zo);
601
602 return &new_obj->zo;
603 }
604
605 static HashTable *php_http_message_body_object_get_gc(zend_object *object, zval **table, int *n)
606 {
607 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(object, NULL);
608 HashTable *props = object->handlers->get_properties(object);
609 uint32_t count = zend_hash_num_elements(props);
610
611 obj->gc = erealloc(obj->gc, (1 + count) * sizeof(zval));
612
613 if (php_http_message_body_stream(obj->body)) {
614 *n = 1;
615 php_stream_to_zval(php_http_message_body_stream(obj->body), obj->gc);
616 } else {
617 *n = 0;
618 }
619
620 if (count) {
621 zval *val;
622
623 ZEND_HASH_FOREACH_VAL(props, val)
624 {
625 ZVAL_COPY_VALUE(&obj->gc[(*n)++], val);
626 }
627 ZEND_HASH_FOREACH_END();
628 }
629 *table = obj->gc;
630
631 return NULL;
632 }
633
634 void php_http_message_body_object_free(zend_object *object)
635 {
636 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(object, NULL);
637
638 PTR_FREE(obj->gc);
639 php_http_message_body_free(&obj->body);
640 zend_object_std_dtor(object);
641 }
642
643 #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \
644 do { \
645 if (!obj->body) { \
646 obj->body = php_http_message_body_init(NULL, NULL); \
647 php_stream_to_zval(php_http_message_body_stream(obj->body), obj->gc); \
648 } \
649 } while(0)
650
651 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___construct, 0, 0, 0)
652 ZEND_ARG_INFO(0, stream)
653 ZEND_END_ARG_INFO();
654 PHP_METHOD(HttpMessageBody, __construct)
655 {
656 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
657 zval *zstream = NULL;
658 php_stream *stream;
659
660 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream), invalid_arg, return);
661
662 if (zstream) {
663 php_http_expect(php_stream_from_zval_no_verify(stream, zstream), unexpected_val, return);
664
665 if (obj->body) {
666 php_http_message_body_free(&obj->body);
667 }
668 obj->body = php_http_message_body_init(NULL, stream);
669 php_stream_to_zval(stream, obj->gc);
670 }
671 }
672
673 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_serialize, 0, 0, 0)
674 ZEND_END_ARG_INFO();
675 PHP_METHOD(HttpMessageBody, serialize)
676 {
677 if (SUCCESS == zend_parse_parameters_none()) {
678 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
679 zend_string *zs;
680
681 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
682
683 zs = php_http_message_body_to_string(obj->body, 0, 0);
684 if (zs) {
685 RETURN_STR(zs);
686 }
687 }
688 RETURN_EMPTY_STRING();
689 }
690
691 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_unserialize, 0, 0, 1)
692 ZEND_ARG_INFO(0, serialized)
693 ZEND_END_ARG_INFO();
694 PHP_METHOD(HttpMessageBody, unserialize)
695 {
696 zend_string *us_str;
697
698 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &us_str)) {
699 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
700 php_stream *s = php_http_mem_stream_open(0, us_str);
701
702 obj->body = php_http_message_body_init(NULL, s);
703 php_stream_to_zval(s, obj->gc);
704 }
705 }
706
707 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(ai_HttpMessageBody___unserialize, 0, 1, IS_VOID, 0)
708 ZEND_ARG_TYPE_INFO(0, data, IS_ARRAY, 0)
709 ZEND_END_ARG_INFO();
710 PHP_METHOD(HttpMessageBody, __unserialize)
711 {
712 HashTable *arr;
713
714 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "h", &arr)) {
715 zval *zv = zend_hash_index_find(arr, 0);
716
717 if (zv) {
718 zend_string *zs = zval_get_string(zv);
719 php_stream *s = php_http_mem_stream_open(0, zs);
720 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
721
722 obj->body = php_http_message_body_init(NULL, s);
723 php_stream_to_zval(s, obj->gc);
724 zend_string_release(zs);
725 }
726 }
727 }
728
729 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(ai_HttpMessageBody___serialize, 0, 0, IS_ARRAY, 0)
730 ZEND_END_ARG_INFO();
731 PHP_METHOD(HttpMessageBody, __serialize)
732 {
733
734 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
735 zend_string *zs;
736
737 zend_parse_parameters_none();
738
739 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
740
741 array_init(return_value);
742 zs = php_http_message_body_to_string(obj->body, 0, 0);
743 if (zs) {
744 add_index_str(return_value, 0, zs);
745 }
746 }
747
748
749
750 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toStream, 0, 0, 1)
751 ZEND_ARG_INFO(0, stream)
752 ZEND_ARG_INFO(0, offset)
753 ZEND_ARG_INFO(0, maxlen)
754 ZEND_END_ARG_INFO();
755 PHP_METHOD(HttpMessageBody, toStream)
756 {
757 zval *zstream;
758 zend_long offset = 0, forlen = 0;
759
760 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &zstream, &offset, &forlen)) {
761 php_stream *stream;
762 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
763
764 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
765
766 php_stream_from_zval(stream, zstream);
767 php_http_message_body_to_stream(obj->body, stream, offset, forlen);
768 RETURN_ZVAL(getThis(), 1, 0);
769 }
770 }
771
772 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toCallback, 0, 0, 1)
773 ZEND_ARG_INFO(0, callback)
774 ZEND_ARG_INFO(0, offset)
775 ZEND_ARG_INFO(0, maxlen)
776 ZEND_END_ARG_INFO();
777 PHP_METHOD(HttpMessageBody, toCallback)
778 {
779 php_http_pass_fcall_arg_t fcd;
780 zend_long offset = 0, forlen = 0;
781
782 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) {
783 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
784
785 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
786
787 ZVAL_COPY(&fcd.fcz, getThis());
788 php_http_message_body_to_callback(obj->body, php_http_pass_fcall_callback, &fcd, offset, forlen);
789 zend_fcall_info_args_clear(&fcd.fci, 1);
790 zval_ptr_dtor(&fcd.fcz);
791 RETURN_ZVAL(getThis(), 1, 0);
792 }
793 }
794
795 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getResource, 0, 0, 0)
796 ZEND_END_ARG_INFO();
797 PHP_METHOD(HttpMessageBody, getResource)
798 {
799 if (SUCCESS == zend_parse_parameters_none()) {
800 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
801
802 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
803
804 php_stream_to_zval(php_http_message_body_stream(obj->body), return_value);
805 Z_ADDREF_P(return_value);
806 }
807 }
808
809 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getBoundary, 0, 0, 0)
810 ZEND_END_ARG_INFO();
811 PHP_METHOD(HttpMessageBody, getBoundary)
812 {
813 if (SUCCESS == zend_parse_parameters_none()) {
814 php_http_message_body_object_t * obj = PHP_HTTP_OBJ(NULL, getThis());
815
816 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
817
818 if (obj->body->boundary) {
819 RETURN_STRING(obj->body->boundary);
820 }
821 }
822 }
823
824 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_append, 0, 0, 1)
825 ZEND_ARG_INFO(0, string)
826 ZEND_END_ARG_INFO();
827 PHP_METHOD(HttpMessageBody, append)
828 {
829 char *str;
830 size_t len;
831 php_http_message_body_object_t *obj;
832
833 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &len), invalid_arg, return);
834
835 obj = PHP_HTTP_OBJ(NULL, getThis());
836 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
837
838 php_http_expect(len == php_http_message_body_append(obj->body, str, len), runtime, return);
839
840 RETURN_ZVAL(getThis(), 1, 0);
841 }
842
843 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addForm, 0, 0, 0)
844 ZEND_ARG_ARRAY_INFO(0, fields, 1)
845 ZEND_ARG_ARRAY_INFO(0, files, 1)
846 ZEND_END_ARG_INFO();
847 PHP_METHOD(HttpMessageBody, addForm)
848 {
849 HashTable *fields = NULL, *files = NULL;
850 php_http_message_body_object_t *obj;
851
852 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|h!h!", &fields, &files), invalid_arg, return);
853
854 obj = PHP_HTTP_OBJ(NULL, getThis());
855 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
856
857 php_http_expect(SUCCESS == php_http_message_body_add_form(obj->body, fields, files), runtime, return);
858
859 RETURN_ZVAL(getThis(), 1, 0);
860 }
861
862 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart, 0, 0, 1)
863 ZEND_ARG_OBJ_INFO(0, message, http\\Message, 0)
864 ZEND_END_ARG_INFO();
865 PHP_METHOD(HttpMessageBody, addPart)
866 {
867 zval *zobj;
868 php_http_message_body_object_t *obj;
869 php_http_message_object_t *mobj;
870 zend_error_handling zeh;
871
872 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zobj, php_http_message_get_class_entry()), invalid_arg, return);
873
874 obj = PHP_HTTP_OBJ(NULL, getThis());
875 mobj = PHP_HTTP_OBJ(NULL, zobj);
876
877 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
878
879 zend_replace_error_handling(EH_THROW, php_http_get_exception_runtime_class_entry(), &zeh);
880 php_http_message_body_add_part(obj->body, mobj->message);
881 zend_restore_error_handling(&zeh);
882
883 if (!EG(exception)) {
884 RETURN_ZVAL(getThis(), 1, 0);
885 }
886 }
887
888 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_etag, 0, 0, 0)
889 ZEND_END_ARG_INFO();
890 PHP_METHOD(HttpMessageBody, etag)
891 {
892 if (SUCCESS == zend_parse_parameters_none()) {
893 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
894 char *etag;
895
896 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
897
898 if ((etag = php_http_message_body_etag(obj->body))) {
899 RETURN_STR(php_http_cs2zs(etag, strlen(etag)));
900 } else {
901 RETURN_FALSE;
902 }
903 }
904 }
905
906 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_stat, 0, 0, 0)
907 ZEND_ARG_INFO(0, field)
908 ZEND_END_ARG_INFO();
909 PHP_METHOD(HttpMessageBody, stat)
910 {
911 char *field_str = NULL;
912 size_t field_len = 0;
913
914 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &field_str, &field_len)) {
915 php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
916 const php_stream_statbuf *sb;
917
918 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
919
920 if ((sb = php_http_message_body_stat(obj->body))) {
921 if (field_str && field_len) {
922 switch (*field_str) {
923 case 's':
924 case 'S':
925 RETURN_LONG(sb->sb.st_size);
926 break;
927 case 'a':
928 case 'A':
929 RETURN_LONG(sb->sb.st_atime);
930 break;
931 case 'm':
932 case 'M':
933 RETURN_LONG(sb->sb.st_mtime);
934 break;
935 case 'c':
936 case 'C':
937 RETURN_LONG(sb->sb.st_ctime);
938 break;
939 default:
940 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);
941 break;
942 }
943 } else {
944 object_init(return_value);
945 add_property_long_ex(return_value, ZEND_STRL("size"), sb->sb.st_size);
946 add_property_long_ex(return_value, ZEND_STRL("atime"), sb->sb.st_atime);
947 add_property_long_ex(return_value, ZEND_STRL("mtime"), sb->sb.st_mtime);
948 add_property_long_ex(return_value, ZEND_STRL("ctime"), sb->sb.st_ctime);
949 }
950 }
951 }
952 }
953
954 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(ai_HttpMessageBody___toString, 0, 0, IS_STRING, 0)
955 ZEND_END_ARG_INFO();
956
957 static zend_function_entry php_http_message_body_methods[] = {
958 PHP_ME(HttpMessageBody, __construct, ai_HttpMessageBody___construct, ZEND_ACC_PUBLIC)
959 PHP_ME(HttpMessageBody, serialize, ai_HttpMessageBody_serialize, ZEND_ACC_PUBLIC)
960 PHP_MALIAS(HttpMessageBody, toString, serialize, ai_HttpMessageBody_serialize, ZEND_ACC_PUBLIC)
961 PHP_MALIAS(HttpMessageBody, __toString, serialize, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
962 PHP_ME(HttpMessageBody, unserialize, ai_HttpMessageBody_unserialize, ZEND_ACC_PUBLIC)
963 PHP_ME(HttpMessageBody, __serialize, ai_HttpMessageBody___serialize, ZEND_ACC_PUBLIC)
964 PHP_ME(HttpMessageBody, __unserialize,ai_HttpMessageBody___unserialize,ZEND_ACC_PUBLIC)
965 PHP_ME(HttpMessageBody, toStream, ai_HttpMessageBody_toStream, ZEND_ACC_PUBLIC)
966 PHP_ME(HttpMessageBody, toCallback, ai_HttpMessageBody_toCallback, ZEND_ACC_PUBLIC)
967 PHP_ME(HttpMessageBody, getResource, ai_HttpMessageBody_getResource, ZEND_ACC_PUBLIC)
968 PHP_ME(HttpMessageBody, getBoundary, ai_HttpMessageBody_getBoundary, ZEND_ACC_PUBLIC)
969 PHP_ME(HttpMessageBody, append, ai_HttpMessageBody_append, ZEND_ACC_PUBLIC)
970 PHP_ME(HttpMessageBody, addForm, ai_HttpMessageBody_addForm, ZEND_ACC_PUBLIC)
971 PHP_ME(HttpMessageBody, addPart, ai_HttpMessageBody_addPart, ZEND_ACC_PUBLIC)
972 PHP_ME(HttpMessageBody, etag, ai_HttpMessageBody_etag, ZEND_ACC_PUBLIC)
973 PHP_ME(HttpMessageBody, stat, ai_HttpMessageBody_stat, ZEND_ACC_PUBLIC)
974 EMPTY_FUNCTION_ENTRY
975 };
976
977 PHP_MINIT_FUNCTION(http_message_body)
978 {
979 zend_class_entry ce = {0};
980
981 INIT_NS_CLASS_ENTRY(ce, "http\\Message", "Body", php_http_message_body_methods);
982 php_http_message_body_class_entry = zend_register_internal_class(&ce);
983 php_http_message_body_class_entry->create_object = php_http_message_body_object_new;
984 memcpy(&php_http_message_body_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
985 php_http_message_body_object_handlers.offset = XtOffsetOf(php_http_message_body_object_t, zo);
986 php_http_message_body_object_handlers.clone_obj = php_http_message_body_object_clone;
987 php_http_message_body_object_handlers.free_obj = php_http_message_body_object_free;
988 php_http_message_body_object_handlers.get_gc = php_http_message_body_object_get_gc;
989 zend_class_implements(php_http_message_body_class_entry, 1, zend_ce_serializable);
990
991 return SUCCESS;
992 }
993
994 /*
995 * Local variables:
996 * tab-width: 4
997 * c-basic-offset: 4
998 * End:
999 * vim600: noet sw=4 ts=4 fdm=marker
1000 * vim<600: noet sw=4 ts=4
1001 */