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