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