- implement more of those useful allocation relays
[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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #define HTTP_WANT_CURL
20 #include "php_http.h"
21
22 #include "SAPI.h"
23
24 #include "php_http_api.h"
25 #include "php_http_encoding_api.h"
26 #include "php_http_headers_api.h"
27 #include "php_http_message_api.h"
28 #include "php_http_request_api.h"
29 #include "php_http_send_api.h"
30 #include "php_http_url_api.h"
31
32 ZEND_EXTERN_MODULE_GLOBALS(http);
33
34 #define http_message_info_callback _http_message_info_callback
35 static void _http_message_info_callback(http_message **message, HashTable **headers, http_info *info TSRMLS_DC)
36 {
37 http_message *old = *message;
38
39 /* advance message */
40 if (old->type || zend_hash_num_elements(&old->hdrs) || PHPSTR_LEN(old)) {
41 (*message) = http_message_new();
42 (*message)->parent = old;
43 (*headers) = &((*message)->hdrs);
44 }
45
46 (*message)->http.version = info->http.version;
47
48 switch (info->type)
49 {
50 case IS_HTTP_REQUEST:
51 (*message)->type = HTTP_MSG_REQUEST;
52 HTTP_INFO(*message).request.URI = estrdup(HTTP_INFO(info).request.URI);
53 HTTP_INFO(*message).request.method = estrdup(HTTP_INFO(info).request.method);
54 break;
55
56 case IS_HTTP_RESPONSE:
57 (*message)->type = HTTP_MSG_RESPONSE;
58 HTTP_INFO(*message).response.code = HTTP_INFO(info).response.code;
59 HTTP_INFO(*message).response.status = estrdup(HTTP_INFO(info).response.status);
60 break;
61 }
62 }
63
64 #define http_message_init_type _http_message_init_type
65 static inline void _http_message_init_type(http_message *message, http_message_type type)
66 {
67 message->http.version = .0;
68
69 switch (message->type = type)
70 {
71 case HTTP_MSG_RESPONSE:
72 message->http.info.response.code = 0;
73 message->http.info.response.status = NULL;
74 break;
75
76 case HTTP_MSG_REQUEST:
77 message->http.info.request.method = NULL;
78 message->http.info.request.URI = NULL;
79 break;
80
81 case HTTP_MSG_NONE:
82 default:
83 break;
84 }
85 }
86
87 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)
88 {
89 if (!message) {
90 message = ecalloc_rel(1, sizeof(http_message));
91 }
92
93 http_message_init_type(message, type);
94 message->parent = NULL;
95 phpstr_init(&message->body);
96 zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
97
98 return message;
99 }
100
101
102 PHP_HTTP_API void _http_message_set_type(http_message *message, http_message_type type)
103 {
104 /* just act if different */
105 if (type != message->type) {
106
107 /* free request info */
108 switch (message->type)
109 {
110 case HTTP_MSG_REQUEST:
111 STR_FREE(message->http.info.request.method);
112 STR_FREE(message->http.info.request.URI);
113 break;
114
115 case HTTP_MSG_RESPONSE:
116 STR_FREE(message->http.info.response.status);
117 break;
118
119 default:
120 break;
121 }
122
123 /* init */
124 http_message_init_type(message, type);
125 }
126 }
127
128 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)
129 {
130 const char *body = NULL;
131 zend_bool free_msg = msg ? 0 : 1;
132
133 if ((!message) || (message_length < HTTP_MSG_MIN_SIZE)) {
134 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Empty or too short HTTP message: '%s'", message);
135 return NULL;
136 }
137
138 msg = http_message_init(msg);
139
140 if (SUCCESS != http_parse_headers_cb(message, &msg->hdrs, 1, (http_info_callback) http_message_info_callback, (void **) &msg)) {
141 if (free_msg) {
142 http_message_free(&msg);
143 }
144 http_error(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Failed to parse message headers");
145 return NULL;
146 }
147
148 /* header parsing stops at (CR)LF (CR)LF */
149 if ((body = http_locate_body(message))) {
150 zval *c;
151 const char *continue_at = NULL;
152 size_t remaining = message + message_length - body;
153
154 /* message has chunked transfer encoding */
155 if ((c = http_message_header(msg, "Transfer-Encoding")) && (!strcasecmp("chunked", Z_STRVAL_P(c)))) {
156 char *decoded;
157 size_t decoded_len;
158
159 /* decode and replace Transfer-Encoding with Content-Length header */
160 if ((continue_at = http_encoding_dechunk(body, message + message_length - body, &decoded, &decoded_len))) {
161 zval *len;
162 char *tmp;
163 int tmp_len;
164
165 tmp_len = (int) spprintf(&tmp, 0, "%zu", decoded_len);
166 MAKE_STD_ZVAL(len);
167 ZVAL_STRINGL(len, tmp, tmp_len, 0);
168
169 ZVAL_ADDREF(c);
170 zend_hash_add(&msg->hdrs, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), (void *) &c, sizeof(zval *), NULL);
171 zend_hash_del(&msg->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding"));
172 zend_hash_del(&msg->hdrs, "Content-Length", sizeof("Content-Length"));
173 zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
174
175 phpstr_from_string_ex(PHPSTR(msg), decoded, decoded_len);
176 efree(decoded);
177 }
178 } else
179
180 /* message has content-length header */
181 if ((c = http_message_header(msg, "Content-Length"))) {
182 ulong len = strtoul(Z_STRVAL_P(c), NULL, 10);
183 if (len > remaining) {
184 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);
185 len = remaining;
186 }
187 phpstr_from_string_ex(PHPSTR(msg), body, len);
188 continue_at = body + len;
189 } else
190
191 /* message has content-range header */
192 if ((c = http_message_header(msg, "Content-Range"))) {
193 ulong total = 0, start = 0, end = 0, len = 0;
194
195 if (!strncasecmp(Z_STRVAL_P(c), "bytes", lenof("bytes")) &&
196 (Z_STRVAL_P(c)[lenof("bytes")] == ':' || Z_STRVAL_P(c)[lenof("bytes")] == ' ')) {
197 char *total_at = NULL, *end_at = NULL;
198 char *start_at = Z_STRVAL_P(c) + sizeof("bytes");
199
200 start = strtoul(start_at, &end_at, 10);
201 if (end_at) {
202 end = strtoul(end_at + 1, &total_at, 10);
203 if (total_at && strncmp(total_at + 1, "*", 1)) {
204 total = strtoul(total_at + 1, NULL, 10);
205 }
206 if ((len = (end + 1 - start)) > remaining) {
207 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);
208 len = remaining;
209 }
210 if (end >= start && (!total || end < total)) {
211 phpstr_from_string_ex(PHPSTR(msg), body, len);
212 continue_at = body + len;
213 }
214 }
215 }
216
217 if (!continue_at) {
218 http_error_ex(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Invalid Content-Range header: %s", Z_STRVAL_P(c));
219 }
220 } else
221
222 /* no headers that indicate content length */
223 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
224 phpstr_from_string_ex(PHPSTR(msg), body, remaining);
225 } else {
226 continue_at = body;
227 }
228
229 #if defined(HTTP_HAVE_ZLIB) || defined(HAVE_ZLIB)
230 /* check for compressed data */
231 if (http_message_header(msg, "Vary") && (c = http_message_header(msg, "Content-Encoding"))) {
232 char *decoded = NULL;
233 size_t decoded_len = 0;
234 # if !defined(HTTP_HAVE_ZLIB)
235 zval func, retval, arg, *args[1];
236 INIT_PZVAL(&func);
237 INIT_PZVAL(&retval);
238 INIT_PZVAL(&arg);
239 args[0] = &arg;
240 # endif /* !HTTP_HAVE_ZLIB */
241
242 # define DECODE_WITH_EXT_ZLIB(function, S, L) \
243 ZVAL_STRINGL(&func, function, lenof(function), 0); \
244 ZVAL_STRINGL(&arg, (S), (L), 0); \
245 if (SUCCESS == call_user_function(EG(function_table), NULL, &func, &retval, 1, args TSRMLS_CC)) { \
246 if (Z_TYPE(retval) == IS_STRING) { \
247 decoded = Z_STRVAL(retval); \
248 decoded_len = Z_STRLEN(retval); \
249 } \
250 }
251
252 if (!strcasecmp(Z_STRVAL_P(c), "gzip") || !strcasecmp(Z_STRVAL_P(c), "x-gzip")) {
253 # ifdef HTTP_HAVE_ZLIB
254 http_encoding_gzdecode(PHPSTR_VAL(msg), PHPSTR_LEN(msg), &decoded, &decoded_len);
255 # else
256 DECODE_WITH_EXT_ZLIB("gzinflate", PHPSTR_VAL(msg) + 10, PHPSTR_LEN(msg) - 18);
257 # endif /* HTTP_HAVE_ZLIB */
258 } else if (!strcasecmp(Z_STRVAL_P(c), "deflate")) {
259 # ifdef HTTP_HAVE_ZLIB
260 http_encoding_inflate(PHPSTR_VAL(msg), PHPSTR_LEN(msg), &decoded, &decoded_len);
261 # else
262 DECODE_WITH_EXT_ZLIB("gzinflate", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
263 # endif /* HTTP_HAVE_ZLIB */
264 } else if (!strcasecmp(Z_STRVAL_P(c), "compress") || !strcasecmp(Z_STRVAL_P(c), "x-compress")) {
265 # ifdef HTTP_HAVE_ZLIB
266 http_encoding_uncompress(PHPSTR_VAL(msg), PHPSTR_LEN(msg), &decoded, &decoded_len);
267 # else
268 DECODE_WITH_EXT_ZLIB("gzuncompress", PHPSTR_VAL(msg), PHPSTR_LEN(msg));
269 # endif /* HTTP_HAVE_ZLIB */
270 }
271
272 if (decoded) {
273 zval *len, **original_len;
274 char *tmp;
275 int tmp_len;
276
277 tmp_len = (int) spprintf(&tmp, 0, "%zu", decoded_len);
278 MAKE_STD_ZVAL(len);
279 ZVAL_STRINGL(len, tmp, tmp_len, 0);
280
281 ZVAL_ADDREF(c);
282 zend_hash_add(&msg->hdrs, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), (void *) &c, sizeof(zval *), NULL);
283 zend_hash_del(&msg->hdrs, "Content-Encoding", sizeof("Content-Encoding"));
284 if (SUCCESS == zend_hash_find(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void **) &original_len)) {
285 ZVAL_ADDREF(*original_len);
286 zend_hash_add(&msg->hdrs, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), (void *) original_len, sizeof(zval *), NULL);
287 zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
288 } else {
289 zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
290 }
291
292 phpstr_dtor(PHPSTR(msg));
293 PHPSTR(msg)->data = decoded;
294 PHPSTR(msg)->used = decoded_len;
295 PHPSTR(msg)->free = 1;
296 }
297 }
298 #endif /* HTTP_HAVE_ZLIB || HAVE_ZLIB */
299
300 /* check for following messages */
301 if (continue_at) {
302 while (isspace(*continue_at)) ++continue_at;
303 if (continue_at < (message + message_length)) {
304 http_message *next = NULL, *most = NULL;
305
306 /* set current message to parent of most parent following messages and return deepest */
307 if ((most = next = http_message_parse(continue_at, message + message_length - continue_at))) {
308 while (most->parent) most = most->parent;
309 most->parent = msg;
310 msg = next;
311 }
312 }
313 }
314 }
315
316 return msg;
317 }
318
319 PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length)
320 {
321 phpstr str;
322 char *key, *data;
323 ulong idx;
324 zval **header;
325 HashPosition pos1;
326
327 phpstr_init_ex(&str, 4096, 0);
328
329 switch (msg->type)
330 {
331 case HTTP_MSG_REQUEST:
332 phpstr_appendf(&str, "%s %s HTTP/%1.1f" HTTP_CRLF,
333 msg->http.info.request.method,
334 msg->http.info.request.URI,
335 msg->http.version);
336 break;
337
338 case HTTP_MSG_RESPONSE:
339 phpstr_appendf(&str, "HTTP/%1.1f %d%s%s" HTTP_CRLF,
340 msg->http.version,
341 msg->http.info.response.code,
342 *msg->http.info.response.status ? " ":"",
343 msg->http.info.response.status);
344 break;
345
346 case HTTP_MSG_NONE:
347 default:
348 break;
349 }
350
351 FOREACH_HASH_KEYVAL(pos1, &msg->hdrs, key, idx, header) {
352 if (key) {
353 zval **single_header;
354
355 switch (Z_TYPE_PP(header))
356 {
357 case IS_STRING:
358 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(header));
359 break;
360
361 case IS_ARRAY:
362 {
363 HashPosition pos2;
364 FOREACH_VAL(pos2, *header, single_header) {
365 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(single_header));
366 }
367 }
368 break;
369 }
370
371 key = NULL;
372 }
373 }
374
375 if (PHPSTR_LEN(msg)) {
376 phpstr_appends(&str, HTTP_CRLF);
377 phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg));
378 phpstr_appends(&str, HTTP_CRLF);
379 }
380
381 data = phpstr_data(&str, string, length);
382 if (!string) {
383 efree(data);
384 }
385
386 phpstr_dtor(&str);
387 }
388
389 PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length)
390 {
391 char *buf;
392 size_t len;
393 phpstr str;
394
395 phpstr_init(&str);
396
397 do {
398 http_message_tostring(message, &buf, &len);
399 phpstr_prepend(&str, buf, len);
400 efree(buf);
401 } while ((message = message->parent));
402
403 buf = phpstr_data(&str, string, length);
404 if (!string) {
405 efree(buf);
406 }
407
408 phpstr_dtor(&str);
409 }
410
411 PHP_HTTP_API void _http_message_tostruct_recursive(http_message *msg, zval *obj TSRMLS_DC)
412 {
413 zval strct;
414 zval *headers;
415
416 INIT_ZARR(strct, HASH_OF(obj));
417
418 add_assoc_long(&strct, "type", msg->type);
419 add_assoc_double(&strct, "httpVersion", msg->http.version);
420 switch (msg->type)
421 {
422 case HTTP_MSG_RESPONSE:
423 add_assoc_long(&strct, "responseCode", msg->http.info.response.code);
424 add_assoc_string(&strct, "responseStatus", msg->http.info.response.status, 1);
425 break;
426
427 case HTTP_MSG_REQUEST:
428 add_assoc_string(&strct, "requestMethod", msg->http.info.request.method, 1);
429 add_assoc_string(&strct, "requestUri", msg->http.info.request.URI, 1);
430 break;
431
432 case HTTP_MSG_NONE:
433 /* avoid compiler warning */
434 break;
435 }
436
437 MAKE_STD_ZVAL(headers);
438 array_init(headers);
439 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
440 add_assoc_zval(&strct, "headers", headers);
441
442 add_assoc_stringl(&strct, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg), 1);
443
444 if (msg->parent) {
445 zval *parent;
446
447 MAKE_STD_ZVAL(parent);
448 if (Z_TYPE_P(obj) == IS_ARRAY) {
449 array_init(parent);
450 } else {
451 object_init(parent);
452 }
453 add_assoc_zval(&strct, "parentMessage", parent);
454 http_message_tostruct_recursive(msg->parent, parent);
455 } else {
456 add_assoc_null(&strct, "parentMessage");
457 }
458 }
459
460 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
461 {
462 STATUS rs = FAILURE;
463
464 switch (message->type)
465 {
466 case HTTP_MSG_RESPONSE:
467 {
468 char *key;
469 ulong idx;
470 zval **val;
471 HashPosition pos1;
472
473 FOREACH_HASH_KEYVAL(pos1, &message->hdrs, key, idx, val) {
474 if (key) {
475 if (Z_TYPE_PP(val) == IS_ARRAY) {
476 zend_bool first = 1;
477 zval **data;
478 HashPosition pos2;
479
480 FOREACH_VAL(pos2, *val, data) {
481 http_send_header_ex(key, strlen(key), Z_STRVAL_PP(data), Z_STRLEN_PP(data), first, NULL);
482 first = 0;
483 }
484 } else {
485 http_send_header_ex(key, strlen(key), Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1, NULL);
486 }
487 key = NULL;
488 }
489 }
490 rs = SUCCESS == http_send_status(message->http.info.response.code) &&
491 SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
492 SUCCESS : FAILURE;
493 }
494 break;
495
496 case HTTP_MSG_REQUEST:
497 {
498 #ifdef HTTP_HAVE_CURL
499 char *uri = NULL;
500 http_request request;
501 zval **zhost, options, headers;
502
503 INIT_PZVAL(&options);
504 INIT_PZVAL(&headers);
505 array_init(&options);
506 array_init(&headers);
507 zend_hash_copy(Z_ARRVAL(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
508 add_assoc_zval(&options, "headers", &headers);
509
510 /* check host header */
511 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void **) &zhost)) {
512 char *colon = NULL, *host = NULL;
513 size_t host_len = 0;
514 int port = 0;
515
516 /* check for port */
517 if ((colon = strchr(Z_STRVAL_PP(zhost), ':'))) {
518 port = atoi(colon + 1);
519 host = estrndup(Z_STRVAL_PP(zhost), host_len = (Z_STRVAL_PP(zhost) - colon - 1));
520 } else {
521 host = estrndup(Z_STRVAL_PP(zhost), host_len = Z_STRLEN_PP(zhost));
522 }
523 uri = http_absolute_uri_ex(
524 message->http.info.request.URI, strlen(message->http.info.request.URI),
525 NULL, 0, host, host_len, port);
526 efree(host);
527 } else {
528 uri = http_absolute_uri(message->http.info.request.URI);
529 }
530
531 if ((request.meth = http_request_method_exists(1, 0, message->http.info.request.method))) {
532 http_request_body body;
533
534 http_request_init_ex(&request, NULL, request.meth, uri);
535 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message), 0);
536 if (SUCCESS == (rs = http_request_prepare(&request, NULL))) {
537 http_request_exec(&request);
538 }
539 http_request_dtor(&request);
540 } else {
541 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD,
542 "Cannot send HttpMessage. Request method %s not supported",
543 message->http.info.request.method);
544 }
545 efree(uri);
546 #else
547 http_error(HE_WARNING, HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
548 #endif
549 }
550 break;
551
552 case HTTP_MSG_NONE:
553 default:
554 http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
555 break;
556 }
557
558 return rs;
559 }
560
561 PHP_HTTP_API http_message *_http_message_dup(http_message *msg TSRMLS_DC)
562 {
563 /*
564 * TODO: unroll
565 */
566 http_message *new;
567 char *serialized_data;
568 size_t serialized_length;
569
570 http_message_serialize(msg, &serialized_data, &serialized_length);
571 new = http_message_parse(serialized_data, serialized_length);
572 efree(serialized_data);
573 return new;
574 }
575
576 PHP_HTTP_API void _http_message_dtor(http_message *message)
577 {
578 if (message) {
579 zend_hash_destroy(&message->hdrs);
580 phpstr_dtor(PHPSTR(message));
581
582 switch (message->type)
583 {
584 case HTTP_MSG_REQUEST:
585 STR_SET(message->http.info.request.method, NULL);
586 STR_SET(message->http.info.request.URI, NULL);
587 break;
588
589 case HTTP_MSG_RESPONSE:
590 STR_SET(message->http.info.response.status, NULL);
591 break;
592
593 default:
594 break;
595 }
596 }
597 }
598
599 PHP_HTTP_API void _http_message_free(http_message **message)
600 {
601 if (*message) {
602 if ((*message)->parent) {
603 http_message_free(&(*message)->parent);
604 }
605 http_message_dtor(*message);
606 efree(*message);
607 *message = NULL;
608 }
609 }
610
611 /*
612 * Local variables:
613 * tab-width: 4
614 * c-basic-offset: 4
615 * End:
616 * vim600: noet sw=4 ts=4 fdm=marker
617 * vim<600: noet sw=4 ts=4
618 */
619