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