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