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