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