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