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