added config.w32 to package.xml
[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 s = php_http_message_body_stream(body);
206 php_stream_seek(s, 0, SEEK_END);
207 return php_stream_write(s, buf, len);
208 }
209
210 PHP_HTTP_API size_t php_http_message_body_appendf(php_http_message_body_t *body, const char *fmt, ...)
211 {
212 va_list argv;
213 char *print_str;
214 size_t print_len;
215
216 va_start(argv, fmt);
217 print_len = vspprintf(&print_str, 0, fmt, argv);
218 va_end(argv);
219
220 print_len = php_http_message_body_append(body, print_str, print_len);
221 efree(print_str);
222
223 return print_len;
224 }
225
226 PHP_HTTP_API STATUS php_http_message_body_add_form(php_http_message_body_t *body, HashTable *fields, HashTable *files)
227 {
228 zval tmp;
229
230 if (fields) {
231 INIT_PZVAL_ARRAY(&tmp, fields);
232 if (SUCCESS != add_recursive_fields(body, NULL, &tmp)) {
233 return FAILURE;
234 }
235 }
236 if (files) {
237 INIT_PZVAL_ARRAY(&tmp, files);
238 if (SUCCESS != add_recursive_files(body, NULL, &tmp)) {
239 return FAILURE;
240 }
241 }
242
243 return SUCCESS;
244 }
245
246 PHP_HTTP_API void php_http_message_body_add_part(php_http_message_body_t *body, php_http_message_t *part)
247 {
248 TSRMLS_FETCH_FROM_CTX(body->ts);
249
250 BOUNDARY_OPEN(body);
251 php_http_message_to_callback(part, (php_http_pass_callback_t) php_http_message_body_append, body);
252 BOUNDARY_CLOSE(body);
253 }
254
255
256 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)
257 {
258 char *safe_name;
259 TSRMLS_FETCH_FROM_CTX(body->ts);
260
261 safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
262
263 BOUNDARY_OPEN(body);
264 php_http_message_body_appendf(
265 body,
266 "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
267 "" PHP_HTTP_CRLF,
268 safe_name
269 );
270 php_http_message_body_append(body, value_str, value_len);
271 BOUNDARY_CLOSE(body);
272
273 efree(safe_name);
274 return SUCCESS;
275 }
276
277 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)
278 {
279 char *safe_name, *path_dup = estrdup(path), *bname;
280 size_t bname_len;
281 TSRMLS_FETCH_FROM_CTX(body->ts);
282
283 safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
284
285 php_basename(path_dup, strlen(path_dup), NULL, 0, &bname, &bname_len TSRMLS_CC);
286
287 BOUNDARY_OPEN(body);
288 php_http_message_body_appendf(
289 body,
290 "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
291 "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
292 "Content-Type: %s" PHP_HTTP_CRLF
293 PHP_HTTP_CRLF,
294 safe_name, bname, ctype
295 );
296 php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL);
297 BOUNDARY_CLOSE(body);
298
299 efree(safe_name);
300 efree(path_dup);
301 efree(bname);
302
303 return SUCCESS;
304 }
305
306 static inline char *format_key(uint type, char *str, ulong num, const char *prefix) {
307 char *new_key = NULL;
308
309 if (prefix && *prefix) {
310 if (type == HASH_KEY_IS_STRING) {
311 spprintf(&new_key, 0, "%s[%s]", prefix, str);
312 } else {
313 spprintf(&new_key, 0, "%s[%lu]", prefix, num);
314 }
315 } else if (type == HASH_KEY_IS_STRING) {
316 new_key = estrdup(str);
317 } else {
318 new_key = estrdup("");
319 }
320
321 return new_key;
322 }
323
324 static STATUS add_recursive_fields(php_http_message_body_t *body, const char *name, zval *value)
325 {
326 if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
327 zval **val;
328 HashTable *ht;
329 HashPosition pos;
330 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
331 TSRMLS_FETCH_FROM_CTX(body->ts);
332
333 ht = HASH_OF(value);
334 if (!ht->nApplyCount) {
335 ++ht->nApplyCount;
336 FOREACH_KEYVAL(pos, value, key, val) {
337 char *str = format_key(key.type, key.str, key.num, name);
338 if (SUCCESS != add_recursive_fields(body, str, *val)) {
339 efree(str);
340 ht->nApplyCount--;
341 return FAILURE;
342 }
343 efree(str);
344 }
345 --ht->nApplyCount;
346 }
347 } else {
348 zval *cpy = php_http_ztyp(IS_STRING, value);
349 php_http_message_body_add_form_field(body, name, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
350 zval_ptr_dtor(&cpy);
351 }
352
353 return SUCCESS;
354 }
355
356 static STATUS add_recursive_files(php_http_message_body_t *body, const char *name, zval *value)
357 {
358 zval **zdata = NULL, **zfile, **zname, **ztype;
359 HashTable *ht;
360 TSRMLS_FETCH_FROM_CTX(body->ts);
361
362 if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) {
363 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Expected array or object (name, type, file) for message body file to add");
364 return FAILURE;
365 }
366
367 ht = HASH_OF(value);
368
369 if ((SUCCESS != zend_hash_find(ht, ZEND_STRS("name"), (void *) &zname))
370 || (SUCCESS != zend_hash_find(ht, ZEND_STRS("type"), (void *) &ztype))
371 || (SUCCESS != zend_hash_find(ht, ZEND_STRS("file"), (void *) &zfile))
372 ) {
373 zval **val;
374 HashPosition pos;
375 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
376
377 if (!ht->nApplyCount) {
378 ++ht->nApplyCount;
379 FOREACH_HASH_KEYVAL(pos, ht, key, val) {
380 if (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT) {
381 char *str = format_key(key.type, key.str, key.num, name);
382
383 if (SUCCESS != add_recursive_files(body, str, *val)) {
384 efree(str);
385 --ht->nApplyCount;
386 return FAILURE;
387 }
388 efree(str);
389 }
390 }
391 --ht->nApplyCount;
392 }
393 return SUCCESS;
394 } else {
395 php_stream *stream;
396 zval *zfc = php_http_ztyp(IS_STRING, *zfile);
397
398 if (SUCCESS == zend_hash_find(ht, ZEND_STRS("data"), (void *) &zdata)) {
399 if (Z_TYPE_PP(zdata) == IS_RESOURCE) {
400 php_stream_from_zval_no_verify(stream, zdata);
401 } else {
402 zval *tmp = php_http_ztyp(IS_STRING, *zdata);
403
404 stream = php_stream_memory_open(TEMP_STREAM_READONLY, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
405 zval_ptr_dtor(&tmp);
406 }
407 } else {
408 stream = php_stream_open_wrapper(Z_STRVAL_P(zfc), "r", REPORT_ERRORS|USE_PATH, NULL);
409 }
410
411 if (!stream) {
412 zval_ptr_dtor(&zfc);
413 return FAILURE;
414 } else {
415 zval *znc = php_http_ztyp(IS_STRING, *zname), *ztc = php_http_ztyp(IS_STRING, *ztype);
416 char *key = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(znc), 0, name);
417 STATUS ret = php_http_message_body_add_form_file(body, key, Z_STRVAL_P(ztc), Z_STRVAL_P(zfc), stream);
418
419 efree(key);
420 zval_ptr_dtor(&znc);
421 zval_ptr_dtor(&ztc);
422 zval_ptr_dtor(&zfc);
423 if (!zdata || Z_TYPE_PP(zdata) != IS_RESOURCE) {
424 php_stream_close(stream);
425 }
426 return ret;
427 }
428
429 }
430 }
431
432 struct splitbody_arg {
433 php_http_buffer_t buf;
434 php_http_message_parser_t *parser;
435 char *boundary_str;
436 size_t boundary_len;
437 size_t consumed;
438 };
439
440 static size_t splitbody(void *opaque, char *buf, size_t len TSRMLS_DC)
441 {
442 struct splitbody_arg *arg = opaque;
443 const char *boundary = NULL;
444 size_t consumed = 0;
445 int first_boundary;
446
447 do {
448 first_boundary = !(consumed || arg->consumed);
449
450 if ((boundary = php_http_locate_str(buf, len, arg->boundary_str + first_boundary, arg->boundary_len - first_boundary))) {
451 size_t real_boundary_len = arg->boundary_len - 1, cut;
452 const char *real_boundary = boundary + !first_boundary;
453 int eol_len = 0;
454
455 if (buf + len <= real_boundary + real_boundary_len) {
456 /* if we just have enough data for the boundary, it's just a byte too less */
457 arg->consumed += consumed;
458 return consumed;
459 }
460
461 if (!first_boundary) {
462 /* this is not the first boundary, read rest of this message */
463 php_http_buffer_append(&arg->buf, buf, real_boundary - buf);
464 php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
465 }
466
467 /* move after the boundary */
468 cut = real_boundary - buf + real_boundary_len;
469 buf += cut;
470 len -= cut;
471 consumed += cut;
472
473 if (buf == php_http_locate_bin_eol(buf, len, &eol_len)) {
474 /* skip CRLF */
475 buf += eol_len;
476 len -= eol_len;
477 consumed += eol_len;
478
479 if (!first_boundary) {
480 /* advance messages */
481 php_http_message_t *msg;
482
483 msg = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
484 msg->parent = arg->parser->message;
485 arg->parser->message = msg;
486 }
487 } else {
488 /* is this the last boundary? */
489 if (*buf == '-') {
490 /* ignore the rest */
491 consumed += len;
492 len = 0;
493 } else {
494 /* let this be garbage */
495 php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Malformed multipart boundary at pos %zu", consumed);
496 return -1;
497 }
498 }
499 }
500 } while (boundary && len);
501
502 /* let there be room for the next boundary */
503 if (len > arg->boundary_len) {
504 consumed += len - arg->boundary_len;
505 php_http_buffer_append(&arg->buf, buf, len - arg->boundary_len);
506 php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
507 }
508
509 arg->consumed += consumed;
510 return consumed;
511 }
512
513 PHP_HTTP_API php_http_message_t *php_http_message_body_split(php_http_message_body_t *body, const char *boundary)
514 {
515 php_stream *s = php_http_message_body_stream(body);
516 php_http_buffer_t *tmp = NULL;
517 php_http_message_t *msg = NULL;
518 struct splitbody_arg arg;
519 TSRMLS_FETCH_FROM_CTX(body->ts);
520
521 php_http_buffer_init(&arg.buf);
522 arg.parser = php_http_message_parser_init(NULL TSRMLS_CC);
523 arg.boundary_len = spprintf(&arg.boundary_str, 0, "\n--%s", boundary);
524 arg.consumed = 0;
525
526 php_stream_rewind(s);
527 while (!php_stream_eof(s)) {
528 php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg TSRMLS_CC);
529 }
530
531 msg = arg.parser->message;
532 arg.parser->message = NULL;
533
534 php_http_buffer_free(&tmp);
535 php_http_message_parser_free(&arg.parser);
536 php_http_buffer_dtor(&arg.buf);
537 STR_FREE(arg.boundary_str);
538
539 return msg;
540 }
541
542 static zend_object_handlers php_http_message_body_object_handlers;
543
544 zend_object_value php_http_message_body_object_new(zend_class_entry *ce TSRMLS_DC)
545 {
546 return php_http_message_body_object_new_ex(ce, NULL, NULL TSRMLS_CC);
547 }
548
549 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)
550 {
551 php_http_message_body_object_t *o;
552
553 o = ecalloc(1, sizeof(php_http_message_body_object_t));
554 zend_object_std_init((zend_object *) o, php_http_message_body_class_entry TSRMLS_CC);
555 object_properties_init((zend_object *) o, ce);
556
557 if (ptr) {
558 *ptr = o;
559 }
560
561 if (body) {
562 o->body = body;
563 }
564
565 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_body_object_free, NULL TSRMLS_CC);
566 o->zv.handlers = &php_http_message_body_object_handlers;
567
568 return o->zv;
569 }
570
571 zend_object_value php_http_message_body_object_clone(zval *object TSRMLS_DC)
572 {
573 zend_object_value new_ov;
574 php_http_message_body_object_t *new_obj = NULL;
575 php_http_message_body_object_t *old_obj = zend_object_store_get_object(object TSRMLS_CC);
576 php_http_message_body_t *body = php_http_message_body_copy(old_obj->body, NULL);
577
578 new_ov = php_http_message_body_object_new_ex(old_obj->zo.ce, body, &new_obj TSRMLS_CC);
579 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(object) TSRMLS_CC);
580
581 return new_ov;
582 }
583
584 void php_http_message_body_object_free(void *object TSRMLS_DC)
585 {
586 php_http_message_body_object_t *obj = object;
587
588 php_http_message_body_free(&obj->body);
589 zend_object_std_dtor((zend_object *) obj TSRMLS_CC);
590 efree(obj);
591 }
592
593 #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \
594 do { \
595 if (!obj->body) { \
596 obj->body = php_http_message_body_init(NULL, NULL TSRMLS_CC); \
597 } \
598 } while(0)
599
600 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___construct, 0, 0, 0)
601 ZEND_ARG_INFO(0, stream)
602 ZEND_END_ARG_INFO();
603 PHP_METHOD(HttpMessageBody, __construct)
604 {
605 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
606 zval *zstream = NULL;
607 php_stream *stream;
608
609 with_error_handling(EH_THROW, php_http_exception_class_entry) {
610 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream)) {
611 if (zstream) {
612 php_stream_from_zval(stream, &zstream);
613
614 if (stream) {
615 if (obj->body) {
616 php_http_message_body_free(&obj->body);
617 }
618 obj->body = php_http_message_body_init(NULL, stream TSRMLS_CC);
619 }
620 }
621 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
622 }
623 } end_error_handling();
624 }
625
626 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___toString, 0, 0, 0)
627 ZEND_END_ARG_INFO();
628 PHP_METHOD(HttpMessageBody, __toString)
629 {
630 if (SUCCESS == zend_parse_parameters_none()) {
631 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
632 char *str;
633 size_t len;
634
635 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
636
637 php_http_message_body_to_string(obj->body, &str, &len, 0, 0);
638 if (str) {
639 RETURN_STRINGL(str, len, 0);
640 }
641 }
642 RETURN_EMPTY_STRING();
643 }
644
645 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_unserialize, 0, 0, 1)
646 ZEND_ARG_INFO(0, serialized)
647 ZEND_END_ARG_INFO();
648 PHP_METHOD(HttpMessageBody, unserialize)
649 {
650 char *us_str;
651 int us_len;
652
653 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &us_str, &us_len)) {
654 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
655 php_stream *s = php_stream_memory_open(0, us_str, us_len);
656
657 obj->body = php_http_message_body_init(NULL, s TSRMLS_CC);
658 }
659 }
660
661 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toStream, 0, 0, 1)
662 ZEND_ARG_INFO(0, stream)
663 ZEND_END_ARG_INFO();
664 PHP_METHOD(HttpMessageBody, toStream)
665 {
666 zval *zstream;
667 long offset = 0, forlen = 0;
668
669 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ll", &zstream, &offset, &forlen)) {
670 php_stream *stream;
671 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
672
673 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
674
675 php_stream_from_zval(stream, &zstream);
676 php_http_message_body_to_stream(obj->body, stream, offset, forlen);
677 RETURN_TRUE;
678 }
679 RETURN_FALSE;
680 }
681
682 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toCallback, 0, 0, 1)
683 ZEND_ARG_INFO(0, callback)
684 ZEND_END_ARG_INFO();
685 PHP_METHOD(HttpMessageBody, toCallback)
686 {
687 php_http_pass_fcall_arg_t fcd;
688 long offset = 0, forlen = 0;
689
690 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) {
691 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
692
693 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
694
695 fcd.fcz = getThis();
696 Z_ADDREF_P(fcd.fcz);
697 TSRMLS_SET_CTX(fcd.ts);
698
699 php_http_message_body_to_callback(obj->body, php_http_pass_fcall_callback, &fcd, offset, forlen);
700 zend_fcall_info_args_clear(&fcd.fci, 1);
701
702 zval_ptr_dtor(&fcd.fcz);
703 RETURN_TRUE;
704 }
705 RETURN_FALSE;
706 }
707
708 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getResource, 0, 0, 0)
709 ZEND_END_ARG_INFO();
710 PHP_METHOD(HttpMessageBody, getResource)
711 {
712 if (SUCCESS == zend_parse_parameters_none()) {
713 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
714
715 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
716
717 zend_list_addref(obj->body->stream_id);
718 RETVAL_RESOURCE(obj->body->stream_id);
719 }
720 }
721
722 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_append, 0, 0, 1)
723 ZEND_ARG_INFO(0, string)
724 ZEND_END_ARG_INFO();
725 PHP_METHOD(HttpMessageBody, append)
726 {
727 char *str;
728 int len;
729
730 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
731 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
732
733 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
734
735 RETURN_LONG(php_http_message_body_append(obj->body, str, len));
736 }
737 RETURN_FALSE;
738 }
739
740 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addForm, 0, 0, 0)
741 ZEND_ARG_ARRAY_INFO(0, fields, 1)
742 ZEND_ARG_ARRAY_INFO(0, files, 1)
743 ZEND_END_ARG_INFO();
744 PHP_METHOD(HttpMessageBody, addForm)
745 {
746 HashTable *fields = NULL, *files = NULL;
747
748 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h!h!", &fields, &files)) {
749 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
750
751 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
752
753 RETURN_BOOL(SUCCESS == php_http_message_body_add_form(obj->body, fields, files));
754 }
755 RETURN_FALSE;
756 }
757
758 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart, 0, 0, 1)
759 ZEND_ARG_OBJ_INFO(0, message, http\\Message, 0)
760 ZEND_END_ARG_INFO();
761 PHP_METHOD(HttpMessageBody, addPart)
762 {
763 zval *zobj;
764
765 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zobj, php_http_message_class_entry)) {
766 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
767 php_http_message_object_t *mobj = zend_object_store_get_object(zobj TSRMLS_CC);
768
769 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
770
771 php_http_message_body_add_part(obj->body, mobj->message);
772 RETURN_TRUE;
773 }
774 RETURN_FALSE;
775 }
776
777 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_etag, 0, 0, 0)
778 ZEND_END_ARG_INFO();
779 PHP_METHOD(HttpMessageBody, etag)
780 {
781 if (SUCCESS == zend_parse_parameters_none()) {
782 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
783 char *etag;
784
785 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
786
787 if ((etag = php_http_message_body_etag(obj->body))) {
788 RETURN_STRING(etag, 0);
789 }
790 }
791 RETURN_FALSE;
792 }
793
794 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_stat, 0, 0, 0)
795 ZEND_ARG_INFO(0, what)
796 ZEND_END_ARG_INFO();
797 PHP_METHOD(HttpMessageBody, stat)
798 {
799 char *field_str = NULL;
800 int field_len = 0;
801
802 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &field_str, &field_len)) {
803 php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
804 const php_stream_statbuf *sb;
805
806 PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
807
808 if ((sb = php_http_message_body_stat(obj->body))) {
809 if (field_str && field_len) {
810 switch (*field_str) {
811 case 's':
812 case 'S':
813 RETURN_LONG(sb->sb.st_size);
814 break;
815 case 'a':
816 case 'A':
817 RETURN_LONG(sb->sb.st_atime);
818 break;
819 case 'm':
820 case 'M':
821 RETURN_LONG(sb->sb.st_mtime);
822 break;
823 case 'c':
824 case 'C':
825 RETURN_LONG(sb->sb.st_ctime);
826 break;
827 default:
828 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);
829 break;
830 }
831 } else {
832 array_init(return_value);
833 add_assoc_long_ex(return_value, ZEND_STRS("size"), sb->sb.st_size);
834 add_assoc_long_ex(return_value, ZEND_STRS("atime"), sb->sb.st_atime);
835 add_assoc_long_ex(return_value, ZEND_STRS("mtime"), sb->sb.st_mtime);
836 add_assoc_long_ex(return_value, ZEND_STRS("ctime"), sb->sb.st_ctime);
837 return;
838 }
839 }
840 }
841 RETURN_FALSE;
842 }
843
844 static zend_function_entry php_http_message_body_methods[] = {
845 PHP_ME(HttpMessageBody, __construct, ai_HttpMessageBody___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
846 PHP_ME(HttpMessageBody, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
847 PHP_MALIAS(HttpMessageBody, toString, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
848 PHP_MALIAS(HttpMessageBody, serialize, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
849 PHP_ME(HttpMessageBody, unserialize, ai_HttpMessageBody_unserialize, ZEND_ACC_PUBLIC)
850 PHP_ME(HttpMessageBody, toStream, ai_HttpMessageBody_toStream, ZEND_ACC_PUBLIC)
851 PHP_ME(HttpMessageBody, toCallback, ai_HttpMessageBody_toCallback, ZEND_ACC_PUBLIC)
852 PHP_ME(HttpMessageBody, getResource, ai_HttpMessageBody_getResource, ZEND_ACC_PUBLIC)
853 PHP_ME(HttpMessageBody, append, ai_HttpMessageBody_append, ZEND_ACC_PUBLIC)
854 PHP_ME(HttpMessageBody, addForm, ai_HttpMessageBody_addForm, ZEND_ACC_PUBLIC)
855 PHP_ME(HttpMessageBody, addPart, ai_HttpMessageBody_addPart, ZEND_ACC_PUBLIC)
856 PHP_ME(HttpMessageBody, etag, ai_HttpMessageBody_etag, ZEND_ACC_PUBLIC)
857 PHP_ME(HttpMessageBody, stat, ai_HttpMessageBody_stat, ZEND_ACC_PUBLIC)
858 EMPTY_FUNCTION_ENTRY
859 };
860
861 zend_class_entry *php_http_message_body_class_entry;
862
863 PHP_MINIT_FUNCTION(http_message_body)
864 {
865 zend_class_entry ce = {0};
866
867 INIT_NS_CLASS_ENTRY(ce, "http\\Message", "Body", php_http_message_body_methods);
868 php_http_message_body_class_entry = zend_register_internal_class_ex(&ce, php_http_object_class_entry, NULL TSRMLS_CC);
869 php_http_message_body_class_entry->create_object = php_http_message_body_object_new;
870 memcpy(&php_http_message_body_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
871 php_http_message_body_object_handlers.clone_obj = php_http_message_body_object_clone;
872 zend_class_implements(php_http_message_body_class_entry TSRMLS_CC, 1, zend_ce_serializable);
873
874 return SUCCESS;
875 }
876
877 /*
878 * Local variables:
879 * tab-width: 4
880 * c-basic-offset: 4
881 * End:
882 * vim600: noet sw=4 ts=4 fdm=marker
883 * vim<600: noet sw=4 ts=4
884 */