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 body
->res
= stream
->res
;
49 ++GC_REFCOUNT(body
->res
);
51 stream
= php_stream_temp_create(TEMP_STREAM_DEFAULT
, 0xffff);
52 body
->res
= stream
->res
;
54 php_stream_auto_cleanup(stream
);
63 unsigned php_http_message_body_addref(php_http_message_body_t
*body
)
65 return ++body
->refcount
;
68 php_http_message_body_t
*php_http_message_body_copy(php_http_message_body_t
*from
, php_http_message_body_t
*to
)
72 php_stream_truncate_set_size(php_http_message_body_stream(to
), 0);
74 to
= php_http_message_body_init(NULL
, NULL
);
76 php_http_message_body_to_stream(from
, php_http_message_body_stream(to
), 0, 0);
82 to
->boundary
= estrdup(from
->boundary
);
90 void php_http_message_body_free(php_http_message_body_t
**body_ptr
)
93 php_http_message_body_t
*body
= *body_ptr
;
95 if (!--body
->refcount
) {
96 /* NOFIXME: shows leakinfo in DEBUG mode */
97 zend_list_delete(body
->res
);
98 PTR_FREE(body
->boundary
);
105 const php_stream_statbuf
*php_http_message_body_stat(php_http_message_body_t
*body
)
107 php_stream_stat(php_http_message_body_stream(body
), &body
->ssb
);
111 const char *php_http_message_body_boundary(php_http_message_body_t
*body
)
113 if (!body
->boundary
) {
114 union { double dbl
; int num
[2]; } data
;
116 data
.dbl
= php_combined_lcg(TSRMLS_C
);
117 spprintf(&body
->boundary
, 0, "%x.%x", data
.num
[0], data
.num
[1]);
119 return body
->boundary
;
122 char *php_http_message_body_etag(php_http_message_body_t
*body
)
124 const php_stream_statbuf
*ssb
= php_http_message_body_stat(body
);
126 /* real file or temp buffer ? */
127 if (ssb
->sb
.st_mtime
) {
130 spprintf(&etag
, 0, "%lx-%lx-%lx", ssb
->sb
.st_ino
, ssb
->sb
.st_mtime
, ssb
->sb
.st_size
);
133 php_http_etag_t
*etag
= php_http_etag_init(PHP_HTTP_G
->env
.etag_mode
);
136 php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_etag_update
, etag
, 0, 0);
137 return php_http_etag_finish(etag
);
144 zend_string
*php_http_message_body_to_string(php_http_message_body_t
*body
, off_t offset
, size_t forlen
)
146 php_stream
*s
= php_http_message_body_stream(body
);
148 php_stream_seek(s
, offset
, SEEK_SET
);
152 return php_stream_copy_to_mem(s
, forlen
, 0);
155 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 php_stream
*s
= php_http_message_body_stream(body
);
159 php_stream_seek(s
, offset
, SEEK_SET
);
164 return php_stream_copy_to_stream_ex(s
, dst
, forlen
, NULL
);
167 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 php_stream
*s
= php_http_message_body_stream(body
);
170 char *buf
= emalloc(0x1000);
172 php_stream_seek(s
, offset
, SEEK_SET
);
177 while (!php_stream_eof(s
)) {
178 size_t read
= php_stream_read(s
, buf
, MIN(forlen
, 0x1000));
181 if (-1 == cb(cb_arg
, buf
, read
)) {
186 if (read
< MIN(forlen
, sizeof(buf
))) {
190 if (forlen
&& !(forlen
-= read
)) {
199 size_t php_http_message_body_append(php_http_message_body_t
*body
, const char *buf
, size_t len
)
204 if (!(s
= php_http_message_body_stream(body
))) {
209 php_stream_seek(s
, 0, SEEK_END
);
212 written
= php_stream_write(s
, buf
, len
);
214 if (written
!= len
) {
215 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to append %zu bytes to body; wrote %zu", len
, written
);
221 size_t php_http_message_body_appendf(php_http_message_body_t
*body
, const char *fmt
, ...)
228 print_len
= vspprintf(&print_str
, 0, fmt
, argv
);
231 print_len
= php_http_message_body_append(body
, print_str
, print_len
);
237 ZEND_RESULT_CODE
php_http_message_body_add_form(php_http_message_body_t
*body
, HashTable
*fields
, HashTable
*files
)
240 if (SUCCESS
!= add_recursive_fields(body
, NULL
, fields
)) {
245 if (SUCCESS
!= add_recursive_files(body
, NULL
, files
)) {
253 void php_http_message_body_add_part(php_http_message_body_t
*body
, php_http_message_t
*part
)
256 php_http_message_to_callback(part
, (php_http_pass_callback_t
) php_http_message_body_append
, body
);
257 BOUNDARY_CLOSE(body
);
261 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 zend_string
*safe_name
= zend_string_init(name
, strlen(name
), 0);
265 safe_name
= php_addslashes(safe_name
, 1);
268 php_http_message_body_appendf(
270 "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
274 php_http_message_body_append(body
, value_str
, value_len
);
275 BOUNDARY_CLOSE(body
);
277 zend_string_release(safe_name
);
281 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 size_t path_len
= strlen(path
);
284 char *path_dup
= estrndup(path
, path_len
);
285 zend_string
*base_name
, *safe_name
= zend_string_init(name
, strlen(name
), 0);
287 safe_name
= php_addslashes(safe_name
, 1);
288 base_name
= php_basename(path_dup
, path_len
, NULL
, 0);
291 php_http_message_body_appendf(
293 "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
294 "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
295 "Content-Type: %s" PHP_HTTP_CRLF
297 safe_name
->val
, base_name
->val
, ctype
299 php_stream_copy_to_stream_ex(in
, php_http_message_body_stream(body
), PHP_STREAM_COPY_ALL
, NULL
);
300 BOUNDARY_CLOSE(body
);
302 zend_string_release(safe_name
);
303 zend_string_release(base_name
);
309 static inline char *format_key(php_http_arrkey_t
*key
, const char *prefix
) {
310 char *new_key
= NULL
;
312 if (prefix
&& *prefix
) {
314 spprintf(&new_key
, 0, "%s[%s]", prefix
, key
->key
->val
);
316 spprintf(&new_key
, 0, "%s[%lu]", prefix
, key
->h
);
318 } else if (key
->key
) {
319 new_key
= estrdup(key
->key
->val
);
321 new_key
= estrdup("");
327 static ZEND_RESULT_CODE
add_recursive_field_value(php_http_message_body_t
*body
, const char *name
, zval
*value
)
329 zend_string
*zs
= zval_get_string(value
);
330 ZEND_RESULT_CODE rc
= php_http_message_body_add_form_field(body
, name
, zs
->val
, zs
->len
);
331 zend_string_release(zs
);
335 static ZEND_RESULT_CODE
add_recursive_fields(php_http_message_body_t
*body
, const char *name
, HashTable
*fields
)
338 php_http_arrkey_t key
;
340 if (!ZEND_HASH_GET_APPLY_COUNT(fields
)) {
341 ZEND_HASH_INC_APPLY_COUNT(fields
);
342 ZEND_HASH_FOREACH_KEY_VAL_IND(fields
, key
.h
, key
.key
, val
)
344 char *str
= format_key(&key
, name
);
346 if (Z_TYPE_P(val
) != IS_ARRAY
&& Z_TYPE_P(val
) != IS_OBJECT
) {
347 if (SUCCESS
!= add_recursive_field_value(body
, str
, val
)) {
349 ZEND_HASH_DEC_APPLY_COUNT(fields
);
352 } else if (SUCCESS
!= add_recursive_fields(body
, str
, HASH_OF(val
))) {
354 ZEND_HASH_DEC_APPLY_COUNT(fields
);
359 ZEND_HASH_FOREACH_END();
360 ZEND_HASH_DEC_APPLY_COUNT(fields
);
366 static ZEND_RESULT_CODE
add_recursive_files(php_http_message_body_t
*body
, const char *name
, HashTable
*files
)
368 zval
*zdata
= NULL
, *zfile
, *zname
, *ztype
;
371 if (!(zname
= zend_hash_str_find(files
, ZEND_STRL("name")))
372 || !(ztype
= zend_hash_str_find(files
, ZEND_STRL("type")))
373 || !(zfile
= zend_hash_str_find(files
, ZEND_STRL("file")))
376 php_http_arrkey_t key
;
378 if (!ZEND_HASH_GET_APPLY_COUNT(files
)) {
379 ZEND_HASH_INC_APPLY_COUNT(files
);
380 ZEND_HASH_FOREACH_KEY_VAL_IND(files
, key
.h
, key
.key
, val
)
382 if (Z_TYPE_P(val
) == IS_ARRAY
|| Z_TYPE_P(val
) == IS_OBJECT
) {
383 char *str
= format_key(&key
, name
);
385 if (SUCCESS
!= add_recursive_files(body
, str
, HASH_OF(val
))) {
387 ZEND_HASH_DEC_APPLY_COUNT(files
);
393 ZEND_HASH_FOREACH_END();
394 ZEND_HASH_DEC_APPLY_COUNT(files
);
400 zend_string
*zfc
= zval_get_string(zfile
);
402 if ((zdata
= zend_hash_str_find(files
, ZEND_STRL("data")))) {
403 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
404 php_stream_from_zval_no_verify(stream
, zdata
);
406 zend_string
*tmp
= zval_get_string(zdata
);
408 stream
= php_stream_memory_open(TEMP_STREAM_READONLY
, tmp
->val
, tmp
->len
);
409 zend_string_release(tmp
);
412 stream
= php_stream_open_wrapper(zfc
->val
, "r", REPORT_ERRORS
|USE_PATH
, NULL
);
416 zend_string_release(zfc
);
419 zend_string
*znc
= zval_get_string(zname
), *ztc
= zval_get_string(ztype
);
420 php_http_arrkey_t arrkey
= {0, znc
};
421 char *key
= format_key(&arrkey
, name
);
422 ZEND_RESULT_CODE ret
= php_http_message_body_add_form_file(body
, key
, ztc
->val
, zfc
->val
, stream
);
425 zend_string_release(znc
);
426 zend_string_release(ztc
);
427 zend_string_release(zfc
);
428 if (!zdata
|| Z_TYPE_P(zdata
) != IS_RESOURCE
) {
429 php_stream_close(stream
);
437 struct splitbody_arg
{
438 php_http_buffer_t buf
;
439 php_http_message_parser_t
*parser
;
445 static size_t splitbody(void *opaque
, char *buf
, size_t len
)
447 struct splitbody_arg
*arg
= opaque
;
448 const char *boundary
= NULL
;
453 first_boundary
= !(consumed
|| arg
->consumed
);
455 if ((boundary
= php_http_locate_str(buf
, len
, arg
->boundary_str
+ first_boundary
, arg
->boundary_len
- first_boundary
))) {
456 size_t real_boundary_len
= arg
->boundary_len
- 1, cut
;
457 const char *real_boundary
= boundary
+ !first_boundary
;
460 if (buf
+ len
<= real_boundary
+ real_boundary_len
) {
461 /* if we just have enough data for the boundary, it's just a byte too less */
462 arg
->consumed
+= consumed
;
466 if (!first_boundary
) {
467 /* this is not the first boundary, read rest of this message */
468 php_http_buffer_append(&arg
->buf
, buf
, real_boundary
- buf
);
469 php_http_message_parser_parse(arg
->parser
, &arg
->buf
, 0, &arg
->parser
->message
);
472 /* move after the boundary */
473 cut
= real_boundary
- buf
+ real_boundary_len
;
478 if (buf
== php_http_locate_bin_eol(buf
, len
, &eol_len
)) {
484 if (!first_boundary
) {
485 /* advance messages */
486 php_http_message_t
*msg
;
488 msg
= php_http_message_init(NULL
, 0, NULL
);
489 msg
->parent
= arg
->parser
->message
;
490 arg
->parser
->message
= msg
;
493 /* is this the last boundary? */
495 /* ignore the rest */
499 /* let this be garbage */
500 php_error_docref(NULL
, E_WARNING
, "Malformed multipart boundary at pos %zu", consumed
);
505 } while (boundary
&& len
);
507 /* let there be room for the next boundary */
508 if (len
> arg
->boundary_len
) {
509 consumed
+= len
- arg
->boundary_len
;
510 php_http_buffer_append(&arg
->buf
, buf
, len
- arg
->boundary_len
);
511 php_http_message_parser_parse(arg
->parser
, &arg
->buf
, 0, &arg
->parser
->message
);
514 arg
->consumed
+= consumed
;
518 php_http_message_t
*php_http_message_body_split(php_http_message_body_t
*body
, const char *boundary
)
520 php_stream
*s
= php_http_message_body_stream(body
);
521 php_http_buffer_t
*tmp
= NULL
;
522 php_http_message_t
*msg
= NULL
;
523 struct splitbody_arg arg
;
525 php_http_buffer_init(&arg
.buf
);
526 arg
.parser
= php_http_message_parser_init(NULL
);
527 arg
.boundary_len
= spprintf(&arg
.boundary_str
, 0, "\n--%s", boundary
);
530 php_stream_rewind(s
);
531 while (!php_stream_eof(s
)) {
532 php_http_buffer_passthru(&tmp
, 0x1000, (php_http_buffer_pass_func_t
) _php_stream_read
, s
, splitbody
, &arg
);
535 msg
= arg
.parser
->message
;
536 arg
.parser
->message
= NULL
;
538 php_http_buffer_free(&tmp
);
539 php_http_message_parser_free(&arg
.parser
);
540 php_http_buffer_dtor(&arg
.buf
);
541 PTR_FREE(arg
.boundary_str
);
546 static zend_object_handlers php_http_message_body_object_handlers
;
548 zend_object
*php_http_message_body_object_new(zend_class_entry
*ce
)
550 return &php_http_message_body_object_new_ex(ce
, NULL
)->zo
;
553 php_http_message_body_object_t
*php_http_message_body_object_new_ex(zend_class_entry
*ce
, php_http_message_body_t
*body
)
555 php_http_message_body_object_t
*o
;
557 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
558 zend_object_std_init(&o
->zo
, php_http_message_body_class_entry
);
559 object_properties_init(&o
->zo
, ce
);
565 o
->zo
.handlers
= &php_http_message_body_object_handlers
;
570 zend_object
*php_http_message_body_object_clone(zval
*object
)
572 php_http_message_body_object_t
*new_obj
= NULL
;
573 php_http_message_body_object_t
*old_obj
= PHP_HTTP_OBJ(NULL
, object
);
574 php_http_message_body_t
*body
= php_http_message_body_copy(old_obj
->body
, NULL
);
576 new_obj
= php_http_message_body_object_new_ex(old_obj
->zo
.ce
, body
);
577 zend_objects_clone_members(&new_obj
->zo
, &old_obj
->zo
);
582 void php_http_message_body_object_free(zend_object
*object
)
584 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(object
, NULL
);
586 php_http_message_body_free(&obj
->body
);
587 zend_object_std_dtor(object TSRMLS_CC
);
590 #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \
593 obj->body = php_http_message_body_init(NULL, NULL); \
597 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___construct
, 0, 0, 0)
598 ZEND_ARG_INFO(0, stream
)
600 PHP_METHOD(HttpMessageBody
, __construct
)
602 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
603 zval
*zstream
= NULL
;
606 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream
), invalid_arg
, return);
609 php_http_expect(php_stream_from_zval_no_verify(stream
, zstream
), unexpected_val
, return);
612 php_http_message_body_free(&obj
->body
);
614 obj
->body
= php_http_message_body_init(NULL
, stream
);
618 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___toString
, 0, 0, 0)
620 PHP_METHOD(HttpMessageBody
, __toString
)
622 if (SUCCESS
== zend_parse_parameters_none()) {
623 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
626 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
628 zs
= php_http_message_body_to_string(obj
->body
, 0, 0);
633 RETURN_EMPTY_STRING();
636 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_unserialize
, 0, 0, 1)
637 ZEND_ARG_INFO(0, serialized
)
639 PHP_METHOD(HttpMessageBody
, unserialize
)
644 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &us_str
, &us_len
)) {
645 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
646 php_stream
*s
= php_stream_memory_open(0, us_str
, us_len
);
648 obj
->body
= php_http_message_body_init(NULL
, s
);
652 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toStream
, 0, 0, 1)
653 ZEND_ARG_INFO(0, stream
)
654 ZEND_ARG_INFO(0, offset
)
655 ZEND_ARG_INFO(0, maxlen
)
657 PHP_METHOD(HttpMessageBody
, toStream
)
660 zend_long offset
= 0, forlen
= 0;
662 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &zstream
, &offset
, &forlen
)) {
664 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
666 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
668 php_stream_from_zval(stream
, zstream
);
669 php_http_message_body_to_stream(obj
->body
, stream
, offset
, forlen
);
670 RETURN_ZVAL(getThis(), 1, 0);
674 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toCallback
, 0, 0, 1)
675 ZEND_ARG_INFO(0, callback
)
676 ZEND_ARG_INFO(0, offset
)
677 ZEND_ARG_INFO(0, maxlen
)
679 PHP_METHOD(HttpMessageBody
, toCallback
)
681 php_http_pass_fcall_arg_t fcd
;
682 zend_long offset
= 0, forlen
= 0;
684 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "f|ll", &fcd
.fci
, &fcd
.fcc
, &offset
, &forlen
)) {
685 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
687 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
689 ZVAL_COPY(&fcd
.fcz
, getThis());
690 php_http_message_body_to_callback(obj
->body
, php_http_pass_fcall_callback
, &fcd
, offset
, forlen
);
691 zend_fcall_info_args_clear(&fcd
.fci
, 1);
692 zval_ptr_dtor(&fcd
.fcz
);
693 RETURN_ZVAL_FAST(getThis());
697 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getResource
, 0, 0, 0)
699 PHP_METHOD(HttpMessageBody
, getResource
)
701 if (SUCCESS
== zend_parse_parameters_none()) {
702 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
704 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
706 ++GC_REFCOUNT(obj
->body
->res
);
707 RETVAL_RES(obj
->body
->res
);
711 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getBoundary
, 0, 0, 0)
713 PHP_METHOD(HttpMessageBody
, getBoundary
)
715 if (SUCCESS
== zend_parse_parameters_none()) {
716 php_http_message_body_object_t
* obj
= PHP_HTTP_OBJ(NULL
, getThis());
718 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
720 if (obj
->body
->boundary
) {
721 RETURN_STRING(obj
->body
->boundary
);
726 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_append
, 0, 0, 1)
727 ZEND_ARG_INFO(0, string
)
729 PHP_METHOD(HttpMessageBody
, append
)
733 php_http_message_body_object_t
*obj
;
735 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
), invalid_arg
, return);
737 obj
= PHP_HTTP_OBJ(NULL
, getThis());
738 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
740 php_http_expect(len
== php_http_message_body_append(obj
->body
, str
, len
), runtime
, return);
742 RETURN_ZVAL_FAST(getThis());
745 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addForm
, 0, 0, 0)
746 ZEND_ARG_ARRAY_INFO(0, fields
, 1)
747 ZEND_ARG_ARRAY_INFO(0, files
, 1)
749 PHP_METHOD(HttpMessageBody
, addForm
)
751 HashTable
*fields
= NULL
, *files
= NULL
;
752 php_http_message_body_object_t
*obj
;
754 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|h!h!", &fields
, &files
), invalid_arg
, return);
756 obj
= PHP_HTTP_OBJ(NULL
, getThis());
757 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
759 php_http_expect(SUCCESS
== php_http_message_body_add_form(obj
->body
, fields
, files
), runtime
, return);
761 RETURN_ZVAL_FAST(getThis());
764 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart
, 0, 0, 1)
765 ZEND_ARG_OBJ_INFO(0, message
, http
\\Message
, 0)
767 PHP_METHOD(HttpMessageBody
, addPart
)
770 php_http_message_body_object_t
*obj
;
771 php_http_message_object_t
*mobj
;
772 zend_error_handling zeh
;
774 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zobj
, php_http_message_class_entry
), invalid_arg
, return);
776 obj
= PHP_HTTP_OBJ(NULL
, getThis());
777 mobj
= PHP_HTTP_OBJ(NULL
, zobj
);
779 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
781 zend_replace_error_handling(EH_THROW
, php_http_exception_runtime_class_entry
, &zeh
);
782 php_http_message_body_add_part(obj
->body
, mobj
->message
);
783 zend_restore_error_handling(&zeh
);
785 if (!EG(exception
)) {
786 RETURN_ZVAL_FAST(getThis());
790 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_etag
, 0, 0, 0)
792 PHP_METHOD(HttpMessageBody
, etag
)
794 if (SUCCESS
== zend_parse_parameters_none()) {
795 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
798 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
800 if ((etag
= php_http_message_body_etag(obj
->body
))) {
801 RETURN_STR(php_http_cs2zs(etag
, strlen(etag
)));
808 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_stat
, 0, 0, 0)
809 ZEND_ARG_INFO(0, field
)
811 PHP_METHOD(HttpMessageBody
, stat
)
813 char *field_str
= NULL
;
814 size_t field_len
= 0;
816 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &field_str
, &field_len
)) {
817 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
818 const php_stream_statbuf
*sb
;
820 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
822 if ((sb
= php_http_message_body_stat(obj
->body
))) {
823 if (field_str
&& field_len
) {
824 switch (*field_str
) {
827 RETURN_LONG(sb
->sb
.st_size
);
831 RETURN_LONG(sb
->sb
.st_atime
);
835 RETURN_LONG(sb
->sb
.st_mtime
);
839 RETURN_LONG(sb
->sb
.st_ctime
);
842 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
);
846 object_init(return_value
);
847 add_property_long_ex(return_value
, ZEND_STRL("size"), sb
->sb
.st_size
);
848 add_property_long_ex(return_value
, ZEND_STRL("atime"), sb
->sb
.st_atime
);
849 add_property_long_ex(return_value
, ZEND_STRL("mtime"), sb
->sb
.st_mtime
);
850 add_property_long_ex(return_value
, ZEND_STRL("ctime"), sb
->sb
.st_ctime
);
856 static zend_function_entry php_http_message_body_methods
[] = {
857 PHP_ME(HttpMessageBody
, __construct
, ai_HttpMessageBody___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
858 PHP_ME(HttpMessageBody
, __toString
, ai_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
859 PHP_MALIAS(HttpMessageBody
, toString
, __toString
, ai_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
860 PHP_MALIAS(HttpMessageBody
, serialize
, __toString
, ai_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
861 PHP_ME(HttpMessageBody
, unserialize
, ai_HttpMessageBody_unserialize
, ZEND_ACC_PUBLIC
)
862 PHP_ME(HttpMessageBody
, toStream
, ai_HttpMessageBody_toStream
, ZEND_ACC_PUBLIC
)
863 PHP_ME(HttpMessageBody
, toCallback
, ai_HttpMessageBody_toCallback
, ZEND_ACC_PUBLIC
)
864 PHP_ME(HttpMessageBody
, getResource
, ai_HttpMessageBody_getResource
, ZEND_ACC_PUBLIC
)
865 PHP_ME(HttpMessageBody
, getBoundary
, ai_HttpMessageBody_getBoundary
, ZEND_ACC_PUBLIC
)
866 PHP_ME(HttpMessageBody
, append
, ai_HttpMessageBody_append
, ZEND_ACC_PUBLIC
)
867 PHP_ME(HttpMessageBody
, addForm
, ai_HttpMessageBody_addForm
, ZEND_ACC_PUBLIC
)
868 PHP_ME(HttpMessageBody
, addPart
, ai_HttpMessageBody_addPart
, ZEND_ACC_PUBLIC
)
869 PHP_ME(HttpMessageBody
, etag
, ai_HttpMessageBody_etag
, ZEND_ACC_PUBLIC
)
870 PHP_ME(HttpMessageBody
, stat
, ai_HttpMessageBody_stat
, ZEND_ACC_PUBLIC
)
874 zend_class_entry
*php_http_message_body_class_entry
;
876 PHP_MINIT_FUNCTION(http_message_body
)
878 zend_class_entry ce
= {0};
880 INIT_NS_CLASS_ENTRY(ce
, "http\\Message", "Body", php_http_message_body_methods
);
881 php_http_message_body_class_entry
= zend_register_internal_class(&ce
);
882 php_http_message_body_class_entry
->create_object
= php_http_message_body_object_new
;
883 memcpy(&php_http_message_body_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
884 php_http_message_body_object_handlers
.offset
= XtOffsetOf(php_http_message_body_object_t
, zo
);
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
);
897 * vim600: noet sw=4 ts=4 fdm=marker
898 * vim<600: noet sw=4 ts=4