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