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