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