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