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