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
) {
40 php_http_message_body_addref(body
);
44 body
= ecalloc(1, sizeof(php_http_message_body_t
));
48 body
->res
= stream
->res
;
51 body
->res
= php_stream_temp_create(TEMP_STREAM_DEFAULT
, 0xffff)->res
;
53 php_stream_auto_cleanup(php_http_message_body_stream(body
));
62 unsigned php_http_message_body_addref(php_http_message_body_t
*body
)
64 return ++body
->refcount
;
67 php_http_message_body_t
*php_http_message_body_copy(php_http_message_body_t
*from
, php_http_message_body_t
*to
)
71 php_stream_truncate_set_size(php_http_message_body_stream(to
), 0);
73 to
= php_http_message_body_init(NULL
, NULL
);
75 php_http_message_body_to_stream(from
, php_http_message_body_stream(to
), 0, 0);
81 to
->boundary
= estrdup(from
->boundary
);
89 void php_http_message_body_free(php_http_message_body_t
**body_ptr
)
92 php_http_message_body_t
*body
= *body_ptr
;
93 if (!--body
->refcount
) {
94 zend_list_delete(body
->res
);
96 PTR_FREE(body
->boundary
);
103 const php_stream_statbuf
*php_http_message_body_stat(php_http_message_body_t
*body
)
105 php_stream_stat(php_http_message_body_stream(body
), &body
->ssb
);
109 const char *php_http_message_body_boundary(php_http_message_body_t
*body
)
111 if (!body
->boundary
) {
112 union { double dbl
; int num
[2]; } data
;
114 data
.dbl
= php_combined_lcg();
115 spprintf(&body
->boundary
, 0, "%x.%x", data
.num
[0], data
.num
[1]);
117 return body
->boundary
;
120 char *php_http_message_body_etag(php_http_message_body_t
*body
)
122 php_http_etag_t
*etag
;
123 php_stream
*s
= php_http_message_body_stream(body
);
125 /* real file or temp buffer ? */
126 if (s
->ops
!= &php_stream_temp_ops
&& s
->ops
!= &php_stream_memory_ops
) {
127 php_stream_stat(php_http_message_body_stream(body
), &body
->ssb
);
129 if (body
->ssb
.sb
.st_mtime
) {
132 spprintf(&etag
, 0, "%lx-%lx-%lx", body
->ssb
.sb
.st_ino
, body
->ssb
.sb
.st_mtime
, body
->ssb
.sb
.st_size
);
138 if ((etag
= php_http_etag_init(PHP_HTTP_G
->env
.etag_mode
))) {
139 php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_etag_update
, etag
, 0, 0);
140 return php_http_etag_finish(etag
);
146 zend_string
*php_http_message_body_to_string(php_http_message_body_t
*body
, off_t offset
, size_t forlen
)
148 php_stream
*s
= php_http_message_body_stream(body
);
150 php_stream_seek(s
, offset
, SEEK_SET
);
154 return php_stream_copy_to_mem(s
, forlen
, 0);
157 ZEND_RESULT_CODE
php_http_message_body_to_stream(php_http_message_body_t
*body
, php_stream
*dst
, off_t offset
, size_t forlen
)
159 php_stream
*s
= php_http_message_body_stream(body
);
161 php_stream_seek(s
, offset
, SEEK_SET
);
166 return php_stream_copy_to_stream_ex(s
, dst
, forlen
, NULL
);
169 ZEND_RESULT_CODE
php_http_message_body_to_callback(php_http_message_body_t
*body
, php_http_pass_callback_t cb
, void *cb_arg
, off_t offset
, size_t forlen
)
171 php_stream
*s
= php_http_message_body_stream(body
);
172 char *buf
= emalloc(0x1000);
174 php_stream_seek(s
, offset
, SEEK_SET
);
179 while (!php_stream_eof(s
)) {
180 size_t read
= php_stream_read(s
, buf
, MIN(forlen
, 0x1000));
183 if (-1 == cb(cb_arg
, buf
, read
)) {
188 if (read
< MIN(forlen
, sizeof(buf
))) {
192 if (forlen
&& !(forlen
-= read
)) {
201 size_t php_http_message_body_append(php_http_message_body_t
*body
, const char *buf
, size_t len
)
206 if (!(s
= php_http_message_body_stream(body
))) {
211 php_stream_seek(s
, 0, SEEK_END
);
214 written
= php_stream_write(s
, buf
, len
);
216 if (written
!= len
) {
217 php_error_docref(NULL
, E_WARNING
, "Failed to append %zu bytes to body; wrote %zu", len
, written
);
223 size_t php_http_message_body_appendf(php_http_message_body_t
*body
, const char *fmt
, ...)
230 print_len
= vspprintf(&print_str
, 0, fmt
, argv
);
233 print_len
= php_http_message_body_append(body
, print_str
, print_len
);
239 ZEND_RESULT_CODE
php_http_message_body_add_form(php_http_message_body_t
*body
, HashTable
*fields
, HashTable
*files
)
242 if (SUCCESS
!= add_recursive_fields(body
, NULL
, fields
)) {
247 if (SUCCESS
!= add_recursive_files(body
, NULL
, files
)) {
255 void php_http_message_body_add_part(php_http_message_body_t
*body
, php_http_message_t
*part
)
258 php_http_message_to_callback(part
, (php_http_pass_callback_t
) php_http_message_body_append
, body
);
259 BOUNDARY_CLOSE(body
);
263 ZEND_RESULT_CODE
php_http_message_body_add_form_field(php_http_message_body_t
*body
, const char *name
, const char *value_str
, size_t value_len
)
265 zend_string
*safe_name
, *zstr_name
= zend_string_init(name
, strlen(name
), 0);
267 #if PHP_VERSION_ID < 70300
268 safe_name
= php_addslashes(zstr_name
, 1);
270 safe_name
= php_addslashes(zstr_name
);
271 zend_string_release_ex(zstr_name
, 0);
275 php_http_message_body_appendf(
277 "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
281 php_http_message_body_append(body
, value_str
, value_len
);
282 BOUNDARY_CLOSE(body
);
284 zend_string_release(safe_name
);
288 ZEND_RESULT_CODE
php_http_message_body_add_form_file(php_http_message_body_t
*body
, const char *name
, const char *ctype
, const char *path
, php_stream
*in
)
290 size_t path_len
= strlen(path
);
291 char *path_dup
= estrndup(path
, path_len
);
292 zend_string
*base_name
, *safe_name
, *zstr_name
= zend_string_init(name
, strlen(name
), 0);
294 #if PHP_VERSION_ID < 70300
295 safe_name
= php_addslashes(zstr_name
, 1);
297 safe_name
= php_addslashes(zstr_name
);
298 zend_string_release_ex(zstr_name
, 0);
300 base_name
= php_basename(path_dup
, path_len
, NULL
, 0);
303 php_http_message_body_appendf(
305 "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
306 "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
307 "Content-Type: %s" PHP_HTTP_CRLF
309 safe_name
->val
, base_name
->val
, ctype
311 php_stream_copy_to_stream_ex(in
, php_http_message_body_stream(body
), PHP_STREAM_COPY_ALL
, NULL
);
312 BOUNDARY_CLOSE(body
);
314 zend_string_release(safe_name
);
315 zend_string_release(base_name
);
321 static inline char *format_key(php_http_arrkey_t
*key
, const char *prefix
) {
322 char *new_key
= NULL
;
324 if (prefix
&& *prefix
) {
326 spprintf(&new_key
, 0, "%s[%s]", prefix
, key
->key
->val
);
328 spprintf(&new_key
, 0, "%s[%lu]", prefix
, key
->h
);
330 } else if (key
->key
) {
331 new_key
= estrdup(key
->key
->val
);
333 new_key
= estrdup("");
339 static ZEND_RESULT_CODE
add_recursive_field_value(php_http_message_body_t
*body
, const char *name
, zval
*value
)
341 zend_string
*zs
= zval_get_string(value
);
342 ZEND_RESULT_CODE rc
= php_http_message_body_add_form_field(body
, name
, zs
->val
, zs
->len
);
343 zend_string_release(zs
);
347 static ZEND_RESULT_CODE
add_recursive_fields(php_http_message_body_t
*body
, const char *name
, HashTable
*fields
)
350 php_http_arrkey_t key
;
352 if (!HT_IS_RECURSIVE(fields
)) {
353 HT_PROTECT_RECURSION(fields
);
354 ZEND_HASH_FOREACH_KEY_VAL_IND(fields
, key
.h
, key
.key
, val
)
356 char *str
= format_key(&key
, name
);
358 if (Z_TYPE_P(val
) != IS_ARRAY
&& Z_TYPE_P(val
) != IS_OBJECT
) {
359 if (SUCCESS
!= add_recursive_field_value(body
, str
, val
)) {
361 HT_UNPROTECT_RECURSION(fields
);
364 } else if (SUCCESS
!= add_recursive_fields(body
, str
, HASH_OF(val
))) {
366 HT_UNPROTECT_RECURSION(fields
);
371 ZEND_HASH_FOREACH_END();
372 HT_UNPROTECT_RECURSION(fields
);
378 static ZEND_RESULT_CODE
add_recursive_files(php_http_message_body_t
*body
, const char *name
, HashTable
*files
)
380 zval
*zdata
= NULL
, *zfile
, *zname
, *ztype
;
383 if (!(zname
= zend_hash_str_find(files
, ZEND_STRL("name")))
384 || !(ztype
= zend_hash_str_find(files
, ZEND_STRL("type")))
385 || !(zfile
= zend_hash_str_find(files
, ZEND_STRL("file")))
388 php_http_arrkey_t key
;
390 if (!HT_IS_RECURSIVE(files
)) {
391 HT_PROTECT_RECURSION(files
);
392 ZEND_HASH_FOREACH_KEY_VAL_IND(files
, key
.h
, key
.key
, val
)
394 if (Z_TYPE_P(val
) == IS_ARRAY
|| Z_TYPE_P(val
) == IS_OBJECT
) {
395 char *str
= format_key(&key
, name
);
397 if (SUCCESS
!= add_recursive_files(body
, str
, HASH_OF(val
))) {
399 HT_UNPROTECT_RECURSION(files
);
405 ZEND_HASH_FOREACH_END();
406 HT_UNPROTECT_RECURSION(files
);
412 zend_string
*zfc
= zval_get_string(zfile
);
414 if ((zdata
= zend_hash_str_find(files
, ZEND_STRL("data")))) {
415 if (Z_TYPE_P(zdata
) == IS_RESOURCE
) {
416 php_stream_from_zval_no_verify(stream
, zdata
);
418 zend_string
*tmp
= zval_get_string(zdata
);
420 stream
= php_stream_memory_open(TEMP_STREAM_READONLY
, tmp
->val
, tmp
->len
);
421 zend_string_release(tmp
);
424 stream
= php_stream_open_wrapper(zfc
->val
, "r", REPORT_ERRORS
|USE_PATH
, NULL
);
428 zend_string_release(zfc
);
431 zend_string
*znc
= zval_get_string(zname
), *ztc
= zval_get_string(ztype
);
432 php_http_arrkey_t arrkey
= {0, znc
};
433 char *key
= format_key(&arrkey
, name
);
434 ZEND_RESULT_CODE ret
= php_http_message_body_add_form_file(body
, key
, ztc
->val
, zfc
->val
, stream
);
437 zend_string_release(znc
);
438 zend_string_release(ztc
);
439 zend_string_release(zfc
);
440 if (!zdata
|| Z_TYPE_P(zdata
) != IS_RESOURCE
) {
441 php_stream_close(stream
);
449 struct splitbody_arg
{
450 php_http_buffer_t buf
;
451 php_http_message_parser_t
*parser
;
457 static size_t splitbody(void *opaque
, char *buf
, size_t len
)
459 struct splitbody_arg
*arg
= opaque
;
460 const char *boundary
= NULL
;
465 first_boundary
= !(consumed
|| arg
->consumed
);
467 if ((boundary
= php_http_locate_str(buf
, len
, arg
->boundary_str
+ first_boundary
, arg
->boundary_len
- first_boundary
))) {
468 size_t real_boundary_len
= arg
->boundary_len
- 1, cut
;
469 const char *real_boundary
= boundary
+ !first_boundary
;
472 if (buf
+ len
<= real_boundary
+ real_boundary_len
) {
473 /* if we just have enough data for the boundary, it's just a byte too less */
474 arg
->consumed
+= consumed
;
478 if (!first_boundary
) {
479 /* this is not the first boundary, read rest of this message */
480 php_http_buffer_append(&arg
->buf
, buf
, real_boundary
- buf
);
481 php_http_message_parser_parse(arg
->parser
, &arg
->buf
, 0, &arg
->parser
->message
);
484 /* move after the boundary */
485 cut
= real_boundary
- buf
+ real_boundary_len
;
490 if (buf
== php_http_locate_bin_eol(buf
, len
, &eol_len
)) {
496 if (!first_boundary
) {
497 /* advance messages */
498 php_http_message_t
*msg
;
500 msg
= php_http_message_init(NULL
, 0, NULL
);
501 msg
->parent
= arg
->parser
->message
;
502 arg
->parser
->message
= msg
;
505 /* is this the last boundary? */
507 /* ignore the rest */
511 /* let this be garbage */
512 php_error_docref(NULL
, E_WARNING
, "Malformed multipart boundary at pos %zu", consumed
);
517 } while (boundary
&& len
);
519 /* let there be room for the next boundary */
520 if (len
> arg
->boundary_len
) {
521 consumed
+= len
- arg
->boundary_len
;
522 php_http_buffer_append(&arg
->buf
, buf
, len
- arg
->boundary_len
);
523 php_http_message_parser_parse(arg
->parser
, &arg
->buf
, 0, &arg
->parser
->message
);
526 arg
->consumed
+= consumed
;
530 php_http_message_t
*php_http_message_body_split(php_http_message_body_t
*body
, const char *boundary
)
532 php_stream
*s
= php_http_message_body_stream(body
);
533 php_http_buffer_t
*tmp
= NULL
;
534 php_http_message_t
*msg
= NULL
;
535 struct splitbody_arg arg
;
537 php_http_buffer_init(&arg
.buf
);
538 arg
.parser
= php_http_message_parser_init(NULL
);
539 arg
.boundary_len
= spprintf(&arg
.boundary_str
, 0, "\n--%s", boundary
);
542 php_stream_rewind(s
);
543 while (!php_stream_eof(s
)) {
544 php_http_buffer_passthru(&tmp
, 0x1000, (php_http_buffer_pass_func_t
) _php_stream_read
, s
, splitbody
, &arg
);
547 msg
= arg
.parser
->message
;
548 arg
.parser
->message
= NULL
;
550 php_http_buffer_free(&tmp
);
551 php_http_message_parser_free(&arg
.parser
);
552 php_http_buffer_dtor(&arg
.buf
);
553 PTR_FREE(arg
.boundary_str
);
558 static zend_class_entry
*php_http_message_body_class_entry
;
559 zend_class_entry
*php_http_get_message_body_class_entry(void)
561 return php_http_message_body_class_entry
;
564 static zend_object_handlers php_http_message_body_object_handlers
;
566 zend_object
*php_http_message_body_object_new(zend_class_entry
*ce
)
568 return &php_http_message_body_object_new_ex(ce
, NULL
)->zo
;
571 php_http_message_body_object_t
*php_http_message_body_object_new_ex(zend_class_entry
*ce
, php_http_message_body_t
*body
)
573 php_http_message_body_object_t
*o
;
575 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
576 zend_object_std_init(&o
->zo
, php_http_message_body_class_entry
);
577 object_properties_init(&o
->zo
, ce
);
579 o
->gc
= emalloc(sizeof(zval
));
585 o
->zo
.handlers
= &php_http_message_body_object_handlers
;
590 zend_object
*php_http_message_body_object_clone(zval
*object
)
592 php_http_message_body_object_t
*new_obj
;
593 php_http_message_body_object_t
*old_obj
= PHP_HTTP_OBJ(NULL
, object
);
594 php_http_message_body_t
*body
= php_http_message_body_copy(old_obj
->body
, NULL
);
596 new_obj
= php_http_message_body_object_new_ex(old_obj
->zo
.ce
, body
);
597 zend_objects_clone_members(&new_obj
->zo
, &old_obj
->zo
);
602 static HashTable
*php_http_message_body_object_get_gc(zval
*object
, zval
**table
, int *n
)
604 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, object
);
605 HashTable
*props
= Z_OBJPROP_P(object
);
606 uint32_t count
= zend_hash_num_elements(props
);
608 obj
->gc
= erealloc(obj
->gc
, (1 + count
) * sizeof(zval
));
610 if (php_http_message_body_stream(obj
->body
)) {
612 php_stream_to_zval(php_http_message_body_stream(obj
->body
), obj
->gc
);
620 ZEND_HASH_FOREACH_VAL(props
, val
)
622 ZVAL_COPY_VALUE(&obj
->gc
[(*n
)++], val
);
624 ZEND_HASH_FOREACH_END();
631 void php_http_message_body_object_free(zend_object
*object
)
633 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(object
, NULL
);
636 php_http_message_body_free(&obj
->body
);
637 zend_object_std_dtor(object
);
640 #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \
643 obj->body = php_http_message_body_init(NULL, NULL); \
644 php_stream_to_zval(php_http_message_body_stream(obj->body), obj->gc); \
648 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___construct
, 0, 0, 0)
649 ZEND_ARG_INFO(0, stream
)
651 PHP_METHOD(HttpMessageBody
, __construct
)
653 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
654 zval
*zstream
= NULL
;
657 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream
), invalid_arg
, return);
660 php_http_expect(php_stream_from_zval_no_verify(stream
, zstream
), unexpected_val
, return);
663 php_http_message_body_free(&obj
->body
);
665 obj
->body
= php_http_message_body_init(NULL
, stream
);
666 php_stream_to_zval(stream
, obj
->gc
);
670 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___toString
, 0, 0, 0)
672 PHP_METHOD(HttpMessageBody
, __toString
)
674 if (SUCCESS
== zend_parse_parameters_none()) {
675 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
678 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
680 zs
= php_http_message_body_to_string(obj
->body
, 0, 0);
685 RETURN_EMPTY_STRING();
688 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_unserialize
, 0, 0, 1)
689 ZEND_ARG_INFO(0, serialized
)
691 PHP_METHOD(HttpMessageBody
, unserialize
)
696 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &us_str
, &us_len
)) {
697 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
698 php_stream
*s
= php_stream_memory_open(0, us_str
, us_len
);
700 obj
->body
= php_http_message_body_init(NULL
, s
);
701 php_stream_to_zval(s
, obj
->gc
);
705 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toStream
, 0, 0, 1)
706 ZEND_ARG_INFO(0, stream
)
707 ZEND_ARG_INFO(0, offset
)
708 ZEND_ARG_INFO(0, maxlen
)
710 PHP_METHOD(HttpMessageBody
, toStream
)
713 zend_long offset
= 0, forlen
= 0;
715 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &zstream
, &offset
, &forlen
)) {
717 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
719 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
721 php_stream_from_zval(stream
, zstream
);
722 php_http_message_body_to_stream(obj
->body
, stream
, offset
, forlen
);
723 RETURN_ZVAL(getThis(), 1, 0);
727 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toCallback
, 0, 0, 1)
728 ZEND_ARG_INFO(0, callback
)
729 ZEND_ARG_INFO(0, offset
)
730 ZEND_ARG_INFO(0, maxlen
)
732 PHP_METHOD(HttpMessageBody
, toCallback
)
734 php_http_pass_fcall_arg_t fcd
;
735 zend_long offset
= 0, forlen
= 0;
737 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "f|ll", &fcd
.fci
, &fcd
.fcc
, &offset
, &forlen
)) {
738 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
740 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
742 ZVAL_COPY(&fcd
.fcz
, getThis());
743 php_http_message_body_to_callback(obj
->body
, php_http_pass_fcall_callback
, &fcd
, offset
, forlen
);
744 zend_fcall_info_args_clear(&fcd
.fci
, 1);
745 zval_ptr_dtor(&fcd
.fcz
);
746 RETURN_ZVAL(getThis(), 1, 0);
750 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getResource
, 0, 0, 0)
752 PHP_METHOD(HttpMessageBody
, getResource
)
754 if (SUCCESS
== zend_parse_parameters_none()) {
755 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
757 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
759 php_stream_to_zval(php_http_message_body_stream(obj
->body
), return_value
);
760 Z_ADDREF_P(return_value
);
764 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getBoundary
, 0, 0, 0)
766 PHP_METHOD(HttpMessageBody
, getBoundary
)
768 if (SUCCESS
== zend_parse_parameters_none()) {
769 php_http_message_body_object_t
* obj
= PHP_HTTP_OBJ(NULL
, getThis());
771 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
773 if (obj
->body
->boundary
) {
774 RETURN_STRING(obj
->body
->boundary
);
779 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_append
, 0, 0, 1)
780 ZEND_ARG_INFO(0, string
)
782 PHP_METHOD(HttpMessageBody
, append
)
786 php_http_message_body_object_t
*obj
;
788 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
), invalid_arg
, return);
790 obj
= PHP_HTTP_OBJ(NULL
, getThis());
791 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
793 php_http_expect(len
== php_http_message_body_append(obj
->body
, str
, len
), runtime
, return);
795 RETURN_ZVAL(getThis(), 1, 0);
798 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addForm
, 0, 0, 0)
799 ZEND_ARG_ARRAY_INFO(0, fields
, 1)
800 ZEND_ARG_ARRAY_INFO(0, files
, 1)
802 PHP_METHOD(HttpMessageBody
, addForm
)
804 HashTable
*fields
= NULL
, *files
= NULL
;
805 php_http_message_body_object_t
*obj
;
807 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|h!h!", &fields
, &files
), invalid_arg
, return);
809 obj
= PHP_HTTP_OBJ(NULL
, getThis());
810 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
812 php_http_expect(SUCCESS
== php_http_message_body_add_form(obj
->body
, fields
, files
), runtime
, return);
814 RETURN_ZVAL(getThis(), 1, 0);
817 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart
, 0, 0, 1)
818 ZEND_ARG_OBJ_INFO(0, message
, http
\\Message
, 0)
820 PHP_METHOD(HttpMessageBody
, addPart
)
823 php_http_message_body_object_t
*obj
;
824 php_http_message_object_t
*mobj
;
825 zend_error_handling zeh
;
827 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zobj
, php_http_message_get_class_entry()), invalid_arg
, return);
829 obj
= PHP_HTTP_OBJ(NULL
, getThis());
830 mobj
= PHP_HTTP_OBJ(NULL
, zobj
);
832 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
834 zend_replace_error_handling(EH_THROW
, php_http_get_exception_runtime_class_entry(), &zeh
);
835 php_http_message_body_add_part(obj
->body
, mobj
->message
);
836 zend_restore_error_handling(&zeh
);
838 if (!EG(exception
)) {
839 RETURN_ZVAL(getThis(), 1, 0);
843 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_etag
, 0, 0, 0)
845 PHP_METHOD(HttpMessageBody
, etag
)
847 if (SUCCESS
== zend_parse_parameters_none()) {
848 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
851 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
853 if ((etag
= php_http_message_body_etag(obj
->body
))) {
854 RETURN_STR(php_http_cs2zs(etag
, strlen(etag
)));
861 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_stat
, 0, 0, 0)
862 ZEND_ARG_INFO(0, field
)
864 PHP_METHOD(HttpMessageBody
, stat
)
866 char *field_str
= NULL
;
867 size_t field_len
= 0;
869 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &field_str
, &field_len
)) {
870 php_http_message_body_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
871 const php_stream_statbuf
*sb
;
873 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
875 if ((sb
= php_http_message_body_stat(obj
->body
))) {
876 if (field_str
&& field_len
) {
877 switch (*field_str
) {
880 RETURN_LONG(sb
->sb
.st_size
);
884 RETURN_LONG(sb
->sb
.st_atime
);
888 RETURN_LONG(sb
->sb
.st_mtime
);
892 RETURN_LONG(sb
->sb
.st_ctime
);
895 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
);
899 object_init(return_value
);
900 add_property_long_ex(return_value
, ZEND_STRL("size"), sb
->sb
.st_size
);
901 add_property_long_ex(return_value
, ZEND_STRL("atime"), sb
->sb
.st_atime
);
902 add_property_long_ex(return_value
, ZEND_STRL("mtime"), sb
->sb
.st_mtime
);
903 add_property_long_ex(return_value
, ZEND_STRL("ctime"), sb
->sb
.st_ctime
);
909 static zend_function_entry php_http_message_body_methods
[] = {
910 PHP_ME(HttpMessageBody
, __construct
, ai_HttpMessageBody___construct
, ZEND_ACC_PUBLIC
)
911 PHP_ME(HttpMessageBody
, __toString
, ai_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
912 PHP_MALIAS(HttpMessageBody
, toString
, __toString
, ai_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
913 PHP_MALIAS(HttpMessageBody
, serialize
, __toString
, ai_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
914 PHP_ME(HttpMessageBody
, unserialize
, ai_HttpMessageBody_unserialize
, ZEND_ACC_PUBLIC
)
915 PHP_ME(HttpMessageBody
, toStream
, ai_HttpMessageBody_toStream
, ZEND_ACC_PUBLIC
)
916 PHP_ME(HttpMessageBody
, toCallback
, ai_HttpMessageBody_toCallback
, ZEND_ACC_PUBLIC
)
917 PHP_ME(HttpMessageBody
, getResource
, ai_HttpMessageBody_getResource
, ZEND_ACC_PUBLIC
)
918 PHP_ME(HttpMessageBody
, getBoundary
, ai_HttpMessageBody_getBoundary
, ZEND_ACC_PUBLIC
)
919 PHP_ME(HttpMessageBody
, append
, ai_HttpMessageBody_append
, ZEND_ACC_PUBLIC
)
920 PHP_ME(HttpMessageBody
, addForm
, ai_HttpMessageBody_addForm
, ZEND_ACC_PUBLIC
)
921 PHP_ME(HttpMessageBody
, addPart
, ai_HttpMessageBody_addPart
, ZEND_ACC_PUBLIC
)
922 PHP_ME(HttpMessageBody
, etag
, ai_HttpMessageBody_etag
, ZEND_ACC_PUBLIC
)
923 PHP_ME(HttpMessageBody
, stat
, ai_HttpMessageBody_stat
, ZEND_ACC_PUBLIC
)
927 PHP_MINIT_FUNCTION(http_message_body
)
929 zend_class_entry ce
= {0};
931 INIT_NS_CLASS_ENTRY(ce
, "http\\Message", "Body", php_http_message_body_methods
);
932 php_http_message_body_class_entry
= zend_register_internal_class(&ce
);
933 php_http_message_body_class_entry
->create_object
= php_http_message_body_object_new
;
934 memcpy(&php_http_message_body_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
935 php_http_message_body_object_handlers
.offset
= XtOffsetOf(php_http_message_body_object_t
, zo
);
936 php_http_message_body_object_handlers
.clone_obj
= php_http_message_body_object_clone
;
937 php_http_message_body_object_handlers
.free_obj
= php_http_message_body_object_free
;
938 php_http_message_body_object_handlers
.get_gc
= php_http_message_body_object_get_gc
;
939 zend_class_implements(php_http_message_body_class_entry
, 1, zend_ce_serializable
);
949 * vim600: noet sw=4 ts=4 fdm=marker
950 * vim<600: noet sw=4 ts=4