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