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