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