2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #include <ext/standard/php_lcg.h>
17 #define BOUNDARY_OPEN(body) \
19 size_t size = php_http_message_body_size(body); \
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)); \
24 php_http_message_body_appendf(body, "--%s" PHP_HTTP_CRLF, php_http_message_body_boundary(body)); \
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))
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
);
34 php_http_message_body_t
*php_http_message_body_init(php_http_message_body_t
**body_ptr
, php_stream
*stream
)
36 php_http_message_body_t
*body
;
38 if (body_ptr
&& *body_ptr
) {
44 body
= ecalloc(1, sizeof(php_http_message_body_t
));
48 php_stream_auto_cleanup(stream
);
49 body
->res
= stream
->res
;
50 ++GC_REFCOUNT(body
->res
);
52 stream
= php_stream_temp_create(TEMP_STREAM_DEFAULT
, 0xffff);
53 php_stream_auto_cleanup(stream
);
54 body
->res
= stream
->res
;
64 unsigned php_http_message_body_addref(php_http_message_body_t
*body
)
66 return ++body
->refcount
;
69 php_http_message_body_t
*php_http_message_body_copy(php_http_message_body_t
*from
, php_http_message_body_t
*to
)
73 php_stream_truncate_set_size(php_http_message_body_stream(to
), 0);
75 to
= php_http_message_body_init(NULL
, NULL
);
77 php_http_message_body_to_stream(from
, php_http_message_body_stream(to
), 0, 0);
83 to
->boundary
= estrdup(from
->boundary
);
91 void php_http_message_body_free(php_http_message_body_t
**body_ptr
)
94 php_http_message_body_t
*body
= *body_ptr
;
96 if (!--body
->refcount
) {
97 /* NOFIXME: shows leakinfo in DEBUG mode */
98 zend_list_delete(body
->res
);
99 PTR_FREE(body
->boundary
);
106 const php_stream_statbuf
*php_http_message_body_stat(php_http_message_body_t
*body
)
108 php_stream_stat(php_http_message_body_stream(body
), &body
->ssb
);
112 const char *php_http_message_body_boundary(php_http_message_body_t
*body
)
114 if (!body
->boundary
) {
115 union { double dbl
; int num
[2]; } data
;
117 data
.dbl
= php_combined_lcg(TSRMLS_C
);
118 spprintf(&body
->boundary
, 0, "%x.%x", data
.num
[0], data
.num
[1]);
120 return body
->boundary
;
123 char *php_http_message_body_etag(php_http_message_body_t
*body
)
125 const php_stream_statbuf
*ssb
= php_http_message_body_stat(body
);
127 /* real file or temp buffer ? */
128 if (ssb
->sb
.st_mtime
) {
131 spprintf(&etag
, 0, "%lx-%lx-%lx", ssb
->sb
.st_ino
, ssb
->sb
.st_mtime
, ssb
->sb
.st_size
);
134 php_http_etag_t
*etag
= php_http_etag_init(PHP_HTTP_G
->env
.etag_mode
);
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
);
145 zend_string
*php_http_message_body_to_string(php_http_message_body_t
*body
, off_t offset
, size_t forlen
)
147 php_stream
*s
= php_http_message_body_stream(body
);
149 php_stream_seek(s
, offset
, SEEK_SET
);
153 return php_stream_copy_to_mem(s
, forlen
, 0);
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
)
158 php_stream
*s
= php_http_message_body_stream(body
);
160 php_stream_seek(s
, offset
, SEEK_SET
);
165 return php_stream_copy_to_stream_ex(s
, dst
, forlen
, NULL
);
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
)
170 php_stream
*s
= php_http_message_body_stream(body
);
171 char *buf
= emalloc(0x1000);
173 php_stream_seek(s
, offset
, SEEK_SET
);
178 while (!php_stream_eof(s
)) {
179 size_t read
= php_stream_read(s
, buf
, MIN(forlen
, 0x1000));
182 if (-1 == cb(cb_arg
, buf
, read
)) {
187 if (read
< MIN(forlen
, sizeof(buf
))) {
191 if (forlen
&& !(forlen
-= read
)) {
200 size_t php_http_message_body_append(php_http_message_body_t
*body
, const char *buf
, size_t len
)
205 if (!(s
= php_http_message_body_stream(body
))) {
210 php_stream_seek(s
, 0, SEEK_END
);
213 written
= php_stream_write(s
, buf
, len
);
215 if (written
!= len
) {
216 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to append %zu bytes to body; wrote %zu", len
, written
);
222 size_t php_http_message_body_appendf(php_http_message_body_t
*body
, const char *fmt
, ...)
229 print_len
= vspprintf(&print_str
, 0, fmt
, argv
);
232 print_len
= php_http_message_body_append(body
, print_str
, print_len
);
238 ZEND_RESULT_CODE
php_http_message_body_add_form(php_http_message_body_t
*body
, HashTable
*fields
, HashTable
*files
)
241 if (SUCCESS
!= add_recursive_fields(body
, NULL
, fields
)) {
246 if (SUCCESS
!= add_recursive_files(body
, NULL
, files
)) {
254 void php_http_message_body_add_part(php_http_message_body_t
*body
, php_http_message_t
*part
)
257 php_http_message_to_callback(part
, (php_http_pass_callback_t
) php_http_message_body_append
, body
);
258 BOUNDARY_CLOSE(body
);
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
)
264 zend_string
*safe_name
;
266 safe_name
= php_addslashes(estrdup(name
), strlen(name
), 1);
269 php_http_message_body_appendf(
271 "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
275 php_http_message_body_append(body
, value_str
, value_len
);
276 BOUNDARY_CLOSE(body
);
278 zend_string_release(safe_name
);
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
)
284 size_t path_len
= strlen(path
);
285 char *path_dup
= estrndup(path
, path_len
);
286 zend_string
*safe_name
, *base_name
;
288 safe_name
= php_addslashes(estrdup(name
), strlen(name
), 1);
289 base_name
= php_basename(path_dup
, path_len
, NULL
, 0);
292 php_http_message_body_appendf(
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
298 safe_name
->val
, base_name
->val
, ctype
300 php_stream_copy_to_stream_ex(in
, php_http_message_body_stream(body
), PHP_STREAM_COPY_ALL
, NULL
);
301 BOUNDARY_CLOSE(body
);
303 zend_string_release(safe_name
);
304 zend_string_release(base_name
);
310 static inline char *format_key(php_http_arrkey_t
*key
, const char *prefix
) {
311 char *new_key
= NULL
;
313 if (prefix
&& *prefix
) {
315 spprintf(&new_key
, 0, "%s[%s]", prefix
, key
->key
->val
);
317 spprintf(&new_key
, 0, "%s[%lu]", prefix
, key
->h
);
319 } else if (key
->key
) {
320 new_key
= estrdup(key
->key
->val
);
322 new_key
= estrdup("");
328 static ZEND_RESULT_CODE
add_recursive_field_value(php_http_message_body_t
*body
, const char *name
, zval
*value
)
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
);
336 static ZEND_RESULT_CODE
add_recursive_fields(php_http_message_body_t
*body
, const char *name
, HashTable
*fields
)
339 php_http_arrkey_t key
;
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
)
345 char *str
= format_key(&key
, name
);
347 if (Z_TYPE_P(val
) != IS_ARRAY
&& Z_TYPE_P(val
) != IS_OBJECT
) {
348 if (SUCCESS
!= add_recursive_field_value(body
, str
, val
)) {
350 ZEND_HASH_DEC_APPLY_COUNT(fields
);
353 } else if (SUCCESS
!= add_recursive_fields(body
, str
, HASH_OF(val
))) {
355 ZEND_HASH_DEC_APPLY_COUNT(fields
);
360 ZEND_HASH_FOREACH_END();
361 ZEND_HASH_DEC_APPLY_COUNT(fields
);
367 static ZEND_RESULT_CODE
add_recursive_files(php_http_message_body_t
*body
, const char *name
, HashTable
*files
)
369 zval
*zdata
= NULL
, *zfile
, *zname
, *ztype
;
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")))
377 php_http_arrkey_t key
;
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
)
383 if (Z_TYPE_P(val
) == IS_ARRAY
|| Z_TYPE_P(val
) == IS_OBJECT
) {
384 char *str
= format_key(&key
, name
);
386 if (SUCCESS
!= add_recursive_files(body
, str
, HASH_OF(val
))) {
388 ZEND_HASH_DEC_APPLY_COUNT(files
);
394 ZEND_HASH_FOREACH_END();
395 ZEND_HASH_DEC_APPLY_COUNT(files
);
401 zend_string
*zfc
= zval_get_string(zfile
);
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
);
407 zend_string
*tmp
= zval_get_string(zdata
);
409 stream
= php_stream_memory_open(TEMP_STREAM_READONLY
, tmp
->val
, tmp
->len
);
410 zend_string_release(tmp
);
413 stream
= php_stream_open_wrapper(zfc
->val
, "r", REPORT_ERRORS
|USE_PATH
, NULL
);
417 zend_string_release(zfc
);
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
);
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
);
438 struct splitbody_arg
{
439 php_http_buffer_t buf
;
440 php_http_message_parser_t
*parser
;
446 static size_t splitbody(void *opaque
, char *buf
, size_t len
)
448 struct splitbody_arg
*arg
= opaque
;
449 const char *boundary
= NULL
;
454 first_boundary
= !(consumed
|| arg
->consumed
);
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
;
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
;
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
);
473 /* move after the boundary */
474 cut
= real_boundary
- buf
+ real_boundary_len
;
479 if (buf
== php_http_locate_bin_eol(buf
, len
, &eol_len
)) {
485 if (!first_boundary
) {
486 /* advance messages */
487 php_http_message_t
*msg
;
489 msg
= php_http_message_init(NULL
, 0, NULL
);
490 msg
->parent
= arg
->parser
->message
;
491 arg
->parser
->message
= msg
;
494 /* is this the last boundary? */
496 /* ignore the rest */
500 /* let this be garbage */
501 php_error_docref(NULL
, E_WARNING
, "Malformed multipart boundary at pos %zu", consumed
);
506 } while (boundary
&& len
);
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
);
515 arg
->consumed
+= consumed
;
519 php_http_message_t
*php_http_message_body_split(php_http_message_body_t
*body
, const char *boundary
)
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
;
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
);
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
);
536 msg
= arg
.parser
->message
;
537 arg
.parser
->message
= NULL
;
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
);
547 static zend_object_handlers php_http_message_body_object_handlers
;
549 zend_object
*php_http_message_body_object_new(zend_class_entry
*ce
)
551 return &php_http_message_body_object_new_ex(ce
, NULL
)->zo
;
554 php_http_message_body_object_t
*php_http_message_body_object_new_ex(zend_class_entry
*ce
, php_http_message_body_t
*body
)
556 php_http_message_body_object_t
*o
;
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
);
566 o
->zo
.handlers
= &php_http_message_body_object_handlers
;
571 zend_object
*php_http_message_body_object_clone(zval
*object
)
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
);
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
);
583 void php_http_message_body_object_free(zend_object
*object
)
585 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(object
, NULL
);
587 php_http_message_body_free(&obj
->body
);
588 zend_object_std_dtor(object TSRMLS_CC
);
591 #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \
594 obj->body = php_http_message_body_init(NULL, NULL); \
598 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___construct
, 0, 0, 0)
599 ZEND_ARG_INFO(0, stream
)
601 PHP_METHOD(HttpMessageBody
, __construct
)
603 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
604 zval
*zstream
= NULL
;
607 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream
), invalid_arg
, return);
610 php_http_expect(php_stream_from_zval_no_verify(stream
, zstream
), unexpected_val
, return);
613 php_http_message_body_free(&obj
->body
);
615 obj
->body
= php_http_message_body_init(NULL
, stream
);
619 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___toString
, 0, 0, 0)
621 PHP_METHOD(HttpMessageBody
, __toString
)
623 if (SUCCESS
== zend_parse_parameters_none()) {
624 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
627 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
629 zs
= php_http_message_body_to_string(obj
->body
, 0, 0);
634 RETURN_EMPTY_STRING();
637 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_unserialize
, 0, 0, 1)
638 ZEND_ARG_INFO(0, serialized
)
640 PHP_METHOD(HttpMessageBody
, unserialize
)
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
);
649 obj
->body
= php_http_message_body_init(NULL
, s
);
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
)
658 PHP_METHOD(HttpMessageBody
, toStream
)
661 zend_long offset
= 0, forlen
= 0;
663 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &zstream
, &offset
, &forlen
)) {
665 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
667 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
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);
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
)
680 PHP_METHOD(HttpMessageBody
, toCallback
)
682 php_http_pass_fcall_arg_t fcd
;
683 zend_long offset
= 0, forlen
= 0;
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());
688 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
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());
698 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getResource
, 0, 0, 0)
700 PHP_METHOD(HttpMessageBody
, getResource
)
702 if (SUCCESS
== zend_parse_parameters_none()) {
703 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
705 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
707 ++GC_REFCOUNT(obj
->body
->res
);
708 RETVAL_RES(obj
->body
->res
);
712 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getBoundary
, 0, 0, 0)
714 PHP_METHOD(HttpMessageBody
, getBoundary
)
716 if (SUCCESS
== zend_parse_parameters_none()) {
717 php_http_message_body_object_t
* obj
= PHP_HTTP_OBJ(NULL
, getThis());
719 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
721 if (obj
->body
->boundary
) {
722 RETURN_STRING(obj
->body
->boundary
);
727 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_append
, 0, 0, 1)
728 ZEND_ARG_INFO(0, string
)
730 PHP_METHOD(HttpMessageBody
, append
)
734 php_http_message_body_object_t
*obj
;
736 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
), invalid_arg
, return);
738 obj
= PHP_HTTP_OBJ(NULL
, getThis());
739 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
741 php_http_expect(len
== php_http_message_body_append(obj
->body
, str
, len
), runtime
, return);
743 RETURN_ZVAL_FAST(getThis());
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)
750 PHP_METHOD(HttpMessageBody
, addForm
)
752 HashTable
*fields
= NULL
, *files
= NULL
;
753 php_http_message_body_object_t
*obj
;
755 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|h!h!", &fields
, &files
), invalid_arg
, return);
757 obj
= PHP_HTTP_OBJ(NULL
, getThis());
758 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
760 php_http_expect(SUCCESS
== php_http_message_body_add_form(obj
->body
, fields
, files
), runtime
, return);
762 RETURN_ZVAL_FAST(getThis());
765 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart
, 0, 0, 1)
766 ZEND_ARG_OBJ_INFO(0, message
, http
\\Message
, 0)
768 PHP_METHOD(HttpMessageBody
, addPart
)
771 php_http_message_body_object_t
*obj
;
772 php_http_message_object_t
*mobj
;
773 zend_error_handling zeh
;
775 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zobj
, php_http_message_class_entry
), invalid_arg
, return);
777 obj
= PHP_HTTP_OBJ(NULL
, getThis());
778 mobj
= PHP_HTTP_OBJ(NULL
, zobj
);
780 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
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
);
786 if (!EG(exception
)) {
787 RETURN_ZVAL_FAST(getThis());
791 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_etag
, 0, 0, 0)
793 PHP_METHOD(HttpMessageBody
, etag
)
795 if (SUCCESS
== zend_parse_parameters_none()) {
796 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
799 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
801 if ((etag
= php_http_message_body_etag(obj
->body
))) {
802 RETURN_STR(php_http_cs2zs(etag
, strlen(etag
)));
809 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_stat
, 0, 0, 0)
810 ZEND_ARG_INFO(0, field
)
812 PHP_METHOD(HttpMessageBody
, stat
)
814 char *field_str
= NULL
;
815 size_t field_len
= 0;
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
;
821 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
823 if ((sb
= php_http_message_body_stat(obj
->body
))) {
824 if (field_str
&& field_len
) {
825 switch (*field_str
) {
828 RETURN_LONG(sb
->sb
.st_size
);
832 RETURN_LONG(sb
->sb
.st_atime
);
836 RETURN_LONG(sb
->sb
.st_mtime
);
840 RETURN_LONG(sb
->sb
.st_ctime
);
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
);
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
);
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
)
875 zend_class_entry
*php_http_message_body_class_entry
;
877 PHP_MINIT_FUNCTION(http_message_body
)
879 zend_class_entry ce
= {0};
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
.offset
= XtOffsetOf(php_http_message_body_object_t
, zo
);
886 php_http_message_body_object_handlers
.clone_obj
= php_http_message_body_object_clone
;
887 php_http_message_body_object_handlers
.free_obj
= php_http_message_body_object_free
;
888 zend_class_implements(php_http_message_body_class_entry
, 1, zend_ce_serializable
);
898 * vim600: noet sw=4 ts=4 fdm=marker
899 * vim<600: noet sw=4 ts=4