fix build with xcode clang
[m6w6/ext-http] / php_http_message_body.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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-2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #include <ext/standard/php_lcg.h>
16
17 #define BOUNDARY_OPEN(body) \
18 do {\
19 size_t size = php_http_message_body_size(body); \
20 if (size) { \
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)); \
23 } else { \
24 php_http_message_body_appendf(body, "--%s" PHP_HTTP_CRLF, php_http_message_body_boundary(body)); \
25 } \
26 } while(0)
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))
29
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);
32
33 PHP_HTTP_API php_http_message_body_t *php_http_message_body_init(php_http_message_body_t **body_ptr, php_stream *stream TSRMLS_DC)
34 {
35 php_http_message_body_t *body;
36
37 if (body_ptr && *body_ptr) {
38 body = *body_ptr;
39 ++body->refcount;
40 return body;
41 }
42
43 body = ecalloc(1, sizeof(php_http_message_body_t));
44 body->refcount = 1;
45
46 if (stream) {
47 php_stream_auto_cleanup(stream);
48 body->stream_id = php_stream_get_resource_id(stream);
49 zend_list_addref(body->stream_id);
50 } else {
51 stream = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff);
52 php_stream_auto_cleanup(stream);
53 body->stream_id = php_stream_get_resource_id(stream);
54 }
55 TSRMLS_SET_CTX(body->ts);
56
57 return body;
58 }
59
60 PHP_HTTP_API unsigned php_http_message_body_addref(php_http_message_body_t *body)
61 {
62 return ++body->refcount;
63 }
64
65 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)
66 {
67 if (from) {
68 TSRMLS_FETCH_FROM_CTX(from->ts);
69
70 if (to) {
71 php_stream_truncate_set_size(php_http_message_body_stream(to), 0);
72 } else {
73 to = php_http_message_body_init(NULL, NULL TSRMLS_CC);
74 }
75 php_http_message_body_to_stream(from, php_http_message_body_stream(to), 0, 0);
76
77 if (to->boundary) {
78 efree(to->boundary);
79 }
80 if (from->boundary) {
81 to->boundary = estrdup(from->boundary);
82 }
83 } else {
84 to = NULL;
85 }
86 return to;
87 }
88
89 PHP_HTTP_API void php_http_message_body_free(php_http_message_body_t **body_ptr)
90 {
91 if (*body_ptr) {
92 php_http_message_body_t *body = *body_ptr;
93
94 if (!--body->refcount) {
95 TSRMLS_FETCH_FROM_CTX(body->ts);
96 /* NO FIXME: shows leakinfo in DEBUG mode */
97 zend_list_delete(body->stream_id);
98 STR_FREE(body->boundary);
99 efree(body);
100 }
101 *body_ptr = NULL;
102 }
103 }
104
105 PHP_HTTP_API const php_stream_statbuf *php_http_message_body_stat(php_http_message_body_t *body)
106 {
107 TSRMLS_FETCH_FROM_CTX(body->ts);
108 php_stream_stat(php_http_message_body_stream(body), &body->ssb);
109 return &body->ssb;
110 }
111
112 PHP_HTTP_API const char *php_http_message_body_boundary(php_http_message_body_t *body)
113 {
114 if (!body->boundary) {
115 union { double dbl; int num[2]; } data;
116 TSRMLS_FETCH_FROM_CTX(body->ts);
117
118 data.dbl = php_combined_lcg(TSRMLS_C);
119 spprintf(&body->boundary, 0, "%x.%x", data.num[0], data.num[1]);
120 }
121 return body->boundary;
122 }
123
124 PHP_HTTP_API char *php_http_message_body_etag(php_http_message_body_t *body)
125 {
126 const php_stream_statbuf *ssb = php_http_message_body_stat(body);
127 TSRMLS_FETCH_FROM_CTX(body->ts);
128
129 /* real file or temp buffer ? */
130 if (ssb && ssb->sb.st_mtime) {
131 char *etag;
132
133 spprintf(&etag, 0, "%lx-%lx-%lx", ssb->sb.st_ino, ssb->sb.st_mtime, ssb->sb.st_size);
134 return etag;
135 } else {
136 php_http_etag_t *etag = php_http_etag_init(PHP_HTTP_G->env.etag_mode TSRMLS_CC);
137
138 if (etag) {
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);
141 } else {
142 return NULL;
143 }
144 }
145 }
146
147 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)
148 {
149 php_stream *s = php_http_message_body_stream(body);
150 TSRMLS_FETCH_FROM_CTX(body->ts);
151
152 php_stream_seek(s, offset, SEEK_SET);
153 if (!forlen) {
154 forlen = -1;
155 }
156 *len = php_stream_copy_to_mem(s, buf, forlen, 0);
157 }
158
159 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)
160 {
161 php_stream *s = php_http_message_body_stream(body);
162 TSRMLS_FETCH_FROM_CTX(body->ts);
163
164 php_stream_seek(s, offset, SEEK_SET);
165 if (!forlen) {
166 forlen = -1;
167 }
168 php_stream_copy_to_stream_ex(s, dst, forlen, NULL);
169 }
170
171 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)
172 {
173 php_stream *s = php_http_message_body_stream(body);
174 char *buf = emalloc(0x1000);
175 TSRMLS_FETCH_FROM_CTX(body->ts);
176
177 php_stream_seek(s, offset, SEEK_SET);
178
179 if (!forlen) {
180 forlen = -1;
181 }
182 while (!php_stream_eof(s)) {
183 size_t read = php_stream_read(s, buf, MIN(forlen, 0x1000));
184
185 if (read) {
186 cb(cb_arg, buf, read);
187 }
188
189 if (read < MIN(forlen, sizeof(buf))) {
190 break;
191 }
192
193 if (forlen && !(forlen -= read)) {
194 break;
195 }
196 }
197 efree(buf);
198 }
199
200 PHP_HTTP_API size_t php_http_message_body_append(php_http_message_body_t *body, const char *buf, size_t len)
201 {
202 php_stream *s;
203 TSRMLS_FETCH_FROM_CTX(body->ts);
204
205 if (!(s = php_http_message_body_stream(body))) {
206 return -1;
207 }
208
209 php_stream_seek(s, 0, SEEK_END);
210 return php_stream_write(s, buf, len);
211 }
212
213 PHP_HTTP_API size_t php_http_message_body_appendf(php_http_message_body_t *body, const char *fmt, ...)
214 {
215 va_list argv;
216 char *print_str;
217 size_t print_len;
218
219 va_start(argv, fmt);
220 print_len = vspprintf(&print_str, 0, fmt, argv);
221 va_end(argv);
222
223 print_len = php_http_message_body_append(body, print_str, print_len);
224 efree(print_str);
225
226 return print_len;
227 }
228
229 PHP_HTTP_API STATUS php_http_message_body_add_form(php_http_message_body_t *body, HashTable *fields, HashTable *files)
230 {
231 zval tmp;
232
233 if (fields) {
234 INIT_PZVAL_ARRAY(&tmp, fields);
235 if (SUCCESS != add_recursive_fields(body, NULL, &tmp)) {
236 return FAILURE;
237 }
238 }
239 if (files) {
240 INIT_PZVAL_ARRAY(&tmp, files);
241 if (SUCCESS != add_recursive_files(body, NULL, &tmp)) {
242 return FAILURE;
243 }
244 }
245
246 return SUCCESS;
247 }
248
249 PHP_HTTP_API void php_http_message_body_add_part(php_http_message_body_t *body, php_http_message_t *part)
250 {
251 TSRMLS_FETCH_FROM_CTX(body->ts);
252
253 BOUNDARY_OPEN(body);
254 php_http_message_to_callback(part, (php_http_pass_callback_t) php_http_message_body_append, body);
255 BOUNDARY_CLOSE(body);
256 }
257
258
259 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)
260 {
261 char *safe_name;
262 TSRMLS_FETCH_FROM_CTX(body->ts);
263
264 safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
265
266 BOUNDARY_OPEN(body);
267 php_http_message_body_appendf(
268 body,
269 "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
270 "" PHP_HTTP_CRLF,
271 safe_name
272 );
273 php_http_message_body_append(body, value_str, value_len);
274 BOUNDARY_CLOSE(body);
275
276 efree(safe_name);
277 return SUCCESS;
278 }
279
280 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)
281 {
282 char *safe_name, *path_dup = estrdup(path), *bname;
283 size_t bname_len;
284 TSRMLS_FETCH_FROM_CTX(body->ts);
285
286 safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
287
288 php_basename(path_dup, strlen(path_dup), NULL, 0, &bname, &bname_len TSRMLS_CC);
289
290 BOUNDARY_OPEN(body);
291 php_http_message_body_appendf(
292 body,
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
296 PHP_HTTP_CRLF,
297 safe_name, bname, ctype
298 );
299 php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL);
300 BOUNDARY_CLOSE(body);
301
302 efree(safe_name);
303 efree(path_dup);
304 efree(bname);
305
306 return SUCCESS;
307 }
308
309 static inline char *format_key(uint type, char *str, ulong num, const char *prefix) {
310 char *new_key = NULL;
311
312 if (prefix && *prefix) {
313 if (type == HASH_KEY_IS_STRING) {
314 spprintf(&new_key, 0, "%s[%s]", prefix, str);
315 } else {
316 spprintf(&new_key, 0, "%s[%lu]", prefix, num);
317 }
318 } else if (type == HASH_KEY_IS_STRING) {
319 new_key = estrdup(str);
320 } else {
321 new_key = estrdup("");
322 }
323
324 return new_key;
325 }
326
327 static STATUS add_recursive_fields(php_http_message_body_t *body, const char *name, zval *value)
328 {
329 if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
330 zval **val;
331 HashTable *ht;
332 HashPosition pos;
333 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
334 TSRMLS_FETCH_FROM_CTX(body->ts);
335
336 ht = HASH_OF(value);
337 if (!ht->nApplyCount) {
338 ++ht->nApplyCount;
339 FOREACH_KEYVAL(pos, value, key, val) {
340 char *str = format_key(key.type, key.str, key.num, name);
341 if (SUCCESS != add_recursive_fields(body, str, *val)) {
342 efree(str);
343 ht->nApplyCount--;
344 return FAILURE;
345 }
346 efree(str);
347 }
348 --ht->nApplyCount;
349 }
350 } else {
351 zval *cpy = php_http_ztyp(IS_STRING, value);
352 php_http_message_body_add_form_field(body, name, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
353 zval_ptr_dtor(&cpy);
354 }
355
356 return SUCCESS;
357 }
358
359 static STATUS add_recursive_files(php_http_message_body_t *body, const char *name, zval *value)
360 {
361 zval **zdata = NULL, **zfile, **zname, **ztype;
362 HashTable *ht;
363 TSRMLS_FETCH_FROM_CTX(body->ts);
364
365 if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) {
366 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Expected array or object (name, type, file) for message body file to add");
367 return FAILURE;
368 }
369
370 ht = HASH_OF(value);
371
372 if ((SUCCESS != zend_hash_find(ht, ZEND_STRS("name"), (void *) &zname))
373 || (SUCCESS != zend_hash_find(ht, ZEND_STRS("type"), (void *) &ztype))
374 || (SUCCESS != zend_hash_find(ht, ZEND_STRS("file"), (void *) &zfile))
375 ) {
376 zval **val;
377 HashPosition pos;
378 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
379
380 if (!ht->nApplyCount) {
381 ++ht->nApplyCount;
382 FOREACH_HASH_KEYVAL(pos, ht, key, val) {
383 if (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT) {
384 char *str = format_key(key.type, key.str, key.num, name);
385
386 if (SUCCESS != add_recursive_files(body, str, *val)) {
387 efree(str);
388 --ht->nApplyCount;
389 return FAILURE;
390 }
391 efree(str);
392 }
393 }
394 --ht->nApplyCount;
395 }
396 return SUCCESS;
397 } else {
398 php_stream *stream;
399 zval *zfc = php_http_ztyp(IS_STRING, *zfile);
400
401 if (SUCCESS == zend_hash_find(ht, ZEND_STRS("data"), (void *) &zdata)) {
402 if (Z_TYPE_PP(zdata) == IS_RESOURCE) {
403 php_stream_from_zval_no_verify(stream, zdata);
404 } else {
405 zval *tmp = php_http_ztyp(IS_STRING, *zdata);
406
407 stream = php_stream_memory_open(TEMP_STREAM_READONLY, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
408 zval_ptr_dtor(&tmp);
409 }
410 } else {
411 stream = php_stream_open_wrapper(Z_STRVAL_P(zfc), "r", REPORT_ERRORS|USE_PATH, NULL);
412 }
413
414 if (!stream) {
415 zval_ptr_dtor(&zfc);
416 return FAILURE;
417 } else {
418 zval *znc = php_http_ztyp(IS_STRING, *zname), *ztc = php_http_ztyp(IS_STRING, *ztype);
419 char *key = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(znc), 0, name);
420 STATUS ret = php_http_message_body_add_form_file(body, key, Z_STRVAL_P(ztc), Z_STRVAL_P(zfc), stream);
421
422 efree(key);
423 zval_ptr_dtor(&znc);
424 zval_ptr_dtor(&ztc);
425 zval_ptr_dtor(&zfc);
426 if (!zdata || Z_TYPE_PP(zdata) != IS_RESOURCE) {
427 php_stream_close(stream);
428 }
429 return ret;
430 }
431
432 }
433 }
434
435 struct splitbody_arg {
436 php_http_buffer_t buf;
437 php_http_message_parser_t *parser;
438 char *boundary_str;
439 size_t boundary_len;
440 size_t consumed;
441 };
442
443 static size_t splitbody(void *opaque, char *buf, size_t len TSRMLS_DC)
444 {
445 struct splitbody_arg *arg = opaque;
446 const char *boundary = NULL;
447 size_t consumed = 0;
448 int first_boundary;
449
450 do {
451 first_boundary = !(consumed || arg->consumed);
452
453 if ((boundary = php_http_locate_str(buf, len, arg->boundary_str + first_boundary, arg->boundary_len - first_boundary))) {
454 size_t real_boundary_len = arg->boundary_len - 1, cut;
455 const char *real_boundary = boundary + !first_boundary;
456 int eol_len = 0;
457
458 if (buf + len <= real_boundary + real_boundary_len) {
459 /* if we just have enough data for the boundary, it's just a byte too less */
460 arg->consumed += consumed;
461 return consumed;
462 }
463
464 if (!first_boundary) {
465 /* this is not the first boundary, read rest of this message */
466 php_http_buffer_append(&arg->buf, buf, real_boundary - buf);
467 php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
468 }
469
470 /* move after the boundary */
471 cut = real_boundary - buf + real_boundary_len;
472 buf += cut;
473 len -= cut;
474 consumed += cut;
475
476 if (buf == php_http_locate_bin_eol(buf, len, &eol_len)) {
477 /* skip CRLF */
478 buf += eol_len;
479 len -= eol_len;
480 consumed += eol_len;
481
482 if (!first_boundary) {
483 /* advance messages */
484 php_http_message_t *msg;
485
486 msg = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
487 msg->parent = arg->parser->message;
488 arg->parser->message = msg;
489 }
490 } else {
491 /* is this the last boundary? */
492 if (*buf == '-') {
493 /* ignore the rest */
494 consumed += len;
495 len = 0;
496 } else {
497 /* let this be garbage */
498 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Malformed multipart boundary at pos %zu", consumed);
499 return -1;
500 }
501 }
502 }
503 } while (boundary && len);
504
505 /* let there be room for the next boundary */
506 if (len > arg->boundary_len) {
507 consumed += len - arg->boundary_len;
508 php_http_buffer_append(&arg->buf, buf, len - arg->boundary_len);
509 php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
510 }
511
512 arg->consumed += consumed;
513 return consumed;
514 }
515
516 PHP_HTTP_API php_http_message_t *php_http_message_body_split(php_http_message_body_t *body, const char *boundary)
517 {
518 php_stream *s = php_http_message_body_stream(body);
519 php_http_buffer_t *tmp = NULL;
520 php_http_message_t *msg = NULL;
521 struct splitbody_arg arg;
522 TSRMLS_FETCH_FROM_CTX(body->ts);
523
524 php_http_buffer_init(&arg.buf);
525 arg.parser = php_http_message_parser_init(NULL TSRMLS_CC);
526 arg.boundary_len = spprintf(&arg.boundary_str, 0, "\n--%s", boundary);
527 arg.consumed = 0;
528
529 php_stream_rewind(s);
530 while (!php_stream_eof(s)) {
531 php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg TSRMLS_CC);
532 }
533
534 msg = arg.parser->message;
535 arg.parser->message = NULL;
536
537 php_http_buffer_free(&tmp);
538 php_http_message_parser_free(&arg.parser);
539 php_http_buffer_dtor(&arg.buf);
540 STR_FREE(arg.boundary_str);
541
542 return msg;
543 }
544
545 static zend_object_handlers php_http_message_body_object_handlers;
546
547 zend_object_value php_http_message_body_object_new(zend_class_entry *ce TSRMLS_DC)
548 {
549 return php_http_message_body_object_new_ex(ce, NULL, NULL TSRMLS_CC);
550 }
551
552 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)
553 {
554 php_http_message_body_object_t *o;
555
556 o = ecalloc(1, sizeof(php_http_message_body_object_t));
557 zend_object_std_init((zend_object *) o, php_http_message_body_class_entry TSRMLS_CC);
558 object_properties_init((zend_object *) o, ce);
559
560 if (ptr) {
561 *ptr = o;
562 }
563
564 if (body) {
565 o->body = body;
566 }
567
568 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_body_object_free, NULL TSRMLS_CC);
569 o->zv.handlers = &php_http_message_body_object_handlers;
570
571 return o->zv;
572 }
573
574 zend_object_value php_http_message_body_object_clone(zval *object TSRMLS_DC)
575 {
576 zend_object_value new_ov;
577 php_http_message_body_object_t *new_obj = NULL;
578 php_http_message_body_object_t *old_obj = zend_object_store_get_object(object TSRMLS_CC);
579 php_http_message_body_t *body = php_http_message_body_copy(old_obj->body, NULL);
580
581 new_ov = php_http_message_body_object_new_ex(old_obj->zo.ce, body, &new_obj TSRMLS_CC);
582 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(object) TSRMLS_CC);
583
584 return new_ov;
585 }
586
587 void php_http_message_body_object_free(void *object TSRMLS_DC)
588 {
589 php_http_message_body_object_t *obj = object;
590
591 php_http_message_body_free(&obj->body);
592 zend_object_std_dtor((zend_object *) obj TSRMLS_CC);
593 efree(obj);
594 }
595
596 #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \
597 do { \
598 if (!obj->body) { \
599 obj->body = php_http_message_body_init(NULL, NULL TSRMLS_CC); \
600 } \
601 } while(0)
602
603 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___construct, 0, 0, 0)
604 ZEND_ARG_INFO(0, stream)
605 ZEND_END_ARG_INFO();
606 PHP_METHOD(HttpMessageBody, __construct)
607 {
608 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
609 zval *zstream = NULL;
610 php_stream *stream;
611
612 with_error_handling(EH_THROW, php_http_exception_class_entry) {
613 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream)) {
614 if (zstream) {
615 php_stream_from_zval(stream, &zstream);
616
617 if (stream) {
618 if (obj->body) {
619 php_http_message_body_free(&obj->body);
620 }
621 obj->body = php_http_message_body_init(NULL, stream TSRMLS_CC);
622 }
623 }
624 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
625 }
626 } end_error_handling();
627 }
628
629 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___toString, 0, 0, 0)
630 ZEND_END_ARG_INFO();
631 PHP_METHOD(HttpMessageBody, __toString)
632 {
633 if (SUCCESS == zend_parse_parameters_none()) {
634 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
635 char *str;
636 size_t len;
637
638 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
639
640 php_http_message_body_to_string(obj->body, &str, &len, 0, 0);
641 if (str) {
642 RETURN_STRINGL(str, len, 0);
643 }
644 }
645 RETURN_EMPTY_STRING();
646 }
647
648 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_unserialize, 0, 0, 1)
649 ZEND_ARG_INFO(0, serialized)
650 ZEND_END_ARG_INFO();
651 PHP_METHOD(HttpMessageBody, unserialize)
652 {
653 char *us_str;
654 int us_len;
655
656 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &us_str, &us_len)) {
657 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
658 php_stream *s = php_stream_memory_open(0, us_str, us_len);
659
660 obj->body = php_http_message_body_init(NULL, s TSRMLS_CC);
661 }
662 }
663
664 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toStream, 0, 0, 1)
665 ZEND_ARG_INFO(0, stream)
666 ZEND_ARG_INFO(0, offset)
667 ZEND_ARG_INFO(0, maxlen)
668 ZEND_END_ARG_INFO();
669 PHP_METHOD(HttpMessageBody, toStream)
670 {
671 zval *zstream;
672 long offset = 0, forlen = 0;
673
674 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ll", &zstream, &offset, &forlen)) {
675 php_stream *stream;
676 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
677
678 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
679
680 php_stream_from_zval(stream, &zstream);
681 php_http_message_body_to_stream(obj->body, stream, offset, forlen);
682 RETURN_ZVAL(getThis(), 1, 0);
683 }
684 }
685
686 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toCallback, 0, 0, 1)
687 ZEND_ARG_INFO(0, callback)
688 ZEND_ARG_INFO(0, offset)
689 ZEND_ARG_INFO(0, maxlen)
690 ZEND_END_ARG_INFO();
691 PHP_METHOD(HttpMessageBody, toCallback)
692 {
693 php_http_pass_fcall_arg_t fcd;
694 long offset = 0, forlen = 0;
695
696 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) {
697 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
698
699 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
700
701 fcd.fcz = getThis();
702 Z_ADDREF_P(fcd.fcz);
703 TSRMLS_SET_CTX(fcd.ts);
704
705 php_http_message_body_to_callback(obj->body, php_http_pass_fcall_callback, &fcd, offset, forlen);
706 zend_fcall_info_args_clear(&fcd.fci, 1);
707
708 zval_ptr_dtor(&fcd.fcz);
709 RETURN_ZVAL(getThis(), 1, 0);
710 }
711 }
712
713 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getResource, 0, 0, 0)
714 ZEND_END_ARG_INFO();
715 PHP_METHOD(HttpMessageBody, getResource)
716 {
717 if (SUCCESS == zend_parse_parameters_none()) {
718 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
719
720 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
721
722 zend_list_addref(obj->body->stream_id);
723 RETVAL_RESOURCE(obj->body->stream_id);
724 }
725 }
726
727 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getBoundary, 0, 0, 0)
728 ZEND_END_ARG_INFO();
729 PHP_METHOD(HttpMessageBody, getBoundary)
730 {
731 if (SUCCESS == zend_parse_parameters_none()) {
732 php_http_message_body_object_t * obj = zend_object_store_get_object(getThis() TSRMLS_CC);
733
734 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
735
736 if (obj->body->boundary) {
737 RETURN_STRING(obj->body->boundary, 1);
738 }
739 }
740 }
741
742 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_append, 0, 0, 1)
743 ZEND_ARG_INFO(0, string)
744 ZEND_END_ARG_INFO();
745 PHP_METHOD(HttpMessageBody, append)
746 {
747 char *str;
748 int len;
749
750 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
751 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
752
753 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
754
755 if (len != php_http_message_body_append(obj->body, str, len)) {
756 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Could not append to body");
757 }
758 RETURN_ZVAL(getThis(), 1, 0);
759 }
760 }
761
762 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addForm, 0, 0, 0)
763 ZEND_ARG_ARRAY_INFO(0, fields, 1)
764 ZEND_ARG_ARRAY_INFO(0, files, 1)
765 ZEND_END_ARG_INFO();
766 PHP_METHOD(HttpMessageBody, addForm)
767 {
768 HashTable *fields = NULL, *files = NULL;
769
770 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h!h!", &fields, &files)) {
771 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
772
773 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
774
775 php_http_message_body_add_form(obj->body, fields, files);
776 RETURN_ZVAL(getThis(), 1, 0);
777 }
778 }
779
780 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart, 0, 0, 1)
781 ZEND_ARG_OBJ_INFO(0, message, http\\Message, 0)
782 ZEND_END_ARG_INFO();
783 PHP_METHOD(HttpMessageBody, addPart)
784 {
785 zval *zobj;
786
787 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zobj, php_http_message_class_entry)) {
788 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
789 php_http_message_object_t *mobj = zend_object_store_get_object(zobj TSRMLS_CC);
790
791 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
792
793 php_http_message_body_add_part(obj->body, mobj->message);
794 RETURN_ZVAL(getThis(), 1, 0);
795 }
796 }
797
798 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_etag, 0, 0, 0)
799 ZEND_END_ARG_INFO();
800 PHP_METHOD(HttpMessageBody, etag)
801 {
802 if (SUCCESS == zend_parse_parameters_none()) {
803 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
804 char *etag;
805
806 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
807
808 if ((etag = php_http_message_body_etag(obj->body))) {
809 RETURN_STRING(etag, 0);
810 } else {
811 RETURN_FALSE;
812 }
813 }
814 }
815
816 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_stat, 0, 0, 0)
817 ZEND_ARG_INFO(0, field)
818 ZEND_END_ARG_INFO();
819 PHP_METHOD(HttpMessageBody, stat)
820 {
821 char *field_str = NULL;
822 int field_len = 0;
823
824 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &field_str, &field_len)) {
825 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
826 const php_stream_statbuf *sb;
827
828 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
829
830 if ((sb = php_http_message_body_stat(obj->body))) {
831 if (field_str && field_len) {
832 switch (*field_str) {
833 case 's':
834 case 'S':
835 RETURN_LONG(sb->sb.st_size);
836 break;
837 case 'a':
838 case 'A':
839 RETURN_LONG(sb->sb.st_atime);
840 break;
841 case 'm':
842 case 'M':
843 RETURN_LONG(sb->sb.st_mtime);
844 break;
845 case 'c':
846 case 'C':
847 RETURN_LONG(sb->sb.st_ctime);
848 break;
849 default:
850 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);
851 break;
852 }
853 } else {
854 object_init(return_value);
855 add_property_long_ex(return_value, ZEND_STRS("size"), sb->sb.st_size TSRMLS_CC);
856 add_property_long_ex(return_value, ZEND_STRS("atime"), sb->sb.st_atime TSRMLS_CC);
857 add_property_long_ex(return_value, ZEND_STRS("mtime"), sb->sb.st_mtime TSRMLS_CC);
858 add_property_long_ex(return_value, ZEND_STRS("ctime"), sb->sb.st_ctime TSRMLS_CC);
859 }
860 }
861 }
862 }
863
864 static zend_function_entry php_http_message_body_methods[] = {
865 PHP_ME(HttpMessageBody, __construct, ai_HttpMessageBody___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
866 PHP_ME(HttpMessageBody, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
867 PHP_MALIAS(HttpMessageBody, toString, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
868 PHP_MALIAS(HttpMessageBody, serialize, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
869 PHP_ME(HttpMessageBody, unserialize, ai_HttpMessageBody_unserialize, ZEND_ACC_PUBLIC)
870 PHP_ME(HttpMessageBody, toStream, ai_HttpMessageBody_toStream, ZEND_ACC_PUBLIC)
871 PHP_ME(HttpMessageBody, toCallback, ai_HttpMessageBody_toCallback, ZEND_ACC_PUBLIC)
872 PHP_ME(HttpMessageBody, getResource, ai_HttpMessageBody_getResource, ZEND_ACC_PUBLIC)
873 PHP_ME(HttpMessageBody, getBoundary, ai_HttpMessageBody_getBoundary, ZEND_ACC_PUBLIC)
874 PHP_ME(HttpMessageBody, append, ai_HttpMessageBody_append, ZEND_ACC_PUBLIC)
875 PHP_ME(HttpMessageBody, addForm, ai_HttpMessageBody_addForm, ZEND_ACC_PUBLIC)
876 PHP_ME(HttpMessageBody, addPart, ai_HttpMessageBody_addPart, ZEND_ACC_PUBLIC)
877 PHP_ME(HttpMessageBody, etag, ai_HttpMessageBody_etag, ZEND_ACC_PUBLIC)
878 PHP_ME(HttpMessageBody, stat, ai_HttpMessageBody_stat, ZEND_ACC_PUBLIC)
879 EMPTY_FUNCTION_ENTRY
880 };
881
882 zend_class_entry *php_http_message_body_class_entry;
883
884 PHP_MINIT_FUNCTION(http_message_body)
885 {
886 zend_class_entry ce = {0};
887
888 INIT_NS_CLASS_ENTRY(ce, "http\\Message", "Body", php_http_message_body_methods);
889 php_http_message_body_class_entry = zend_register_internal_class_ex(&ce, php_http_object_class_entry, NULL TSRMLS_CC);
890 php_http_message_body_class_entry->create_object = php_http_message_body_object_new;
891 memcpy(&php_http_message_body_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
892 php_http_message_body_object_handlers.clone_obj = php_http_message_body_object_clone;
893 zend_class_implements(php_http_message_body_class_entry TSRMLS_CC, 1, zend_ce_serializable);
894
895 return SUCCESS;
896 }
897
898 /*
899 * Local variables:
900 * tab-width: 4
901 * c-basic-offset: 4
902 * End:
903 * vim600: noet sw=4 ts=4 fdm=marker
904 * vim<600: noet sw=4 ts=4
905 */