5a2bdb763670023e5769d16f838bd598822964d0
[m6w6/ext-http] / http_message_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "php_http.h"
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_message_api.h"
27 #include "php_http_headers_api.h"
28 #include "php_http_send_api.h"
29 #include "php_http_request_api.h"
30 #include "php_http_url_api.h"
31
32 #include "phpstr/phpstr.h"
33
34 ZEND_EXTERN_MODULE_GLOBALS(http);
35
36 #define http_message_info_callback _http_message_info_callback
37 static void _http_message_info_callback(http_message **message, HashTable **headers, http_info *info TSRMLS_DC)
38 {
39 http_message *old = *message;
40
41 /* advance message */
42 if (old->type || zend_hash_num_elements(&old->hdrs) || PHPSTR_LEN(old)) {
43 (*message) = http_message_new();
44 (*message)->parent = old;
45 (*headers) = &((*message)->hdrs);
46 }
47
48 (*message)->http.version = info->http.version;
49
50 switch (info->type)
51 {
52 case IS_HTTP_REQUEST:
53 (*message)->type = HTTP_MSG_REQUEST;
54 HTTP_INFO(*message).request.URI = estrdup(HTTP_INFO(info).request.URI);
55 HTTP_INFO(*message).request.method = estrdup(HTTP_INFO(info).request.method);
56 break;
57
58 case IS_HTTP_RESPONSE:
59 (*message)->type = HTTP_MSG_RESPONSE;
60 HTTP_INFO(*message).response.code = HTTP_INFO(info).response.code;
61 HTTP_INFO(*message).response.status = estrdup(HTTP_INFO(info).response.status);
62 break;
63 }
64 }
65
66 #define http_message_init_type _http_message_init_type
67 static inline void _http_message_init_type(http_message *message, http_message_type type)
68 {
69 message->http.version = .0;
70
71 switch (message->type = type)
72 {
73 case HTTP_MSG_RESPONSE:
74 message->http.info.response.code = 0;
75 message->http.info.response.status = NULL;
76 break;
77
78 case HTTP_MSG_REQUEST:
79 message->http.info.request.method = NULL;
80 message->http.info.request.URI = NULL;
81 break;
82
83 case HTTP_MSG_NONE:
84 default:
85 break;
86 }
87 }
88
89 PHP_HTTP_API http_message *_http_message_init_ex(http_message *message, http_message_type type)
90 {
91 if (!message) {
92 message = ecalloc(1, sizeof(http_message));
93 }
94
95 http_message_init_type(message, type);
96 message->parent = NULL;
97 phpstr_init(&message->body);
98 zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
99
100 return message;
101 }
102
103
104 PHP_HTTP_API void _http_message_set_type(http_message *message, http_message_type type)
105 {
106 /* just act if different */
107 if (type != message->type) {
108
109 /* free request info */
110 switch (message->type)
111 {
112 case HTTP_MSG_REQUEST:
113 STR_FREE(message->http.info.request.method);
114 STR_FREE(message->http.info.request.URI);
115 break;
116
117 case HTTP_MSG_RESPONSE:
118 STR_FREE(message->http.info.response.status);
119 break;
120
121 default:
122 break;
123 }
124
125 /* init */
126 http_message_init_type(message, type);
127 }
128 }
129
130 PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char *message, size_t message_length TSRMLS_DC)
131 {
132 const char *body = NULL;
133 zend_bool free_msg = msg ? 0 : 1;
134
135 if ((!message) || (message_length < HTTP_MSG_MIN_SIZE)) {
136 return NULL;
137 }
138
139 msg = http_message_init(msg);
140
141 if (SUCCESS != http_parse_headers_cb(message, &msg->hdrs, 1, (http_info_callback) http_message_info_callback, &msg)) {
142 if (free_msg) {
143 http_message_free(&msg);
144 }
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
153 /* message has content-length header */
154 if (c = http_message_header(msg, "Content-Length")) {
155 long len = atol(Z_STRVAL_P(c));
156 phpstr_from_string_ex(PHPSTR(msg), body, len);
157 continue_at = body + len;
158 } else
159
160 /* message has chunked transfer encoding */
161 if (c = http_message_header(msg, "Transfer-Encoding")) {
162 if (!strcasecmp("chunked", Z_STRVAL_P(c))) {
163 char *decoded;
164 size_t decoded_len;
165
166 /* decode and replace Transfer-Encoding with Content-Length header */
167 if (continue_at = http_chunked_decode(body, message + message_length - body, &decoded, &decoded_len)) {
168 phpstr_from_string_ex(PHPSTR(msg), decoded, decoded_len);
169 efree(decoded);
170 {
171 zval *len;
172 char *tmp;
173
174 spprintf(&tmp, 0, "%lu", (ulong) decoded_len);
175 MAKE_STD_ZVAL(len);
176 ZVAL_STRING(len, tmp, 0);
177
178 zend_hash_del(&msg->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding"));
179 zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
180 }
181 }
182 }
183 } else
184
185 /* message has content-range header */
186 if (c = http_message_header(msg, "Content-Range")) {
187 ulong start = 0, end = 0;
188
189 sscanf(Z_STRVAL_P(c), "bytes=%lu-%lu", &start, &end);
190 if (end > start) {
191 phpstr_from_string_ex(PHPSTR(msg), body, (size_t) (end - start));
192 continue_at = body + (end - start);
193 }
194 } else
195
196 /* no headers that indicate content length */
197 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
198 phpstr_from_string_ex(PHPSTR(msg), body, message + message_length - body);
199 } else {
200 continue_at = body;
201 }
202
203 /* check for following messages */
204 if (continue_at) {
205 while (isspace(*continue_at)) ++continue_at;
206 if (continue_at < (message + message_length)) {
207 http_message *next = NULL, *most = NULL;
208
209 /* set current message to parent of most parent following messages and return deepest */
210 if (most = next = http_message_parse(continue_at, message + message_length - continue_at)) {
211 while (most->parent) most = most->parent;
212 most->parent = msg;
213 msg = next;
214 }
215 }
216 }
217 }
218
219 return msg;
220 }
221
222 PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length)
223 {
224 phpstr str;
225 char *key, *data;
226 ulong idx;
227 zval **header;
228
229 phpstr_init_ex(&str, 4096, 0);
230
231 switch (msg->type)
232 {
233 case HTTP_MSG_REQUEST:
234 phpstr_appendf(&str, "%s %s HTTP/%1.1f" HTTP_CRLF,
235 msg->http.info.request.method,
236 msg->http.info.request.URI,
237 msg->http.version);
238 break;
239
240 case HTTP_MSG_RESPONSE:
241 phpstr_appendf(&str, "HTTP/%1.1f %d%s%s" HTTP_CRLF,
242 msg->http.version,
243 msg->http.info.response.code,
244 *msg->http.info.response.status ? " ":"",
245 msg->http.info.response.status);
246 break;
247
248 case HTTP_MSG_NONE:
249 default:
250 break;
251 }
252
253 FOREACH_HASH_KEYVAL(&msg->hdrs, key, idx, header) {
254 if (key) {
255 zval **single_header;
256
257 switch (Z_TYPE_PP(header))
258 {
259 case IS_STRING:
260 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(header));
261 break;
262
263 case IS_ARRAY:
264 FOREACH_VAL(*header, single_header) {
265 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(single_header));
266 }
267 break;
268 }
269
270 key = NULL;
271 }
272 }
273
274 if (PHPSTR_LEN(msg)) {
275 phpstr_appends(&str, HTTP_CRLF);
276 phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg));
277 phpstr_appends(&str, HTTP_CRLF);
278 }
279
280 data = phpstr_data(&str, string, length);
281 if (!string) {
282 efree(data);
283 }
284
285 phpstr_dtor(&str);
286 }
287
288 PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length)
289 {
290 char *buf;
291 size_t len;
292 phpstr str;
293
294 phpstr_init(&str);
295
296 do {
297 http_message_tostring(message, &buf, &len);
298 phpstr_prepend(&str, buf, len);
299 efree(buf);
300 } while (message = message->parent);
301
302 buf = phpstr_data(&str, string, length);
303 if (!string) {
304 efree(buf);
305 }
306
307 phpstr_dtor(&str);
308 }
309
310 PHP_HTTP_API void _http_message_tostruct_recursive(http_message *msg, zval *obj TSRMLS_DC)
311 {
312 zval strct;
313 zval *headers;
314
315 Z_TYPE(strct) = IS_ARRAY;
316 Z_ARRVAL(strct) = HASH_OF(obj);
317
318 add_assoc_long(&strct, "type", msg->type);
319 add_assoc_double(&strct, "httpVersion", msg->http.version);
320 switch (msg->type)
321 {
322 case HTTP_MSG_RESPONSE:
323 add_assoc_long(&strct, "responseCode", msg->http.info.response.code);
324 add_assoc_string(&strct, "responseStatus", msg->http.info.response.status, 1);
325 break;
326
327 case HTTP_MSG_REQUEST:
328 add_assoc_string(&strct, "requestMethod", msg->http.info.request.method, 1);
329 add_assoc_string(&strct, "requestUri", msg->http.info.request.URI, 1);
330 break;
331 }
332
333 MAKE_STD_ZVAL(headers);
334 array_init(headers);
335 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
336 add_assoc_zval(&strct, "headers", headers);
337 zval_ptr_dtor(&headers);
338
339 add_assoc_stringl(&strct, "body", PHPSTR_VAL(msg), PHPSTR_LEN(msg), 1);
340
341 if (msg->parent) {
342 zval *parent;
343
344 MAKE_STD_ZVAL(parent);
345 if (Z_TYPE_P(obj) == IS_ARRAY) {
346 array_init(parent);
347 } else {
348 object_init(parent);
349 }
350 add_assoc_zval(&strct, "parentMessage", parent);
351 http_message_tostruct_recursive(msg->parent, parent);
352 zval_ptr_dtor(&parent);
353 } else {
354 add_assoc_null(&strct, "parentMessage");
355 }
356 http_message_dtor(msg);
357 efree(msg);
358 }
359
360 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
361 {
362 STATUS rs = FAILURE;
363
364 switch (message->type)
365 {
366 case HTTP_MSG_RESPONSE:
367 {
368 char *key;
369 ulong idx;
370 zval **val;
371
372 FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) {
373 if (key) {
374 if (Z_TYPE_PP(val) == IS_ARRAY) {
375 zend_bool first = 1;
376 zval **data;
377
378 FOREACH_VAL(*val, data) {
379 http_send_header_ex(key, strlen(key), Z_STRVAL_PP(data), Z_STRLEN_PP(data), first, NULL);
380 first = 0;
381 }
382 } else {
383 http_send_header_ex(key, strlen(key), Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1, NULL);
384 }
385 key = NULL;
386 }
387 }
388 rs = SUCCESS == http_send_status(message->http.info.response.code) &&
389 SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
390 SUCCESS : FAILURE;
391 }
392 break;
393
394 case HTTP_MSG_REQUEST:
395 {
396 #ifdef HTTP_HAVE_CURL
397 char *uri = NULL;
398 zval **zhost, options, headers;
399
400 array_init(&options);
401 array_init(&headers);
402 zend_hash_copy(Z_ARRVAL(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
403 add_assoc_zval(&options, "headers", &headers);
404
405 /* check host header */
406 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void **) &zhost)) {
407 char *colon = NULL, *host = NULL;
408 size_t host_len = 0;
409 int port = 0;
410
411 /* check for port */
412 if (colon = strchr(Z_STRVAL_PP(zhost), ':')) {
413 port = atoi(colon + 1);
414 host = estrndup(Z_STRVAL_PP(zhost), host_len = (Z_STRVAL_PP(zhost) - colon - 1));
415 } else {
416 host = estrndup(Z_STRVAL_PP(zhost), host_len = Z_STRLEN_PP(zhost));
417 }
418 uri = http_absolute_uri_ex(
419 message->http.info.request.URI, strlen(message->http.info.request.URI),
420 NULL, 0, host, host_len, port);
421 efree(host);
422 } else {
423 uri = http_absolute_uri(message->http.info.request.URI);
424 }
425
426 if (!strcasecmp("POST", message->http.info.request.method)) {
427 http_request_body body = {HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message)};
428 rs = http_post(uri, &body, Z_ARRVAL(options), NULL, NULL);
429 } else
430 if (!strcasecmp("GET", message->http.info.request.method)) {
431 rs = http_get(uri, Z_ARRVAL(options), NULL, NULL);
432 } else
433 if (!strcasecmp("HEAD", message->http.info.request.method)) {
434 rs = http_head(uri, Z_ARRVAL(options), NULL, NULL);
435 } else {
436 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD,
437 "Cannot send HttpMessage. Request method %s not supported",
438 message->http.info.request.method);
439 }
440
441 efree(uri);
442 #else
443 http_error(HE_WARNING, HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
444 #endif
445 }
446 break;
447
448 case HTTP_MSG_NONE:
449 default:
450 http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
451 break;
452 }
453
454 return rs;
455 }
456
457 PHP_HTTP_API http_message *_http_message_dup(http_message *msg TSRMLS_DC)
458 {
459 /*
460 * TODO: unroll
461 */
462 http_message *new;
463 char *serialized_data;
464 size_t serialized_length;
465
466 http_message_serialize(msg, &serialized_data, &serialized_length);
467 new = http_message_parse(serialized_data, serialized_length);
468 efree(serialized_data);
469 return new;
470 }
471
472 PHP_HTTP_API void _http_message_dtor(http_message *message)
473 {
474 if (message) {
475 zend_hash_destroy(&message->hdrs);
476 phpstr_dtor(PHPSTR(message));
477
478 switch (message->type)
479 {
480 case HTTP_MSG_REQUEST:
481 STR_SET(message->http.info.request.method, NULL);
482 STR_SET(message->http.info.request.URI, NULL);
483 break;
484
485 case HTTP_MSG_RESPONSE:
486 STR_SET(message->http.info.response.status, NULL);
487 break;
488
489 default:
490 break;
491 }
492 }
493 }
494
495 PHP_HTTP_API void _http_message_free(http_message **message)
496 {
497 if (*message) {
498 if ((*message)->parent) {
499 http_message_free(&(*message)->parent);
500 }
501 http_message_dtor(*message);
502 efree(*message);
503 *message = NULL;
504 }
505 }
506
507 /*
508 * Local variables:
509 * tab-width: 4
510 * c-basic-offset: 4
511 * End:
512 * vim600: noet sw=4 ts=4 fdm=marker
513 * vim<600: noet sw=4 ts=4
514 */
515