hack PHP_5_4 compatibility into v1
[m6w6/ext-http] / http_message_api.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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_ZLIB
18 #include "php_http.h"
19
20 #include "php_http_api.h"
21 #include "php_http_encoding_api.h"
22 #include "php_http_headers_api.h"
23 #include "php_http_message_api.h"
24 #include "php_http_request_api.h"
25 #include "php_http_send_api.h"
26 #include "php_http_url_api.h"
27
28 #define http_message_info_callback _http_message_info_callback
29 static void _http_message_info_callback(http_message **message, HashTable **headers, http_info *info TSRMLS_DC)
30 {
31 http_message *old = *message;
32
33 /* advance message */
34 if (old->type || zend_hash_num_elements(&old->hdrs) || PHPSTR_LEN(old)) {
35 (*message) = http_message_new();
36 (*message)->parent = old;
37 (*headers) = &((*message)->hdrs);
38 }
39
40 http_message_set_info(*message, info);
41 }
42
43 #define http_message_init_type _http_message_init_type
44 static inline void _http_message_init_type(http_message *message, http_message_type type)
45 {
46 message->http.version = .0;
47
48 switch (message->type = type) {
49 case HTTP_MSG_RESPONSE:
50 message->http.info.response.code = 0;
51 message->http.info.response.status = NULL;
52 break;
53
54 case HTTP_MSG_REQUEST:
55 message->http.info.request.method = NULL;
56 message->http.info.request.url = NULL;
57 break;
58
59 case HTTP_MSG_NONE:
60 default:
61 break;
62 }
63 }
64
65 PHP_HTTP_API http_message *_http_message_init_ex(http_message *message, http_message_type type ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
66 {
67 if (!message) {
68 message = ecalloc_rel(1, sizeof(http_message));
69 }
70
71 http_message_init_type(message, type);
72 message->parent = NULL;
73 phpstr_init(&message->body);
74 zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
75
76 return message;
77 }
78
79 PHP_HTTP_API http_message *_http_message_init_env(http_message *message, http_message_type type TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
80 {
81 int free_msg;
82 http_info inf;
83 zval *sval, tval;
84 char *body_str;
85 size_t body_len;
86
87 if ((free_msg = !message)) {
88 message = http_message_init_rel(NULL, HTTP_MSG_NONE);
89 }
90
91 memset(&inf, 0, sizeof(http_info));
92 switch (inf.type = type) {
93 case HTTP_MSG_REQUEST:
94 if ((sval = http_get_server_var("SERVER_PROTOCOL", 1)) && !strncmp(Z_STRVAL_P(sval), "HTTP/", lenof("HTTP/"))) {
95 inf.http.version = zend_strtod(Z_STRVAL_P(sval) + lenof("HTTP/"), NULL);
96 } else {
97 inf.http.version = 1.1;
98 }
99 if ((sval = http_get_server_var("REQUEST_METHOD", 1))) {
100 inf.http.info.request.method = estrdup(Z_STRVAL_P(sval));
101 }
102 if ((sval = http_get_server_var("REQUEST_URI", 1))) {
103 inf.http.info.request.url = estrdup(Z_STRVAL_P(sval));
104 }
105
106 http_message_set_info(message, &inf);
107 http_get_request_headers(&message->hdrs);
108 if (SUCCESS == http_get_request_body_ex(&body_str, &body_len, 0)) {
109 phpstr_from_string_ex(&message->body, body_str, body_len);
110 }
111 break;
112
113 case HTTP_MSG_RESPONSE:
114 if (!SG(sapi_headers).http_status_line || SUCCESS != http_info_parse_ex(SG(sapi_headers).http_status_line, &inf, 0)) {
115 inf.http.version = 1.1;
116 inf.http.info.response.code = 200;
117 inf.http.info.response.status = estrdup("Ok");
118 }
119
120 http_message_set_info(message, &inf);
121 http_get_response_headers(&message->hdrs);
122 #ifdef PHP_OUTPUT_NEWAPI
123 if (SUCCESS == php_output_get_contents(&tval TSRMLS_CC)) {
124 #else
125 if (SUCCESS == php_ob_get_buffer(&tval TSRMLS_CC)) {
126 #endif
127 message->body.data = Z_STRVAL(tval);
128 message->body.used = Z_STRLEN(tval);
129 message->body.free = 1; /* "\0" */
130 }
131 break;
132
133 default:
134 if (free_msg) {
135 http_message_free(&message);
136 } else {
137 message = NULL;
138 }
139 break;
140 }
141 http_info_dtor(&inf);
142
143 return message;
144 }
145
146 PHP_HTTP_API void _http_message_set_type(http_message *message, http_message_type type)
147 {
148 /* just act if different */
149 if (type != message->type) {
150
151 /* free request info */
152 switch (message->type) {
153 case HTTP_MSG_REQUEST:
154 STR_FREE(message->http.info.request.method);
155 STR_FREE(message->http.info.request.url);
156 break;
157
158 case HTTP_MSG_RESPONSE:
159 STR_FREE(message->http.info.response.status);
160 break;
161
162 default:
163 break;
164 }
165
166 /* init */
167 http_message_init_type(message, type);
168 }
169 }
170
171 PHP_HTTP_API void _http_message_set_info(http_message *message, http_info *info)
172 {
173 http_message_set_type(message, info->type);
174 message->http.version = info->http.version;
175 switch (message->type) {
176 case IS_HTTP_REQUEST:
177 STR_SET(HTTP_INFO(message).request.url, HTTP_INFO(info).request.url ? estrdup(HTTP_INFO(info).request.url) : NULL);
178 STR_SET(HTTP_INFO(message).request.method, HTTP_INFO(info).request.method ? estrdup(HTTP_INFO(info).request.method) : NULL);
179 break;
180
181 case IS_HTTP_RESPONSE:
182 HTTP_INFO(message).response.code = HTTP_INFO(info).response.code;
183 STR_SET(HTTP_INFO(message).response.status, HTTP_INFO(info).response.status ? estrdup(HTTP_INFO(info).response.status) : NULL);
184 break;
185
186 default:
187 break;
188 }
189 }
190
191 #define http_message_body_parse(m, ms, ml, c) _http_message_body_parse((m), (ms), (ml), (c) TSRMLS_CC)
192 static inline void _http_message_body_parse(http_message *msg, const char *message, size_t message_length, const char **continue_at TSRMLS_DC)
193 {
194 zval *c;
195 size_t remaining;
196 const char *body;
197
198 *continue_at = NULL;
199 if ((body = http_locate_body(message))) {
200 remaining = message + message_length - body;
201
202 if ((c = http_message_header(msg, "Transfer-Encoding"))) {
203 if (strstr(Z_STRVAL_P(c), "chunked")) {
204 /* message has chunked transfer encoding */
205 char *decoded;
206 size_t decoded_len;
207
208 /* decode and replace Transfer-Encoding with Content-Length header */
209 if ((*continue_at = http_encoding_dechunk(body, message + message_length - body, &decoded, &decoded_len))) {
210 zval *len;
211 char *tmp;
212 int tmp_len;
213
214 tmp_len = (int) spprintf(&tmp, 0, "%zu", decoded_len);
215 MAKE_STD_ZVAL(len);
216 ZVAL_STRINGL(len, tmp, tmp_len, 0);
217
218 ZVAL_ADDREF(c);
219 zend_hash_update(&msg->hdrs, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), (void *) &c, sizeof(zval *), NULL);
220 zend_hash_del(&msg->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding"));
221 zend_hash_del(&msg->hdrs, "Content-Length", sizeof("Content-Length"));
222 zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
223
224 phpstr_from_string_ex(PHPSTR(msg), decoded, decoded_len);
225 efree(decoded);
226 }
227 }
228 zval_ptr_dtor(&c);
229 }
230
231 if (!*continue_at && (c = http_message_header(msg, "Content-Length"))) {
232 /* message has content-length header */
233 ulong len = strtoul(Z_STRVAL_P(c), NULL, 10);
234 if (len > remaining) {
235 http_error_ex(HE_NOTICE, HTTP_E_MALFORMED_HEADERS, "The Content-Length header pretends a larger body than actually received (expected %lu bytes; got %lu bytes)", len, remaining);
236 len = remaining;
237 }
238 phpstr_from_string_ex(PHPSTR(msg), body, len);
239 *continue_at = body + len;
240 zval_ptr_dtor(&c);
241 }
242
243 if (!*continue_at && (c = http_message_header(msg, "Content-Range"))) {
244 /* message has content-range header */
245 ulong total = 0, start = 0, end = 0, len = 0;
246
247 if (!strncasecmp(Z_STRVAL_P(c), "bytes", lenof("bytes")) &&
248 ( Z_STRVAL_P(c)[lenof("bytes")] == ':' ||
249 Z_STRVAL_P(c)[lenof("bytes")] == ' ' ||
250 Z_STRVAL_P(c)[lenof("bytes")] == '=')) {
251 char *total_at = NULL, *end_at = NULL;
252 char *start_at = Z_STRVAL_P(c) + sizeof("bytes");
253
254 start = strtoul(start_at, &end_at, 10);
255 if (end_at) {
256 end = strtoul(end_at + 1, &total_at, 10);
257 if (total_at && strncmp(total_at + 1, "*", 1)) {
258 total = strtoul(total_at + 1, NULL, 10);
259 }
260 if ((len = (end + 1 - start)) > remaining) {
261 http_error_ex(HE_NOTICE, HTTP_E_MALFORMED_HEADERS, "The Content-Range header pretends a larger body than actually received (expected %lu bytes; got %lu bytes)", len, remaining);
262 len = remaining;
263 }
264 if (end >= start && (!total || end < total)) {
265 phpstr_from_string_ex(PHPSTR(msg), body, len);
266 *continue_at = body + len;
267 }
268 }
269 }
270
271 if (!*continue_at) {
272 http_error_ex(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Invalid Content-Range header: %s", Z_STRVAL_P(c));
273 }
274 zval_ptr_dtor(&c);
275 }
276
277 if (!*continue_at) {
278 /* no headers that indicate content length */
279 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
280 phpstr_from_string_ex(PHPSTR(msg), body, remaining);
281 } else {
282 *continue_at = body;
283 }
284 }
285
286 #ifdef HTTP_HAVE_ZLIB
287 /* check for compressed data */
288 if ((c = http_message_header(msg, "Content-Encoding"))) {
289 char *decoded = NULL;
290 size_t decoded_len = 0;
291
292 if ( !strcasecmp(Z_STRVAL_P(c), "gzip") ||
293 !strcasecmp(Z_STRVAL_P(c), "x-gzip") ||
294 !strcasecmp(Z_STRVAL_P(c), "deflate")) {
295 http_encoding_inflate(PHPSTR_VAL(msg), PHPSTR_LEN(msg), &decoded, &decoded_len);
296 }
297
298 if (decoded) {
299 zval *len, **original_len;
300 char *tmp;
301 int tmp_len;
302
303 tmp_len = (int) spprintf(&tmp, 0, "%zu", decoded_len);
304 MAKE_STD_ZVAL(len);
305 ZVAL_STRINGL(len, tmp, tmp_len, 0);
306
307 ZVAL_ADDREF(c);
308 zend_hash_update(&msg->hdrs, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), (void *) &c, sizeof(zval *), NULL);
309 zend_hash_del(&msg->hdrs, "Content-Encoding", sizeof("Content-Encoding"));
310 if (SUCCESS == zend_hash_find(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &original_len)) {
311 ZVAL_ADDREF(*original_len);
312 zend_hash_update(&msg->hdrs, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), (void *) original_len, sizeof(zval *), NULL);
313 zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
314 } else {
315 zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
316 }
317
318 phpstr_dtor(PHPSTR(msg));
319 PHPSTR(msg)->data = decoded;
320 PHPSTR(msg)->used = decoded_len;
321 PHPSTR(msg)->free = 1;
322 }
323
324 zval_ptr_dtor(&c);
325 }
326 #endif /* HTTP_HAVE_ZLIB */
327 }
328 }
329
330 PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char *message, size_t message_length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
331 {
332 const char *continue_at;
333 zend_bool free_msg = msg ? 0 : 1;
334
335 if ((!message) || (message_length < HTTP_MSG_MIN_SIZE)) {
336 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Empty or too short HTTP message: '%s'", message);
337 return NULL;
338 }
339
340 msg = http_message_init_rel(msg, 0);
341
342 if (SUCCESS != http_parse_headers_cb(message, &msg->hdrs, 1, (http_info_callback) http_message_info_callback, (void *) &msg)) {
343 if (free_msg) {
344 http_message_free(&msg);
345 }
346 http_error(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Failed to parse message headers");
347 return NULL;
348 }
349
350 http_message_body_parse(msg, message, message_length, &continue_at);
351
352 /* check for following messages */
353 if (continue_at && (continue_at < (message + message_length))) {
354 while (HTTP_IS_CTYPE(space, *continue_at)) ++continue_at;
355 if (continue_at < (message + message_length)) {
356 http_message *next = NULL, *most = NULL;
357
358 /* set current message to parent of most parent following messages and return deepest */
359 if ((most = next = http_message_parse_rel(NULL, continue_at, message + message_length - continue_at))) {
360 while (most->parent) most = most->parent;
361 most->parent = msg;
362 msg = next;
363 }
364 }
365 }
366
367 return msg;
368 }
369
370 PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length)
371 {
372 phpstr str;
373 HashKey key = initHashKey(0);
374 zval **header;
375 char *data;
376 HashPosition pos1;
377
378 phpstr_init_ex(&str, 4096, 0);
379
380 switch (msg->type) {
381 case HTTP_MSG_REQUEST:
382 phpstr_appendf(&str, HTTP_INFO_REQUEST_FMT_ARGS(&msg->http, HTTP_CRLF));
383 break;
384
385 case HTTP_MSG_RESPONSE:
386 phpstr_appendf(&str, HTTP_INFO_RESPONSE_FMT_ARGS(&msg->http, HTTP_CRLF));
387 break;
388
389 case HTTP_MSG_NONE:
390 default:
391 break;
392 }
393
394 FOREACH_HASH_KEYVAL(pos1, &msg->hdrs, key, header) {
395 if (key.type == HASH_KEY_IS_STRING) {
396 HashPosition pos2;
397 zval **single_header;
398
399 switch (Z_TYPE_PP(header)) {
400 case IS_BOOL:
401 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key.str, Z_BVAL_PP(header)?"true":"false");
402 break;
403
404 case IS_LONG:
405 phpstr_appendf(&str, "%s: %ld" HTTP_CRLF, key.str, Z_LVAL_PP(header));
406 break;
407
408 case IS_DOUBLE:
409 phpstr_appendf(&str, "%s: %f" HTTP_CRLF, key.str, Z_DVAL_PP(header));
410 break;
411
412 case IS_STRING:
413 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key.str, Z_STRVAL_PP(header));
414 break;
415
416 case IS_ARRAY:
417 FOREACH_VAL(pos2, *header, single_header) {
418 switch (Z_TYPE_PP(single_header)) {
419 case IS_BOOL:
420 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key.str, Z_BVAL_PP(single_header)?"true":"false");
421 break;
422
423 case IS_LONG:
424 phpstr_appendf(&str, "%s: %ld" HTTP_CRLF, key.str, Z_LVAL_PP(single_header));
425 break;
426
427 case IS_DOUBLE:
428 phpstr_appendf(&str, "%s: %f" HTTP_CRLF, key.str, Z_DVAL_PP(single_header));
429 break;
430
431 case IS_STRING:
432 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key.str, Z_STRVAL_PP(single_header));
433 break;
434 }
435 }
436 break;
437 }
438 }
439 }
440
441 if (PHPSTR_LEN(msg)) {
442 phpstr_appends(&str, HTTP_CRLF);
443 phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg));
444 phpstr_appends(&str, HTTP_CRLF);
445 }
446
447 data = phpstr_data(&str, string, length);
448 if (!string) {
449 efree(data);
450 }
451
452 phpstr_dtor(&str);
453 }
454
455 PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length)
456 {
457 char *buf;
458 size_t len;
459 phpstr str;
460
461 phpstr_init(&str);
462
463 do {
464 http_message_tostring(message, &buf, &len);
465 phpstr_prepend(&str, buf, len);
466 efree(buf);
467 } while ((message = message->parent));
468
469 buf = phpstr_data(&str, string, length);
470 if (!string) {
471 efree(buf);
472 }
473
474 phpstr_dtor(&str);
475 }
476
477 PHP_HTTP_API http_message *_http_message_reverse(http_message *msg)
478 {
479 int i, c;
480
481 http_message_count(c, msg);
482
483 if (c > 1) {
484 http_message *tmp = msg, **arr = ecalloc(c, sizeof(http_message *));
485
486 for (i = 0; i < c; ++i) {
487 arr[i] = tmp;
488 tmp = tmp->parent;
489 }
490 arr[0]->parent = NULL;
491 for (i = 0; i < c-1; ++i) {
492 arr[i+1]->parent = arr[i];
493 }
494
495 msg = arr[c-1];
496 efree(arr);
497 }
498
499 return msg;
500 }
501
502 PHP_HTTP_API http_message *_http_message_interconnect(http_message *m1, http_message *m2)
503 {
504 if (m1 && m2) {
505 int i = 0, c1, c2;
506 http_message *t1 = m1, *t2 = m2, *p1, *p2;
507
508 http_message_count(c1, m1);
509 http_message_count(c2, m2);
510
511 while (i++ < (c1 - c2)) {
512 t1 = t1->parent;
513 }
514 while (i++ <= c1) {
515 p1 = t1->parent;
516 p2 = t2->parent;
517 t1->parent = t2;
518 t2->parent = p1;
519 t1 = p1;
520 t2 = p2;
521 }
522 } else if (!m1 && m2) {
523 m1 = m2;
524 }
525 return m1;
526 }
527
528 PHP_HTTP_API void _http_message_tostruct_recursive(http_message *msg, zval *obj TSRMLS_DC)
529 {
530 zval strct;
531 zval *headers;
532
533 INIT_ZARR(strct, HASH_OF(obj));
534
535 add_assoc_long(&strct, "type", msg->type);
536 add_assoc_double(&strct, "httpVersion", msg->http.version);
537 switch (msg->type)
538 {
539 case HTTP_MSG_RESPONSE:
540 add_assoc_long(&strct, "responseCode", msg->http.info.response.code);
541 add_assoc_string(&strct, "responseStatus", STR_PTR(msg->http.info.response.status), 1);
542 break;
543
544 case HTTP_MSG_REQUEST:
545 add_assoc_string(&strct, "requestMethod", STR_PTR(msg->http.info.request.method), 1);
546 add_assoc_string(&strct, "requestUrl", STR_PTR(msg->http.info.request.url), 1);
547 break;
548
549 case HTTP_MSG_NONE:
550 /* avoid compiler warning */
551 break;
552 }
553
554 MAKE_STD_ZVAL(headers);
555 array_init(headers);
556 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
557 add_assoc_zval(&strct, "headers", headers);
558
559 add_assoc_stringl(&strct, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg), 1);
560
561 if (msg->parent) {
562 zval *parent;
563
564 MAKE_STD_ZVAL(parent);
565 if (Z_TYPE_P(obj) == IS_ARRAY) {
566 array_init(parent);
567 } else {
568 object_init(parent);
569 }
570 add_assoc_zval(&strct, "parentMessage", parent);
571 http_message_tostruct_recursive(msg->parent, parent);
572 } else {
573 add_assoc_null(&strct, "parentMessage");
574 }
575 }
576
577 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
578 {
579 STATUS rs = FAILURE;
580
581 switch (message->type) {
582 case HTTP_MSG_RESPONSE:
583 {
584 HashKey key = initHashKey(0);
585 zval **val;
586 HashPosition pos;
587
588 FOREACH_HASH_KEYVAL(pos, &message->hdrs, key, val) {
589 if (key.type == HASH_KEY_IS_STRING) {
590 http_send_header_zval_ex(key.str, key.len-1, val, 1);
591 }
592 }
593 rs = SUCCESS == http_send_status(message->http.info.response.code) &&
594 SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
595 SUCCESS : FAILURE;
596 break;
597 }
598
599 case HTTP_MSG_REQUEST:
600 {
601 #ifdef HTTP_HAVE_CURL
602 char *uri = NULL;
603 http_request request;
604 zval **zhost, *options, *headers;
605
606 MAKE_STD_ZVAL(options);
607 MAKE_STD_ZVAL(headers);
608 array_init(options);
609 array_init(headers);
610 zend_hash_copy(Z_ARRVAL_P(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
611 add_assoc_zval(options, "headers", headers);
612
613 /* check host header */
614 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void *) &zhost) && Z_TYPE_PP(zhost) == IS_STRING) {
615 char *colon = NULL;
616 php_url parts, *url = php_url_parse(message->http.info.request.url);
617
618 memset(&parts, 0, sizeof(php_url));
619
620 /* check for port */
621 if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
622 parts.port = atoi(colon + 1);
623 parts.host = estrndup(Z_STRVAL_PP(zhost), (Z_STRVAL_PP(zhost) - colon - 1));
624 } else {
625 parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost));
626 }
627
628 http_build_url(HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL);
629 php_url_free(url);
630 efree(parts.host);
631 } else {
632 uri = http_absolute_url(message->http.info.request.url);
633 }
634
635 if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
636 http_request_body body;
637
638 http_request_init_ex(&request, NULL, request.meth, uri);
639 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message), 0);
640 if (SUCCESS == (rs = http_request_prepare(&request, Z_ARRVAL_P(options)))) {
641 http_request_exec(&request);
642 }
643 http_request_dtor(&request);
644 } else {
645 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD,
646 "Cannot send HttpMessage. Request method %s not supported",
647 message->http.info.request.method);
648 }
649 efree(uri);
650 zval_ptr_dtor(&options);
651 #else
652 http_error(HE_WARNING, HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
653 #endif
654 break;
655 }
656
657 case HTTP_MSG_NONE:
658 default:
659 http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
660 break;
661 }
662
663 return rs;
664 }
665
666 PHP_HTTP_API http_message *_http_message_dup(http_message *orig TSRMLS_DC)
667 {
668 http_message *temp, *copy = NULL;
669 http_info info;
670
671 if (orig) {
672 info.type = orig->type;
673 info.http = orig->http;
674
675 copy = temp = http_message_new();
676 http_message_set_info(temp, &info);
677 zend_hash_copy(&temp->hdrs, &orig->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
678 phpstr_append(&temp->body, orig->body.data, orig->body.used);
679
680 while (orig->parent) {
681 info.type = orig->parent->type;
682 info.http = orig->parent->http;
683
684 temp->parent = http_message_new();
685 http_message_set_info(temp->parent, &info);
686 zend_hash_copy(&temp->parent->hdrs, &orig->parent->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
687 phpstr_append(&temp->parent->body, orig->parent->body.data, orig->parent->body.used);
688
689 temp = temp->parent;
690 orig = orig->parent;
691 }
692 }
693
694 return copy;
695 }
696
697 PHP_HTTP_API void _http_message_dtor(http_message *message)
698 {
699 if (message) {
700 zend_hash_destroy(&message->hdrs);
701 phpstr_dtor(PHPSTR(message));
702
703 switch (message->type) {
704 case HTTP_MSG_REQUEST:
705 STR_SET(message->http.info.request.method, NULL);
706 STR_SET(message->http.info.request.url, NULL);
707 break;
708
709 case HTTP_MSG_RESPONSE:
710 STR_SET(message->http.info.response.status, NULL);
711 break;
712
713 default:
714 break;
715 }
716 }
717 }
718
719 PHP_HTTP_API void _http_message_free(http_message **message)
720 {
721 if (*message) {
722 if ((*message)->parent) {
723 http_message_free(&(*message)->parent);
724 }
725 http_message_dtor(*message);
726 efree(*message);
727 *message = NULL;
728 }
729 }
730
731 /*
732 * Local variables:
733 * tab-width: 4
734 * c-basic-offset: 4
735 * End:
736 * vim600: noet sw=4 ts=4 fdm=marker
737 * vim<600: noet sw=4 ts=4
738 */
739