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