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