fix httpVersion retrieval 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 HashTable *props = zend_get_std_object_handlers()->get_properties(object TSRMLS_CC);
941 zval array, *parent, *body;
942 char *version;
943 int verlen;
944
945 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
946 INIT_PZVAL_ARRAY(&array, props);
947
948 #define ASSOC_PROP(ptype, n, val) \
949 do { \
950 zend_property_info *pi; \
951 if (SUCCESS == zend_hash_find(&obj->zo.ce->properties_info, n, sizeof(n), (void *) &pi)) { \
952 add_assoc_ ##ptype## _ex(&array, pi->name, pi->name_length + 1, val); \
953 } \
954 } while(0) \
955
956 #define ASSOC_STRING(name, val) ASSOC_STRINGL(name, val, strlen(val))
957 #define ASSOC_STRINGL(name, val, len) ASSOC_STRINGL_EX(name, val, len, 1)
958 #define ASSOC_STRINGL_EX(n, val, len, cpy) \
959 do { \
960 zend_property_info *pi; \
961 if (SUCCESS == zend_hash_find(&obj->zo.ce->properties_info, n, sizeof(n), (void *) &pi)) { \
962 add_assoc_stringl_ex(&array, pi->name, pi->name_length + 1, val, len, cpy); \
963 } \
964 } while(0)
965
966 ASSOC_PROP(long, "type", obj->message->type);
967 verlen = spprintf(&version, 0, "%u.%u", obj->message->http.version.major, obj->message->http.version.minor);
968 ASSOC_STRINGL_EX("httpVersion", version, verlen, 0);
969
970 switch (obj->message->type) {
971 case PHP_HTTP_REQUEST:
972 ASSOC_PROP(long, "responseCode", 0);
973 ASSOC_STRINGL("responseStatus", "", 0);
974 ASSOC_STRING("requestMethod", STR_PTR(obj->message->http.info.request.method));
975 ASSOC_STRING("requestUrl", STR_PTR(obj->message->http.info.request.url));
976 break;
977
978 case PHP_HTTP_RESPONSE:
979 ASSOC_PROP(long, "responseCode", obj->message->http.info.response.code);
980 ASSOC_STRING("responseStatus", STR_PTR(obj->message->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), &obj->message->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 ZVAL_OBJVAL(body, obj->body->zv, 1);
1002 } else {
1003 ZVAL_NULL(body);
1004 }
1005 ASSOC_PROP(zval, "body", body);
1006
1007 MAKE_STD_ZVAL(parent);
1008 if (obj->message->parent) {
1009 ZVAL_OBJVAL(parent, obj->parent->zv, 1);
1010 } else {
1011 ZVAL_NULL(parent);
1012 }
1013 ASSOC_PROP(zval, "parentMessage", parent);
1014
1015 return props;
1016 }
1017
1018 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___construct, 0, 0, 0)
1019 ZEND_ARG_INFO(0, message)
1020 ZEND_ARG_INFO(0, greedy)
1021 ZEND_END_ARG_INFO();
1022 static PHP_METHOD(HttpMessage, __construct)
1023 {
1024 zend_bool greedy = 1;
1025 zval *zmessage = NULL;
1026 php_http_message_t *msg = NULL;
1027 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1028 zend_error_handling zeh;
1029
1030 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!b", &zmessage, &greedy), invalid_arg, return);
1031
1032 zend_replace_error_handling(EH_THROW, php_http_exception_bad_message_class_entry, &zeh TSRMLS_CC);
1033 if (zmessage && Z_TYPE_P(zmessage) == IS_RESOURCE) {
1034 php_stream *s;
1035 php_http_message_parser_t p;
1036 zend_error_handling zeh;
1037
1038 zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh TSRMLS_CC);
1039 php_stream_from_zval(s, &zmessage);
1040 zend_restore_error_handling(&zeh TSRMLS_CC);
1041
1042 if (s && php_http_message_parser_init(&p TSRMLS_CC)) {
1043 unsigned flags = (greedy ? PHP_HTTP_MESSAGE_PARSER_GREEDY : 0);
1044
1045 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse_stream(&p, s, flags, &msg)) {
1046 if (!EG(exception)) {
1047 php_http_throw(bad_message, "Could not parse message from stream", NULL);
1048 }
1049 }
1050
1051 php_http_message_parser_dtor(&p);
1052 }
1053
1054 if (!msg && !EG(exception)) {
1055 php_http_throw(bad_message, "Empty message received from stream", NULL);
1056 }
1057 } else if (zmessage) {
1058 zmessage = php_http_ztyp(IS_STRING, zmessage);
1059 msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage), greedy TSRMLS_CC);
1060
1061 if (!msg && !EG(exception)) {
1062 php_http_throw(bad_message, "Could not parse message: %.*s", MIN(25, Z_STRLEN_P(zmessage)), Z_STRVAL_P(zmessage));
1063 }
1064 zval_ptr_dtor(&zmessage);
1065 }
1066
1067 if (msg) {
1068 php_http_message_dtor(obj->message);
1069 obj->message = msg;
1070 if (obj->message->parent) {
1071 php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, &obj->parent TSRMLS_CC);
1072 }
1073 }
1074 zend_restore_error_handling(&zeh TSRMLS_CC);
1075 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1076 }
1077
1078 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getBody, 0, 0, 0)
1079 ZEND_END_ARG_INFO();
1080 static PHP_METHOD(HttpMessage, getBody)
1081 {
1082 php_http_message_object_t *obj;
1083
1084 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1085
1086 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1087
1088 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1089
1090 if (!obj->body) {
1091 php_http_message_object_init_body_object(obj);
1092
1093 }
1094 if (obj->body) {
1095 RETVAL_OBJVAL(obj->body->zv, 1);
1096 }
1097 }
1098
1099 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setBody, 0, 0, 1)
1100 ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 0)
1101 ZEND_END_ARG_INFO();
1102 static PHP_METHOD(HttpMessage, setBody)
1103 {
1104 zval *zbody;
1105
1106 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbody, php_http_message_body_class_entry)) {
1107 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1108
1109 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1110 php_http_message_object_prophandler_set_body(obj, zbody TSRMLS_CC);
1111 }
1112 RETVAL_ZVAL(getThis(), 1, 0);
1113 }
1114
1115 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addBody, 0, 0, 1)
1116 ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 0)
1117 ZEND_END_ARG_INFO();
1118 static PHP_METHOD(HttpMessage, addBody)
1119 {
1120 zval *new_body;
1121
1122 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &new_body, php_http_message_body_class_entry)) {
1123 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1124 php_http_message_body_object_t *new_obj = zend_object_store_get_object(new_body TSRMLS_CC);
1125
1126 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1127 php_http_message_body_to_callback(new_obj->body, (php_http_pass_callback_t) php_http_message_body_append, obj->message->body, 0, 0);
1128 }
1129 RETVAL_ZVAL(getThis(), 1, 0);
1130 }
1131
1132 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeader, 0, 0, 1)
1133 ZEND_ARG_INFO(0, header)
1134 ZEND_ARG_INFO(0, into_class)
1135 ZEND_END_ARG_INFO();
1136 static PHP_METHOD(HttpMessage, getHeader)
1137 {
1138 char *header_str;
1139 int header_len;
1140 zend_class_entry *header_ce = NULL;
1141
1142 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C!", &header_str, &header_len, &header_ce)) {
1143 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1144 zval *header;
1145
1146 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1147
1148 if ((header = php_http_message_header(obj->message, header_str, header_len, 0))) {
1149 if (!header_ce) {
1150 RETURN_ZVAL(header, 1, 1);
1151 } else if (instanceof_function(header_ce, php_http_header_class_entry TSRMLS_CC)) {
1152 zval *header_name, **argv[2];
1153
1154 MAKE_STD_ZVAL(header_name);
1155 ZVAL_STRINGL(header_name, header_str, header_len, 1);
1156 Z_ADDREF_P(header);
1157
1158 argv[0] = &header_name;
1159 argv[1] = &header;
1160
1161 object_init_ex(return_value, header_ce);
1162 php_http_method_call(return_value, ZEND_STRL("__construct"), 2, argv, NULL TSRMLS_CC);
1163
1164 zval_ptr_dtor(&header_name);
1165 zval_ptr_dtor(&header);
1166
1167 return;
1168 } else {
1169 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class '%s' is not as descendant of http\\Header", header_ce->name);
1170 }
1171 }
1172 }
1173 RETURN_FALSE;
1174 }
1175
1176 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeaders, 0, 0, 0)
1177 ZEND_END_ARG_INFO();
1178 static PHP_METHOD(HttpMessage, getHeaders)
1179 {
1180 if (SUCCESS == zend_parse_parameters_none()) {
1181 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1182
1183 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1184
1185 array_init(return_value);
1186 array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
1187 }
1188 }
1189
1190 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeader, 0, 0, 1)
1191 ZEND_ARG_INFO(0, header)
1192 ZEND_ARG_INFO(0, value)
1193 ZEND_END_ARG_INFO();
1194 static PHP_METHOD(HttpMessage, setHeader)
1195 {
1196 zval *zvalue = NULL;
1197 char *name_str;
1198 int name_len;
1199
1200 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!", &name_str, &name_len, &zvalue)) {
1201 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1202 char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
1203
1204 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1205
1206 if (!zvalue) {
1207 zend_symtable_del(&obj->message->hdrs, name, name_len + 1);
1208 } else {
1209 Z_ADDREF_P(zvalue);
1210 zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
1211 }
1212 efree(name);
1213 }
1214 RETVAL_ZVAL(getThis(), 1, 0);
1215 }
1216
1217 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeaders, 0, 0, 1)
1218 ZEND_ARG_ARRAY_INFO(0, headers, 1)
1219 ZEND_END_ARG_INFO();
1220 static PHP_METHOD(HttpMessage, setHeaders)
1221 {
1222 zval *new_headers = NULL;
1223
1224 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &new_headers)) {
1225 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1226
1227 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1228
1229 zend_hash_clean(&obj->message->hdrs);
1230 if (new_headers) {
1231 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, 0, ARRAY_JOIN_PRETTIFY|ARRAY_JOIN_STRONLY);
1232 }
1233 }
1234 RETVAL_ZVAL(getThis(), 1, 0);
1235 }
1236
1237 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader, 0, 0, 2)
1238 ZEND_ARG_INFO(0, header)
1239 ZEND_ARG_INFO(0, value)
1240 ZEND_END_ARG_INFO();
1241 static PHP_METHOD(HttpMessage, addHeader)
1242 {
1243 zval *zvalue;
1244 char *name_str;
1245 int name_len;
1246
1247 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name_str, &name_len, &zvalue)) {
1248 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1249 char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
1250 zval *header;
1251
1252 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1253
1254 Z_ADDREF_P(zvalue);
1255 if ((header = php_http_message_header(obj->message, name, name_len, 0))) {
1256 convert_to_array(header);
1257 zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL);
1258 zval_ptr_dtor(&header);
1259 } else {
1260 zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
1261 }
1262 efree(name);
1263 }
1264 RETVAL_ZVAL(getThis(), 1, 0);
1265 }
1266
1267 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeaders, 0, 0, 1)
1268 ZEND_ARG_ARRAY_INFO(0, headers, 0)
1269 ZEND_ARG_INFO(0, append)
1270 ZEND_END_ARG_INFO();
1271 static PHP_METHOD(HttpMessage, addHeaders)
1272 {
1273 zval *new_headers;
1274 zend_bool append = 0;
1275
1276 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
1277 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1278
1279 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1280
1281 array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY);
1282 }
1283 RETVAL_ZVAL(getThis(), 1, 0);
1284 }
1285
1286 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getType, 0, 0, 0)
1287 ZEND_END_ARG_INFO();
1288 static PHP_METHOD(HttpMessage, getType)
1289 {
1290 if (SUCCESS == zend_parse_parameters_none()) {
1291 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1292
1293 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1294
1295 RETURN_LONG(obj->message->type);
1296 }
1297 }
1298
1299 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setType, 0, 0, 1)
1300 ZEND_ARG_INFO(0, type)
1301 ZEND_END_ARG_INFO();
1302 static PHP_METHOD(HttpMessage, setType)
1303 {
1304 long type;
1305
1306 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
1307 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1308
1309 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1310
1311 php_http_message_set_type(obj->message, type);
1312 }
1313 RETVAL_ZVAL(getThis(), 1, 0);
1314 }
1315
1316 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getInfo, 0, 0, 0)
1317 ZEND_END_ARG_INFO();
1318 static PHP_METHOD(HttpMessage, getInfo)
1319 {
1320 if (SUCCESS == zend_parse_parameters_none()) {
1321 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1322
1323 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1324
1325 switch (obj->message->type) {
1326 case PHP_HTTP_REQUEST:
1327 Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, ""));
1328 break;
1329 case PHP_HTTP_RESPONSE:
1330 Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, ""));
1331 break;
1332 default:
1333 RETURN_NULL();
1334 break;
1335 }
1336 Z_TYPE_P(return_value) = IS_STRING;
1337 return;
1338 }
1339 }
1340
1341 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setInfo, 0, 0, 1)
1342 ZEND_ARG_INFO(0, http_info)
1343 ZEND_END_ARG_INFO();
1344 static PHP_METHOD(HttpMessage, setInfo)
1345 {
1346 char *str;
1347 int len;
1348 php_http_message_object_t *obj;
1349 php_http_info_t inf;
1350
1351 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len), invalid_arg, return);
1352
1353 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1354 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1355
1356 if (!php_http_info_parse(&inf, str TSRMLS_CC)) {
1357 php_http_throw(bad_header, "Could not parse message info '%s'", str);
1358 return;
1359 }
1360
1361 php_http_message_set_info(obj->message, &inf);
1362 php_http_info_dtor(&inf);
1363
1364 RETVAL_ZVAL(getThis(), 1, 0);
1365 }
1366
1367 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHttpVersion, 0, 0, 0)
1368 ZEND_END_ARG_INFO();
1369 static PHP_METHOD(HttpMessage, getHttpVersion)
1370 {
1371 if (SUCCESS == zend_parse_parameters_none()) {
1372 char *str;
1373 size_t len;
1374 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1375
1376 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1377
1378 php_http_version_to_string(&obj->message->http.version, &str, &len, NULL, NULL TSRMLS_CC);
1379 RETURN_STRINGL(str, len, 0);
1380 }
1381 }
1382
1383 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHttpVersion, 0, 0, 1)
1384 ZEND_ARG_INFO(0, http_version)
1385 ZEND_END_ARG_INFO();
1386 static PHP_METHOD(HttpMessage, setHttpVersion)
1387 {
1388 char *v_str;
1389 int v_len;
1390 php_http_version_t version;
1391 php_http_message_object_t *obj;
1392
1393 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &v_str, &v_len), invalid_arg, return);
1394
1395 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1396 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1397
1398 php_http_expect(php_http_version_parse(&version, v_str TSRMLS_CC), unexpected_val, return);
1399
1400 obj->message->http.version = version;
1401
1402 RETVAL_ZVAL(getThis(), 1, 0);
1403 }
1404
1405 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseCode, 0, 0, 0)
1406 ZEND_END_ARG_INFO();
1407 static PHP_METHOD(HttpMessage, getResponseCode)
1408 {
1409 if (SUCCESS == zend_parse_parameters_none()) {
1410 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1411
1412 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1413
1414 if (obj->message->type != PHP_HTTP_RESPONSE) {
1415 php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not if type response");
1416 RETURN_FALSE;
1417 }
1418
1419 RETURN_LONG(obj->message->http.info.response.code);
1420 }
1421 }
1422
1423 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseCode, 0, 0, 1)
1424 ZEND_ARG_INFO(0, response_code)
1425 ZEND_ARG_INFO(0, strict)
1426 ZEND_END_ARG_INFO();
1427 static PHP_METHOD(HttpMessage, setResponseCode)
1428 {
1429 long code;
1430 zend_bool strict = 1;
1431 php_http_message_object_t *obj;
1432
1433 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &code, &strict), invalid_arg, return);
1434
1435 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1436
1437 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1438
1439 if (obj->message->type != PHP_HTTP_RESPONSE) {
1440 php_http_throw(bad_method_call, "http\\Message is not of type response", NULL);
1441 return;
1442 }
1443
1444 if (strict && (code < 100 || code > 599)) {
1445 php_http_throw(invalid_arg, "Invalid response code (100-599): %ld", code);
1446 return;
1447 }
1448
1449 obj->message->http.info.response.code = code;
1450 STR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(code)));
1451
1452 RETVAL_ZVAL(getThis(), 1, 0);
1453 }
1454
1455 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseStatus, 0, 0, 0)
1456 ZEND_END_ARG_INFO();
1457 static PHP_METHOD(HttpMessage, getResponseStatus)
1458 {
1459 if (SUCCESS == zend_parse_parameters_none()) {
1460 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1461
1462 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1463
1464 if (obj->message->type != PHP_HTTP_RESPONSE) {
1465 php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not of type response");
1466 }
1467
1468 if (obj->message->http.info.response.status) {
1469 RETURN_STRING(obj->message->http.info.response.status, 1);
1470 } else {
1471 RETURN_EMPTY_STRING();
1472 }
1473 }
1474 }
1475
1476 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setResponseStatus, 0, 0, 1)
1477 ZEND_ARG_INFO(0, response_status)
1478 ZEND_END_ARG_INFO();
1479 static PHP_METHOD(HttpMessage, setResponseStatus)
1480 {
1481 char *status;
1482 int status_len;
1483 php_http_message_object_t *obj;
1484
1485 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len), invalid_arg, return);
1486
1487 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1488
1489 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1490
1491 if (obj->message->type != PHP_HTTP_RESPONSE) {
1492 php_http_throw(bad_method_call, "http\\Message is not of type response", NULL);
1493 }
1494
1495 STR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
1496 RETVAL_ZVAL(getThis(), 1, 0);
1497 }
1498
1499 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestMethod, 0, 0, 0)
1500 ZEND_END_ARG_INFO();
1501 static PHP_METHOD(HttpMessage, getRequestMethod)
1502 {
1503 if (SUCCESS == zend_parse_parameters_none()) {
1504 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1505
1506 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1507
1508 if (obj->message->type != PHP_HTTP_REQUEST) {
1509 php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not of type request");
1510 RETURN_FALSE;
1511 }
1512
1513 if (obj->message->http.info.request.method) {
1514 RETURN_STRING(obj->message->http.info.request.method, 1);
1515 } else {
1516 RETURN_EMPTY_STRING();
1517 }
1518 }
1519 }
1520
1521 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestMethod, 0, 0, 1)
1522 ZEND_ARG_INFO(0, request_method)
1523 ZEND_END_ARG_INFO();
1524 static PHP_METHOD(HttpMessage, setRequestMethod)
1525 {
1526 char *method;
1527 int method_len;
1528 php_http_message_object_t *obj;
1529
1530 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len), invalid_arg, return);
1531
1532 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1533
1534 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1535
1536 if (obj->message->type != PHP_HTTP_REQUEST) {
1537 php_http_throw(bad_method_call, "http\\Message is not of type request", NULL);
1538 return;
1539 }
1540
1541 if (method_len < 1) {
1542 php_http_throw(invalid_arg, "Cannot set http\\Message's request method to an empty string", NULL);
1543 return;
1544 }
1545
1546 STR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
1547 RETVAL_ZVAL(getThis(), 1, 0);
1548 }
1549
1550 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestUrl, 0, 0, 0)
1551 ZEND_END_ARG_INFO();
1552 static PHP_METHOD(HttpMessage, getRequestUrl)
1553 {
1554 if (SUCCESS == zend_parse_parameters_none()) {
1555 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1556
1557 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1558
1559 if (obj->message->type != PHP_HTTP_REQUEST) {
1560 php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not of type request");
1561 RETURN_FALSE;
1562 }
1563
1564 if (obj->message->http.info.request.url) {
1565 RETURN_STRING(obj->message->http.info.request.url, 1);
1566 } else {
1567 RETURN_EMPTY_STRING();
1568 }
1569 }
1570 }
1571
1572 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setRequestUrl, 0, 0, 1)
1573 ZEND_ARG_INFO(0, url)
1574 ZEND_END_ARG_INFO();
1575 static PHP_METHOD(HttpMessage, setRequestUrl)
1576 {
1577 char *url_str;
1578 int url_len;
1579 php_http_message_object_t *obj;
1580
1581 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &url_str, &url_len), invalid_arg, return);
1582
1583 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1584
1585 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1586
1587 if (obj->message->type != PHP_HTTP_REQUEST) {
1588 php_http_throw(bad_method_call, "http\\Message is not of type request", NULL);
1589 return;
1590 }
1591
1592 if (url_len < 1) {
1593 php_http_throw(invalid_arg, "Cannot set http\\Message's request url to an empty string", NULL);
1594 return;
1595 }
1596
1597 STR_SET(obj->message->http.info.request.url, estrndup(url_str, url_len));
1598 RETVAL_ZVAL(getThis(), 1, 0);
1599 }
1600
1601 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getParentMessage, 0, 0, 0)
1602 ZEND_END_ARG_INFO();
1603 static PHP_METHOD(HttpMessage, getParentMessage)
1604 {
1605 php_http_message_object_t *obj;
1606
1607 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1608
1609 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1610
1611 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1612
1613 if (!obj->message->parent) {
1614 php_http_throw(unexpected_val, "http\\Message has not parent message", NULL);
1615 return;
1616 }
1617
1618 RETVAL_OBJVAL(obj->parent->zv, 1);
1619 }
1620
1621 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___toString, 0, 0, 0)
1622 ZEND_END_ARG_INFO();
1623 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toString, 0, 0, 0)
1624 ZEND_ARG_INFO(0, include_parent)
1625 ZEND_END_ARG_INFO();
1626 static PHP_METHOD(HttpMessage, toString)
1627 {
1628 zend_bool include_parent = 0;
1629
1630 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
1631 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1632 char *string;
1633 size_t length;
1634
1635 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1636
1637 if (include_parent) {
1638 php_http_message_serialize(obj->message, &string, &length);
1639 } else {
1640 php_http_message_to_string(obj->message, &string, &length);
1641 }
1642 if (string) {
1643 RETURN_STRINGL(string, length, 0);
1644 }
1645 }
1646 RETURN_EMPTY_STRING();
1647 }
1648
1649 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toStream, 0, 0, 1)
1650 ZEND_ARG_INFO(0, stream)
1651 ZEND_END_ARG_INFO();
1652 static PHP_METHOD(HttpMessage, toStream)
1653 {
1654 zval *zstream;
1655
1656 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream)) {
1657 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1658 php_stream *s;
1659
1660 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1661
1662 php_stream_from_zval(s, &zstream);
1663 php_http_message_to_callback(obj->message, (php_http_pass_callback_t) _php_stream_write, s);
1664 }
1665 }
1666
1667 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toCallback, 0, 0, 1)
1668 ZEND_ARG_INFO(0, callback)
1669 ZEND_END_ARG_INFO();
1670 static PHP_METHOD(HttpMessage, toCallback)
1671 {
1672 php_http_pass_fcall_arg_t fcd;
1673
1674 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f", &fcd.fci, &fcd.fcc)) {
1675 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1676
1677 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1678
1679 fcd.fcz = getThis();
1680 Z_ADDREF_P(fcd.fcz);
1681 TSRMLS_SET_CTX(fcd.ts);
1682
1683 php_http_message_to_callback(obj->message, php_http_pass_fcall_callback, &fcd);
1684 zend_fcall_info_args_clear(&fcd.fci, 1);
1685
1686 zval_ptr_dtor(&fcd.fcz);
1687 RETURN_ZVAL(getThis(), 1, 0);
1688 }
1689 }
1690
1691 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_serialize, 0, 0, 0)
1692 ZEND_END_ARG_INFO();
1693 static PHP_METHOD(HttpMessage, serialize)
1694 {
1695 if (SUCCESS == zend_parse_parameters_none()) {
1696 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1697 char *string;
1698 size_t length;
1699
1700 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1701
1702 php_http_message_serialize(obj->message, &string, &length);
1703 RETURN_STRINGL(string, length, 0);
1704 }
1705 RETURN_EMPTY_STRING();
1706 }
1707
1708 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_unserialize, 0, 0, 1)
1709 ZEND_ARG_INFO(0, serialized)
1710 ZEND_END_ARG_INFO();
1711 static PHP_METHOD(HttpMessage, unserialize)
1712 {
1713 int length;
1714 char *serialized;
1715
1716 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
1717 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1718 php_http_message_t *msg;
1719
1720 if (obj->message) {
1721 php_http_message_dtor(obj->message);
1722 efree(obj->message);
1723 }
1724 if ((msg = php_http_message_parse(NULL, serialized, (size_t) length, 1 TSRMLS_CC))) {
1725 obj->message = msg;
1726 } else {
1727 obj->message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
1728 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not unserialize http\\Message");
1729 }
1730 }
1731 }
1732
1733 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_detach, 0, 0, 0)
1734 ZEND_END_ARG_INFO();
1735 static PHP_METHOD(HttpMessage, detach)
1736 {
1737 php_http_message_object_t *obj;
1738
1739 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1740
1741 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1742
1743 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1744
1745 RETVAL_OBJVAL(php_http_message_object_new_ex(obj->zo.ce, php_http_message_copy_ex(obj->message, NULL, 0), NULL TSRMLS_CC), 0);
1746 }
1747
1748 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_prepend, 0, 0, 1)
1749 ZEND_ARG_OBJ_INFO(0, message, http\\Message, 0)
1750 ZEND_ARG_INFO(0, top)
1751 ZEND_END_ARG_INFO();
1752 static PHP_METHOD(HttpMessage, prepend)
1753 {
1754 zval *prepend;
1755 zend_bool top = 1;
1756 php_http_message_t *msg[2];
1757 php_http_message_object_t *obj, *prepend_obj;
1758
1759 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, php_http_message_class_entry, &top), invalid_arg, return);
1760
1761 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1762 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1763 prepend_obj = zend_object_store_get_object(prepend TSRMLS_CC);
1764 PHP_HTTP_MESSAGE_OBJECT_INIT(prepend_obj);
1765
1766 /* safety check */
1767 for (msg[0] = obj->message; msg[0]; msg[0] = msg[0]->parent) {
1768 for (msg[1] = prepend_obj->message; msg[1]; msg[1] = msg[1]->parent) {
1769 if (msg[0] == msg[1]) {
1770 php_http_throw(unexpected_val, "Cannot prepend a message located within the same message chain", NULL);
1771 return;
1772 }
1773 }
1774 }
1775
1776 php_http_message_object_prepend(getThis(), prepend, top TSRMLS_CC);
1777 RETURN_ZVAL(getThis(), 1, 0);
1778 }
1779
1780 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_reverse, 0, 0, 0)
1781 ZEND_END_ARG_INFO();
1782 static PHP_METHOD(HttpMessage, reverse)
1783 {
1784 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1785
1786 php_http_message_object_reverse(getThis(), return_value TSRMLS_CC);
1787 }
1788
1789 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_isMultipart, 0, 0, 0)
1790 ZEND_ARG_INFO(1, boundary)
1791 ZEND_END_ARG_INFO();
1792 static PHP_METHOD(HttpMessage, isMultipart)
1793 {
1794 zval *zboundary = NULL;
1795
1796 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &zboundary)) {
1797 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1798 char *boundary = NULL;
1799
1800 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1801
1802 RETVAL_BOOL(php_http_message_is_multipart(obj->message, zboundary ? &boundary : NULL));
1803
1804 if (zboundary && boundary) {
1805 zval_dtor(zboundary);
1806 ZVAL_STRING(zboundary, boundary, 0);
1807 }
1808 }
1809 }
1810
1811 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_splitMultipartBody, 0, 0, 0)
1812 ZEND_END_ARG_INFO();
1813 static PHP_METHOD(HttpMessage, splitMultipartBody)
1814 {
1815 php_http_message_object_t *obj;
1816 php_http_message_t *msg;
1817 char *boundary = NULL;
1818
1819 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1820
1821 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1822
1823 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1824
1825 if (!php_http_message_is_multipart(obj->message, &boundary)) {
1826 php_http_throw(bad_method_call, "http\\Message is not a multipart message", NULL);
1827 return;
1828 }
1829
1830 php_http_expect(msg = php_http_message_body_split(obj->message->body, boundary), bad_message, return);
1831
1832 STR_FREE(boundary);
1833
1834 RETURN_OBJVAL(php_http_message_object_new_ex(php_http_message_class_entry, msg, NULL TSRMLS_CC), 0);
1835 }
1836
1837 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_count, 0, 0, 0)
1838 ZEND_END_ARG_INFO();
1839 static PHP_METHOD(HttpMessage, count)
1840 {
1841 long count_mode = -1;
1842
1843 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &count_mode)) {
1844 long i = 0;
1845 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1846
1847 PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
1848
1849 php_http_message_count(i, obj->message);
1850 RETURN_LONG(i);
1851 }
1852 }
1853
1854 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_rewind, 0, 0, 0)
1855 ZEND_END_ARG_INFO();
1856 static PHP_METHOD(HttpMessage, rewind)
1857 {
1858 if (SUCCESS == zend_parse_parameters_none()) {
1859 zval *zobj = getThis();
1860 php_http_message_object_t *obj = zend_object_store_get_object(zobj TSRMLS_CC);
1861
1862 if (obj->iterator) {
1863 zval_ptr_dtor(&obj->iterator);
1864 }
1865 Z_ADDREF_P(zobj);
1866 obj->iterator = zobj;
1867 }
1868 }
1869
1870 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_valid, 0, 0, 0)
1871 ZEND_END_ARG_INFO();
1872 static PHP_METHOD(HttpMessage, valid)
1873 {
1874 if (SUCCESS == zend_parse_parameters_none()) {
1875 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1876
1877 RETURN_BOOL(obj->iterator != NULL);
1878 }
1879 }
1880
1881 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_next, 0, 0, 0)
1882 ZEND_END_ARG_INFO();
1883 static PHP_METHOD(HttpMessage, next)
1884 {
1885 if (SUCCESS == zend_parse_parameters_none()) {
1886 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1887
1888 if (obj->iterator) {
1889 php_http_message_object_t *itr = zend_object_store_get_object(obj->iterator TSRMLS_CC);
1890
1891 if (itr && itr->parent) {
1892 zval *old = obj->iterator;
1893 MAKE_STD_ZVAL(obj->iterator);
1894 ZVAL_OBJVAL(obj->iterator, itr->parent->zv, 1);
1895 zval_ptr_dtor(&old);
1896 } else {
1897 zval_ptr_dtor(&obj->iterator);
1898 obj->iterator = NULL;
1899 }
1900 }
1901 }
1902 }
1903
1904 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_key, 0, 0, 0)
1905 ZEND_END_ARG_INFO();
1906 static PHP_METHOD(HttpMessage, key)
1907 {
1908 if (SUCCESS == zend_parse_parameters_none()) {
1909 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1910
1911 RETURN_LONG(obj->iterator ? obj->iterator->value.obj.handle:0);
1912 }
1913 }
1914
1915 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_current, 0, 0, 0)
1916 ZEND_END_ARG_INFO();
1917 static PHP_METHOD(HttpMessage, current)
1918 {
1919 if (SUCCESS == zend_parse_parameters_none()) {
1920 php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1921
1922 if (obj->iterator) {
1923 RETURN_ZVAL(obj->iterator, 1, 0);
1924 }
1925 }
1926 }
1927
1928 static zend_function_entry php_http_message_methods[] = {
1929 PHP_ME(HttpMessage, __construct, ai_HttpMessage___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1930 PHP_ME(HttpMessage, getBody, ai_HttpMessage_getBody, ZEND_ACC_PUBLIC)
1931 PHP_ME(HttpMessage, setBody, ai_HttpMessage_setBody, ZEND_ACC_PUBLIC)
1932 PHP_ME(HttpMessage, addBody, ai_HttpMessage_addBody, ZEND_ACC_PUBLIC)
1933 PHP_ME(HttpMessage, getHeader, ai_HttpMessage_getHeader, ZEND_ACC_PUBLIC)
1934 PHP_ME(HttpMessage, setHeader, ai_HttpMessage_setHeader, ZEND_ACC_PUBLIC)
1935 PHP_ME(HttpMessage, addHeader, ai_HttpMessage_addHeader, ZEND_ACC_PUBLIC)
1936 PHP_ME(HttpMessage, getHeaders, ai_HttpMessage_getHeaders, ZEND_ACC_PUBLIC)
1937 PHP_ME(HttpMessage, setHeaders, ai_HttpMessage_setHeaders, ZEND_ACC_PUBLIC)
1938 PHP_ME(HttpMessage, addHeaders, ai_HttpMessage_addHeaders, ZEND_ACC_PUBLIC)
1939 PHP_ME(HttpMessage, getType, ai_HttpMessage_getType, ZEND_ACC_PUBLIC)
1940 PHP_ME(HttpMessage, setType, ai_HttpMessage_setType, ZEND_ACC_PUBLIC)
1941 PHP_ME(HttpMessage, getInfo, ai_HttpMessage_getInfo, ZEND_ACC_PUBLIC)
1942 PHP_ME(HttpMessage, setInfo, ai_HttpMessage_setInfo, ZEND_ACC_PUBLIC)
1943 PHP_ME(HttpMessage, getResponseCode, ai_HttpMessage_getResponseCode, ZEND_ACC_PUBLIC)
1944 PHP_ME(HttpMessage, setResponseCode, ai_HttpMessage_setResponseCode, ZEND_ACC_PUBLIC)
1945 PHP_ME(HttpMessage, getResponseStatus, ai_HttpMessage_getResponseStatus, ZEND_ACC_PUBLIC)
1946 PHP_ME(HttpMessage, setResponseStatus, ai_HttpMessage_setResponseStatus, ZEND_ACC_PUBLIC)
1947 PHP_ME(HttpMessage, getRequestMethod, ai_HttpMessage_getRequestMethod, ZEND_ACC_PUBLIC)
1948 PHP_ME(HttpMessage, setRequestMethod, ai_HttpMessage_setRequestMethod, ZEND_ACC_PUBLIC)
1949 PHP_ME(HttpMessage, getRequestUrl, ai_HttpMessage_getRequestUrl, ZEND_ACC_PUBLIC)
1950 PHP_ME(HttpMessage, setRequestUrl, ai_HttpMessage_setRequestUrl, ZEND_ACC_PUBLIC)
1951 PHP_ME(HttpMessage, getHttpVersion, ai_HttpMessage_getHttpVersion, ZEND_ACC_PUBLIC)
1952 PHP_ME(HttpMessage, setHttpVersion, ai_HttpMessage_setHttpVersion, ZEND_ACC_PUBLIC)
1953 PHP_ME(HttpMessage, getParentMessage, ai_HttpMessage_getParentMessage, ZEND_ACC_PUBLIC)
1954 PHP_ME(HttpMessage, toString, ai_HttpMessage_toString, ZEND_ACC_PUBLIC)
1955 PHP_ME(HttpMessage, toCallback, ai_HttpMessage_toCallback, ZEND_ACC_PUBLIC)
1956 PHP_ME(HttpMessage, toStream, ai_HttpMessage_toStream, ZEND_ACC_PUBLIC)
1957
1958 /* implements Countable */
1959 PHP_ME(HttpMessage, count, ai_HttpMessage_count, ZEND_ACC_PUBLIC)
1960
1961 /* implements Serializable */
1962 PHP_ME(HttpMessage, serialize, ai_HttpMessage_serialize, ZEND_ACC_PUBLIC)
1963 PHP_ME(HttpMessage, unserialize, ai_HttpMessage_unserialize, ZEND_ACC_PUBLIC)
1964
1965 /* implements Iterator */
1966 PHP_ME(HttpMessage, rewind, ai_HttpMessage_rewind, ZEND_ACC_PUBLIC)
1967 PHP_ME(HttpMessage, valid, ai_HttpMessage_valid, ZEND_ACC_PUBLIC)
1968 PHP_ME(HttpMessage, current, ai_HttpMessage_current, ZEND_ACC_PUBLIC)
1969 PHP_ME(HttpMessage, key, ai_HttpMessage_key, ZEND_ACC_PUBLIC)
1970 PHP_ME(HttpMessage, next, ai_HttpMessage_next, ZEND_ACC_PUBLIC)
1971
1972 ZEND_MALIAS(HttpMessage, __toString, toString, ai_HttpMessage___toString, ZEND_ACC_PUBLIC)
1973
1974 PHP_ME(HttpMessage, detach, ai_HttpMessage_detach, ZEND_ACC_PUBLIC)
1975 PHP_ME(HttpMessage, prepend, ai_HttpMessage_prepend, ZEND_ACC_PUBLIC)
1976 PHP_ME(HttpMessage, reverse, ai_HttpMessage_reverse, ZEND_ACC_PUBLIC)
1977
1978 PHP_ME(HttpMessage, isMultipart, ai_HttpMessage_isMultipart, ZEND_ACC_PUBLIC)
1979 PHP_ME(HttpMessage, splitMultipartBody, ai_HttpMessage_splitMultipartBody, ZEND_ACC_PUBLIC)
1980
1981 EMPTY_FUNCTION_ENTRY
1982 };
1983
1984 zend_class_entry *php_http_message_class_entry;
1985
1986 PHP_MINIT_FUNCTION(http_message)
1987 {
1988 zend_class_entry ce = {0};
1989
1990 INIT_NS_CLASS_ENTRY(ce, "http", "Message", php_http_message_methods);
1991 php_http_message_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
1992 php_http_message_class_entry->create_object = php_http_message_object_new;
1993 memcpy(&php_http_message_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1994 php_http_message_object_handlers.clone_obj = php_http_message_object_clone;
1995 php_http_message_object_handlers.read_property = php_http_message_object_read_prop;
1996 php_http_message_object_handlers.write_property = php_http_message_object_write_prop;
1997 php_http_message_object_handlers.get_properties = php_http_message_object_get_props;
1998 php_http_message_object_handlers.get_property_ptr_ptr = NULL;
1999
2000 zend_class_implements(php_http_message_class_entry TSRMLS_CC, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
2001
2002 zend_hash_init(&php_http_message_object_prophandlers, 9, NULL, NULL, 1);
2003 zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("type"), PHP_HTTP_NONE, ZEND_ACC_PROTECTED TSRMLS_CC);
2004 php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type, php_http_message_object_prophandler_set_type);
2005 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("body"), ZEND_ACC_PROTECTED TSRMLS_CC);
2006 php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body, php_http_message_object_prophandler_set_body);
2007 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
2008 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);
2009 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
2010 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);
2011 zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
2012 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);
2013 zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
2014 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);
2015 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC);
2016 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);
2017 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC);
2018 php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers, php_http_message_object_prophandler_set_headers);
2019 zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC);
2020 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);
2021
2022 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC);
2023 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC);
2024 zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC);
2025
2026 return SUCCESS;
2027 }
2028
2029 PHP_MSHUTDOWN_FUNCTION(http_message)
2030 {
2031 zend_hash_destroy(&php_http_message_object_prophandlers);
2032
2033 return SUCCESS;
2034 }
2035
2036 /*
2037 * Local variables:
2038 * tab-width: 4
2039 * c-basic-offset: 4
2040 * End:
2041 * vim600: noet sw=4 ts=4 fdm=marker
2042 * vim<600: noet sw=4 ts=4
2043 */
2044