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