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