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-2011, 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)); \
27 #define BOUNDARY_CLOSE(body) \
28 php_http_message_body_appendf(body, PHP_HTTP_CRLF "--%s--" PHP_HTTP_CRLF, php_http_message_body_boundary(body))
30 static STATUS
add_recursive_fields(php_http_message_body_t
*body
, const char *name
, zval
*value
);
31 static STATUS
add_recursive_files(php_http_message_body_t
*body
, const char *name
, zval
*value
);
33 PHP_HTTP_API php_http_message_body_t
*php_http_message_body_init(php_http_message_body_t
*body
, php_stream
*stream TSRMLS_DC
)
36 body
= emalloc(sizeof(php_http_message_body_t
));
38 memset(body
, 0, sizeof(*body
));
41 php_stream_auto_cleanup(stream
);
42 body
->stream_id
= php_stream_get_resource_id(stream
);
43 zend_list_addref(body
->stream_id
);
45 stream
= php_stream_temp_create(TEMP_STREAM_DEFAULT
, 0xffff);
46 php_stream_auto_cleanup(stream
);
47 body
->stream_id
= php_stream_get_resource_id(stream
);
49 TSRMLS_SET_CTX(body
->ts
);
54 PHP_HTTP_API php_http_message_body_t
*php_http_message_body_copy(php_http_message_body_t
*from
, php_http_message_body_t
*to
, zend_bool dup_internal_stream_and_contents
)
59 TSRMLS_FETCH_FROM_CTX(from
->ts
);
61 if (dup_internal_stream_and_contents
) {
62 to
= php_http_message_body_init(to
, NULL TSRMLS_CC
);
63 php_http_message_body_to_stream(from
, php_http_message_body_stream(to
), 0, 0);
65 to
= php_http_message_body_init(to
, php_http_message_body_stream(from
) TSRMLS_CC
);
69 to
->boundary
= estrdup(from
->boundary
);
76 PHP_HTTP_API
void php_http_message_body_dtor(php_http_message_body_t
*body
)
78 TSRMLS_FETCH_FROM_CTX(body
->ts
);
79 /* NO FIXME: shows leakinfo in DEBUG mode */
80 zend_list_delete(body
->stream_id
);
81 STR_FREE(body
->boundary
);
84 PHP_HTTP_API
void php_http_message_body_free(php_http_message_body_t
**body
)
87 php_http_message_body_dtor(*body
);
93 PHP_HTTP_API
const php_stream_statbuf
*php_http_message_body_stat(php_http_message_body_t
*body
)
95 TSRMLS_FETCH_FROM_CTX(body
->ts
);
96 php_stream_stat(php_http_message_body_stream(body
), &body
->ssb
);
100 PHP_HTTP_API
const char *php_http_message_body_boundary(php_http_message_body_t
*body
)
102 if (!body
->boundary
) {
103 union { double dbl
; int num
[2]; } data
;
104 TSRMLS_FETCH_FROM_CTX(body
->ts
);
106 data
.dbl
= php_combined_lcg(TSRMLS_C
);
107 spprintf(&body
->boundary
, 0, "%x.%x", data
.num
[0], data
.num
[1]);
109 return body
->boundary
;
112 PHP_HTTP_API
char *php_http_message_body_etag(php_http_message_body_t
*body
)
114 const php_stream_statbuf
*ssb
= php_http_message_body_stat(body
);
115 TSRMLS_FETCH_FROM_CTX(body
->ts
);
117 /* real file or temp buffer ? */
118 if (ssb
&& ssb
->sb
.st_mtime
) {
121 spprintf(&etag
, 0, "%lx-%lx-%lx", ssb
->sb
.st_ino
, ssb
->sb
.st_mtime
, ssb
->sb
.st_size
);
124 php_http_etag_t
*etag
= php_http_etag_init(PHP_HTTP_G
->env
.etag_mode TSRMLS_CC
);
127 php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_etag_update
, etag
, 0, 0);
128 return php_http_etag_finish(etag
);
135 PHP_HTTP_API
void php_http_message_body_to_string(php_http_message_body_t
*body
, char **buf
, size_t *len
, off_t offset
, size_t forlen
)
137 php_stream
*s
= php_http_message_body_stream(body
);
138 TSRMLS_FETCH_FROM_CTX(body
->ts
);
140 php_stream_seek(s
, offset
, SEEK_SET
);
144 *len
= php_stream_copy_to_mem(s
, buf
, forlen
, 0);
147 PHP_HTTP_API
void php_http_message_body_to_stream(php_http_message_body_t
*body
, php_stream
*dst
, off_t offset
, size_t forlen
)
149 php_stream
*s
= php_http_message_body_stream(body
);
150 TSRMLS_FETCH_FROM_CTX(body
->ts
);
152 php_stream_seek(s
, offset
, SEEK_SET
);
156 php_stream_copy_to_stream_ex(s
, dst
, forlen
, NULL
);
159 PHP_HTTP_API
void 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
)
161 php_stream
*s
= php_http_message_body_stream(body
);
162 char *buf
= emalloc(0x1000);
163 TSRMLS_FETCH_FROM_CTX(body
->ts
);
165 php_stream_seek(s
, offset
, SEEK_SET
);
170 while (!php_stream_eof(s
)) {
171 size_t read
= php_stream_read(s
, buf
, MIN(forlen
, 0x1000));
174 cb(cb_arg
, buf
, read
);
177 if (read
< MIN(forlen
, sizeof(buf
))) {
181 if (forlen
&& !(forlen
-= read
)) {
188 PHP_HTTP_API
size_t php_http_message_body_append(php_http_message_body_t
*body
, const char *buf
, size_t len
)
191 TSRMLS_FETCH_FROM_CTX(body
->ts
);
193 s
= php_http_message_body_stream(body
);
194 php_stream_seek(s
, 0, SEEK_END
);
195 return php_stream_write(s
, buf
, len
);
198 PHP_HTTP_API
size_t php_http_message_body_appendf(php_http_message_body_t
*body
, const char *fmt
, ...)
205 print_len
= vspprintf(&print_str
, 0, fmt
, argv
);
208 print_len
= php_http_message_body_append(body
, print_str
, print_len
);
214 PHP_HTTP_API STATUS
php_http_message_body_add_form(php_http_message_body_t
*body
, HashTable
*fields
, HashTable
*files
)
219 INIT_PZVAL_ARRAY(&tmp
, fields
);
220 if (SUCCESS
!= add_recursive_fields(body
, NULL
, &tmp
)) {
225 INIT_PZVAL_ARRAY(&tmp
, files
);
226 if (SUCCESS
!= add_recursive_files(body
, NULL
, &tmp
)) {
234 PHP_HTTP_API
void php_http_message_body_add_part(php_http_message_body_t
*body
, php_http_message_t
*part
)
236 TSRMLS_FETCH_FROM_CTX(body
->ts
);
239 php_http_message_to_callback(part
, (php_http_pass_callback_t
) php_http_message_body_append
, body
);
240 BOUNDARY_CLOSE(body
);
244 PHP_HTTP_API STATUS
php_http_message_body_add_form_field(php_http_message_body_t
*body
, const char *name
, const char *value_str
, size_t value_len
)
247 TSRMLS_FETCH_FROM_CTX(body
->ts
);
249 safe_name
= php_addslashes(estrdup(name
), strlen(name
), NULL
, 1 TSRMLS_CC
);
252 php_http_message_body_appendf(
254 "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
258 php_http_message_body_append(body
, value_str
, value_len
);
259 BOUNDARY_CLOSE(body
);
265 PHP_HTTP_API STATUS
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
)
267 char *safe_name
, *path_dup
= estrdup(path
), *bname
;
269 TSRMLS_FETCH_FROM_CTX(body
->ts
);
271 safe_name
= php_addslashes(estrdup(name
), strlen(name
), NULL
, 1 TSRMLS_CC
);
273 php_basename(path_dup
, strlen(path_dup
), NULL
, 0, &bname
, &bname_len TSRMLS_CC
);
276 php_http_message_body_appendf(
278 "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
279 "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
280 "Content-Type: %s" PHP_HTTP_CRLF
282 safe_name
, bname
, ctype
284 php_stream_copy_to_stream_ex(in
, php_http_message_body_stream(body
), PHP_STREAM_COPY_ALL
, NULL
);
285 BOUNDARY_CLOSE(body
);
293 static inline char *format_key(uint type
, char *str
, ulong num
, const char *prefix
) {
294 char *new_key
= NULL
;
296 if (prefix
&& *prefix
) {
297 if (type
== HASH_KEY_IS_STRING
) {
298 spprintf(&new_key
, 0, "%s[%s]", prefix
, str
);
300 spprintf(&new_key
, 0, "%s[%lu]", prefix
, num
);
302 } else if (type
== HASH_KEY_IS_STRING
) {
303 new_key
= estrdup(str
);
305 new_key
= estrdup("");
311 static STATUS
add_recursive_fields(php_http_message_body_t
*body
, const char *name
, zval
*value
)
313 if (Z_TYPE_P(value
) == IS_ARRAY
|| Z_TYPE_P(value
) == IS_OBJECT
) {
317 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
318 TSRMLS_FETCH_FROM_CTX(body
->ts
);
321 if (!ht
->nApplyCount
) {
323 FOREACH_KEYVAL(pos
, value
, key
, val
) {
324 char *str
= format_key(key
.type
, key
.str
, key
.num
, name
);
325 if (SUCCESS
!= add_recursive_fields(body
, str
, *val
)) {
335 zval
*cpy
= php_http_ztyp(IS_STRING
, value
);
336 php_http_message_body_add_form_field(body
, name
, Z_STRVAL_P(cpy
), Z_STRLEN_P(cpy
));
343 static STATUS
add_recursive_files(php_http_message_body_t
*body
, const char *name
, zval
*value
)
345 zval
**zdata
= NULL
, **zfile
, **zname
, **ztype
;
347 TSRMLS_FETCH_FROM_CTX(body
->ts
);
349 if (Z_TYPE_P(value
) != IS_ARRAY
&& Z_TYPE_P(value
) != IS_OBJECT
) {
350 php_http_error(HE_WARNING
, PHP_HTTP_E_MESSAGE_BODY
, "Expected array or object (name, type, file) for message body file to add");
356 if ((SUCCESS
!= zend_hash_find(ht
, ZEND_STRS("name"), (void *) &zname
))
357 || (SUCCESS
!= zend_hash_find(ht
, ZEND_STRS("type"), (void *) &ztype
))
358 || (SUCCESS
!= zend_hash_find(ht
, ZEND_STRS("file"), (void *) &zfile
))
362 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
364 if (!ht
->nApplyCount
) {
366 FOREACH_HASH_KEYVAL(pos
, ht
, key
, val
) {
367 if (Z_TYPE_PP(val
) == IS_ARRAY
|| Z_TYPE_PP(val
) == IS_OBJECT
) {
368 char *str
= format_key(key
.type
, key
.str
, key
.num
, name
);
370 if (SUCCESS
!= add_recursive_files(body
, str
, *val
)) {
383 zval
*zfc
= php_http_ztyp(IS_STRING
, *zfile
);
385 if (SUCCESS
== zend_hash_find(ht
, ZEND_STRS("data"), (void *) &zdata
)) {
386 if (Z_TYPE_PP(zdata
) == IS_RESOURCE
) {
387 php_stream_from_zval_no_verify(stream
, zdata
);
389 zval
*tmp
= php_http_ztyp(IS_STRING
, *zdata
);
391 stream
= php_stream_memory_open(TEMP_STREAM_READONLY
, Z_STRVAL_P(tmp
), Z_STRLEN_P(tmp
));
395 stream
= php_stream_open_wrapper(Z_STRVAL_P(zfc
), "r", REPORT_ERRORS
|USE_PATH
, NULL
);
402 zval
*znc
= php_http_ztyp(IS_STRING
, *zname
), *ztc
= php_http_ztyp(IS_STRING
, *ztype
);
403 char *key
= format_key(HASH_KEY_IS_STRING
, Z_STRVAL_P(znc
), 0, name
);
404 STATUS ret
= php_http_message_body_add_form_file(body
, key
, Z_STRVAL_P(ztc
), Z_STRVAL_P(zfc
), stream
);
410 if (!zdata
|| Z_TYPE_PP(zdata
) != IS_RESOURCE
) {
411 php_stream_close(stream
);
419 struct splitbody_arg
{
420 php_http_buffer_t buf
;
421 php_http_message_parser_t
*parser
;
427 static size_t splitbody(void *opaque
, char *buf
, size_t len TSRMLS_DC
)
429 struct splitbody_arg
*arg
= opaque
;
430 const char *boundary
= NULL
;
435 first_boundary
= !(consumed
|| arg
->consumed
);
437 if ((boundary
= php_http_locate_str(buf
, len
, arg
->boundary_str
+ first_boundary
, arg
->boundary_len
- first_boundary
))) {
438 size_t real_boundary_len
= arg
->boundary_len
- 1, cut
;
439 const char *real_boundary
= boundary
+ !first_boundary
;
442 if (buf
+ len
<= real_boundary
+ real_boundary_len
) {
443 /* if we just have enough data for the boundary, it's just a byte too less */
444 arg
->consumed
+= consumed
;
448 if (!first_boundary
) {
449 /* this is not the first boundary, read rest of this message */
450 php_http_buffer_append(&arg
->buf
, buf
, real_boundary
- buf
);
451 php_http_message_parser_parse(arg
->parser
, &arg
->buf
, 0, &arg
->parser
->message
);
454 /* move after the boundary */
455 cut
= real_boundary
- buf
+ real_boundary_len
;
460 if (buf
== php_http_locate_bin_eol(buf
, len
, &eol_len
)) {
466 if (!first_boundary
) {
467 /* advance messages */
468 php_http_message_t
*msg
;
470 msg
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
471 msg
->parent
= arg
->parser
->message
;
472 arg
->parser
->message
= msg
;
475 /* is this the last boundary? */
477 /* ignore the rest */
481 /* let this be garbage */
482 php_http_error(HE_WARNING
, PHP_HTTP_E_MESSAGE_BODY
, "Malformed multipart boundary at pos %zu", consumed
);
487 } while (boundary
&& len
);
489 /* let there be room for the next boundary */
490 if (len
> arg
->boundary_len
) {
491 consumed
+= len
- arg
->boundary_len
;
492 php_http_buffer_append(&arg
->buf
, buf
, len
- arg
->boundary_len
);
493 php_http_message_parser_parse(arg
->parser
, &arg
->buf
, 0, &arg
->parser
->message
);
496 arg
->consumed
+= consumed
;
500 PHP_HTTP_API php_http_message_t
*php_http_message_body_split(php_http_message_body_t
*body
, const char *boundary
)
502 php_stream
*s
= php_http_message_body_stream(body
);
503 php_http_buffer_t
*tmp
= NULL
;
504 php_http_message_t
*msg
= NULL
;
505 struct splitbody_arg arg
;
506 TSRMLS_FETCH_FROM_CTX(body
->ts
);
508 php_http_buffer_init(&arg
.buf
);
509 arg
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
510 arg
.boundary_len
= spprintf(&arg
.boundary_str
, 0, "\n--%s", boundary
);
513 php_stream_rewind(s
);
514 while (!php_stream_eof(s
)) {
515 php_http_buffer_passthru(&tmp
, 0x1000, (php_http_buffer_pass_func_t
) _php_stream_read
, s
, splitbody
, &arg TSRMLS_CC
);
518 msg
= arg
.parser
->message
;
519 arg
.parser
->message
= NULL
;
521 php_http_buffer_free(&tmp
);
522 php_http_message_parser_free(&arg
.parser
);
523 php_http_buffer_dtor(&arg
.buf
);
524 STR_FREE(arg
.boundary_str
);
531 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpMessageBody, method, 0, req_args)
532 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpMessageBody, method, 0)
533 #define PHP_HTTP_MESSAGE_BODY_ME(method, visibility) PHP_ME(HttpMessageBody, method, PHP_HTTP_ARGS(HttpMessageBody, method), visibility)
535 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
536 PHP_HTTP_ARG_VAL(stream
, 0)
539 PHP_HTTP_EMPTY_ARGS(__toString
);
540 PHP_HTTP_BEGIN_ARGS(unserialize
, 1)
541 PHP_HTTP_ARG_VAL(serialized
, 0)
544 PHP_HTTP_BEGIN_ARGS(toStream
, 1)
545 PHP_HTTP_ARG_VAL(stream
, 0)
548 PHP_HTTP_BEGIN_ARGS(toCallback
, 1)
549 PHP_HTTP_ARG_VAL(callback
, 0)
552 PHP_HTTP_EMPTY_ARGS(getResource
);
554 PHP_HTTP_BEGIN_ARGS(append
, 1)
555 PHP_HTTP_ARG_VAL(string
, 0)
558 PHP_HTTP_BEGIN_ARGS(addForm
, 0)
559 PHP_HTTP_ARG_ARR(fields
, 1, 0)
560 PHP_HTTP_ARG_ARR(files
, 1, 0)
563 PHP_HTTP_BEGIN_ARGS(addPart
, 1)
564 PHP_HTTP_ARG_OBJ(http
\\Message
, message
, 0)
567 PHP_HTTP_EMPTY_ARGS(etag
);
569 PHP_HTTP_BEGIN_ARGS(stat
, 0)
570 PHP_HTTP_ARG_VAL(what
, 0)
573 static zend_class_entry
*php_http_message_body_class_entry
;
575 zend_class_entry
*php_http_message_body_get_class_entry(void)
577 return php_http_message_body_class_entry
;
580 static zend_function_entry php_http_message_body_method_entry
[] = {
581 PHP_HTTP_MESSAGE_BODY_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
582 PHP_HTTP_MESSAGE_BODY_ME(__toString
, ZEND_ACC_PUBLIC
)
583 PHP_MALIAS(HttpMessageBody
, toString
, __toString
, args_for_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
584 PHP_MALIAS(HttpMessageBody
, serialize
, __toString
, args_for_HttpMessageBody___toString
, ZEND_ACC_PUBLIC
)
585 PHP_HTTP_MESSAGE_BODY_ME(unserialize
, ZEND_ACC_PUBLIC
)
586 PHP_HTTP_MESSAGE_BODY_ME(toStream
, ZEND_ACC_PUBLIC
)
587 PHP_HTTP_MESSAGE_BODY_ME(toCallback
, ZEND_ACC_PUBLIC
)
588 PHP_HTTP_MESSAGE_BODY_ME(getResource
, ZEND_ACC_PUBLIC
)
589 PHP_HTTP_MESSAGE_BODY_ME(append
, ZEND_ACC_PUBLIC
)
590 PHP_HTTP_MESSAGE_BODY_ME(addForm
, ZEND_ACC_PUBLIC
)
591 PHP_HTTP_MESSAGE_BODY_ME(addPart
, ZEND_ACC_PUBLIC
)
592 PHP_HTTP_MESSAGE_BODY_ME(etag
, ZEND_ACC_PUBLIC
)
593 PHP_HTTP_MESSAGE_BODY_ME(stat
, ZEND_ACC_PUBLIC
)
596 static zend_object_handlers php_http_message_body_object_handlers
;
598 PHP_MINIT_FUNCTION(http_message_body
)
600 PHP_HTTP_REGISTER_CLASS(http
\\Message
, Body
, http_message_body
, php_http_object_get_class_entry(), 0);
601 php_http_message_body_class_entry
->create_object
= php_http_message_body_object_new
;
602 memcpy(&php_http_message_body_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
603 php_http_message_body_object_handlers
.clone_obj
= php_http_message_body_object_clone
;
604 zend_class_implements(php_http_message_body_class_entry TSRMLS_CC
, 1, zend_ce_serializable
);
608 zend_object_value
php_http_message_body_object_new(zend_class_entry
*ce TSRMLS_DC
)
610 return php_http_message_body_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
613 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
)
615 zend_object_value ov
;
616 php_http_message_body_object_t
*o
;
618 o
= ecalloc(1, sizeof(php_http_message_body_object_t
));
619 zend_object_std_init((zend_object
*) o
, php_http_message_body_class_entry TSRMLS_CC
);
620 object_properties_init((zend_object
*) o
, ce
);
630 ov
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_http_message_body_object_free
, NULL TSRMLS_CC
);
631 ov
.handlers
= &php_http_message_body_object_handlers
;
636 zend_object_value
php_http_message_body_object_clone(zval
*object TSRMLS_DC
)
638 zend_object_value new_ov
;
639 php_http_message_body_object_t
*new_obj
= NULL
;
640 php_http_message_body_object_t
*old_obj
= zend_object_store_get_object(object TSRMLS_CC
);
642 new_ov
= php_http_message_body_object_new_ex(old_obj
->zo
.ce
, php_http_message_body_copy(old_obj
->body
, NULL
, 1), &new_obj TSRMLS_CC
);
643 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(object
) TSRMLS_CC
);
648 void php_http_message_body_object_free(void *object TSRMLS_DC
)
650 php_http_message_body_object_t
*obj
= object
;
652 php_http_message_body_free(&obj
->body
);
654 zend_object_std_dtor((zend_object
*) obj TSRMLS_CC
);
658 PHP_METHOD(HttpMessageBody
, __construct
)
660 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
661 zval
*zstream
= NULL
;
664 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
665 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|r!", &zstream
)) {
667 php_stream_from_zval(stream
, &zstream
);
671 php_http_message_body_dtor(obj
->body
);
673 obj
->body
= php_http_message_body_init(obj
->body
, stream TSRMLS_CC
);
677 obj
->body
= php_http_message_body_init(NULL
, NULL TSRMLS_CC
);
680 } end_error_handling();
683 PHP_METHOD(HttpMessageBody
, __toString
)
685 if (SUCCESS
== zend_parse_parameters_none()) {
686 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
690 php_http_message_body_to_string(obj
->body
, &str
, &len
, 0, 0);
692 RETURN_STRINGL(str
, len
, 0);
695 RETURN_EMPTY_STRING();
698 PHP_METHOD(HttpMessageBody
, unserialize
)
703 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &us_str
, &us_len
)) {
704 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
705 php_stream
*s
= php_stream_memory_open(0, us_str
, us_len
);
707 obj
->body
= php_http_message_body_init(NULL
, s TSRMLS_CC
);
711 PHP_METHOD(HttpMessageBody
, toStream
)
714 long offset
= 0, forlen
= 0;
716 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r|ll", &zstream
, &offset
, &forlen
)) {
718 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
720 php_stream_from_zval(stream
, &zstream
);
721 php_http_message_body_to_stream(obj
->body
, stream
, offset
, forlen
);
728 PHP_METHOD(HttpMessageBody
, toCallback
)
730 php_http_pass_fcall_arg_t fcd
;
731 long offset
= 0, forlen
= 0;
733 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "f|ll", &fcd
.fci
, &fcd
.fcc
, &offset
, &forlen
)) {
734 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
738 TSRMLS_SET_CTX(fcd
.ts
);
740 php_http_message_body_to_callback(obj
->body
, php_http_pass_fcall_callback
, &fcd
, offset
, forlen
);
741 zend_fcall_info_args_clear(&fcd
.fci
, 1);
743 zval_ptr_dtor(&fcd
.fcz
);
749 PHP_METHOD(HttpMessageBody
, getResource
)
751 if (SUCCESS
== zend_parse_parameters_none()) {
752 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
754 zend_list_addref(obj
->body
->stream_id
);
755 RETVAL_RESOURCE(obj
->body
->stream_id
);
759 PHP_METHOD(HttpMessageBody
, append
)
764 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
765 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
767 RETURN_LONG(php_http_message_body_append(obj
->body
, str
, len
));
772 PHP_METHOD(HttpMessageBody
, addForm
)
774 HashTable
*fields
= NULL
, *files
= NULL
;
776 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|h!h!", &fields
, &files
)) {
777 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
779 RETURN_SUCCESS(php_http_message_body_add_form(obj
->body
, fields
, files
));
784 PHP_METHOD(HttpMessageBody
, addPart
)
788 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zobj
, php_http_message_get_class_entry())) {
789 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
790 php_http_message_object_t
*mobj
= zend_object_store_get_object(zobj TSRMLS_CC
);
792 php_http_message_body_add_part(obj
->body
, mobj
->message
);
798 PHP_METHOD(HttpMessageBody
, etag
)
800 if (SUCCESS
== zend_parse_parameters_none()) {
801 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
802 char *etag
= php_http_message_body_etag(obj
->body
);
805 RETURN_STRING(etag
, 0);
811 PHP_METHOD(HttpMessageBody
, stat
)
813 char *field_str
= NULL
;
816 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &field_str
, &field_len
)) {
817 php_http_message_body_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
818 const php_stream_statbuf
*sb
= php_http_message_body_stat(obj
->body
);
821 if (field_str
&& field_len
) {
822 switch (*field_str
) {
825 RETURN_LONG(sb
->sb
.st_size
);
829 RETURN_LONG(sb
->sb
.st_atime
);
833 RETURN_LONG(sb
->sb
.st_mtime
);
837 RETURN_LONG(sb
->sb
.st_ctime
);
840 php_http_error(HE_WARNING
, PHP_HTTP_E_MESSAGE_BODY
, "unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str
);
844 array_init(return_value
);
845 add_assoc_long_ex(return_value
, ZEND_STRS("size"), sb
->sb
.st_size
);
846 add_assoc_long_ex(return_value
, ZEND_STRS("atime"), sb
->sb
.st_atime
);
847 add_assoc_long_ex(return_value
, ZEND_STRS("mtime"), sb
->sb
.st_mtime
);
848 add_assoc_long_ex(return_value
, ZEND_STRS("ctime"), sb
->sb
.st_ctime
);
861 * vim600: noet sw=4 ts=4 fdm=marker
862 * vim<600: noet sw=4 ts=4