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