flush WIP
[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 TSRMLS_CC);
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 typedef void (*php_http_message_object_prophandler_func_t)(php_http_message_object_t *o, zval *v);
502
503 typedef struct php_http_message_object_prophandler {
504 php_http_message_object_prophandler_func_t read;
505 php_http_message_object_prophandler_func_t write;
506 } php_http_message_object_prophandler_t;
507
508 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) {
509 php_http_message_object_prophandler_t h = { read, write };
510 if (!zend_hash_str_add_mem(&php_http_message_object_prophandlers, prop_str, prop_len, (void *) &h, sizeof(h))) {
511 return FAILURE;
512 }
513 return SUCCESS;
514 }
515 static php_http_message_object_prophandler_t *php_http_message_object_get_prophandler(zend_string *name_str) {
516 return zend_hash_str_find_ptr(&php_http_message_object_prophandlers, name_str->val, name_str->len);
517 }
518 static void php_http_message_object_prophandler_get_type(php_http_message_object_t *obj, zval *return_value) {
519 RETVAL_LONG(obj->message->type);
520 }
521 static void php_http_message_object_prophandler_set_type(php_http_message_object_t *obj, zval *value) {
522 php_http_message_set_type(obj->message, zval_get_long(value));
523 }
524 static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t *obj, zval *return_value) {
525 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message) && obj->message->http.info.request.method) {
526 RETVAL_STRING(obj->message->http.info.request.method);
527 } else {
528 RETVAL_NULL();
529 }
530 }
531 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t *obj, zval *value) {
532 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
533 zend_string *zs = zval_get_string(value);
534 PTR_SET(obj->message->http.info.request.method, estrndup(zs->val, zs->len));
535 zend_string_release(zs);
536 }
537 }
538 static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t *obj, zval *return_value) {
539 char *url_str;
540 size_t url_len;
541
542 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)) {
543 RETVAL_STR(php_http_cs2zs(url_str, url_len));
544 } else {
545 RETVAL_NULL();
546 }
547 }
548 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t *obj, zval *value) {
549 if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
550 PTR_SET(obj->message->http.info.request.url, php_http_url_from_zval(value, ~0));
551 }
552 }
553 static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t *obj, zval *return_value) {
554 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message) && obj->message->http.info.response.status) {
555 RETVAL_STRING(obj->message->http.info.response.status);
556 } else {
557 RETVAL_NULL();
558 }
559 }
560 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t *obj, zval *value) {
561 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
562 zend_string *zs = zval_get_string(value);
563 PTR_SET(obj->message->http.info.response.status, estrndup(zs->val, zs->len));
564 zend_string_release(zs);
565 }
566 }
567 static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t *obj, zval *return_value) {
568 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
569 RETVAL_LONG(obj->message->http.info.response.code);
570 } else {
571 RETVAL_NULL();
572 }
573 }
574 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t *obj, zval *value) {
575 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
576 obj->message->http.info.response.code = zval_get_long(value);
577 PTR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(obj->message->http.info.response.code)));
578 }
579 }
580 static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t *obj, zval *return_value) {
581 char *version_str;
582 size_t version_len;
583
584 php_http_version_to_string(&obj->message->http.version, &version_str, &version_len, NULL, NULL);
585 RETVAL_STR(php_http_cs2zs(version_str, version_len));
586 }
587 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t *obj, zval *value) {
588 zend_string *zs = zval_get_string(value);
589 php_http_version_parse(&obj->message->http.version, zs->val);
590 zend_string_release(zs);
591 }
592 static void php_http_message_object_prophandler_get_headers(php_http_message_object_t *obj, zval *return_value ) {
593 array_init(return_value);
594 array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
595 }
596 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t *obj, zval *value) {
597 HashTable *headers;
598 zval *orig_value = value;
599
600 if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) {
601 convert_to_array_ex(value);
602 } else {
603 headers = HASH_OF(value);
604 }
605
606 zend_hash_clean(&obj->message->hdrs);
607 array_copy(headers, &obj->message->hdrs);
608
609 if (orig_value != value) {
610 zval_ptr_dtor(value);
611 }
612 }
613 static void php_http_message_object_prophandler_get_body(php_http_message_object_t *obj, zval *return_value) {
614 if (obj->body) {
615 RETVAL_OBJECT(&obj->body->zo, 1);
616 } else {
617 RETVAL_NULL();
618 }
619 }
620 static void php_http_message_object_prophandler_set_body(php_http_message_object_t *obj, zval *value) {
621 php_http_message_object_set_body(obj, value);
622 }
623 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t *obj, zval *return_value) {
624 if (obj->message->parent) {
625 RETVAL_OBJECT(&obj->parent->zo, 1);
626 } else {
627 RETVAL_NULL();
628 }
629 }
630 static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t *obj, zval *value) {
631 if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_message_class_entry)) {
632 php_http_message_object_t *parent_obj = PHP_HTTP_OBJ(NULL, value);
633
634 if (obj->message->parent) {
635 zend_objects_store_del(&obj->parent->zo);
636 }
637 Z_ADDREF_P(value);
638 obj->parent = parent_obj;
639 obj->message->parent = parent_obj->message;
640 }
641 }
642
643 #define PHP_HTTP_MESSAGE_OBJECT_INIT(obj) \
644 do { \
645 if (!obj->message) { \
646 obj->message = php_http_message_init(NULL, 0, NULL); \
647 } \
648 } while(0)
649
650
651 void php_http_message_object_reverse(zval *zmsg, zval *return_value)
652 {
653 size_t i;
654 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, zmsg);
655
656 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
657
658 /* count */
659 i = php_http_message_count(obj->message);
660
661 if (i > 1) {
662 php_http_message_object_t **objects;
663 int last;
664
665 objects = ecalloc(i, sizeof(**objects));
666
667 /* we are the first message */
668 objects[0] = obj;
669
670 /* fetch parents */
671 for (i = 1; obj->parent; ++i) {
672 objects[i] = obj = obj->parent;
673 }
674
675 /* reorder parents */
676 for (last = --i; i; --i) {
677 objects[i]->message->parent = objects[i-1]->message;
678 objects[i]->parent = objects[i-1];
679 }
680
681 objects[0]->message->parent = NULL;
682 objects[0]->parent = NULL;
683
684 /* add ref, because we previously have not been a parent message */
685 Z_ADDREF_P(zmsg);
686 RETVAL_OBJ(&objects[last]->zo);
687
688 efree(objects);
689 } else {
690 RETURN_ZVAL_FAST(zmsg);
691 }
692 }
693
694 void php_http_message_object_prepend(zval *this_ptr, zval *prepend, zend_bool top)
695 {
696 php_http_message_t *save_parent_msg = NULL;
697 php_http_message_object_t *save_parent_obj = NULL, *obj = PHP_HTTP_OBJ(NULL, this_ptr);
698 php_http_message_object_t *prepend_obj = PHP_HTTP_OBJ(NULL, prepend);
699
700 if (!top) {
701 save_parent_obj = obj->parent;
702 save_parent_msg = obj->message->parent;
703 } else {
704 /* iterate to the most parent object */
705 while (obj->parent) {
706 obj = obj->parent;
707 }
708 }
709
710 /* prepend */
711 obj->parent = prepend_obj;
712 obj->message->parent = prepend_obj->message;
713
714 /* add ref */
715 Z_ADDREF_P(prepend);
716
717 if (!top) {
718 prepend_obj->parent = save_parent_obj;
719 prepend_obj->message->parent = save_parent_msg;
720 }
721 }
722
723 ZEND_RESULT_CODE php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval *zbody)
724 {
725 php_stream *s;
726 zend_string *body_str;
727 php_http_message_body_t *body;
728 php_http_message_body_object_t *body_obj;
729
730 switch (Z_TYPE_P(zbody)) {
731 case IS_RESOURCE:
732 php_stream_from_zval_no_verify(s, zbody);
733 if (!s) {
734 php_http_throw(unexpected_val, "The stream is not a valid resource", NULL);
735 return FAILURE;
736 }
737
738 is_resource:
739
740 body = php_http_message_body_init(NULL, s);
741 if (!(body_obj = php_http_message_body_object_new_ex(php_http_message_body_class_entry, body))) {
742 php_http_message_body_free(&body);
743 return FAILURE;
744 }
745 break;
746
747 case IS_OBJECT:
748 if (instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry)) {
749 Z_ADDREF_P(zbody);
750 body_obj = PHP_HTTP_OBJ(NULL, zbody);
751 break;
752 }
753 /* no break */
754
755 default:
756 body_str = zval_get_string(zbody);
757 s = php_stream_temp_new();
758 php_stream_write(s, body_str->val, body_str->len);
759 zend_string_release(body_str);
760 goto is_resource;
761
762 }
763
764 if (!body_obj->body) {
765 body_obj->body = php_http_message_body_init(NULL, NULL);
766 }
767 if (msg_obj->body) {
768 zend_objects_store_del(&msg_obj->body->zo);
769 }
770 if (msg_obj->message) {
771 php_http_message_body_free(&msg_obj->message->body);
772 msg_obj->message->body = php_http_message_body_init(&body_obj->body, NULL);
773 } else {
774 msg_obj->message = php_http_message_init(NULL, 0, php_http_message_body_init(&body_obj->body, NULL));
775 }
776 msg_obj->body = body_obj;
777
778 return SUCCESS;
779 }
780
781 ZEND_RESULT_CODE php_http_message_object_init_body_object(php_http_message_object_t *obj)
782 {
783 php_http_message_body_addref(obj->message->body);
784 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);
785 }
786
787 zend_object *php_http_message_object_new(zend_class_entry *ce)
788 {
789 return &php_http_message_object_new_ex(ce, NULL)->zo;
790 }
791
792 php_http_message_object_t *php_http_message_object_new_ex(zend_class_entry *ce, php_http_message_t *msg)
793 {
794 php_http_message_object_t *o;
795
796 o = ecalloc(1, sizeof(php_http_message_object_t) + (ce->default_properties_count - 1) * sizeof(zval));
797 zend_object_std_init(&o->zo, ce);
798 object_properties_init(&o->zo, ce);
799
800 if (msg) {
801 o->message = msg;
802 if (msg->parent) {
803 o->parent = php_http_message_object_new_ex(ce, msg->parent);
804 }
805 o->body = php_http_message_body_object_new_ex(php_http_message_body_class_entry, php_http_message_body_init(&msg->body, NULL));
806 }
807
808 o->zo.handlers = &php_http_message_object_handlers;
809
810 return o;
811 }
812
813 zend_object *php_http_message_object_clone(zval *this_ptr TSRMLS_DC)
814 {
815 php_http_message_object_t *new_obj = NULL;
816 php_http_message_object_t *old_obj = PHP_HTTP_OBJ(NULL, this_ptr);
817
818 new_obj = php_http_message_object_new_ex(old_obj->zo.ce, php_http_message_copy(old_obj->message, NULL));
819 zend_objects_clone_members(&new_obj->zo, &old_obj->zo);
820
821 return &new_obj->zo;
822 }
823
824 void php_http_message_object_free(zend_object *object)
825 {
826 php_http_message_object_t *o = PHP_HTTP_OBJ(object, NULL);
827
828 if (!Z_ISUNDEF(o->iterator)) {
829 zval_ptr_dtor(&o->iterator);
830 ZVAL_UNDEF(&o->iterator);
831 }
832 if (o->message) {
833 /* do NOT free recursivly */
834 php_http_message_dtor(o->message);
835 efree(o->message);
836 o->message = NULL;
837 }
838 if (o->parent) {
839 zend_objects_store_del(&o->parent->zo);
840 o->parent = NULL;
841 }
842 if (o->body) {
843 zend_objects_store_del(&o->body->zo);
844 o->body = NULL;
845 }
846 zend_object_std_dtor(object);
847 }
848
849 static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *return_value)
850 {
851 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
852 php_http_message_object_prophandler_t *handler;
853 zend_string *member_name = zval_get_string(member);
854
855 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
856
857 if ((handler = php_http_message_object_get_prophandler(member_name))) {
858 if (type == BP_VAR_R) {
859 handler->read(obj, return_value);
860 } else {
861 php_property_proxy_t *proxy = php_property_proxy_init(object, member_name);
862 RETVAL_OBJ(&php_property_proxy_object_new_ex(php_property_proxy_get_class_entry(), proxy)->zo);
863 }
864 } else {
865 zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, return_value);
866 }
867
868 zend_string_release(member_name);
869
870 return return_value;
871 }
872
873 static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot)
874 {
875 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
876 php_http_message_object_prophandler_t *handler;
877 zend_string *member_name = zval_get_string(member);
878
879 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
880
881 if ((handler = php_http_message_object_get_prophandler(member_name))) {
882 handler->write(obj, value);
883 } else {
884 zend_get_std_object_handlers()->write_property(object, member, value, cache_slot);
885 }
886
887 zend_string_release(member_name);
888 }
889
890 static HashTable *php_http_message_object_get_props(zval *object)
891 {
892 zval tmp;
893 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
894 HashTable *props = zend_get_std_object_handlers()->get_properties(object);
895 char *ver_str, *url_str = NULL;
896 size_t ver_len, url_len = 0;
897
898 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
899
900 #define UPDATE_PROP(name_str, action_with_tmp) \
901 do { \
902 zend_property_info *pi; \
903 if ((pi = zend_hash_str_find_ptr(&obj->zo.ce->properties_info, name_str, lenof(name_str)))) { \
904 action_with_tmp; \
905 zend_hash_update(props, pi->name, &tmp); \
906 } \
907 } while(0)
908
909 UPDATE_PROP("type", ZVAL_LONG(&tmp, obj->message->type));
910
911 ver_len = spprintf(&ver_str, 0, "%u.%u", obj->message->http.version.major, obj->message->http.version.minor);
912 UPDATE_PROP("httpVersion", ZVAL_STR(&tmp, php_http_cs2zs(ver_str, ver_len)));
913
914 switch (obj->message->type) {
915 case PHP_HTTP_REQUEST:
916 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp, 0));
917 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp));
918 UPDATE_PROP("requestMethod", ZVAL_STRING(&tmp, STR_PTR(obj->message->http.info.request.method)));
919 if (obj->message->http.info.request.url) {
920 php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0);
921 UPDATE_PROP("requestUrl", ZVAL_STR(&tmp, php_http_cs2zs(url_str, url_len)));
922 } else {
923 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp));
924 }
925
926 break;
927
928 case PHP_HTTP_RESPONSE:
929 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp, obj->message->http.info.response.code));
930 UPDATE_PROP("responseStatus", ZVAL_STRING(&tmp, STR_PTR(obj->message->http.info.response.status)));
931 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp));
932 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp));
933 break;
934
935 case PHP_HTTP_NONE:
936 default:
937 UPDATE_PROP("responseCode", ZVAL_LONG(&tmp, 0));
938 UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp));
939 UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp));
940 UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp));
941 break;
942 }
943
944 UPDATE_PROP("headers",
945 array_init(&tmp);
946 array_copy(&obj->message->hdrs, Z_ARRVAL(tmp));
947 );
948
949 UPDATE_PROP("body",
950 if (obj->body) {
951 ZVAL_OBJECT(&tmp, &obj->body->zo, 1);
952 } else {
953 ZVAL_NULL(&tmp);
954 }
955 );
956
957 UPDATE_PROP("parentMessage",
958 if (obj->message->parent) {
959 ZVAL_OBJECT(&tmp, &obj->parent->zo, 1);
960 } else {
961 ZVAL_NULL(&tmp);
962 }
963 );
964
965 return props;
966 }
967
968 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___construct, 0, 0, 0)
969 ZEND_ARG_INFO(0, message)
970 ZEND_ARG_INFO(0, greedy)
971 ZEND_END_ARG_INFO();
972 static PHP_METHOD(HttpMessage, __construct)
973 {
974 zend_bool greedy = 1;
975 zval *zmessage = NULL;
976 php_http_message_t *msg = NULL;
977 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
978 zend_error_handling zeh;
979
980 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!b", &zmessage, &greedy), invalid_arg, return);
981
982 zend_replace_error_handling(EH_THROW, php_http_exception_bad_message_class_entry, &zeh);
983 if (zmessage && Z_TYPE_P(zmessage) == IS_RESOURCE) {
984 php_stream *s;
985 php_http_message_parser_t p;
986 zend_error_handling zeh;
987
988 zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh);
989 php_stream_from_zval(s, zmessage);
990 zend_restore_error_handling(&zeh);
991
992 if (s && php_http_message_parser_init(&p)) {
993 unsigned flags = (greedy ? PHP_HTTP_MESSAGE_PARSER_GREEDY : 0);
994 php_http_buffer_t buf;
995
996 php_http_buffer_init_ex(&buf, 0x1000, PHP_HTTP_BUFFER_INIT_PREALLOC);
997 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse_stream(&p, &buf, s, flags, &msg)) {
998 if (!EG(exception)) {
999 php_http_throw(bad_message, "Could not parse message from stream", NULL);
1000 }
1001 }
1002 php_http_buffer_dtor(&buf);
1003 php_http_message_parser_dtor(&p);
1004 }
1005
1006 if (!msg && !EG(exception)) {
1007 php_http_throw(bad_message, "Empty message received from stream", NULL);
1008 }
1009 } else if (zmessage) {
1010 zend_string *zs_msg = zval_get_string(zmessage);
1011
1012 msg = php_http_message_parse(NULL, zs_msg->val, zs_msg->len, greedy);
1013
1014 if (!msg && !EG(exception)) {
1015 php_http_throw(bad_message, "Could not parse message: %.*s", MIN(25, zs_msg->len), zs_msg->len);
1016 }
1017 zend_string_release(zs_msg);
1018 }
1019
1020 if (msg) {
1021 php_http_message_dtor(obj->message);
1022 obj->message = msg;
1023 if (obj->message->parent) {
1024 obj->parent = php_http_message_object_new_ex(obj->zo.ce, obj->message->parent);
1025 }
1026 }
1027 zend_restore_error_handling(&zeh);
1028 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1029 }
1030
1031 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getBody, 0, 0, 0)
1032 ZEND_END_ARG_INFO();
1033 static PHP_METHOD(HttpMessage, getBody)
1034 {
1035 php_http_message_object_t *obj;
1036
1037 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1038
1039 obj = PHP_HTTP_OBJ(NULL, getThis());
1040 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1041
1042 if (!obj->body) {
1043 php_http_message_object_init_body_object(obj);
1044
1045 }
1046 if (obj->body) {
1047 RETVAL_OBJECT(&obj->body->zo, 1);
1048 }
1049 }
1050
1051 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setBody, 0, 0, 1)
1052 ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 0)
1053 ZEND_END_ARG_INFO();
1054 static PHP_METHOD(HttpMessage, setBody)
1055 {
1056 zval *zbody;
1057
1058 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zbody, php_http_message_body_class_entry)) {
1059 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1060
1061 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1062 php_http_message_object_prophandler_set_body(obj, zbody);
1063 }
1064 RETVAL_ZVAL_FAST(getThis());
1065 }
1066
1067 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addBody, 0, 0, 1)
1068 ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 0)
1069 ZEND_END_ARG_INFO();
1070 static PHP_METHOD(HttpMessage, addBody)
1071 {
1072 zval *new_body;
1073
1074 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &new_body, php_http_message_body_class_entry)) {
1075 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1076 php_http_message_body_object_t *new_obj = PHP_HTTP_OBJ(NULL, new_body);
1077
1078 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1079 php_http_message_body_to_callback(new_obj->body, (php_http_pass_callback_t) php_http_message_body_append, obj->message->body, 0, 0);
1080 }
1081 RETVAL_ZVAL_FAST(getThis());
1082 }
1083
1084 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeader, 0, 0, 1)
1085 ZEND_ARG_INFO(0, header)
1086 ZEND_ARG_INFO(0, into_class)
1087 ZEND_END_ARG_INFO();
1088 static PHP_METHOD(HttpMessage, getHeader)
1089 {
1090 char *header_str;
1091 size_t header_len;
1092 zend_class_entry *header_ce = NULL;
1093
1094 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|C!", &header_str, &header_len, &header_ce)) {
1095 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1096 zval *header;
1097
1098 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1099
1100 if ((header = php_http_message_header(obj->message, header_str, header_len))) {
1101 if (!header_ce) {
1102 RETURN_ZVAL(header, 1, 1);
1103 } else if (instanceof_function(header_ce, php_http_header_class_entry)) {
1104 zval argv[2];
1105
1106 ZVAL_STRINGL(&argv[0], header_str, header_len);
1107 ZVAL_COPY(&argv[1], header);
1108
1109 object_init_ex(return_value, header_ce);
1110 php_http_method_call(return_value, ZEND_STRL("__construct"), 2, argv, NULL);
1111
1112 zval_ptr_dtor(&argv[0]);
1113 zval_ptr_dtor(&argv[1]);
1114
1115 return;
1116 } else {
1117 php_error_docref(NULL, E_WARNING, "Class '%s' is not as descendant of http\\Header", header_ce->name->val);
1118 }
1119 }
1120 }
1121 RETURN_FALSE;
1122 }
1123
1124 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeaders, 0, 0, 0)
1125 ZEND_END_ARG_INFO();
1126 static PHP_METHOD(HttpMessage, getHeaders)
1127 {
1128 if (SUCCESS == zend_parse_parameters_none()) {
1129 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1130
1131 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1132
1133 array_init(return_value);
1134 array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
1135 }
1136 }
1137
1138 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeader, 0, 0, 1)
1139 ZEND_ARG_INFO(0, header)
1140 ZEND_ARG_INFO(0, value)
1141 ZEND_END_ARG_INFO();
1142 static PHP_METHOD(HttpMessage, setHeader)
1143 {
1144 zval *zvalue = NULL;
1145 char *name_str;
1146 size_t name_len;
1147
1148 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!", &name_str, &name_len, &zvalue)) {
1149 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1150 char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
1151
1152 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1153
1154 if (!zvalue) {
1155 zend_symtable_str_del(&obj->message->hdrs, name, name_len);
1156 } else {
1157 Z_TRY_ADDREF_P(zvalue);
1158 zend_symtable_str_update(&obj->message->hdrs, name, name_len, zvalue);
1159 }
1160 efree(name);
1161 }
1162 RETVAL_ZVAL_FAST(getThis());
1163 }
1164
1165 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeaders, 0, 0, 1)
1166 ZEND_ARG_ARRAY_INFO(0, headers, 1)
1167 ZEND_END_ARG_INFO();
1168 static PHP_METHOD(HttpMessage, setHeaders)
1169 {
1170 zval *new_headers = NULL;
1171
1172 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "a/!", &new_headers)) {
1173 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1174
1175 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1176
1177 zend_hash_clean(&obj->message->hdrs);
1178 if (new_headers) {
1179 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, 0, ARRAY_JOIN_PRETTIFY|ARRAY_JOIN_STRONLY);
1180 }
1181 }
1182 RETVAL_ZVAL_FAST(getThis());
1183 }
1184
1185 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader, 0, 0, 2)
1186 ZEND_ARG_INFO(0, header)
1187 ZEND_ARG_INFO(0, value)
1188 ZEND_END_ARG_INFO();
1189 static PHP_METHOD(HttpMessage, addHeader)
1190 {
1191 zval *zvalue;
1192 char *name_str;
1193 size_t name_len;
1194
1195 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name_str, &name_len, &zvalue)) {
1196 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1197 char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
1198 zval *header;
1199
1200 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1201
1202 Z_TRY_ADDREF_P(zvalue);
1203 if ((header = php_http_message_header(obj->message, name, name_len))) {
1204 convert_to_array(header);
1205 zend_hash_next_index_insert(Z_ARRVAL_P(header), zvalue);
1206 } else {
1207 zend_symtable_str_update(&obj->message->hdrs, name, name_len, zvalue);
1208 }
1209 efree(name);
1210 }
1211 RETVAL_ZVAL_FAST(getThis());
1212 }
1213
1214 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeaders, 0, 0, 1)
1215 ZEND_ARG_ARRAY_INFO(0, headers, 0)
1216 ZEND_ARG_INFO(0, append)
1217 ZEND_END_ARG_INFO();
1218 static PHP_METHOD(HttpMessage, addHeaders)
1219 {
1220 zval *new_headers;
1221 zend_bool append = 0;
1222
1223 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &new_headers, &append)) {
1224 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1225
1226 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1227
1228 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY);
1229 }
1230 RETVAL_ZVAL_FAST(getThis());
1231 }
1232
1233 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getType, 0, 0, 0)
1234 ZEND_END_ARG_INFO();
1235 static PHP_METHOD(HttpMessage, getType)
1236 {
1237 if (SUCCESS == zend_parse_parameters_none()) {
1238 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1239
1240 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1241
1242 RETURN_LONG(obj->message->type);
1243 }
1244 }
1245
1246 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setType, 0, 0, 1)
1247 ZEND_ARG_INFO(0, type)
1248 ZEND_END_ARG_INFO();
1249 static PHP_METHOD(HttpMessage, setType)
1250 {
1251 zend_long type;
1252
1253 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type)) {
1254 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1255
1256 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1257
1258 php_http_message_set_type(obj->message, type);
1259 }
1260 RETVAL_ZVAL_FAST(getThis());
1261 }
1262
1263 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getInfo, 0, 0, 0)
1264 ZEND_END_ARG_INFO();
1265 static PHP_METHOD(HttpMessage, getInfo)
1266 {
1267 if (SUCCESS == zend_parse_parameters_none()) {
1268 char *str, *tmp = NULL;
1269 size_t len;
1270 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1271
1272 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1273
1274 switch (obj->message->type) {
1275 case PHP_HTTP_REQUEST:
1276 len = spprintf(&str, 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, tmp, ""));
1277 PTR_FREE(tmp);
1278 break;
1279 case PHP_HTTP_RESPONSE:
1280 len = spprintf(&str, 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, tmp, ""));
1281 PTR_FREE(tmp);
1282 break;
1283 default:
1284 RETURN_NULL();
1285 break;
1286 }
1287
1288 RETVAL_STR(php_http_cs2zs(str, len));
1289 }
1290 }
1291
1292 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setInfo, 0, 0, 1)
1293 ZEND_ARG_INFO(0, http_info)
1294 ZEND_END_ARG_INFO();
1295 static PHP_METHOD(HttpMessage, setInfo)
1296 {
1297 char *str;
1298 size_t len;
1299 php_http_message_object_t *obj;
1300 php_http_info_t inf;
1301
1302 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &len), invalid_arg, return);
1303
1304 obj = PHP_HTTP_OBJ(NULL, getThis());
1305 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1306
1307 if (!php_http_info_parse(&inf, str)) {
1308 php_http_throw(bad_header, "Could not parse message info '%s'", str);
1309 return;
1310 }
1311
1312 php_http_message_set_info(obj->message, &inf);
1313 php_http_info_dtor(&inf);
1314
1315 RETVAL_ZVAL_FAST(getThis());
1316 }
1317
1318 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHttpVersion, 0, 0, 0)
1319 ZEND_END_ARG_INFO();
1320 static PHP_METHOD(HttpMessage, getHttpVersion)
1321 {
1322 if (SUCCESS == zend_parse_parameters_none()) {
1323 char *str;
1324 size_t len;
1325 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1326
1327 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1328
1329 php_http_version_to_string(&obj->message->http.version, &str, &len, NULL, NULL);
1330 RETURN_STR(php_http_cs2zs(str, len));
1331 }
1332 }
1333
1334 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHttpVersion, 0, 0, 1)
1335 ZEND_ARG_INFO(0, http_version)
1336 ZEND_END_ARG_INFO();
1337 static PHP_METHOD(HttpMessage, setHttpVersion)
1338 {
1339 char *v_str;
1340 size_t v_len;
1341 php_http_version_t version;
1342 php_http_message_object_t *obj;
1343
1344 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &v_str, &v_len), invalid_arg, return);
1345
1346 obj = PHP_HTTP_OBJ(NULL, getThis());
1347 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1348
1349 php_http_expect(php_http_version_parse(&version, v_str), unexpected_val, return);
1350
1351 obj->message->http.version = version;
1352
1353 RETVAL_ZVAL_FAST(getThis());
1354 }
1355
1356 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseCode, 0, 0, 0)
1357 ZEND_END_ARG_INFO();
1358 static PHP_METHOD(HttpMessage, getResponseCode)
1359 {
1360 if (SUCCESS == zend_parse_parameters_none()) {
1361 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1362
1363 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1364
1365 if (obj->message->type != PHP_HTTP_RESPONSE) {
1366 php_error_docref(NULL, E_WARNING, "http\\Message is not if type response");
1367 RETURN_FALSE;
1368 }
1369
1370 RETURN_LONG(obj->message->http.info.response.code);
1371 }
1372 }
1373
1374 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseCode, 0, 0, 1)
1375 ZEND_ARG_INFO(0, response_code)
1376 ZEND_ARG_INFO(0, strict)
1377 ZEND_END_ARG_INFO();
1378 static PHP_METHOD(HttpMessage, setResponseCode)
1379 {
1380 zend_long code;
1381 zend_bool strict = 1;
1382 php_http_message_object_t *obj;
1383
1384 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &code, &strict), invalid_arg, return);
1385
1386 obj = PHP_HTTP_OBJ(NULL, getThis());
1387 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1388
1389 if (obj->message->type != PHP_HTTP_RESPONSE) {
1390 php_http_throw(bad_method_call, "http\\Message is not of type response", NULL);
1391 return;
1392 }
1393
1394 if (strict && (code < 100 || code > 599)) {
1395 php_http_throw(invalid_arg, "Invalid response code (100-599): %ld", code);
1396 return;
1397 }
1398
1399 obj->message->http.info.response.code = code;
1400 PTR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(code)));
1401
1402 RETVAL_ZVAL_FAST(getThis());
1403 }
1404
1405 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseStatus, 0, 0, 0)
1406 ZEND_END_ARG_INFO();
1407 static PHP_METHOD(HttpMessage, getResponseStatus)
1408 {
1409 if (SUCCESS == zend_parse_parameters_none()) {
1410 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1411
1412 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1413
1414 if (obj->message->type != PHP_HTTP_RESPONSE) {
1415 php_error_docref(NULL, E_WARNING, "http\\Message is not of type response");
1416 }
1417
1418 if (obj->message->http.info.response.status) {
1419 RETURN_STRING(obj->message->http.info.response.status);
1420 } else {
1421 RETURN_EMPTY_STRING();
1422 }
1423 }
1424 }
1425
1426 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseStatus, 0, 0, 1)
1427 ZEND_ARG_INFO(0, response_status)
1428 ZEND_END_ARG_INFO();
1429 static PHP_METHOD(HttpMessage, setResponseStatus)
1430 {
1431 char *status;
1432 int status_len;
1433 php_http_message_object_t *obj;
1434
1435 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len), invalid_arg, return);
1436
1437 obj = PHP_HTTP_OBJ(NULL, getThis());
1438
1439 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1440
1441 if (obj->message->type != PHP_HTTP_RESPONSE) {
1442 php_http_throw(bad_method_call, "http\\Message is not of type response", NULL);
1443 }
1444
1445 PTR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
1446 RETVAL_ZVAL_FAST(getThis());
1447 }
1448
1449 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestMethod, 0, 0, 0)
1450 ZEND_END_ARG_INFO();
1451 static PHP_METHOD(HttpMessage, getRequestMethod)
1452 {
1453 if (SUCCESS == zend_parse_parameters_none()) {
1454 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1455
1456 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1457
1458 if (obj->message->type != PHP_HTTP_REQUEST) {
1459 php_error_docref(NULL, E_WARNING, "http\\Message is not of type request");
1460 RETURN_FALSE;
1461 }
1462
1463 if (obj->message->http.info.request.method) {
1464 RETURN_STRING(obj->message->http.info.request.method);
1465 } else {
1466 RETURN_EMPTY_STRING();
1467 }
1468 }
1469 }
1470
1471 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestMethod, 0, 0, 1)
1472 ZEND_ARG_INFO(0, request_method)
1473 ZEND_END_ARG_INFO();
1474 static PHP_METHOD(HttpMessage, setRequestMethod)
1475 {
1476 char *method;
1477 size_t method_len;
1478 php_http_message_object_t *obj;
1479
1480 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &method, &method_len), invalid_arg, return);
1481
1482 obj = PHP_HTTP_OBJ(NULL, getThis());
1483
1484 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1485
1486 if (obj->message->type != PHP_HTTP_REQUEST) {
1487 php_http_throw(bad_method_call, "http\\Message is not of type request", NULL);
1488 return;
1489 }
1490
1491 if (method_len < 1) {
1492 php_http_throw(invalid_arg, "Cannot set http\\Message's request method to an empty string", NULL);
1493 return;
1494 }
1495
1496 PTR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
1497 RETVAL_ZVAL_FAST(getThis());
1498 }
1499
1500 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestUrl, 0, 0, 0)
1501 ZEND_END_ARG_INFO();
1502 static PHP_METHOD(HttpMessage, getRequestUrl)
1503 {
1504 if (SUCCESS == zend_parse_parameters_none()) {
1505 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1506
1507 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1508
1509 if (obj->message->type != PHP_HTTP_REQUEST) {
1510 php_error_docref(NULL, E_WARNING, "http\\Message is not of type request");
1511 RETURN_FALSE;
1512 }
1513
1514 if (obj->message->http.info.request.url) {
1515 char *url_str;
1516 size_t url_len;
1517
1518 php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0);
1519 RETURN_STR(php_http_cs2zs(url_str, url_len));
1520 } else {
1521 RETURN_EMPTY_STRING();
1522 }
1523 }
1524 }
1525
1526 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestUrl, 0, 0, 1)
1527 ZEND_ARG_INFO(0, url)
1528 ZEND_END_ARG_INFO();
1529 static PHP_METHOD(HttpMessage, setRequestUrl)
1530 {
1531 zval *zurl;
1532 php_http_url_t *url;
1533 php_http_message_object_t *obj;
1534 zend_error_handling zeh;
1535
1536 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zurl), invalid_arg, return);
1537
1538 obj = PHP_HTTP_OBJ(NULL, getThis());
1539
1540 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1541
1542 if (obj->message->type != PHP_HTTP_REQUEST) {
1543 php_http_throw(bad_method_call, "http\\Message is not of type request", NULL);
1544 return;
1545 }
1546
1547 zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
1548 url = php_http_url_from_zval(zurl, ~0);
1549 zend_restore_error_handling(&zeh);
1550
1551 if (php_http_url_is_empty(url)) {
1552 php_http_url_free(&url);
1553 php_http_throw(invalid_arg, "Cannot set http\\Message's request url to an empty string", NULL);
1554 } else {
1555 PTR_SET(obj->message->http.info.request.url, url);
1556 }
1557
1558 RETVAL_ZVAL_FAST(getThis());
1559 }
1560
1561 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getParentMessage, 0, 0, 0)
1562 ZEND_END_ARG_INFO();
1563 static PHP_METHOD(HttpMessage, getParentMessage)
1564 {
1565 php_http_message_object_t *obj;
1566
1567 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1568
1569 obj = PHP_HTTP_OBJ(NULL, getThis());
1570 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1571
1572 if (!obj->message->parent) {
1573 php_http_throw(unexpected_val, "http\\Message has not parent message", NULL);
1574 return;
1575 }
1576
1577 RETVAL_OBJECT(&obj->parent->zo, 1);
1578 }
1579
1580 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___toString, 0, 0, 0)
1581 ZEND_END_ARG_INFO();
1582 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toString, 0, 0, 0)
1583 ZEND_ARG_INFO(0, include_parent)
1584 ZEND_END_ARG_INFO();
1585 static PHP_METHOD(HttpMessage, toString)
1586 {
1587 zend_bool include_parent = 0;
1588
1589 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &include_parent)) {
1590 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1591 char *string;
1592 size_t length;
1593
1594 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1595
1596 if (include_parent) {
1597 php_http_message_serialize(obj->message, &string, &length);
1598 } else {
1599 php_http_message_to_string(obj->message, &string, &length);
1600 }
1601 if (string) {
1602 RETURN_STR(php_http_cs2zs(string, length));
1603 }
1604 }
1605 RETURN_EMPTY_STRING();
1606 }
1607
1608 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toStream, 0, 0, 1)
1609 ZEND_ARG_INFO(0, stream)
1610 ZEND_END_ARG_INFO();
1611 static PHP_METHOD(HttpMessage, toStream)
1612 {
1613 zval *zstream;
1614
1615 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream)) {
1616 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1617 php_stream *s;
1618
1619 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1620
1621 php_stream_from_zval(s, zstream);
1622 php_http_message_to_callback(obj->message, (php_http_pass_callback_t) _php_stream_write, s);
1623 }
1624 }
1625
1626 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toCallback, 0, 0, 1)
1627 ZEND_ARG_INFO(0, callback)
1628 ZEND_END_ARG_INFO();
1629 static PHP_METHOD(HttpMessage, toCallback)
1630 {
1631 php_http_pass_fcall_arg_t fcd;
1632
1633 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fcd.fci, &fcd.fcc)) {
1634 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1635
1636 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1637
1638 ZVAL_COPY(&fcd.fcz, getThis());
1639 php_http_message_to_callback(obj->message, php_http_pass_fcall_callback, &fcd);
1640 zend_fcall_info_args_clear(&fcd.fci, 1);
1641 zval_ptr_dtor(&fcd.fcz);
1642
1643 RETURN_ZVAL_FAST(getThis());
1644 }
1645 }
1646
1647 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_serialize, 0, 0, 0)
1648 ZEND_END_ARG_INFO();
1649 static PHP_METHOD(HttpMessage, serialize)
1650 {
1651 if (SUCCESS == zend_parse_parameters_none()) {
1652 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1653 char *string;
1654 size_t length;
1655
1656 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1657
1658 php_http_message_serialize(obj->message, &string, &length);
1659 RETURN_STR(php_http_cs2zs(string, length));
1660 }
1661 RETURN_EMPTY_STRING();
1662 }
1663
1664 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_unserialize, 0, 0, 1)
1665 ZEND_ARG_INFO(0, serialized)
1666 ZEND_END_ARG_INFO();
1667 static PHP_METHOD(HttpMessage, unserialize)
1668 {
1669 size_t length;
1670 char *serialized;
1671
1672 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &length)) {
1673 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1674 php_http_message_t *msg;
1675
1676 if (obj->message) {
1677 /* do not free recursively */
1678 php_http_message_dtor(obj->message);
1679 efree(obj->message);
1680 }
1681 if ((msg = php_http_message_parse(NULL, serialized, length, 1))) {
1682 obj->message = msg;
1683 } else {
1684 obj->message = php_http_message_init(NULL, 0, NULL);
1685 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not unserialize http\\Message");
1686 }
1687 }
1688 }
1689
1690 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_detach, 0, 0, 0)
1691 ZEND_END_ARG_INFO();
1692 static PHP_METHOD(HttpMessage, detach)
1693 {
1694 php_http_message_object_t *obj, *new_obj;
1695 php_http_message_t *msg_cpy;
1696
1697 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1698
1699 obj = PHP_HTTP_OBJ(NULL, getThis());
1700 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1701
1702 msg_cpy = php_http_message_copy_ex(obj->message, NULL, 0);
1703 new_obj = php_http_message_object_new_ex(obj->zo.ce, msg_cpy);
1704
1705 RETVAL_OBJ(&new_obj->zo);
1706 }
1707
1708 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_prepend, 0, 0, 1)
1709 ZEND_ARG_OBJ_INFO(0, message, http\\Message, 0)
1710 ZEND_ARG_INFO(0, top)
1711 ZEND_END_ARG_INFO();
1712 static PHP_METHOD(HttpMessage, prepend)
1713 {
1714 zval *prepend;
1715 zend_bool top = 1;
1716 php_http_message_t *msg[2];
1717 php_http_message_object_t *obj, *prepend_obj;
1718
1719 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &prepend, php_http_message_class_entry, &top), invalid_arg, return);
1720
1721 obj = PHP_HTTP_OBJ(NULL, getThis());
1722 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1723 prepend_obj = PHP_HTTP_OBJ(NULL, prepend);
1724 PHP_HTTP_MESSAGE_OBJECT_INIT(prepend_obj);
1725
1726 /* safety check */
1727 for (msg[0] = obj->message; msg[0]; msg[0] = msg[0]->parent) {
1728 for (msg[1] = prepend_obj->message; msg[1]; msg[1] = msg[1]->parent) {
1729 if (msg[0] == msg[1]) {
1730 php_http_throw(unexpected_val, "Cannot prepend a message located within the same message chain", NULL);
1731 return;
1732 }
1733 }
1734 }
1735
1736 php_http_message_object_prepend(getThis(), prepend, top);
1737 RETURN_ZVAL_FAST(getThis());
1738 }
1739
1740 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_reverse, 0, 0, 0)
1741 ZEND_END_ARG_INFO();
1742 static PHP_METHOD(HttpMessage, reverse)
1743 {
1744 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1745
1746 php_http_message_object_reverse(getThis(), return_value);
1747 }
1748
1749 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_isMultipart, 0, 0, 0)
1750 ZEND_ARG_INFO(1, boundary)
1751 ZEND_END_ARG_INFO();
1752 static PHP_METHOD(HttpMessage, isMultipart)
1753 {
1754 zval *zboundary = NULL;
1755
1756 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!", &zboundary)) {
1757 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1758 char *boundary = NULL;
1759
1760 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1761
1762 if (php_http_message_is_multipart(obj->message, zboundary ? &boundary : NULL)) {
1763 RETVAL_TRUE;
1764 } else {
1765 RETVAL_FALSE;
1766 }
1767
1768 if (zboundary && boundary) {
1769 zval_dtor(zboundary);
1770 ZVAL_STR(zboundary, php_http_cs2zs(boundary, strlen(boundary)));
1771 }
1772 }
1773 }
1774
1775 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_splitMultipartBody, 0, 0, 0)
1776 ZEND_END_ARG_INFO();
1777 static PHP_METHOD(HttpMessage, splitMultipartBody)
1778 {
1779 php_http_message_object_t *obj;
1780 php_http_message_t *msg;
1781 char *boundary = NULL;
1782
1783 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1784
1785 obj = PHP_HTTP_OBJ(NULL, getThis());
1786 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1787
1788 if (!php_http_message_is_multipart(obj->message, &boundary)) {
1789 php_http_throw(bad_method_call, "http\\Message is not a multipart message", NULL);
1790 return;
1791 }
1792
1793 php_http_expect(msg = php_http_message_body_split(obj->message->body, boundary), bad_message, return);
1794
1795 PTR_FREE(boundary);
1796
1797 RETURN_OBJ(&php_http_message_object_new_ex(obj->zo.ce, msg)->zo);
1798 }
1799
1800 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_count, 0, 0, 0)
1801 ZEND_END_ARG_INFO();
1802 static PHP_METHOD(HttpMessage, count)
1803 {
1804 zend_long count_mode = -1;
1805
1806 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &count_mode)) {
1807 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1808
1809 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1810
1811 RETURN_LONG(php_http_message_count(obj->message));
1812 }
1813 }
1814
1815 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_rewind, 0, 0, 0)
1816 ZEND_END_ARG_INFO();
1817 static PHP_METHOD(HttpMessage, rewind)
1818 {
1819 if (SUCCESS == zend_parse_parameters_none()) {
1820 zval *zobj = getThis();
1821 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1822
1823 if (!Z_ISUNDEF(obj->iterator)) {
1824 zval_ptr_dtor(&obj->iterator);
1825 }
1826 ZVAL_COPY(&obj->iterator, zobj);
1827 }
1828 }
1829
1830 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_valid, 0, 0, 0)
1831 ZEND_END_ARG_INFO();
1832 static PHP_METHOD(HttpMessage, valid)
1833 {
1834 if (SUCCESS == zend_parse_parameters_none()) {
1835 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1836
1837 RETURN_BOOL(!Z_ISUNDEF(obj->iterator));
1838 }
1839 }
1840
1841 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_next, 0, 0, 0)
1842 ZEND_END_ARG_INFO();
1843 static PHP_METHOD(HttpMessage, next)
1844 {
1845 if (SUCCESS == zend_parse_parameters_none()) {
1846 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1847
1848 if (!Z_ISUNDEF(obj->iterator)) {
1849 php_http_message_object_t *itr = PHP_HTTP_OBJ(NULL, &obj->iterator);
1850
1851 if (itr->parent) {
1852 zval tmp;
1853
1854 ZVAL_OBJECT(&tmp, &itr->parent->zo, 1);
1855 zval_ptr_dtor(&obj->iterator);
1856 obj->iterator = tmp;
1857 } else {
1858 zval_ptr_dtor(&obj->iterator);
1859 ZVAL_UNDEF(&obj->iterator);
1860 }
1861 }
1862 }
1863 }
1864
1865 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_key, 0, 0, 0)
1866 ZEND_END_ARG_INFO();
1867 static PHP_METHOD(HttpMessage, key)
1868 {
1869 if (SUCCESS == zend_parse_parameters_none()) {
1870 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1871
1872 RETURN_LONG(Z_ISUNDEF(obj->iterator) ? 0 : Z_OBJ_HANDLE(obj->iterator));
1873 }
1874 }
1875
1876 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_current, 0, 0, 0)
1877 ZEND_END_ARG_INFO();
1878 static PHP_METHOD(HttpMessage, current)
1879 {
1880 if (SUCCESS == zend_parse_parameters_none()) {
1881 php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1882
1883 if (!Z_ISUNDEF(obj->iterator)) {
1884 RETURN_ZVAL_FAST(&obj->iterator);
1885 }
1886 }
1887 }
1888
1889 static zend_function_entry php_http_message_methods[] = {
1890 PHP_ME(HttpMessage, __construct, ai_HttpMessage___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1891 PHP_ME(HttpMessage, getBody, ai_HttpMessage_getBody, ZEND_ACC_PUBLIC)
1892 PHP_ME(HttpMessage, setBody, ai_HttpMessage_setBody, ZEND_ACC_PUBLIC)
1893 PHP_ME(HttpMessage, addBody, ai_HttpMessage_addBody, ZEND_ACC_PUBLIC)
1894 PHP_ME(HttpMessage, getHeader, ai_HttpMessage_getHeader, ZEND_ACC_PUBLIC)
1895 PHP_ME(HttpMessage, setHeader, ai_HttpMessage_setHeader, ZEND_ACC_PUBLIC)
1896 PHP_ME(HttpMessage, addHeader, ai_HttpMessage_addHeader, ZEND_ACC_PUBLIC)
1897 PHP_ME(HttpMessage, getHeaders, ai_HttpMessage_getHeaders, ZEND_ACC_PUBLIC)
1898 PHP_ME(HttpMessage, setHeaders, ai_HttpMessage_setHeaders, ZEND_ACC_PUBLIC)
1899 PHP_ME(HttpMessage, addHeaders, ai_HttpMessage_addHeaders, ZEND_ACC_PUBLIC)
1900 PHP_ME(HttpMessage, getType, ai_HttpMessage_getType, ZEND_ACC_PUBLIC)
1901 PHP_ME(HttpMessage, setType, ai_HttpMessage_setType, ZEND_ACC_PUBLIC)
1902 PHP_ME(HttpMessage, getInfo, ai_HttpMessage_getInfo, ZEND_ACC_PUBLIC)
1903 PHP_ME(HttpMessage, setInfo, ai_HttpMessage_setInfo, ZEND_ACC_PUBLIC)
1904 PHP_ME(HttpMessage, getResponseCode, ai_HttpMessage_getResponseCode, ZEND_ACC_PUBLIC)
1905 PHP_ME(HttpMessage, setResponseCode, ai_HttpMessage_setResponseCode, ZEND_ACC_PUBLIC)
1906 PHP_ME(HttpMessage, getResponseStatus, ai_HttpMessage_getResponseStatus, ZEND_ACC_PUBLIC)
1907 PHP_ME(HttpMessage, setResponseStatus, ai_HttpMessage_setResponseStatus, ZEND_ACC_PUBLIC)
1908 PHP_ME(HttpMessage, getRequestMethod, ai_HttpMessage_getRequestMethod, ZEND_ACC_PUBLIC)
1909 PHP_ME(HttpMessage, setRequestMethod, ai_HttpMessage_setRequestMethod, ZEND_ACC_PUBLIC)
1910 PHP_ME(HttpMessage, getRequestUrl, ai_HttpMessage_getRequestUrl, ZEND_ACC_PUBLIC)
1911 PHP_ME(HttpMessage, setRequestUrl, ai_HttpMessage_setRequestUrl, ZEND_ACC_PUBLIC)
1912 PHP_ME(HttpMessage, getHttpVersion, ai_HttpMessage_getHttpVersion, ZEND_ACC_PUBLIC)
1913 PHP_ME(HttpMessage, setHttpVersion, ai_HttpMessage_setHttpVersion, ZEND_ACC_PUBLIC)
1914 PHP_ME(HttpMessage, getParentMessage, ai_HttpMessage_getParentMessage, ZEND_ACC_PUBLIC)
1915 PHP_ME(HttpMessage, toString, ai_HttpMessage_toString, ZEND_ACC_PUBLIC)
1916 PHP_ME(HttpMessage, toCallback, ai_HttpMessage_toCallback, ZEND_ACC_PUBLIC)
1917 PHP_ME(HttpMessage, toStream, ai_HttpMessage_toStream, ZEND_ACC_PUBLIC)
1918
1919 /* implements Countable */
1920 PHP_ME(HttpMessage, count, ai_HttpMessage_count, ZEND_ACC_PUBLIC)
1921
1922 /* implements Serializable */
1923 PHP_ME(HttpMessage, serialize, ai_HttpMessage_serialize, ZEND_ACC_PUBLIC)
1924 PHP_ME(HttpMessage, unserialize, ai_HttpMessage_unserialize, ZEND_ACC_PUBLIC)
1925
1926 /* implements Iterator */
1927 PHP_ME(HttpMessage, rewind, ai_HttpMessage_rewind, ZEND_ACC_PUBLIC)
1928 PHP_ME(HttpMessage, valid, ai_HttpMessage_valid, ZEND_ACC_PUBLIC)
1929 PHP_ME(HttpMessage, current, ai_HttpMessage_current, ZEND_ACC_PUBLIC)
1930 PHP_ME(HttpMessage, key, ai_HttpMessage_key, ZEND_ACC_PUBLIC)
1931 PHP_ME(HttpMessage, next, ai_HttpMessage_next, ZEND_ACC_PUBLIC)
1932
1933 ZEND_MALIAS(HttpMessage, __toString, toString, ai_HttpMessage___toString, ZEND_ACC_PUBLIC)
1934
1935 PHP_ME(HttpMessage, detach, ai_HttpMessage_detach, ZEND_ACC_PUBLIC)
1936 PHP_ME(HttpMessage, prepend, ai_HttpMessage_prepend, ZEND_ACC_PUBLIC)
1937 PHP_ME(HttpMessage, reverse, ai_HttpMessage_reverse, ZEND_ACC_PUBLIC)
1938
1939 PHP_ME(HttpMessage, isMultipart, ai_HttpMessage_isMultipart, ZEND_ACC_PUBLIC)
1940 PHP_ME(HttpMessage, splitMultipartBody, ai_HttpMessage_splitMultipartBody, ZEND_ACC_PUBLIC)
1941
1942 EMPTY_FUNCTION_ENTRY
1943 };
1944
1945 zend_class_entry *php_http_message_class_entry;
1946
1947 PHP_MINIT_FUNCTION(http_message)
1948 {
1949 zend_class_entry ce = {0};
1950
1951 INIT_NS_CLASS_ENTRY(ce, "http", "Message", php_http_message_methods);
1952 php_http_message_class_entry = zend_register_internal_class(&ce);
1953 php_http_message_class_entry->create_object = php_http_message_object_new;
1954 memcpy(&php_http_message_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1955 php_http_message_object_handlers.offset = XtOffsetOf(php_http_message_object_t, zo);
1956 php_http_message_object_handlers.clone_obj = php_http_message_object_clone;
1957 php_http_message_object_handlers.dtor_obj = php_http_message_object_free;
1958 php_http_message_object_handlers.read_property = php_http_message_object_read_prop;
1959 php_http_message_object_handlers.write_property = php_http_message_object_write_prop;
1960 php_http_message_object_handlers.get_properties = php_http_message_object_get_props;
1961 php_http_message_object_handlers.get_property_ptr_ptr = NULL;
1962
1963 zend_class_implements(php_http_message_class_entry, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
1964
1965 zend_hash_init(&php_http_message_object_prophandlers, 9, NULL, NULL, 1);
1966 zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("type"), PHP_HTTP_NONE, ZEND_ACC_PROTECTED);
1967 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type, php_http_message_object_prophandler_set_type);
1968 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("body"), ZEND_ACC_PROTECTED);
1969 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body, php_http_message_object_prophandler_set_body);
1970 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED);
1971 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);
1972 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED);
1973 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);
1974 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED);
1975 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);
1976 zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED);
1977 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);
1978 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED);
1979 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);
1980 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("headers"), ZEND_ACC_PROTECTED);
1981 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers, php_http_message_object_prophandler_set_headers);
1982 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED);
1983 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);
1984
1985 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE);
1986 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST);
1987 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE);
1988
1989 return SUCCESS;
1990 }
1991
1992 PHP_MSHUTDOWN_FUNCTION(http_message)
1993 {
1994 zend_hash_destroy(&php_http_message_object_prophandlers);
1995
1996 return SUCCESS;
1997 }
1998
1999 /*
2000 * Local variables:
2001 * tab-width: 4
2002 * c-basic-offset: 4
2003 * End:
2004 * vim600: noet sw=4 ts=4 fdm=marker
2005 * vim<600: noet sw=4 ts=4
2006 */
2007