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
, zval
*value
);
32 static ZEND_RESULT_CODE
add_recursive_files(php_http_message_body_t
*body
, const char *name
, zval
*value
);
34 php_http_message_body_t
*php_http_message_body_init(php_http_message_body_t
**body_ptr
, php_stream
*stream TSRMLS_DC
)
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
->stream_id
= php_stream_get_resource_id(stream
);
50 zend_list_addref(body
->stream_id
);
52 stream
= php_stream_temp_create(TEMP_STREAM_DEFAULT
, 0xffff);
53 php_stream_auto_cleanup(stream
);
54 body
->stream_id
= php_stream_get_resource_id(stream
);
56 TSRMLS_SET_CTX(body
->ts
);
65 unsigned php_http_message_body_addref(php_http_message_body_t
*body
)
67 return ++body
->refcount
;
70 php_http_message_body_t
*php_http_message_body_copy(php_http_message_body_t
*from
, php_http_message_body_t
*to
)
73 TSRMLS_FETCH_FROM_CTX(from
->ts
);
76 php_stream_truncate_set_size(php_http_message_body_stream(to
), 0);
78 to
= php_http_message_body_init(NULL
, NULL TSRMLS_CC
);
80 php_http_message_body_to_stream(from
, php_http_message_body_stream(to
), 0, 0);
86 to
->boundary
= estrdup(from
->boundary
);
94 void php_http_message_body_free(php_http_message_body_t
**body_ptr
)
97 php_http_message_body_t
*body
= *body_ptr
;
99 if (!--body
->refcount
) {
100 TSRMLS_FETCH_FROM_CTX(body
->ts
);
101 /* NOFIXME: shows leakinfo in DEBUG mode */
102 zend_list_delete(body
->stream_id
);
103 PTR_FREE(body
->boundary
);
110 const php_stream_statbuf
*php_http_message_body_stat(php_http_message_body_t
*body
)
112 TSRMLS_FETCH_FROM_CTX(body
->ts
);
113 php_stream_stat(php_http_message_body_stream(body
), &body
->ssb
);
117 const char *php_http_message_body_boundary(php_http_message_body_t
*body
)
119 if (!body
->boundary
) {
120 union { double dbl
; int num
[2]; } data
;
121 TSRMLS_FETCH_FROM_CTX(body
->ts
);
123 data
.dbl
= php_combined_lcg(TSRMLS_C
);
124 spprintf(&body
->boundary
, 0, "%x.%x", data
.num
[0], data
.num
[1]);
126 return body
->boundary
;
129 char *php_http_message_body_etag(php_http_message_body_t
*body
)
131 php_http_etag_t
*etag
;
132 php_stream
*s
= php_http_message_body_stream(body
);
133 TSRMLS_FETCH_FROM_CTX(body
->ts
);
135 /* real file or temp buffer ? */
136 if (s
->ops
!= &php_stream_temp_ops
&& s
->ops
!= &php_stream_memory_ops
) {
137 php_stream_stat(php_http_message_body_stream(body
), &body
->ssb
);
139 if (body
->ssb
.sb
.st_mtime
) {
142 spprintf(&etag
, 0, "%lx-%lx-%lx", body
->ssb
.sb
.st_ino
, body
->ssb
.sb
.st_mtime
, body
->ssb
.sb
.st_size
);
148 if ((etag
= php_http_etag_init(PHP_HTTP_G
->env
.etag_mode TSRMLS_CC
))) {
149 php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_etag_update
, etag
, 0, 0);
150 return php_http_etag_finish(etag
);
156 void php_http_message_body_to_string(php_http_message_body_t
*body
, char **buf
, size_t *len
, off_t offset
, size_t forlen
)
158 php_stream
*s
= php_http_message_body_stream(body
);
159 TSRMLS_FETCH_FROM_CTX(body
->ts
);
161 php_stream_seek(s
, offset
, SEEK_SET
);
165 *len
= php_stream_copy_to_mem(s
, buf
, forlen
, 0);
168 ZEND_RESULT_CODE
php_http_message_body_to_stream(php_http_message_body_t
*body
, php_stream
*dst
, off_t offset
, size_t forlen
)
170 php_stream
*s
= php_http_message_body_stream(body
);
171 TSRMLS_FETCH_FROM_CTX(body
->ts
);
173 php_stream_seek(s
, offset
, SEEK_SET
);
178 return php_stream_copy_to_stream_ex(s
, dst
, forlen
, NULL
);
181 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
)
183 php_stream
*s
= php_http_message_body_stream(body
);
184 char *buf
= emalloc(0x1000);
185 TSRMLS_FETCH_FROM_CTX(body
->ts
);
187 php_stream_seek(s
, offset
, SEEK_SET
);
192 while (!php_stream_eof(s
)) {
193 size_t read
= php_stream_read(s
, buf
, MIN(forlen
, 0x1000));
196 if (-1 == cb(cb_arg
, buf
, read
)) {
201 if (read
< MIN(forlen
, sizeof(buf
))) {
205 if (forlen
&& !(forlen
-= read
)) {
214 size_t php_http_message_body_append(php_http_message_body_t
*body
, const char *buf
, size_t len
)
218 TSRMLS_FETCH_FROM_CTX(body
->ts
);
220 if (!(s
= php_http_message_body_stream(body
))) {
225 php_stream_seek(s
, 0, SEEK_END
);
228 written
= php_stream_write(s
, buf
, len
);
230 if (written
!= len
) {
231 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to append %zu bytes to body; wrote %zu", len
, written
);
237 size_t php_http_message_body_appendf(php_http_message_body_t
*body
, const char *fmt
, ...)
244 print_len
= vspprintf(&print_str
, 0, fmt
, argv
);
247 print_len
= php_http_message_body_append(body
, print_str
, print_len
);
253 ZEND_RESULT_CODE
php_http_message_body_add_form(php_http_message_body_t
*body
, HashTable
*fields
, HashTable
*files
)
258 INIT_PZVAL_ARRAY(&tmp
, fields
);
259 if (SUCCESS
!= add_recursive_fields(body
, NULL
, &tmp
)) {
264 INIT_PZVAL_ARRAY(&tmp
, files
);
265 if (SUCCESS
!= add_recursive_files(body
, NULL
, &tmp
)) {
273 void php_http_message_body_add_part(php_http_message_body_t
*body
, php_http_message_t
*part
)
275 TSRMLS_FETCH_FROM_CTX(body
->ts
);
278 php_http_message_to_callback(part
, (php_http_pass_callback_t
) php_http_message_body_append
, body
);
279 BOUNDARY_CLOSE(body
);
283 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
)
286 TSRMLS_FETCH_FROM_CTX(body
->ts
);
288 safe_name
= php_addslashes(estrdup(name
), strlen(name
), NULL
, 1 TSRMLS_CC
);
291 php_http_message_body_appendf(
293 "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
297 php_http_message_body_append(body
, value_str
, value_len
);
298 BOUNDARY_CLOSE(body
);
304 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
)
306 char *safe_name
, *path_dup
= estrdup(path
), *bname
;
308 TSRMLS_FETCH_FROM_CTX(body
->ts
);
310 safe_name
= php_addslashes(estrdup(name
), strlen(name
), NULL
, 1 TSRMLS_CC
);
312 php_basename(path_dup
, strlen(path_dup
), NULL
, 0, &bname
, &bname_len TSRMLS_CC
);
315 php_http_message_body_appendf(
317 "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
318 "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
319 "Content-Type: %s" PHP_HTTP_CRLF
321 safe_name
, bname
, ctype
323 php_stream_copy_to_stream_ex(in
, php_http_message_body_stream(body
), PHP_STREAM_COPY_ALL
, NULL
);
324 BOUNDARY_CLOSE(body
);
333 static inline char *format_key(uint type
, char *str
, ulong num
, const char *prefix
) {
334 char *new_key
= NULL
;
336 if (prefix
&& *prefix
) {
337 if (type
== HASH_KEY_IS_STRING
) {
338 spprintf(&new_key
, 0, "%s[%s]", prefix
, str
);
340 spprintf(&new_key
, 0, "%s[%lu]", prefix
, num
);
342 } else if (type
== HASH_KEY_IS_STRING
) {
343 new_key
= estrdup(str
);
345 new_key
= estrdup("");
351 static ZEND_RESULT_CODE
add_recursive_fields(php_http_message_body_t
*body
, const char *name
, zval
*value
)
353 if (Z_TYPE_P(value
) == IS_ARRAY
|| Z_TYPE_P(value
) == IS_OBJECT
) {
357 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
358 TSRMLS_FETCH_FROM_CTX(body
->ts
);
361 if (!ht
->nApplyCount
) {
363 FOREACH_KEYVAL(pos
, value
, key
, val
) {
364 char *str
= format_key(key
.type
, key
.str
, key
.num
, name
);
365 if (SUCCESS
!= add_recursive_fields(body
, str
, *val
)) {
375 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
376 php_http_message_body_add_form_field(body
, name
, Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
383 static ZEND_RESULT_CODE
add_recursive_files(php_http_message_body_t
*body
, const char *name
, zval
*value
)
385 zval
**zdata
= NULL
, **zfile
, **zname
, **ztype
;
387 TSRMLS_FETCH_FROM_CTX(body
->ts
);
389 if (Z_TYPE_P(value
) != IS_ARRAY
&& Z_TYPE_P(value
) != IS_OBJECT
) {
390 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Expected array or object (name, type, file) for message body file to add");
396 if ((SUCCESS
!= zend_hash_find(ht
, ZEND_STRS("name"), (void *) &zname
))
397 || (SUCCESS
!= zend_hash_find(ht
, ZEND_STRS("type"), (void *) &ztype
))
398 || (SUCCESS
!= zend_hash_find(ht
, ZEND_STRS("file"), (void *) &zfile
))
402 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
404 if (!ht
->nApplyCount
) {
406 FOREACH_HASH_KEYVAL(pos
, ht
, key
, val
) {
407 if (Z_TYPE_PP(val
) == IS_ARRAY
|| Z_TYPE_PP(val
) == IS_OBJECT
) {
408 char *str
= format_key(key
.type
, key
.str
, key
.num
, name
);
410 if (SUCCESS
!= add_recursive_files(body
, str
, *val
)) {
423 zval
*zfc
= php_http_ztyp(IS_STRING
, *zfile
);
425 if (SUCCESS
== zend_hash_find(ht
, ZEND_STRS("data"), (void *) &zdata
)) {
426 if (Z_TYPE_PP(zdata
) == IS_RESOURCE
) {
427 php_stream_from_zval_no_verify(stream
, zdata
);
429 zval
*tmp
= php_http_ztyp(IS_STRING
, *zdata
);
431 stream
= php_stream_memory_open(TEMP_STREAM_READONLY
, Z_STRVAL_P(tmp
), Z_STRLEN_P(tmp
));
435 stream
= php_stream_open_wrapper(Z_STRVAL_P(zfc
), "r", REPORT_ERRORS
|USE_PATH
, NULL
);
442 zval
*znc
= php_http_ztyp(IS_STRING
, *zname
), *ztc
= php_http_ztyp(IS_STRING
, *ztype
);
443 char *key
= format_key(HASH_KEY_IS_STRING
, Z_STRVAL_P(znc
), 0, name
);
444 ZEND_RESULT_CODE ret
= php_http_message_body_add_form_file(body
, key
, Z_STRVAL_P(ztc
), Z_STRVAL_P(zfc
), stream
);
450 if (!zdata
|| Z_TYPE_PP(zdata
) != IS_RESOURCE
) {
451 php_stream_close(stream
);
459 struct splitbody_arg
{
460 php_http_buffer_t buf
;
461 php_http_message_parser_t
*parser
;
467 static size_t splitbody(void *opaque
, char *buf
, size_t len TSRMLS_DC
)
469 struct splitbody_arg
*arg
= opaque
;
470 const char *boundary
= NULL
;
475 first_boundary
= !(consumed
|| arg
->consumed
);
477 if ((boundary
= php_http_locate_str(buf
, len
, arg
->boundary_str
+ first_boundary
, arg
->boundary_len
- first_boundary
))) {
478 size_t real_boundary_len
= arg
->boundary_len
- 1, cut
;
479 const char *real_boundary
= boundary
+ !first_boundary
;
482 if (buf
+ len
<= real_boundary
+ real_boundary_len
) {
483 /* if we just have enough data for the boundary, it's just a byte too less */
484 arg
->consumed
+= consumed
;
488 if (!first_boundary
) {
489 /* this is not the first boundary, read rest of this message */
490 php_http_buffer_append(&arg
->buf
, buf
, real_boundary
- buf
);
491 php_http_message_parser_parse(arg
->parser
, &arg
->buf
, 0, &arg
->parser
->message
);
494 /* move after the boundary */
495 cut
= real_boundary
- buf
+ real_boundary_len
;
500 if (buf
== php_http_locate_bin_eol(buf
, len
, &eol_len
)) {
506 if (!first_boundary
) {
507 /* advance messages */
508 php_http_message_t
*msg
;
510 msg
= php_http_message_init(NULL
, 0, NULL TSRMLS_CC
);
511 msg
->parent
= arg
->parser
->message
;
512 arg
->parser
->message
= msg
;
515 /* is this the last boundary? */
517 /* ignore the rest */
521 /* let this be garbage */
522 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Malformed multipart boundary at pos %zu", consumed
);
527 } while (boundary
&& len
);
529 /* let there be room for the next boundary */
530 if (len
> arg
->boundary_len
) {
531 consumed
+= len
- arg
->boundary_len
;
532 php_http_buffer_append(&arg
->buf
, buf
, len
- arg
->boundary_len
);
533 php_http_message_parser_parse(arg
->parser
, &arg
->buf
, 0, &arg
->parser
->message
);
536 arg
->consumed
+= consumed
;
540 php_http_message_t
*php_http_message_body_split(php_http_message_body_t
*body
, const char *boundary
)
542 php_stream
*s
= php_http_message_body_stream(body
);
543 php_http_buffer_t
*tmp
= NULL
;
544 php_http_message_t
*msg
= NULL
;
545 struct splitbody_arg arg
;
546 TSRMLS_FETCH_FROM_CTX(body
->ts
);
548 php_http_buffer_init(&arg
.buf
);
549 arg
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
550 arg
.boundary_len
= spprintf(&arg
.boundary_str
, 0, "\n--%s", boundary
);
553 php_stream_rewind(s
);
554 while (!php_stream_eof(s
)) {
555 php_http_buffer_passthru(&tmp
, 0x1000, (php_http_buffer_pass_func_t
) _php_stream_read
, s
, splitbody
, &arg TSRMLS_CC
);
558 msg
= arg
.parser
->message
;
559 arg
.parser
->message
= NULL
;
561 php_http_buffer_free(&tmp
);
562 php_http_message_parser_free(&arg
.parser
);
563 php_http_buffer_dtor(&arg
.buf
);
564 PTR_FREE(arg
.boundary_str
);
569 static zend_object_handlers php_http_message_body_object_handlers
;
571 zend_object_value
php_http_message_body_object_new(zend_class_entry
*ce TSRMLS_DC
)
573 return php_http_message_body_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
576 zend_object_value
php_http_message_body_object_new_ex(zend_class_entry
*ce
, php_http_message_body_t
*body
, php_http_message_body_object_t
**ptr TSRMLS_DC
)
578 php_http_message_body_object_t
*o
;
580 o
= ecalloc(1, sizeof(php_http_message_body_object_t
));
581 zend_object_std_init((zend_object
*) o
, php_http_message_body_class_entry TSRMLS_CC
);
582 object_properties_init((zend_object
*) o
, ce
);
592 o
->zv
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_http_message_body_object_free
, NULL TSRMLS_CC
);
593 o
->zv
.handlers
= &php_http_message_body_object_handlers
;
598 zend_object_value
php_http_message_body_object_clone(zval
*object TSRMLS_DC
)
600 zend_object_value new_ov
;
601 php_http_message_body_object_t
*new_obj
= NULL
;
602 php_http_message_body_object_t
*old_obj
= zend_object_store_get_object(object TSRMLS_CC
);
603 php_http_message_body_t
*body
= php_http_message_body_copy(old_obj
->body
, NULL
);
605 new_ov
= php_http_message_body_object_new_ex(old_obj
->zo
.ce
, body
, &new_obj TSRMLS_CC
);
606 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(object
) TSRMLS_CC
);
611 void php_http_message_body_object_free(void *object TSRMLS_DC
)
613 php_http_message_body_object_t
*obj
= object
;
615 php_http_message_body_free(&obj
->body
);
616 zend_object_std_dtor((zend_object
*) obj TSRMLS_CC
);
620 #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \
623 obj->body = php_http_message_body_init(NULL, NULL TSRMLS_CC); \
627 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___construct
, 0, 0, 0)
628 ZEND_ARG_INFO(0, stream
)
630 PHP_METHOD(HttpMessageBody
, __construct
)
632 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
633 zval
*zstream
= NULL
;
636 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|r!", &zstream
), invalid_arg
, return);
639 php_http_expect(php_stream_from_zval_no_verify(stream
, &zstream
), unexpected_val
, return);
642 php_http_message_body_free(&obj
->body
);
644 obj
->body
= php_http_message_body_init(NULL
, stream TSRMLS_CC
);
648 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___toString
, 0, 0, 0)
650 PHP_METHOD(HttpMessageBody
, __toString
)
652 if (SUCCESS
== zend_parse_parameters_none()) {
653 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
657 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
659 php_http_message_body_to_string(obj
->body
, &str
, &len
, 0, 0);
661 RETURN_STRINGL(str
, len
, 0);
664 RETURN_EMPTY_STRING();
667 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_unserialize
, 0, 0, 1)
668 ZEND_ARG_INFO(0, serialized
)
670 PHP_METHOD(HttpMessageBody
, unserialize
)
675 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &us_str
, &us_len
)) {
676 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
677 php_stream
*s
= php_stream_memory_open(0, us_str
, us_len
);
679 obj
->body
= php_http_message_body_init(NULL
, s TSRMLS_CC
);
683 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toStream
, 0, 0, 1)
684 ZEND_ARG_INFO(0, stream
)
685 ZEND_ARG_INFO(0, offset
)
686 ZEND_ARG_INFO(0, maxlen
)
688 PHP_METHOD(HttpMessageBody
, toStream
)
691 long offset
= 0, forlen
= 0;
693 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r|ll", &zstream
, &offset
, &forlen
)) {
695 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
697 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
699 php_stream_from_zval(stream
, &zstream
);
700 php_http_message_body_to_stream(obj
->body
, stream
, offset
, forlen
);
701 RETURN_ZVAL(getThis(), 1, 0);
705 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toCallback
, 0, 0, 1)
706 ZEND_ARG_INFO(0, callback
)
707 ZEND_ARG_INFO(0, offset
)
708 ZEND_ARG_INFO(0, maxlen
)
710 PHP_METHOD(HttpMessageBody
, toCallback
)
712 php_http_pass_fcall_arg_t fcd
;
713 long offset
= 0, forlen
= 0;
715 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "f|ll", &fcd
.fci
, &fcd
.fcc
, &offset
, &forlen
)) {
716 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
718 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
722 TSRMLS_SET_CTX(fcd
.ts
);
724 php_http_message_body_to_callback(obj
->body
, php_http_pass_fcall_callback
, &fcd
, offset
, forlen
);
725 zend_fcall_info_args_clear(&fcd
.fci
, 1);
727 zval_ptr_dtor(&fcd
.fcz
);
728 RETURN_ZVAL(getThis(), 1, 0);
732 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getResource
, 0, 0, 0)
734 PHP_METHOD(HttpMessageBody
, getResource
)
736 if (SUCCESS
== zend_parse_parameters_none()) {
737 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
739 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
741 zend_list_addref(obj
->body
->stream_id
);
742 RETVAL_RESOURCE(obj
->body
->stream_id
);
746 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getBoundary
, 0, 0, 0)
748 PHP_METHOD(HttpMessageBody
, getBoundary
)
750 if (SUCCESS
== zend_parse_parameters_none()) {
751 php_http_message_body_object_t
* obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
753 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
755 if (obj
->body
->boundary
) {
756 RETURN_STRING(obj
->body
->boundary
, 1);
761 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_append
, 0, 0, 1)
762 ZEND_ARG_INFO(0, string
)
764 PHP_METHOD(HttpMessageBody
, append
)
768 php_http_message_body_object_t
*obj
;
770 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
), invalid_arg
, return);
772 obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
774 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
776 php_http_expect(len
== php_http_message_body_append(obj
->body
, str
, len
), runtime
, return);
778 RETURN_ZVAL(getThis(), 1, 0);
781 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addForm
, 0, 0, 0)
782 ZEND_ARG_ARRAY_INFO(0, fields
, 1)
783 ZEND_ARG_ARRAY_INFO(0, files
, 1)
785 PHP_METHOD(HttpMessageBody
, addForm
)
787 HashTable
*fields
= NULL
, *files
= NULL
;
788 php_http_message_body_object_t
*obj
;
790 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|h!h!", &fields
, &files
), invalid_arg
, return);
792 obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
794 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
796 php_http_expect(SUCCESS
== php_http_message_body_add_form(obj
->body
, fields
, files
), runtime
, return);
798 RETURN_ZVAL(getThis(), 1, 0);
801 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart
, 0, 0, 1)
802 ZEND_ARG_OBJ_INFO(0, message
, http
\\Message
, 0)
804 PHP_METHOD(HttpMessageBody
, addPart
)
807 php_http_message_body_object_t
*obj
;
808 php_http_message_object_t
*mobj
;
809 zend_error_handling zeh
;
811 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zobj
, php_http_message_class_entry
), invalid_arg
, return);
813 obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
814 mobj
= zend_object_store_get_object(zobj TSRMLS_CC
);
816 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
818 zend_replace_error_handling(EH_THROW
, php_http_exception_runtime_class_entry
, &zeh TSRMLS_CC
);
819 php_http_message_body_add_part(obj
->body
, mobj
->message
);
820 zend_restore_error_handling(&zeh TSRMLS_CC
);
822 if (!EG(exception
)) {
823 RETURN_ZVAL(getThis(), 1, 0);
827 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_etag
, 0, 0, 0)
829 PHP_METHOD(HttpMessageBody
, etag
)
831 if (SUCCESS
== zend_parse_parameters_none()) {
832 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
835 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
837 if ((etag
= php_http_message_body_etag(obj
->body
))) {
838 RETURN_STRING(etag
, 0);
845 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_stat
, 0, 0, 0)
846 ZEND_ARG_INFO(0, field
)
848 PHP_METHOD(HttpMessageBody
, stat
)
850 char *field_str
= NULL
;
853 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &field_str
, &field_len
)) {
854 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
855 const php_stream_statbuf
*sb
;
857 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj
);
859 if ((sb
= php_http_message_body_stat(obj
->body
))) {
860 if (field_str
&& field_len
) {
861 switch (*field_str
) {
864 RETURN_LONG(sb
->sb
.st_size
);
868 RETURN_LONG(sb
->sb
.st_atime
);
872 RETURN_LONG(sb
->sb
.st_mtime
);
876 RETURN_LONG(sb
->sb
.st_ctime
);
879 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str
);
883 object_init(return_value
);
884 add_property_long_ex(return_value
, ZEND_STRS("size"), sb
->sb
.st_size TSRMLS_CC
);
885 add_property_long_ex(return_value
, ZEND_STRS("atime"), sb
->sb
.st_atime TSRMLS_CC
);
886 add_property_long_ex(return_value
, ZEND_STRS("mtime"), sb
->sb
.st_mtime TSRMLS_CC
);
887 add_property_long_ex(return_value
, ZEND_STRS("ctime"), sb
->sb
.st_ctime TSRMLS_CC
);
893 static zend_function_entry php_http_message_body_methods
[] = {
894 PHP_ME(HttpMessageBody
, __construct
, ai_HttpMessageBody___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
895 PHP_ME(HttpMessageBody
, __toString
, ai_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
896 PHP_MALIAS(HttpMessageBody
, toString
, __toString
, ai_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
897 PHP_MALIAS(HttpMessageBody
, serialize
, __toString
, ai_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
898 PHP_ME(HttpMessageBody
, unserialize
, ai_HttpMessageBody_unserialize
, ZEND_ACC_PUBLIC
)
899 PHP_ME(HttpMessageBody
, toStream
, ai_HttpMessageBody_toStream
, ZEND_ACC_PUBLIC
)
900 PHP_ME(HttpMessageBody
, toCallback
, ai_HttpMessageBody_toCallback
, ZEND_ACC_PUBLIC
)
901 PHP_ME(HttpMessageBody
, getResource
, ai_HttpMessageBody_getResource
, ZEND_ACC_PUBLIC
)
902 PHP_ME(HttpMessageBody
, getBoundary
, ai_HttpMessageBody_getBoundary
, ZEND_ACC_PUBLIC
)
903 PHP_ME(HttpMessageBody
, append
, ai_HttpMessageBody_append
, ZEND_ACC_PUBLIC
)
904 PHP_ME(HttpMessageBody
, addForm
, ai_HttpMessageBody_addForm
, ZEND_ACC_PUBLIC
)
905 PHP_ME(HttpMessageBody
, addPart
, ai_HttpMessageBody_addPart
, ZEND_ACC_PUBLIC
)
906 PHP_ME(HttpMessageBody
, etag
, ai_HttpMessageBody_etag
, ZEND_ACC_PUBLIC
)
907 PHP_ME(HttpMessageBody
, stat
, ai_HttpMessageBody_stat
, ZEND_ACC_PUBLIC
)
911 zend_class_entry
*php_http_message_body_class_entry
;
913 PHP_MINIT_FUNCTION(http_message_body
)
915 zend_class_entry ce
= {0};
917 INIT_NS_CLASS_ENTRY(ce
, "http\\Message", "Body", php_http_message_body_methods
);
918 php_http_message_body_class_entry
= zend_register_internal_class(&ce TSRMLS_CC
);
919 php_http_message_body_class_entry
->create_object
= php_http_message_body_object_new
;
920 memcpy(&php_http_message_body_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
921 php_http_message_body_object_handlers
.clone_obj
= php_http_message_body_object_clone
;
922 zend_class_implements(php_http_message_body_class_entry TSRMLS_CC
, 1, zend_ce_serializable
);
932 * vim600: noet sw=4 ts=4 fdm=marker
933 * vim<600: noet sw=4 ts=4