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