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