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