fix message parser content length
[m6w6/ext-http] / php_http_message.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 static void message_headers(php_http_message_t *msg, php_http_buffer_t *str);
16
17 zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info)
18 {
19 php_http_message_t *old = *message;
20
21 /* advance message */
22 if (!old || old->type || zend_hash_num_elements(&old->hdrs)) {
23 (*message) = php_http_message_init(NULL, 0, NULL);
24 (*message)->parent = old;
25 if (headers) {
26 (*headers) = &((*message)->hdrs);
27 }
28 }
29
30 if (info) {
31 php_http_message_set_info(*message, info);
32 }
33
34 return old != *message;
35 }
36
37 php_http_message_t *php_http_message_init(php_http_message_t *message, php_http_message_type_t type, php_http_message_body_t *body)
38 {
39 if (!message) {
40 message = emalloc(sizeof(*message));
41 }
42 memset(message, 0, sizeof(*message));
43
44 php_http_message_set_type(message, type);
45 message->http.version.major = 1;
46 message->http.version.minor = 1;
47 zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
48 message->body = body ? body : php_http_message_body_init(NULL, NULL);
49
50 return message;
51 }
52
53 php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_http_message_type_t type)
54 {
55 int free_msg = !message;
56 zval *sval, tval;
57 php_http_message_body_t *mbody;
58
59 switch (type) {
60 case PHP_HTTP_REQUEST:
61 mbody = php_http_env_get_request_body();
62 php_http_message_body_addref(mbody);
63 message = php_http_message_init(message, type, mbody);
64 if ((sval = php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1)) && !strncmp(Z_STRVAL_P(sval), "HTTP/", lenof("HTTP/"))) {
65 php_http_version_parse(&message->http.version, Z_STRVAL_P(sval));
66 }
67 if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1))) {
68 message->http.info.request.method = estrdup(Z_STRVAL_P(sval));
69 }
70 if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1))) {
71 message->http.info.request.url = php_http_url_parse(Z_STRVAL_P(sval), Z_STRLEN_P(sval), ~0);
72 }
73
74 php_http_env_get_request_headers(&message->hdrs);
75 break;
76
77 case PHP_HTTP_RESPONSE:
78 message = php_http_message_init(NULL, type, NULL);
79 if (!SG(sapi_headers).http_status_line || !php_http_info_parse((php_http_info_t *) &message->http, SG(sapi_headers).http_status_line)) {
80 if (!(message->http.info.response.code = SG(sapi_headers).http_response_code)) {
81 message->http.info.response.code = 200;
82 }
83 message->http.info.response.status = estrdup(php_http_env_get_response_status_for_code(message->http.info.response.code));
84 }
85
86 php_http_env_get_response_headers(&message->hdrs);
87 if (php_output_get_level()) {
88 if (php_output_get_status() & PHP_OUTPUT_SENT) {
89 php_error_docref(NULL, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C), php_output_get_start_lineno(TSRMLS_C));
90 goto error;
91 } else if (SUCCESS != php_output_get_contents(&tval)) {
92 php_error_docref(NULL, E_WARNING, "Could not fetch response body");
93 goto error;
94 } else {
95 php_http_message_body_append(message->body, Z_STRVAL(tval), Z_STRLEN(tval));
96 zval_dtor(&tval);
97 }
98 }
99 break;
100
101 default:
102 error:
103 if (free_msg) {
104 if (message) {
105 php_http_message_free(&message);
106 }
107 } else {
108 message = NULL;
109 }
110 break;
111 }
112
113 return message;
114 }
115
116 php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len, zend_bool greedy)
117 {
118 php_http_message_parser_t p;
119 php_http_buffer_t buf;
120 unsigned flags = PHP_HTTP_MESSAGE_PARSER_CLEANUP;
121 int free_msg;
122
123 php_http_buffer_from_string_ex(&buf, str, len);
124 php_http_message_parser_init(&p);
125
126 if ((free_msg = !msg)) {
127 msg = php_http_message_init(NULL, 0, NULL);
128 }
129
130 if (greedy) {
131 flags |= PHP_HTTP_MESSAGE_PARSER_GREEDY;
132 }
133 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(&p, &buf, flags, &msg)) {
134 if (free_msg) {
135 php_http_message_free(&msg);
136 }
137 msg = NULL;
138 }
139
140 php_http_message_parser_dtor(&p);
141 php_http_buffer_dtor(&buf);
142
143 return msg;
144 }
145
146 zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len)
147 {
148 zval *ret;
149 char *key;
150 ALLOCA_FLAG(free_key);
151
152 key = do_alloca(key_len + 1, free_key);
153
154 memcpy(key, key_str, key_len);
155 key[key_len] = '\0';
156 php_http_pretty_key(key, key_len, 1, 1);
157
158 ret = zend_symtable_str_find(&msg->hdrs, key, key_len);
159
160 free_alloca(key, free_key);
161
162 return ret;
163 }
164
165 zend_bool php_http_message_is_multipart(php_http_message_t *msg, char **boundary)
166 {
167 zend_string *ct = php_http_message_header_string(msg, ZEND_STRL("Content-Type"));
168 zend_bool is_multipart = 0;
169
170 if (ct) {
171 php_http_params_opts_t popts;
172 HashTable params;
173
174 ZEND_INIT_SYMTABLE(&params);
175 php_http_params_opts_default_get(&popts);
176 popts.input.str = ct->val;
177 popts.input.len = ct->len;
178
179 if (php_http_params_parse(&params, &popts)) {
180 zval *cur, *arg;
181 zend_string *ct_str;
182 zend_ulong index;
183
184 zend_hash_internal_pointer_reset(&params);
185
186 if ((cur = zend_hash_get_current_data(&params))
187 && (Z_TYPE_P(cur) == IS_ARRAY)
188 && (HASH_KEY_IS_STRING == zend_hash_get_current_key(&params, &ct_str, &index))
189 ) {
190 if (php_http_match(ct_str->val, "multipart", PHP_HTTP_MATCH_WORD)) {
191 is_multipart = 1;
192
193 /* get boundary */
194 if (boundary
195 && (arg = zend_hash_str_find(Z_ARRVAL_P(cur), ZEND_STRL("arguments")))
196 && Z_TYPE_P(arg) == IS_ARRAY
197 ) {
198 zval *val;
199 php_http_arrkey_t key;
200
201 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arg), key.h, key.key, val)
202 {
203 if (key.key && key.key->len == lenof("boundary") && !strcasecmp(key.key->val, "boundary")) {
204 zend_string *bnd = zval_get_string(val);
205
206 if (bnd->len) {
207 *boundary = estrndup(bnd->val, bnd->len);
208 }
209 zend_string_release(bnd);
210 }
211 }
212 ZEND_HASH_FOREACH_END();
213 }
214 }
215 }
216 }
217 zend_hash_destroy(&params);
218 zend_string_release(ct);
219 }
220
221 return is_multipart;
222 }
223
224 /* */
225 void php_http_message_set_type(php_http_message_t *message, php_http_message_type_t type)
226 {
227 /* just act if different */
228 if (type != message->type) {
229
230 /* free request info */
231 switch (message->type) {
232 case PHP_HTTP_REQUEST:
233 PTR_FREE(message->http.info.request.method);
234 PTR_FREE(message->http.info.request.url);
235 break;
236
237 case PHP_HTTP_RESPONSE:
238 PTR_FREE(message->http.info.response.status);
239 break;
240
241 default:
242 break;
243 }
244
245 message->type = type;
246 memset(&message->http, 0, sizeof(message->http));
247 }
248 }
249
250 void php_http_message_set_info(php_http_message_t *message, php_http_info_t *info)
251 {
252 php_http_message_set_type(message, info->type);
253 message->http.version = info->http.version;
254 switch (message->type) {
255 case PHP_HTTP_REQUEST:
256 PTR_SET(PHP_HTTP_INFO(message).request.url, PHP_HTTP_INFO(info).request.url ? php_http_url_copy(PHP_HTTP_INFO(info).request.url, 0) : NULL);
257 PTR_SET(PHP_HTTP_INFO(message).request.method, PHP_HTTP_INFO(info).request.method ? estrdup(PHP_HTTP_INFO(info).request.method) : NULL);
258 break;
259
260 case PHP_HTTP_RESPONSE:
261 PHP_HTTP_INFO(message).response.code = PHP_HTTP_INFO(info).response.code;
262 PTR_SET(PHP_HTTP_INFO(message).response.status, PHP_HTTP_INFO(info).response.status ? estrdup(PHP_HTTP_INFO(info).response.status) : NULL);
263 break;
264
265 default:
266 break;
267 }
268 }
269
270 void php_http_message_update_headers(php_http_message_t *msg)
271 {
272 zval h;
273 size_t size;
274 zend_string *cl;
275
276 if (php_http_message_body_stream(msg->body)->readfilters.head) {
277 /* if a read stream filter is attached to the body the caller must also care for the headers */
278 } else if ((size = php_http_message_body_size(msg->body))) {
279 ZVAL_LONG(&h, size);
280 zend_hash_str_update(&msg->hdrs, "Content-Length", lenof("Content-Length"), &h);
281
282 if (msg->body->boundary) {
283 char *str;
284 size_t len;
285 zend_string *ct;
286
287 if (!(ct = php_http_message_header_string(msg, ZEND_STRL("Content-Type")))) {
288 len = spprintf(&str, 0, "multipart/form-data; boundary=\"%s\"", msg->body->boundary);
289 ZVAL_STR(&h, php_http_cs2zs(str, len));
290 zend_hash_str_update(&msg->hdrs, "Content-Type", lenof("Content-Type"), &h);
291 } else if (!php_http_match(ct->val, "boundary=", PHP_HTTP_MATCH_WORD)) {
292 len = spprintf(&str, 0, "%s; boundary=\"%s\"", ct->val, msg->body->boundary);
293 ZVAL_STR(&h, php_http_cs2zs(str, len));
294 zend_hash_str_update(&msg->hdrs, "Content-Type", lenof("Content-Type"), &h);
295 zend_string_release(ct);
296 } else {
297 zend_string_release(ct);
298 }
299 }
300 } else if ((cl = php_http_message_header_string(msg, ZEND_STRL("Content-Length")))) {
301 if (!zend_string_equals_literal(cl, "0")) {
302 zend_hash_str_del(&msg->hdrs, ZEND_STRL("Content-Length"));
303 }
304 zend_string_release(cl);
305 }
306 }
307
308 static void message_headers(php_http_message_t *msg, php_http_buffer_t *str)
309 {
310 char *tmp = NULL;
311
312 switch (msg->type) {
313 case PHP_HTTP_REQUEST:
314 php_http_buffer_appendf(str, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg->http, tmp, PHP_HTTP_CRLF));
315 PTR_FREE(tmp);
316 break;
317
318 case PHP_HTTP_RESPONSE:
319 php_http_buffer_appendf(str, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg->http, tmp, PHP_HTTP_CRLF));
320 PTR_FREE(tmp);
321 break;
322
323 default:
324 break;
325 }
326
327 php_http_message_update_headers(msg);
328 php_http_header_to_string(str, &msg->hdrs);
329 }
330
331 void php_http_message_to_callback(php_http_message_t *msg, php_http_pass_callback_t cb, void *cb_arg)
332 {
333 php_http_buffer_t str;
334
335 php_http_buffer_init_ex(&str, 0x1000, 0);
336 message_headers(msg, &str);
337 cb(cb_arg, str.data, str.used);
338 php_http_buffer_dtor(&str);
339
340 if (php_http_message_body_size(msg->body)) {
341 cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF));
342 php_http_message_body_to_callback(msg->body, cb, cb_arg, 0, 0);
343 }
344 }
345
346 void php_http_message_to_string(php_http_message_t *msg, char **string, size_t *length)
347 {
348 php_http_buffer_t str;
349 char *data;
350
351 php_http_buffer_init_ex(&str, 0x1000, 0);
352 message_headers(msg, &str);
353 if (php_http_message_body_size(msg->body)) {
354 php_http_buffer_appends(&str, PHP_HTTP_CRLF);
355 php_http_message_body_to_callback(msg->body, (php_http_pass_callback_t) php_http_buffer_append, &str, 0, 0);
356 }
357
358 data = php_http_buffer_data(&str, string, length);
359 if (!string) {
360 efree(data);
361 }
362
363 php_http_buffer_dtor(&str);
364 }
365
366 void php_http_message_serialize(php_http_message_t *message, char **string, size_t *length)
367 {
368 char *buf;
369 php_http_buffer_t str;
370 php_http_message_t *msg;
371
372 php_http_buffer_init(&str);
373
374 msg = message = php_http_message_reverse(message);
375 do {
376 php_http_message_to_callback(message, (php_http_pass_callback_t) php_http_buffer_append, &str);
377 php_http_buffer_appends(&str, PHP_HTTP_CRLF);
378 } while ((message = message->parent));
379 php_http_message_reverse(msg);
380
381 buf = php_http_buffer_data(&str, string, length);
382 if (!string) {
383 efree(buf);
384 }
385
386 php_http_buffer_dtor(&str);
387 }
388
389 php_http_message_t *php_http_message_reverse(php_http_message_t *msg)
390 {
391 size_t i, c = php_http_message_count(msg);
392
393 if (c > 1) {
394 php_http_message_t *tmp = msg, **arr;
395
396 arr = ecalloc(c, sizeof(**arr));
397 for (i = 0; i < c; ++i) {
398 arr[i] = tmp;
399 tmp = tmp->parent;
400 }
401 arr[0]->parent = NULL;
402 for (i = 0; i < c-1; ++i) {
403 arr[i+1]->parent = arr[i];
404 }
405
406 msg = arr[c-1];
407 efree(arr);
408 }
409
410 return msg;
411 }
412
413 php_http_message_t *php_http_message_zip(php_http_message_t *dst, php_http_message_t *src)
414 {
415 php_http_message_t *tmp_dst, *tmp_src, *ret = dst;
416
417 while (dst && src) {
418 tmp_dst = dst->parent;
419 tmp_src = src->parent;
420 dst->parent = src;
421 if (tmp_dst) {
422 src->parent = tmp_dst;
423 }
424 src = tmp_src;
425 dst = tmp_dst;
426 }
427
428 return ret;
429 }
430
431 php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_message_t *to, zend_bool parents)
432 {
433 php_http_message_t *temp, *copy = NULL;
434 php_http_info_t info;
435
436 if (from) {
437 info.type = from->type;
438 info.http = from->http;
439
440 copy = temp = php_http_message_init(to, 0, php_http_message_body_copy(from->body, NULL));
441 php_http_message_set_info(temp, &info);
442 zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref);
443
444 if (parents) while (from->parent) {
445 info.type = from->parent->type;
446 info.http = from->parent->http;
447
448 temp->parent = php_http_message_init(NULL, 0, php_http_message_body_copy(from->parent->body, NULL));
449 php_http_message_set_info(temp->parent, &info);
450 array_copy(&from->parent->hdrs, &temp->parent->hdrs);
451
452 temp = temp->parent;
453 from = from->parent;
454 }
455 }
456
457 return copy;
458 }
459
460 void php_http_message_dtor(php_http_message_t *message)
461 {
462 if (message) {
463 zend_hash_destroy(&message->hdrs);
464 php_http_message_body_free(&message->body);
465
466 switch (message->type) {
467 case PHP_HTTP_REQUEST:
468 PTR_SET(message->http.info.request.method, NULL);
469 PTR_SET(message->http.info.request.url, NULL);
470 break;
471
472 case PHP_HTTP_RESPONSE:
473 PTR_SET(message->http.info.response.status, NULL);
474 break;
475
476 default:
477 break;
478 }
479 }
480 }
481
482 void php_http_message_free(php_http_message_t **message)
483 {
484 if (*message) {
485 if ((*message)->parent) {
486 php_http_message_free(&(*message)->parent);
487 }
488 php_http_message_dtor(*message);
489 efree(*message);
490 *message = NULL;
491 }
492 }
493
494 static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *rv);
495 static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot);
496 static HashTable *php_http_message_object_get_props(zval *object);
497
498 static zend_object_handlers php_http_message_object_handlers;
499 static HashTable php_http_message_object_prophandlers;
500
501 static void php_http_message_object_prophandler_hash_dtor(zval *pData)
502 {
503 pefree(Z_PTR_P(pData), 1);
504 }
505
506 typedef void (*php_http_message_object_prophandler_func_t)(php_http_message_object_t *o, zval *v);
507
508 typedef struct php_http_message_object_prophandler {
509 php_http_message_object_prophandler_func_t read;
510 php_http_message_object_prophandler_func_t write;
511 } php_http_message_object_prophandler_t;
512
513 static ZEND_RESULT_CODE php_http_message_object_add_prophandler(const char *prop_str, size_t prop_len, php_http_message_object_prophandler_func_t read, php_http_message_object_prophandler_func_t write) {
514 php_http_message_object_prophandler_t h = { read, write };
515 if (!zend_hash_str_add_mem(&php_http_message_object_prophandlers, prop_str, prop_len, (void *) &h, sizeof(h))) {
516 return FAILURE;
517 }
518 return SUCCESS;
519 }
520 static php_http_message_object_prophandler_t *php_http_message_object_get_prophandler(zend_string *name_str) {
521 return zend_hash_str_find_ptr(&php_http_message_object_prophandlers, name_str->val, name_str->len);
522 }
523 static void php_http_message_object_prophandler_get_type(php_http_message_object_t *obj, zval *return_value) {
524 RETVAL_LONG(obj->message->type);
525 }
526 static void php_http_message_object_prophandler_set_type(php_http_message_object_t *obj, zval *value) {
527 php_http_message_set_type(obj->message, zval_get_long(value));
528 }
529 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t *obj, zval *return_value) {
530 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message) && obj->message->http.info.request.method) {
531 RETVAL_STRING(obj->message->http.info.request.method);
532 } else {
533 RETVAL_NULL();
534 }
535 }
536 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t *obj, zval *value) {
537 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
538 zend_string *zs = zval_get_string(value);
539 PTR_SET(obj->message->http.info.request.method, estrndup(zs->val, zs->len));
540 zend_string_release(zs);
541 }
542 }
543 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t *obj, zval *return_value) {
544 char *url_str;
545 size_t url_len;
546
547 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message) && obj->message->http.info.request.url && php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0)) {
548 RETVAL_STR(php_http_cs2zs(url_str, url_len));
549 } else {
550 RETVAL_NULL();
551 }
552 }
553 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t *obj, zval *value) {
554 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
555 PTR_SET(obj->message->http.info.request.url, php_http_url_from_zval(value, ~0));
556 }
557 }
558 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t *obj, zval *return_value) {
559 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message) && obj->message->http.info.response.status) {
560 RETVAL_STRING(obj->message->http.info.response.status);
561 } else {
562 RETVAL_NULL();
563 }
564 }
565 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t *obj, zval *value) {
566 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
567 zend_string *zs = zval_get_string(value);
568 PTR_SET(obj->message->http.info.response.status, estrndup(zs->val, zs->len));
569 zend_string_release(zs);
570 }
571 }
572 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t *obj, zval *return_value) {
573 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
574 RETVAL_LONG(obj->message->http.info.response.code);
575 } else {
576 RETVAL_NULL();
577 }
578 }
579 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t *obj, zval *value) {
580 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
581 obj->message->http.info.response.code = zval_get_long(value);
582 PTR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(obj->message->http.info.response.code)));
583 }
584 }
585 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t *obj, zval *return_value) {
586 char *version_str;
587 size_t version_len;
588
589 php_http_version_to_string(&obj->message->http.version, &version_str, &version_len, NULL, NULL);
590 RETVAL_STR(php_http_cs2zs(version_str, version_len));
591 }
592 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t *obj, zval *value) {
593 zend_string *zs = zval_get_string(value);
594 php_http_version_parse(&obj->message->http.version, zs->val);
595 zend_string_release(zs);
596 }
597 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t *obj, zval *return_value ) {
598 array_init(return_value);
599 array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
600 }
601 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t *obj, zval *value) {
602 HashTable *headers;
603 zval *orig_value = value;
604
605 if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) {
606 convert_to_array_ex(value);
607 } else {
608 headers = HASH_OF(value);
609 }
610
611 zend_hash_clean(&obj->message->hdrs);
612 array_copy(headers, &obj->message->hdrs);
613
614 if (orig_value != value) {
615 zval_ptr_dtor(value);
616 }
617 }
618 static void php_http_message_object_prophandler_get_body(php_http_message_object_t *obj, zval *return_value) {
619 if (obj->body) {
620 RETVAL_OBJECT(&obj->body->zo, 1);
621 } else {
622 RETVAL_NULL();
623 }
624 }
625 static void php_http_message_object_prophandler_set_body(php_http_message_object_t *obj, zval *value) {
626 php_http_message_object_set_body(obj, value);
627 }
628 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t *obj, zval *return_value) {
629 if (obj->message->parent) {
630 RETVAL_OBJECT(&obj->parent->zo, 1);
631 } else {
632 RETVAL_NULL();
633 }
634 }
635 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t *obj, zval *value) {
636 if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_message_class_entry)) {
637 php_http_message_object_t *parent_obj = PHP_HTTP_OBJ(NULL, value);
638
639 if (obj->message->parent) {
640 zend_objects_store_del(&obj->parent->zo);
641 }
642 Z_ADDREF_P(value);
643 obj->parent = parent_obj;
644 obj->message->parent = parent_obj->message;
645 }
646 }
647
648 #define PHP_HTTP_MESSAGE_OBJECT_INIT(obj) \
649 do { \
650 if (!obj->message) { \
651 obj->message = php_http_message_init(NULL, 0, NULL); \
652 } \
653 } while(0)
654
655
656 void php_http_message_object_reverse(zval *zmsg, zval *return_value)
657 {
658 size_t i;
659 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, zmsg);
660
661 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
662
663 /* count */
664 i = php_http_message_count(obj->message);
665
666 if (i > 1) {
667 php_http_message_object_t **objects;
668 int last;
669
670 objects = ecalloc(i, sizeof(**objects));
671
672 /* we are the first message */
673 objects[0] = obj;
674
675 /* fetch parents */
676 for (i = 1; obj->parent; ++i) {
677 objects[i] = obj = obj->parent;
678 }
679
680 /* reorder parents */
681 for (last = --i; i; --i) {
682 objects[i]->message->parent = objects[i-1]->message;
683 objects[i]->parent = objects[i-1];
684 }
685
686 objects[0]->message->parent = NULL;
687 objects[0]->parent = NULL;
688
689 /* add ref, because we previously have not been a parent message */
690 Z_ADDREF_P(zmsg);
691 RETVAL_OBJ(&objects[last]->zo);
692
693 efree(objects);
694 } else {
695 RETURN_ZVAL_FAST(zmsg);
696 }
697 }
698
699 void php_http_message_object_prepend(zval *this_ptr, zval *prepend, zend_bool top)
700 {
701 php_http_message_t *save_parent_msg = NULL;
702 php_http_message_object_t *save_parent_obj = NULL, *obj = PHP_HTTP_OBJ(NULL, this_ptr);
703 php_http_message_object_t *prepend_obj = PHP_HTTP_OBJ(NULL, prepend);
704
705 if (!top) {
706 save_parent_obj = obj->parent;
707 save_parent_msg = obj->message->parent;
708 } else {
709 /* iterate to the most parent object */
710 while (obj->parent) {
711 obj = obj->parent;
712 }
713 }
714
715 /* prepend */
716 obj->parent = prepend_obj;
717 obj->message->parent = prepend_obj->message;
718
719 /* add ref */
720 Z_ADDREF_P(prepend);
721
722 if (!top) {
723 prepend_obj->parent = save_parent_obj;
724 prepend_obj->message->parent = save_parent_msg;
725 }
726 }
727
728 ZEND_RESULT_CODE php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval *zbody)
729 {
730 php_stream *s;
731 zend_string *body_str;
732 php_http_message_body_t *body;
733 php_http_message_body_object_t *body_obj;
734
735 switch (Z_TYPE_P(zbody)) {
736 case IS_RESOURCE:
737 php_stream_from_zval_no_verify(s, zbody);
738 if (!s) {
739 php_http_throw(unexpected_val, "The stream is not a valid resource", NULL);
740 return FAILURE;
741 }
742
743 is_resource:
744
745 body = php_http_message_body_init(NULL, s);
746 if (!(body_obj = php_http_message_body_object_new_ex(php_http_message_body_class_entry, body))) {
747 php_http_message_body_free(&body);
748 return FAILURE;
749 }
750 break;
751
752 case IS_OBJECT:
753 if (instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry)) {
754 Z_ADDREF_P(zbody);
755 body_obj = PHP_HTTP_OBJ(NULL, zbody);
756 break;
757 }
758 /* no break */
759
760 default:
761 body_str = zval_get_string(zbody);
762 s = php_stream_temp_new();
763 php_stream_write(s, body_str->val, body_str->len);
764 zend_string_release(body_str);
765 goto is_resource;
766
767 }
768
769 if (!body_obj->body) {
770 body_obj->body = php_http_message_body_init(NULL, NULL);
771 }
772 if (msg_obj->body) {
773 zend_objects_store_del(&msg_obj->body->zo);
774 }
775 if (msg_obj->message) {
776 php_http_message_body_free(&msg_obj->message->body);
777 msg_obj->message->body = php_http_message_body_init(&body_obj->body, NULL);
778 } else {
779 msg_obj->message = php_http_message_init(NULL, 0, php_http_message_body_init(&body_obj->body, NULL));
780 }
781 msg_obj->body = body_obj;
782
783 return SUCCESS;
784 }
785
786 ZEND_RESULT_CODE php_http_message_object_init_body_object(php_http_message_object_t *obj)
787 {
788 php_http_message_body_addref(obj->message->body);
789 return php_http_new((void *) &obj->body, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, obj->message->body);
790 }
791
792 zend_object *php_http_message_object_new(zend_class_entry *ce)
793 {
794 return &php_http_message_object_new_ex(ce, NULL)->zo;
795 }
796
797 php_http_message_object_t *php_http_message_object_new_ex(zend_class_entry *ce, php_http_message_t *msg)
798 {
799 php_http_message_object_t *o;
800
801 o = ecalloc(1, sizeof(php_http_message_object_t) + (ce->default_properties_count - 1) * sizeof(zval));
802 zend_object_std_init(&o->zo, ce);
803 object_properties_init(&o->zo, ce);
804
805 if (msg) {
806 o->message = msg;
807 if (msg->parent) {
808 o->parent = php_http_message_object_new_ex(ce, msg->parent);
809 }
810 o->body = php_http_message_body_object_new_ex(php_http_message_body_class_entry, php_http_message_body_init(&msg->body, NULL));
811 }
812
813 o->zo.handlers = &php_http_message_object_handlers;
814
815 return o;
816 }
817
818 zend_object *php_http_message_object_clone(zval *this_ptr TSRMLS_DC)
819 {
820 php_http_message_object_t *new_obj = NULL;
821 php_http_message_object_t *old_obj = PHP_HTTP_OBJ(NULL, this_ptr);
822
823 new_obj = php_http_message_object_new_ex(old_obj->zo.ce, php_http_message_copy(old_obj->message, NULL));
824 zend_objects_clone_members(&new_obj->zo, &old_obj->zo);
825
826 return &new_obj->zo;
827 }
828
829 void php_http_message_object_free(zend_object *object)
830 {
831 php_http_message_object_t *o = PHP_HTTP_OBJ(object, NULL);
832
833 if (!Z_ISUNDEF(o->iterator)) {
834 zval_ptr_dtor(&o->iterator);
835 ZVAL_UNDEF(&o->iterator);
836 }
837 if (o->message) {
838 /* do NOT free recursivly */
839 php_http_message_dtor(o->message);
840 efree(o->message);
841 o->message = NULL;
842 }
843 if (o->parent) {
844 zend_objects_store_del(&o->parent->zo);
845 o->parent = NULL;
846 }
847 if (o->body) {
848 zend_objects_store_del(&o->body->zo);
849 o->body = NULL;
850 }
851 zend_object_std_dtor(object);
852 }
853
854 static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *return_value)
855 {
856 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
857 php_http_message_object_prophandler_t *handler;
858 zend_string *member_name = zval_get_string(member);
859
860 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
861
862 if ((handler = php_http_message_object_get_prophandler(member_name))) {
863 if (type == BP_VAR_R) {
864 handler->read(obj, return_value);
865 } else {
866 php_property_proxy_t *proxy = php_property_proxy_init(object, member_name);
867 RETVAL_OBJ(&php_property_proxy_object_new_ex(php_property_proxy_get_class_entry(), proxy)->zo);
868 }
869 } else {
870 zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, return_value);
871 }
872
873 zend_string_release(member_name);
874
875 return return_value;
876 }
877
878 static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot)
879 {
880 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
881 php_http_message_object_prophandler_t *handler;
882 zend_string *member_name = zval_get_string(member);
883
884 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
885
886 if ((handler = php_http_message_object_get_prophandler(member_name))) {
887 handler->write(obj, value);
888 } else {
889 zend_get_std_object_handlers()->write_property(object, member, value, cache_slot);
890 }
891
892 zend_string_release(member_name);
893 }
894
895 static HashTable *php_http_message_object_get_props(zval *object)
896 {
897 zval tmp;
898 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
899 HashTable *props = zend_get_std_object_handlers()->get_properties(object);
900 char *ver_str, *url_str = NULL;
901 size_t ver_len, url_len = 0;
902
903 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
904
905 #define UPDATE_PROP(name_str, action_with_tmp) \
906 do { \
907 zend_property_info *pi; \
908 if ((pi = zend_hash_str_find_ptr(&obj->zo.ce->properties_info, name_str, lenof(name_str)))) { \
909 action_with_tmp; \
910 zend_hash_update(props, pi->name, &tmp); \
911 } \
912 } while(0)
913
914 UPDATE_PROP("type", ZVAL_LONG(&tmp, obj->message->type));
915
916 ver_len = spprintf(&ver_str, 0, "%u.%u", obj->message->http.version.major, obj->message->http.version.minor);
917 UPDATE_PROP("httpVersion", ZVAL_STR(&tmp, php_http_cs2zs(ver_str, ver_len)));
918
919 switch (obj->message->type) {
920 case PHP_HTTP_REQUEST:
921 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp, 0));
922 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp));
923 UPDATE_PROP("requestMethod", ZVAL_STRING(&tmp, STR_PTR(obj->message->http.info.request.method)));
924 if (obj->message->http.info.request.url) {
925 php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0);
926 UPDATE_PROP("requestUrl", ZVAL_STR(&tmp, php_http_cs2zs(url_str, url_len)));
927 } else {
928 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp));
929 }
930
931 break;
932
933 case PHP_HTTP_RESPONSE:
934 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp, obj->message->http.info.response.code));
935 UPDATE_PROP("responseStatus", ZVAL_STRING(&tmp, STR_PTR(obj->message->http.info.response.status)));
936 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp));
937 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp));
938 break;
939
940 case PHP_HTTP_NONE:
941 default:
942 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp, 0));
943 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp));
944 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp));
945 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp));
946 break;
947 }
948
949 UPDATE_PROP("headers",
950 array_init(&tmp);
951 array_copy(&obj->message->hdrs, Z_ARRVAL(tmp));
952 );
953
954 UPDATE_PROP("body",
955 if (obj->body) {
956 ZVAL_OBJECT(&tmp, &obj->body->zo, 1);
957 } else {
958 ZVAL_NULL(&tmp);
959 }
960 );
961
962 UPDATE_PROP("parentMessage",
963 if (obj->message->parent) {
964 ZVAL_OBJECT(&tmp, &obj->parent->zo, 1);
965 } else {
966 ZVAL_NULL(&tmp);
967 }
968 );
969
970 return props;
971 }
972
973 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___construct, 0, 0, 0)
974 ZEND_ARG_INFO(0, message)
975 ZEND_ARG_INFO(0, greedy)
976 ZEND_END_ARG_INFO();
977 static PHP_METHOD(HttpMessage, __construct)
978 {
979 zend_bool greedy = 1;
980 zval *zmessage = NULL;
981 php_http_message_t *msg = NULL;
982 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
983 zend_error_handling zeh;
984
985 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!b", &zmessage, &greedy), invalid_arg, return);
986
987 zend_replace_error_handling(EH_THROW, php_http_exception_bad_message_class_entry, &zeh);
988 if (zmessage && Z_TYPE_P(zmessage) == IS_RESOURCE) {
989 php_stream *s;
990 php_http_message_parser_t p;
991 zend_error_handling zeh;
992
993 zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh);
994 php_stream_from_zval(s, zmessage);
995 zend_restore_error_handling(&zeh);
996
997 if (s && php_http_message_parser_init(&p)) {
998 unsigned flags = (greedy ? PHP_HTTP_MESSAGE_PARSER_GREEDY : 0);
999 php_http_buffer_t buf;
1000
1001 php_http_buffer_init_ex(&buf, 0x1000, PHP_HTTP_BUFFER_INIT_PREALLOC);
1002 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse_stream(&p, &buf, s, flags, &msg)) {
1003 if (!EG(exception)) {
1004 php_http_throw(bad_message, "Could not parse message from stream", NULL);
1005 }
1006 }
1007 php_http_buffer_dtor(&buf);
1008 php_http_message_parser_dtor(&p);
1009 }
1010
1011 if (!msg && !EG(exception)) {
1012 php_http_throw(bad_message, "Empty message received from stream", NULL);
1013 }
1014 } else if (zmessage) {
1015 zend_string *zs_msg = zval_get_string(zmessage);
1016
1017 msg = php_http_message_parse(NULL, zs_msg->val, zs_msg->len, greedy);
1018
1019 if (!msg && !EG(exception)) {
1020 php_http_throw(bad_message, "Could not parse message: %.*s", MIN(25, zs_msg->len), zs_msg->val);
1021 }
1022 zend_string_release(zs_msg);
1023 }
1024
1025 if (msg) {
1026 php_http_message_dtor(obj->message);
1027 obj->message = msg;
1028 if (obj->message->parent) {
1029 obj->parent = php_http_message_object_new_ex(obj->zo.ce, obj->message->parent);
1030 }
1031 }
1032 zend_restore_error_handling(&zeh);
1033 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1034 }
1035
1036 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getBody, 0, 0, 0)
1037 ZEND_END_ARG_INFO();
1038 static PHP_METHOD(HttpMessage, getBody)
1039 {
1040 php_http_message_object_t *obj;
1041
1042 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1043
1044 obj = PHP_HTTP_OBJ(NULL, getThis());
1045 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1046
1047 if (!obj->body) {
1048 php_http_message_object_init_body_object(obj);
1049
1050 }
1051 if (obj->body) {
1052 RETVAL_OBJECT(&obj->body->zo, 1);
1053 }
1054 }
1055
1056 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setBody, 0, 0, 1)
1057 ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 0)
1058 ZEND_END_ARG_INFO();
1059 static PHP_METHOD(HttpMessage, setBody)
1060 {
1061 zval *zbody;
1062
1063 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zbody, php_http_message_body_class_entry)) {
1064 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1065
1066 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1067 php_http_message_object_prophandler_set_body(obj, zbody);
1068 }
1069 RETVAL_ZVAL_FAST(getThis());
1070 }
1071
1072 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addBody, 0, 0, 1)
1073 ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 0)
1074 ZEND_END_ARG_INFO();
1075 static PHP_METHOD(HttpMessage, addBody)
1076 {
1077 zval *new_body;
1078
1079 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &new_body, php_http_message_body_class_entry)) {
1080 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1081 php_http_message_body_object_t *new_obj = PHP_HTTP_OBJ(NULL, new_body);
1082
1083 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1084 php_http_message_body_to_callback(new_obj->body, (php_http_pass_callback_t) php_http_message_body_append, obj->message->body, 0, 0);
1085 }
1086 RETVAL_ZVAL_FAST(getThis());
1087 }
1088
1089 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeader, 0, 0, 1)
1090 ZEND_ARG_INFO(0, header)
1091 ZEND_ARG_INFO(0, into_class)
1092 ZEND_END_ARG_INFO();
1093 static PHP_METHOD(HttpMessage, getHeader)
1094 {
1095 char *header_str;
1096 size_t header_len;
1097 zend_class_entry *header_ce = NULL;
1098
1099 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|C!", &header_str, &header_len, &header_ce)) {
1100 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1101 zval *header;
1102
1103 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1104
1105 if ((header = php_http_message_header(obj->message, header_str, header_len))) {
1106 if (!header_ce) {
1107 RETURN_ZVAL(header, 1, 1);
1108 } else if (instanceof_function(header_ce, php_http_header_class_entry)) {
1109 zval argv[2];
1110
1111 ZVAL_STRINGL(&argv[0], header_str, header_len);
1112 ZVAL_COPY(&argv[1], header);
1113
1114 object_init_ex(return_value, header_ce);
1115 php_http_method_call(return_value, ZEND_STRL("__construct"), 2, argv, NULL);
1116
1117 zval_ptr_dtor(&argv[0]);
1118 zval_ptr_dtor(&argv[1]);
1119
1120 return;
1121 } else {
1122 php_error_docref(NULL, E_WARNING, "Class '%s' is not as descendant of http\\Header", header_ce->name->val);
1123 }
1124 }
1125 }
1126 RETURN_FALSE;
1127 }
1128
1129 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeaders, 0, 0, 0)
1130 ZEND_END_ARG_INFO();
1131 static PHP_METHOD(HttpMessage, getHeaders)
1132 {
1133 if (SUCCESS == zend_parse_parameters_none()) {
1134 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1135
1136 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1137
1138 array_init(return_value);
1139 array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
1140 }
1141 }
1142
1143 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeader, 0, 0, 1)
1144 ZEND_ARG_INFO(0, header)
1145 ZEND_ARG_INFO(0, value)
1146 ZEND_END_ARG_INFO();
1147 static PHP_METHOD(HttpMessage, setHeader)
1148 {
1149 zval *zvalue = NULL;
1150 char *name_str;
1151 size_t name_len;
1152
1153 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!", &name_str, &name_len, &zvalue)) {
1154 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1155 char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
1156
1157 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1158
1159 if (!zvalue) {
1160 zend_symtable_str_del(&obj->message->hdrs, name, name_len);
1161 } else {
1162 Z_TRY_ADDREF_P(zvalue);
1163 zend_symtable_str_update(&obj->message->hdrs, name, name_len, zvalue);
1164 }
1165 efree(name);
1166 }
1167 RETVAL_ZVAL_FAST(getThis());
1168 }
1169
1170 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeaders, 0, 0, 1)
1171 ZEND_ARG_ARRAY_INFO(0, headers, 1)
1172 ZEND_END_ARG_INFO();
1173 static PHP_METHOD(HttpMessage, setHeaders)
1174 {
1175 zval *new_headers = NULL;
1176
1177 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "a/!", &new_headers)) {
1178 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1179
1180 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1181
1182 zend_hash_clean(&obj->message->hdrs);
1183 if (new_headers) {
1184 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, 0, ARRAY_JOIN_PRETTIFY|ARRAY_JOIN_STRONLY);
1185 }
1186 }
1187 RETVAL_ZVAL_FAST(getThis());
1188 }
1189
1190 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader, 0, 0, 2)
1191 ZEND_ARG_INFO(0, header)
1192 ZEND_ARG_INFO(0, value)
1193 ZEND_END_ARG_INFO();
1194 static PHP_METHOD(HttpMessage, addHeader)
1195 {
1196 zval *zvalue;
1197 char *name_str;
1198 size_t name_len;
1199
1200 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name_str, &name_len, &zvalue)) {
1201 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1202 char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
1203 zval *header;
1204
1205 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1206
1207 Z_TRY_ADDREF_P(zvalue);
1208 if ((header = php_http_message_header(obj->message, name, name_len))) {
1209 convert_to_array(header);
1210 zend_hash_next_index_insert(Z_ARRVAL_P(header), zvalue);
1211 } else {
1212 zend_symtable_str_update(&obj->message->hdrs, name, name_len, zvalue);
1213 }
1214 efree(name);
1215 }
1216 RETVAL_ZVAL_FAST(getThis());
1217 }
1218
1219 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeaders, 0, 0, 1)
1220 ZEND_ARG_ARRAY_INFO(0, headers, 0)
1221 ZEND_ARG_INFO(0, append)
1222 ZEND_END_ARG_INFO();
1223 static PHP_METHOD(HttpMessage, addHeaders)
1224 {
1225 zval *new_headers;
1226 zend_bool append = 0;
1227
1228 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &new_headers, &append)) {
1229 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1230
1231 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1232
1233 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY);
1234 }
1235 RETVAL_ZVAL_FAST(getThis());
1236 }
1237
1238 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getType, 0, 0, 0)
1239 ZEND_END_ARG_INFO();
1240 static PHP_METHOD(HttpMessage, getType)
1241 {
1242 if (SUCCESS == zend_parse_parameters_none()) {
1243 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1244
1245 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1246
1247 RETURN_LONG(obj->message->type);
1248 }
1249 }
1250
1251 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setType, 0, 0, 1)
1252 ZEND_ARG_INFO(0, type)
1253 ZEND_END_ARG_INFO();
1254 static PHP_METHOD(HttpMessage, setType)
1255 {
1256 zend_long type;
1257
1258 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type)) {
1259 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1260
1261 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1262
1263 php_http_message_set_type(obj->message, type);
1264 }
1265 RETVAL_ZVAL_FAST(getThis());
1266 }
1267
1268 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getInfo, 0, 0, 0)
1269 ZEND_END_ARG_INFO();
1270 static PHP_METHOD(HttpMessage, getInfo)
1271 {
1272 if (SUCCESS == zend_parse_parameters_none()) {
1273 char *str, *tmp = NULL;
1274 size_t len;
1275 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1276
1277 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1278
1279 switch (obj->message->type) {
1280 case PHP_HTTP_REQUEST:
1281 len = spprintf(&str, 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, tmp, ""));
1282 PTR_FREE(tmp);
1283 break;
1284 case PHP_HTTP_RESPONSE:
1285 len = spprintf(&str, 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, tmp, ""));
1286 PTR_FREE(tmp);
1287 break;
1288 default:
1289 RETURN_NULL();
1290 break;
1291 }
1292
1293 RETVAL_STR(php_http_cs2zs(str, len));
1294 }
1295 }
1296
1297 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setInfo, 0, 0, 1)
1298 ZEND_ARG_INFO(0, http_info)
1299 ZEND_END_ARG_INFO();
1300 static PHP_METHOD(HttpMessage, setInfo)
1301 {
1302 char *str;
1303 size_t len;
1304 php_http_message_object_t *obj;
1305 php_http_info_t inf;
1306
1307 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &len), invalid_arg, return);
1308
1309 obj = PHP_HTTP_OBJ(NULL, getThis());
1310 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1311
1312 if (!php_http_info_parse(&inf, str)) {
1313 php_http_throw(bad_header, "Could not parse message info '%s'", str);
1314 return;
1315 }
1316
1317 php_http_message_set_info(obj->message, &inf);
1318 php_http_info_dtor(&inf);
1319
1320 RETVAL_ZVAL_FAST(getThis());
1321 }
1322
1323 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHttpVersion, 0, 0, 0)
1324 ZEND_END_ARG_INFO();
1325 static PHP_METHOD(HttpMessage, getHttpVersion)
1326 {
1327 if (SUCCESS == zend_parse_parameters_none()) {
1328 char *str;
1329 size_t len;
1330 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1331
1332 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1333
1334 php_http_version_to_string(&obj->message->http.version, &str, &len, NULL, NULL);
1335 RETURN_STR(php_http_cs2zs(str, len));
1336 }
1337 }
1338
1339 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHttpVersion, 0, 0, 1)
1340 ZEND_ARG_INFO(0, http_version)
1341 ZEND_END_ARG_INFO();
1342 static PHP_METHOD(HttpMessage, setHttpVersion)
1343 {
1344 char *v_str;
1345 size_t v_len;
1346 php_http_version_t version;
1347 php_http_message_object_t *obj;
1348
1349 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &v_str, &v_len), invalid_arg, return);
1350
1351 obj = PHP_HTTP_OBJ(NULL, getThis());
1352 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1353
1354 php_http_expect(php_http_version_parse(&version, v_str), unexpected_val, return);
1355
1356 obj->message->http.version = version;
1357
1358 RETVAL_ZVAL_FAST(getThis());
1359 }
1360
1361 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseCode, 0, 0, 0)
1362 ZEND_END_ARG_INFO();
1363 static PHP_METHOD(HttpMessage, getResponseCode)
1364 {
1365 if (SUCCESS == zend_parse_parameters_none()) {
1366 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1367
1368 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1369
1370 if (obj->message->type != PHP_HTTP_RESPONSE) {
1371 php_error_docref(NULL, E_WARNING, "http\\Message is not if type response");
1372 RETURN_FALSE;
1373 }
1374
1375 RETURN_LONG(obj->message->http.info.response.code);
1376 }
1377 }
1378
1379 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseCode, 0, 0, 1)
1380 ZEND_ARG_INFO(0, response_code)
1381 ZEND_ARG_INFO(0, strict)
1382 ZEND_END_ARG_INFO();
1383 static PHP_METHOD(HttpMessage, setResponseCode)
1384 {
1385 zend_long code;
1386 zend_bool strict = 1;
1387 php_http_message_object_t *obj;
1388
1389 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &code, &strict), invalid_arg, return);
1390
1391 obj = PHP_HTTP_OBJ(NULL, getThis());
1392 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1393
1394 if (obj->message->type != PHP_HTTP_RESPONSE) {
1395 php_http_throw(bad_method_call, "http\\Message is not of type response", NULL);
1396 return;
1397 }
1398
1399 if (strict && (code < 100 || code > 599)) {
1400 php_http_throw(invalid_arg, "Invalid response code (100-599): %ld", code);
1401 return;
1402 }
1403
1404 obj->message->http.info.response.code = code;
1405 PTR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(code)));
1406
1407 RETVAL_ZVAL_FAST(getThis());
1408 }
1409
1410 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseStatus, 0, 0, 0)
1411 ZEND_END_ARG_INFO();
1412 static PHP_METHOD(HttpMessage, getResponseStatus)
1413 {
1414 if (SUCCESS == zend_parse_parameters_none()) {
1415 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1416
1417 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1418
1419 if (obj->message->type != PHP_HTTP_RESPONSE) {
1420 php_error_docref(NULL, E_WARNING, "http\\Message is not of type response");
1421 }
1422
1423 if (obj->message->http.info.response.status) {
1424 RETURN_STRING(obj->message->http.info.response.status);
1425 } else {
1426 RETURN_EMPTY_STRING();
1427 }
1428 }
1429 }
1430
1431 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseStatus, 0, 0, 1)
1432 ZEND_ARG_INFO(0, response_status)
1433 ZEND_END_ARG_INFO();
1434 static PHP_METHOD(HttpMessage, setResponseStatus)
1435 {
1436 char *status;
1437 int status_len;
1438 php_http_message_object_t *obj;
1439
1440 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len), invalid_arg, return);
1441
1442 obj = PHP_HTTP_OBJ(NULL, getThis());
1443
1444 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1445
1446 if (obj->message->type != PHP_HTTP_RESPONSE) {
1447 php_http_throw(bad_method_call, "http\\Message is not of type response", NULL);
1448 }
1449
1450 PTR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
1451 RETVAL_ZVAL_FAST(getThis());
1452 }
1453
1454 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestMethod, 0, 0, 0)
1455 ZEND_END_ARG_INFO();
1456 static PHP_METHOD(HttpMessage, getRequestMethod)
1457 {
1458 if (SUCCESS == zend_parse_parameters_none()) {
1459 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1460
1461 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1462
1463 if (obj->message->type != PHP_HTTP_REQUEST) {
1464 php_error_docref(NULL, E_WARNING, "http\\Message is not of type request");
1465 RETURN_FALSE;
1466 }
1467
1468 if (obj->message->http.info.request.method) {
1469 RETURN_STRING(obj->message->http.info.request.method);
1470 } else {
1471 RETURN_EMPTY_STRING();
1472 }
1473 }
1474 }
1475
1476 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestMethod, 0, 0, 1)
1477 ZEND_ARG_INFO(0, request_method)
1478 ZEND_END_ARG_INFO();
1479 static PHP_METHOD(HttpMessage, setRequestMethod)
1480 {
1481 char *method;
1482 size_t method_len;
1483 php_http_message_object_t *obj;
1484
1485 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &method, &method_len), invalid_arg, return);
1486
1487 obj = PHP_HTTP_OBJ(NULL, getThis());
1488
1489 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1490
1491 if (obj->message->type != PHP_HTTP_REQUEST) {
1492 php_http_throw(bad_method_call, "http\\Message is not of type request", NULL);
1493 return;
1494 }
1495
1496 if (method_len < 1) {
1497 php_http_throw(invalid_arg, "Cannot set http\\Message's request method to an empty string", NULL);
1498 return;
1499 }
1500
1501 PTR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
1502 RETVAL_ZVAL_FAST(getThis());
1503 }
1504
1505 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestUrl, 0, 0, 0)
1506 ZEND_END_ARG_INFO();
1507 static PHP_METHOD(HttpMessage, getRequestUrl)
1508 {
1509 if (SUCCESS == zend_parse_parameters_none()) {
1510 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1511
1512 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1513
1514 if (obj->message->type != PHP_HTTP_REQUEST) {
1515 php_error_docref(NULL, E_WARNING, "http\\Message is not of type request");
1516 RETURN_FALSE;
1517 }
1518
1519 if (obj->message->http.info.request.url) {
1520 char *url_str;
1521 size_t url_len;
1522
1523 php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0);
1524 RETURN_STR(php_http_cs2zs(url_str, url_len));
1525 } else {
1526 RETURN_EMPTY_STRING();
1527 }
1528 }
1529 }
1530
1531 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestUrl, 0, 0, 1)
1532 ZEND_ARG_INFO(0, url)
1533 ZEND_END_ARG_INFO();
1534 static PHP_METHOD(HttpMessage, setRequestUrl)
1535 {
1536 zval *zurl;
1537 php_http_url_t *url;
1538 php_http_message_object_t *obj;
1539 zend_error_handling zeh;
1540
1541 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zurl), invalid_arg, return);
1542
1543 obj = PHP_HTTP_OBJ(NULL, getThis());
1544
1545 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1546
1547 if (obj->message->type != PHP_HTTP_REQUEST) {
1548 php_http_throw(bad_method_call, "http\\Message is not of type request", NULL);
1549 return;
1550 }
1551
1552 zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
1553 url = php_http_url_from_zval(zurl, ~0);
1554 zend_restore_error_handling(&zeh);
1555
1556 if (php_http_url_is_empty(url)) {
1557 php_http_url_free(&url);
1558 php_http_throw(invalid_arg, "Cannot set http\\Message's request url to an empty string", NULL);
1559 } else {
1560 PTR_SET(obj->message->http.info.request.url, url);
1561 }
1562
1563 RETVAL_ZVAL_FAST(getThis());
1564 }
1565
1566 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getParentMessage, 0, 0, 0)
1567 ZEND_END_ARG_INFO();
1568 static PHP_METHOD(HttpMessage, getParentMessage)
1569 {
1570 php_http_message_object_t *obj;
1571
1572 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1573
1574 obj = PHP_HTTP_OBJ(NULL, getThis());
1575 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1576
1577 if (!obj->message->parent) {
1578 php_http_throw(unexpected_val, "http\\Message has not parent message", NULL);
1579 return;
1580 }
1581
1582 RETVAL_OBJECT(&obj->parent->zo, 1);
1583 }
1584
1585 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___toString, 0, 0, 0)
1586 ZEND_END_ARG_INFO();
1587 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toString, 0, 0, 0)
1588 ZEND_ARG_INFO(0, include_parent)
1589 ZEND_END_ARG_INFO();
1590 static PHP_METHOD(HttpMessage, toString)
1591 {
1592 zend_bool include_parent = 0;
1593
1594 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &include_parent)) {
1595 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1596 char *string;
1597 size_t length;
1598
1599 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1600
1601 if (include_parent) {
1602 php_http_message_serialize(obj->message, &string, &length);
1603 } else {
1604 php_http_message_to_string(obj->message, &string, &length);
1605 }
1606 if (string) {
1607 RETURN_STR(php_http_cs2zs(string, length));
1608 }
1609 }
1610 RETURN_EMPTY_STRING();
1611 }
1612
1613 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toStream, 0, 0, 1)
1614 ZEND_ARG_INFO(0, stream)
1615 ZEND_END_ARG_INFO();
1616 static PHP_METHOD(HttpMessage, toStream)
1617 {
1618 zval *zstream;
1619
1620 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream)) {
1621 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1622 php_stream *s;
1623
1624 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1625
1626 php_stream_from_zval(s, zstream);
1627 php_http_message_to_callback(obj->message, (php_http_pass_callback_t) _php_stream_write, s);
1628 }
1629 }
1630
1631 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toCallback, 0, 0, 1)
1632 ZEND_ARG_INFO(0, callback)
1633 ZEND_END_ARG_INFO();
1634 static PHP_METHOD(HttpMessage, toCallback)
1635 {
1636 php_http_pass_fcall_arg_t fcd;
1637
1638 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fcd.fci, &fcd.fcc)) {
1639 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1640
1641 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1642
1643 ZVAL_COPY(&fcd.fcz, getThis());
1644 php_http_message_to_callback(obj->message, php_http_pass_fcall_callback, &fcd);
1645 zend_fcall_info_args_clear(&fcd.fci, 1);
1646 zval_ptr_dtor(&fcd.fcz);
1647
1648 RETURN_ZVAL_FAST(getThis());
1649 }
1650 }
1651
1652 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_serialize, 0, 0, 0)
1653 ZEND_END_ARG_INFO();
1654 static PHP_METHOD(HttpMessage, serialize)
1655 {
1656 if (SUCCESS == zend_parse_parameters_none()) {
1657 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1658 char *string;
1659 size_t length;
1660
1661 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1662
1663 php_http_message_serialize(obj->message, &string, &length);
1664 RETURN_STR(php_http_cs2zs(string, length));
1665 }
1666 RETURN_EMPTY_STRING();
1667 }
1668
1669 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_unserialize, 0, 0, 1)
1670 ZEND_ARG_INFO(0, serialized)
1671 ZEND_END_ARG_INFO();
1672 static PHP_METHOD(HttpMessage, unserialize)
1673 {
1674 size_t length;
1675 char *serialized;
1676
1677 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &length)) {
1678 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1679 php_http_message_t *msg;
1680
1681 if (obj->message) {
1682 /* do not free recursively */
1683 php_http_message_dtor(obj->message);
1684 efree(obj->message);
1685 }
1686 if ((msg = php_http_message_parse(NULL, serialized, length, 1))) {
1687 obj->message = msg;
1688 } else {
1689 obj->message = php_http_message_init(NULL, 0, NULL);
1690 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not unserialize http\\Message");
1691 }
1692 }
1693 }
1694
1695 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_detach, 0, 0, 0)
1696 ZEND_END_ARG_INFO();
1697 static PHP_METHOD(HttpMessage, detach)
1698 {
1699 php_http_message_object_t *obj, *new_obj;
1700 php_http_message_t *msg_cpy;
1701
1702 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1703
1704 obj = PHP_HTTP_OBJ(NULL, getThis());
1705 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1706
1707 msg_cpy = php_http_message_copy_ex(obj->message, NULL, 0);
1708 new_obj = php_http_message_object_new_ex(obj->zo.ce, msg_cpy);
1709
1710 RETVAL_OBJ(&new_obj->zo);
1711 }
1712
1713 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_prepend, 0, 0, 1)
1714 ZEND_ARG_OBJ_INFO(0, message, http\\Message, 0)
1715 ZEND_ARG_INFO(0, top)
1716 ZEND_END_ARG_INFO();
1717 static PHP_METHOD(HttpMessage, prepend)
1718 {
1719 zval *prepend;
1720 zend_bool top = 1;
1721 php_http_message_t *msg[2];
1722 php_http_message_object_t *obj, *prepend_obj;
1723
1724 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &prepend, php_http_message_class_entry, &top), invalid_arg, return);
1725
1726 obj = PHP_HTTP_OBJ(NULL, getThis());
1727 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1728 prepend_obj = PHP_HTTP_OBJ(NULL, prepend);
1729 PHP_HTTP_MESSAGE_OBJECT_INIT(prepend_obj);
1730
1731 /* safety check */
1732 for (msg[0] = obj->message; msg[0]; msg[0] = msg[0]->parent) {
1733 for (msg[1] = prepend_obj->message; msg[1]; msg[1] = msg[1]->parent) {
1734 if (msg[0] == msg[1]) {
1735 php_http_throw(unexpected_val, "Cannot prepend a message located within the same message chain", NULL);
1736 return;
1737 }
1738 }
1739 }
1740
1741 php_http_message_object_prepend(getThis(), prepend, top);
1742 RETURN_ZVAL_FAST(getThis());
1743 }
1744
1745 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_reverse, 0, 0, 0)
1746 ZEND_END_ARG_INFO();
1747 static PHP_METHOD(HttpMessage, reverse)
1748 {
1749 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1750
1751 php_http_message_object_reverse(getThis(), return_value);
1752 }
1753
1754 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_isMultipart, 0, 0, 0)
1755 ZEND_ARG_INFO(1, boundary)
1756 ZEND_END_ARG_INFO();
1757 static PHP_METHOD(HttpMessage, isMultipart)
1758 {
1759 zval *zboundary = NULL;
1760
1761 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!", &zboundary)) {
1762 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1763 char *boundary = NULL;
1764
1765 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1766
1767 if (php_http_message_is_multipart(obj->message, zboundary ? &boundary : NULL)) {
1768 RETVAL_TRUE;
1769 } else {
1770 RETVAL_FALSE;
1771 }
1772
1773 if (zboundary && boundary) {
1774 ZVAL_DEREF(zboundary);
1775 zval_dtor(zboundary);
1776 ZVAL_STR(zboundary, php_http_cs2zs(boundary, strlen(boundary)));
1777 }
1778 }
1779 }
1780
1781 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_splitMultipartBody, 0, 0, 0)
1782 ZEND_END_ARG_INFO();
1783 static PHP_METHOD(HttpMessage, splitMultipartBody)
1784 {
1785 php_http_message_object_t *obj;
1786 php_http_message_t *msg;
1787 char *boundary = NULL;
1788
1789 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1790
1791 obj = PHP_HTTP_OBJ(NULL, getThis());
1792 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1793
1794 if (!php_http_message_is_multipart(obj->message, &boundary)) {
1795 php_http_throw(bad_method_call, "http\\Message is not a multipart message", NULL);
1796 return;
1797 }
1798
1799 php_http_expect(msg = php_http_message_body_split(obj->message->body, boundary), bad_message, return);
1800
1801 PTR_FREE(boundary);
1802
1803 RETURN_OBJ(&php_http_message_object_new_ex(obj->zo.ce, msg)->zo);
1804 }
1805
1806 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_count, 0, 0, 0)
1807 ZEND_END_ARG_INFO();
1808 static PHP_METHOD(HttpMessage, count)
1809 {
1810 zend_long count_mode = -1;
1811
1812 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &count_mode)) {
1813 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1814
1815 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1816
1817 RETURN_LONG(php_http_message_count(obj->message));
1818 }
1819 }
1820
1821 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_rewind, 0, 0, 0)
1822 ZEND_END_ARG_INFO();
1823 static PHP_METHOD(HttpMessage, rewind)
1824 {
1825 if (SUCCESS == zend_parse_parameters_none()) {
1826 zval *zobj = getThis();
1827 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1828
1829 if (!Z_ISUNDEF(obj->iterator)) {
1830 zval_ptr_dtor(&obj->iterator);
1831 }
1832 ZVAL_COPY(&obj->iterator, zobj);
1833 }
1834 }
1835
1836 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_valid, 0, 0, 0)
1837 ZEND_END_ARG_INFO();
1838 static PHP_METHOD(HttpMessage, valid)
1839 {
1840 if (SUCCESS == zend_parse_parameters_none()) {
1841 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1842
1843 RETURN_BOOL(!Z_ISUNDEF(obj->iterator));
1844 }
1845 }
1846
1847 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_next, 0, 0, 0)
1848 ZEND_END_ARG_INFO();
1849 static PHP_METHOD(HttpMessage, next)
1850 {
1851 if (SUCCESS == zend_parse_parameters_none()) {
1852 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1853
1854 if (!Z_ISUNDEF(obj->iterator)) {
1855 php_http_message_object_t *itr = PHP_HTTP_OBJ(NULL, &obj->iterator);
1856
1857 if (itr->parent) {
1858 zval tmp;
1859
1860 ZVAL_OBJECT(&tmp, &itr->parent->zo, 1);
1861 zval_ptr_dtor(&obj->iterator);
1862 obj->iterator = tmp;
1863 } else {
1864 zval_ptr_dtor(&obj->iterator);
1865 ZVAL_UNDEF(&obj->iterator);
1866 }
1867 }
1868 }
1869 }
1870
1871 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_key, 0, 0, 0)
1872 ZEND_END_ARG_INFO();
1873 static PHP_METHOD(HttpMessage, key)
1874 {
1875 if (SUCCESS == zend_parse_parameters_none()) {
1876 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1877
1878 RETURN_LONG(Z_ISUNDEF(obj->iterator) ? 0 : Z_OBJ_HANDLE(obj->iterator));
1879 }
1880 }
1881
1882 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_current, 0, 0, 0)
1883 ZEND_END_ARG_INFO();
1884 static PHP_METHOD(HttpMessage, current)
1885 {
1886 if (SUCCESS == zend_parse_parameters_none()) {
1887 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1888
1889 if (!Z_ISUNDEF(obj->iterator)) {
1890 RETURN_ZVAL_FAST(&obj->iterator);
1891 }
1892 }
1893 }
1894
1895 static zend_function_entry php_http_message_methods[] = {
1896 PHP_ME(HttpMessage, __construct, ai_HttpMessage___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1897 PHP_ME(HttpMessage, getBody, ai_HttpMessage_getBody, ZEND_ACC_PUBLIC)
1898 PHP_ME(HttpMessage, setBody, ai_HttpMessage_setBody, ZEND_ACC_PUBLIC)
1899 PHP_ME(HttpMessage, addBody, ai_HttpMessage_addBody, ZEND_ACC_PUBLIC)
1900 PHP_ME(HttpMessage, getHeader, ai_HttpMessage_getHeader, ZEND_ACC_PUBLIC)
1901 PHP_ME(HttpMessage, setHeader, ai_HttpMessage_setHeader, ZEND_ACC_PUBLIC)
1902 PHP_ME(HttpMessage, addHeader, ai_HttpMessage_addHeader, ZEND_ACC_PUBLIC)
1903 PHP_ME(HttpMessage, getHeaders, ai_HttpMessage_getHeaders, ZEND_ACC_PUBLIC)
1904 PHP_ME(HttpMessage, setHeaders, ai_HttpMessage_setHeaders, ZEND_ACC_PUBLIC)
1905 PHP_ME(HttpMessage, addHeaders, ai_HttpMessage_addHeaders, ZEND_ACC_PUBLIC)
1906 PHP_ME(HttpMessage, getType, ai_HttpMessage_getType, ZEND_ACC_PUBLIC)
1907 PHP_ME(HttpMessage, setType, ai_HttpMessage_setType, ZEND_ACC_PUBLIC)
1908 PHP_ME(HttpMessage, getInfo, ai_HttpMessage_getInfo, ZEND_ACC_PUBLIC)
1909 PHP_ME(HttpMessage, setInfo, ai_HttpMessage_setInfo, ZEND_ACC_PUBLIC)
1910 PHP_ME(HttpMessage, getResponseCode, ai_HttpMessage_getResponseCode, ZEND_ACC_PUBLIC)
1911 PHP_ME(HttpMessage, setResponseCode, ai_HttpMessage_setResponseCode, ZEND_ACC_PUBLIC)
1912 PHP_ME(HttpMessage, getResponseStatus, ai_HttpMessage_getResponseStatus, ZEND_ACC_PUBLIC)
1913 PHP_ME(HttpMessage, setResponseStatus, ai_HttpMessage_setResponseStatus, ZEND_ACC_PUBLIC)
1914 PHP_ME(HttpMessage, getRequestMethod, ai_HttpMessage_getRequestMethod, ZEND_ACC_PUBLIC)
1915 PHP_ME(HttpMessage, setRequestMethod, ai_HttpMessage_setRequestMethod, ZEND_ACC_PUBLIC)
1916 PHP_ME(HttpMessage, getRequestUrl, ai_HttpMessage_getRequestUrl, ZEND_ACC_PUBLIC)
1917 PHP_ME(HttpMessage, setRequestUrl, ai_HttpMessage_setRequestUrl, ZEND_ACC_PUBLIC)
1918 PHP_ME(HttpMessage, getHttpVersion, ai_HttpMessage_getHttpVersion, ZEND_ACC_PUBLIC)
1919 PHP_ME(HttpMessage, setHttpVersion, ai_HttpMessage_setHttpVersion, ZEND_ACC_PUBLIC)
1920 PHP_ME(HttpMessage, getParentMessage, ai_HttpMessage_getParentMessage, ZEND_ACC_PUBLIC)
1921 PHP_ME(HttpMessage, toString, ai_HttpMessage_toString, ZEND_ACC_PUBLIC)
1922 PHP_ME(HttpMessage, toCallback, ai_HttpMessage_toCallback, ZEND_ACC_PUBLIC)
1923 PHP_ME(HttpMessage, toStream, ai_HttpMessage_toStream, ZEND_ACC_PUBLIC)
1924
1925 /* implements Countable */
1926 PHP_ME(HttpMessage, count, ai_HttpMessage_count, ZEND_ACC_PUBLIC)
1927
1928 /* implements Serializable */
1929 PHP_ME(HttpMessage, serialize, ai_HttpMessage_serialize, ZEND_ACC_PUBLIC)
1930 PHP_ME(HttpMessage, unserialize, ai_HttpMessage_unserialize, ZEND_ACC_PUBLIC)
1931
1932 /* implements Iterator */
1933 PHP_ME(HttpMessage, rewind, ai_HttpMessage_rewind, ZEND_ACC_PUBLIC)
1934 PHP_ME(HttpMessage, valid, ai_HttpMessage_valid, ZEND_ACC_PUBLIC)
1935 PHP_ME(HttpMessage, current, ai_HttpMessage_current, ZEND_ACC_PUBLIC)
1936 PHP_ME(HttpMessage, key, ai_HttpMessage_key, ZEND_ACC_PUBLIC)
1937 PHP_ME(HttpMessage, next, ai_HttpMessage_next, ZEND_ACC_PUBLIC)
1938
1939 ZEND_MALIAS(HttpMessage, __toString, toString, ai_HttpMessage___toString, ZEND_ACC_PUBLIC)
1940
1941 PHP_ME(HttpMessage, detach, ai_HttpMessage_detach, ZEND_ACC_PUBLIC)
1942 PHP_ME(HttpMessage, prepend, ai_HttpMessage_prepend, ZEND_ACC_PUBLIC)
1943 PHP_ME(HttpMessage, reverse, ai_HttpMessage_reverse, ZEND_ACC_PUBLIC)
1944
1945 PHP_ME(HttpMessage, isMultipart, ai_HttpMessage_isMultipart, ZEND_ACC_PUBLIC)
1946 PHP_ME(HttpMessage, splitMultipartBody, ai_HttpMessage_splitMultipartBody, ZEND_ACC_PUBLIC)
1947
1948 EMPTY_FUNCTION_ENTRY
1949 };
1950
1951 zend_class_entry *php_http_message_class_entry;
1952
1953 PHP_MINIT_FUNCTION(http_message)
1954 {
1955 zend_class_entry ce = {0};
1956
1957 INIT_NS_CLASS_ENTRY(ce, "http", "Message", php_http_message_methods);
1958 php_http_message_class_entry = zend_register_internal_class(&ce);
1959 php_http_message_class_entry->create_object = php_http_message_object_new;
1960 memcpy(&php_http_message_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1961 php_http_message_object_handlers.offset = XtOffsetOf(php_http_message_object_t, zo);
1962 php_http_message_object_handlers.clone_obj = php_http_message_object_clone;
1963 php_http_message_object_handlers.free_obj = php_http_message_object_free;
1964 php_http_message_object_handlers.read_property = php_http_message_object_read_prop;
1965 php_http_message_object_handlers.write_property = php_http_message_object_write_prop;
1966 php_http_message_object_handlers.get_properties = php_http_message_object_get_props;
1967 php_http_message_object_handlers.get_property_ptr_ptr = NULL;
1968
1969 zend_class_implements(php_http_message_class_entry, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
1970
1971 zend_hash_init(&php_http_message_object_prophandlers, 9, NULL, php_http_message_object_prophandler_hash_dtor, 1);
1972 zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("type"), PHP_HTTP_NONE, ZEND_ACC_PROTECTED);
1973 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type, php_http_message_object_prophandler_set_type);
1974 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("body"), ZEND_ACC_PROTECTED);
1975 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body, php_http_message_object_prophandler_set_body);
1976 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED);
1977 php_http_message_object_add_prophandler(ZEND_STRL("requestMethod"), php_http_message_object_prophandler_get_request_method, php_http_message_object_prophandler_set_request_method);
1978 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED);
1979 php_http_message_object_add_prophandler(ZEND_STRL("requestUrl"), php_http_message_object_prophandler_get_request_url, php_http_message_object_prophandler_set_request_url);
1980 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED);
1981 php_http_message_object_add_prophandler(ZEND_STRL("responseStatus"), php_http_message_object_prophandler_get_response_status, php_http_message_object_prophandler_set_response_status);
1982 zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED);
1983 php_http_message_object_add_prophandler(ZEND_STRL("responseCode"), php_http_message_object_prophandler_get_response_code, php_http_message_object_prophandler_set_response_code);
1984 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED);
1985 php_http_message_object_add_prophandler(ZEND_STRL("httpVersion"), php_http_message_object_prophandler_get_http_version, php_http_message_object_prophandler_set_http_version);
1986 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("headers"), ZEND_ACC_PROTECTED);
1987 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers, php_http_message_object_prophandler_set_headers);
1988 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED);
1989 php_http_message_object_add_prophandler(ZEND_STRL("parentMessage"), php_http_message_object_prophandler_get_parent_message, php_http_message_object_prophandler_set_parent_message);
1990
1991 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE);
1992 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST);
1993 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE);
1994
1995 return SUCCESS;
1996 }
1997
1998 PHP_MSHUTDOWN_FUNCTION(http_message)
1999 {
2000 zend_hash_destroy(&php_http_message_object_prophandlers);
2001
2002 return SUCCESS;
2003 }
2004
2005 /*
2006 * Local variables:
2007 * tab-width: 4
2008 * c-basic-offset: 4
2009 * End:
2010 * vim600: noet sw=4 ts=4 fdm=marker
2011 * vim<600: noet sw=4 ts=4
2012 */
2013