- support for proprietary Unless-Modified-Since header (eq If-Unmodified-Since)
[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_headers_cb _http_message_headers_cb
37 static void _http_message_headers_cb(const char *http_line, HashTable **headers, void **message TSRMLS_DC)
38 {
39 size_t line_length;
40 char *crlf = NULL;
41 http_message *new, *old = (http_message *) *message;
42
43 if (crlf = strstr(http_line, HTTP_CRLF)) {
44 line_length = crlf - http_line;
45 } else {
46 line_length = strlen(http_line);
47 }
48
49 if (old->type || zend_hash_num_elements(&old->hdrs) || PHPSTR_LEN(old)) {
50 new = http_message_new();
51
52 new->parent = old;
53 *message = new;
54 *headers = &new->hdrs;
55 } else {
56 new = old;
57 }
58
59 while (isspace(http_line[line_length-1])) --line_length;
60
61 // response
62 if (!strncmp(http_line, "HTTP/1.", lenof("HTTP/1."))) {
63 new->type = HTTP_MSG_RESPONSE;
64 new->info.response.http_version = atof(http_line + lenof("HTTP/"));
65 new->info.response.code = atoi(http_line + lenof("HTTP/1.1 "));
66 } else
67 // request
68 if (!strncmp(http_line + line_length - lenof("HTTP/1.1"), "HTTP/1.", lenof("HTTP/1."))) {
69 const char *method_sep_uri = strchr(http_line, ' ');
70 new->type = HTTP_MSG_REQUEST;
71 new->info.request.http_version = atof(http_line + line_length - lenof("1.1"));
72 new->info.request.method = estrndup(http_line, method_sep_uri - http_line);
73 new->info.request.URI = estrndup(method_sep_uri + 1, http_line + line_length - method_sep_uri - 1 - lenof(" HTTP/1.1"));
74 }
75 }
76
77 #define http_message_init_type _http_message_init_type
78 static inline void _http_message_init_type(http_message *message, http_message_type type)
79 {
80 switch (message->type = type)
81 {
82 case HTTP_MSG_RESPONSE:
83 message->info.response.http_version = .0;
84 message->info.response.code = 0;
85 break;
86
87 case HTTP_MSG_REQUEST:
88 message->info.request.http_version = .0;
89 message->info.request.method = NULL;
90 message->info.request.URI = NULL;
91 break;
92
93 case HTTP_MSG_NONE:
94 default:
95 break;
96 }
97 }
98
99 PHP_HTTP_API http_message *_http_message_init_ex(http_message *message, http_message_type type)
100 {
101 if (!message) {
102 message = ecalloc(1, sizeof(http_message));
103 }
104
105 http_message_init_type(message, type);
106 message->parent = NULL;
107 phpstr_init(&message->body);
108 zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
109
110 return message;
111 }
112
113
114 PHP_HTTP_API void _http_message_set_type(http_message *message, http_message_type type)
115 {
116 /* just act if different */
117 if (type != message->type) {
118
119 /* free request info */
120 if (message->type == HTTP_MSG_REQUEST) {
121 if (message->info.request.method) {
122 efree(message->info.request.method);
123 }
124 if (message->info.request.URI) {
125 efree(message->info.request.URI);
126 }
127 }
128
129 /* init */
130 http_message_init_type(message, type);
131 }
132 }
133
134 PHP_HTTP_API http_message *_http_message_parse_ex(http_message *msg, const char *message, size_t message_length TSRMLS_DC)
135 {
136 char *body = NULL;
137 zend_bool free_msg = msg ? 0 : 1;
138
139 if (message_length < HTTP_MSG_MIN_SIZE) {
140 return NULL;
141 }
142
143 if (!message) {
144 return NULL;
145 }
146
147 msg = http_message_init(msg);
148
149 if (SUCCESS != http_parse_headers_cb(message, &msg->hdrs, 1, http_message_headers_cb, (void **) &msg)) {
150 if (free_msg) {
151 http_message_free(msg);
152 }
153 return NULL;
154 }
155
156 /* header parsing stops at CRLF CRLF */
157 if (body = strstr(message, HTTP_CRLF HTTP_CRLF)) {
158 zval *c;
159 const char *continue_at = NULL;
160
161 body += lenof(HTTP_CRLF HTTP_CRLF);
162
163 /* message has content-length header */
164 if (c = http_message_header(msg, "Content-Length")) {
165 long len = atol(Z_STRVAL_P(c));
166 phpstr_from_string_ex(PHPSTR(msg), body, len);
167 continue_at = body + len;
168 } else
169
170 /* message has chunked transfer encoding */
171 if (c = http_message_header(msg, "Transfer-Encoding")) {
172 if (!strcasecmp("chunked", Z_STRVAL_P(c))) {
173 char *decoded;
174 size_t decoded_len;
175
176 /* decode and replace Transfer-Encoding with Content-Length header */
177 if (continue_at = http_chunked_decode(body, message + message_length - body, &decoded, &decoded_len)) {
178 phpstr_from_string_ex(PHPSTR(msg), decoded, decoded_len);
179 efree(decoded);
180 {
181 zval *len;
182 char *tmp;
183
184 spprintf(&tmp, 0, "%lu", (ulong) decoded_len);
185 MAKE_STD_ZVAL(len);
186 ZVAL_STRING(len, tmp, 0);
187
188 zend_hash_del(&msg->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding"));
189 zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &len, sizeof(zval *), NULL);
190 }
191 }
192 }
193 } else
194
195 /* message has content-range header */
196 if (c = http_message_header(msg, "Content-Range")) {
197 ulong start = 0, end = 0;
198
199 sscanf(Z_STRVAL_P(c), "bytes=%lu-%lu", &start, &end);
200 if (end > start) {
201 phpstr_from_string_ex(PHPSTR(msg), body, (size_t) (end - start));
202 continue_at = body + (end - start);
203 }
204 } else
205
206 /* no headers that indicate content length */
207 if (HTTP_MSG_TYPE(RESPONSE, msg)) {
208 phpstr_from_string_ex(PHPSTR(msg), body, message + message_length - body);
209 } else {
210 continue_at = body;
211 }
212
213 /* check for following messages */
214 if (continue_at) {
215 while (isspace(*continue_at)) ++continue_at;
216 if (continue_at < (message + message_length)) {
217 http_message *next = NULL, *most = NULL;
218
219 /* set current message to parent of most parent following messages and return deepest */
220 if (most = next = http_message_parse(continue_at, message + message_length - continue_at)) {
221 while (most->parent) most = most->parent;
222 most->parent = msg;
223 msg = next;
224 }
225 }
226 }
227 }
228
229 return msg;
230 }
231
232 PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length)
233 {
234 phpstr str;
235 char *key, *data;
236 ulong idx;
237 zval **header;
238
239 phpstr_init_ex(&str, 4096, 0);
240
241 switch (msg->type)
242 {
243 case HTTP_MSG_REQUEST:
244 phpstr_appendf(&str, "%s %s HTTP/%1.1f" HTTP_CRLF,
245 msg->info.request.method,
246 msg->info.request.URI,
247 msg->info.request.http_version);
248 break;
249
250 case HTTP_MSG_RESPONSE:
251 phpstr_appendf(&str, "HTTP/%1.1f %d" HTTP_CRLF,
252 msg->info.response.http_version,
253 msg->info.response.code);
254 break;
255
256 case HTTP_MSG_NONE:
257 default:
258 break;
259 }
260
261 FOREACH_HASH_KEYVAL(&msg->hdrs, key, idx, header) {
262 if (key) {
263 zval **single_header;
264
265 switch (Z_TYPE_PP(header))
266 {
267 case IS_STRING:
268 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(header));
269 break;
270
271 case IS_ARRAY:
272 FOREACH_VAL(*header, single_header) {
273 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(single_header));
274 }
275 break;
276 }
277
278 key = NULL;
279 }
280 }
281
282 if (PHPSTR_LEN(msg)) {
283 phpstr_appends(&str, HTTP_CRLF);
284 phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg));
285 phpstr_appends(&str, HTTP_CRLF);
286 }
287
288 data = phpstr_data(&str, string, length);
289 if (!string) {
290 efree(data);
291 }
292
293 phpstr_dtor(&str);
294 }
295
296 PHP_HTTP_API void _http_message_serialize(http_message *message, char **string, size_t *length)
297 {
298 char *buf;
299 size_t len;
300 phpstr str;
301
302 phpstr_init(&str);
303
304 do {
305 http_message_tostring(message, &buf, &len);
306 phpstr_prepend(&str, buf, len);
307 efree(buf);
308 } while (message = message->parent);
309
310 buf = phpstr_data(&str, string, length);
311 if (!string) {
312 efree(buf);
313 }
314
315 phpstr_dtor(&str);
316 }
317
318 PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
319 {
320 STATUS rs = FAILURE;
321
322 switch (message->type)
323 {
324 case HTTP_MSG_RESPONSE:
325 {
326 char *key;
327 ulong idx;
328 zval **val;
329
330 FOREACH_HASH_KEYVAL(&message->hdrs, key, idx, val) {
331 if (key) {
332 if (Z_TYPE_PP(val) == IS_ARRAY) {
333 zend_bool first = 1;
334 zval **data;
335
336 FOREACH_VAL(*val, data) {
337 http_send_header_ex(key, strlen(key), Z_STRVAL_PP(data), Z_STRLEN_PP(data), first);
338 first = 0;
339 }
340 } else {
341 http_send_header_ex(key, strlen(key), Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1);
342 }
343 key = NULL;
344 }
345 }
346 rs = SUCCESS == http_send_status(message->info.response.code) &&
347 SUCCESS == http_send_data(PHPSTR_VAL(message), PHPSTR_LEN(message)) ?
348 SUCCESS : FAILURE;
349 }
350 break;
351
352 case HTTP_MSG_REQUEST:
353 {
354 #ifdef HTTP_HAVE_CURL
355 char *uri = NULL;
356 zval **zhost, options, headers;
357
358 array_init(&options);
359 array_init(&headers);
360 zend_hash_copy(Z_ARRVAL(headers), &message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
361 add_assoc_zval(&options, "headers", &headers);
362
363 /* check host header */
364 if (SUCCESS == zend_hash_find(&message->hdrs, "Host", sizeof("Host"), (void **) &zhost)) {
365 char *colon = NULL, *host = NULL;
366 size_t host_len = 0;
367 int port = 0;
368
369 /* check for port */
370 if (colon = strchr(Z_STRVAL_PP(zhost), ':')) {
371 port = atoi(colon + 1);
372 host = estrndup(Z_STRVAL_PP(zhost), host_len = (Z_STRVAL_PP(zhost) - colon - 1));
373 } else {
374 host = estrndup(Z_STRVAL_PP(zhost), host_len = Z_STRLEN_PP(zhost));
375 }
376 uri = http_absolute_uri_ex(
377 message->info.request.URI, strlen(message->info.request.URI),
378 NULL, 0, host, host_len, port);
379 efree(host);
380 } else {
381 uri = http_absolute_uri(message->info.request.URI);
382 }
383
384 if (!strcasecmp("POST", message->info.request.method)) {
385 http_request_body body = {HTTP_REQUEST_BODY_CSTRING, PHPSTR_VAL(message), PHPSTR_LEN(message)};
386 rs = http_post(uri, &body, Z_ARRVAL(options), NULL, NULL);
387 } else
388 if (!strcasecmp("GET", message->info.request.method)) {
389 rs = http_get(uri, Z_ARRVAL(options), NULL, NULL);
390 } else
391 if (!strcasecmp("HEAD", message->info.request.method)) {
392 rs = http_head(uri, Z_ARRVAL(options), NULL, NULL);
393 } else {
394 http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD,
395 "Cannot send HttpMessage. Request method %s not supported",
396 message->info.request.method);
397 }
398
399 efree(uri);
400 #else
401 http_error(HE_WARNING, HTTP_E_RUNTIME, "HTTP requests not supported - ext/http was not linked against libcurl.");
402 #endif
403 }
404 break;
405
406 case HTTP_MSG_NONE:
407 default:
408 http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is neither of type HTTP_MSG_REQUEST nor HTTP_MSG_RESPONSE");
409 break;
410 }
411
412 return rs;
413 }
414
415 PHP_HTTP_API http_message *_http_message_dup(http_message *msg TSRMLS_DC)
416 {
417 /*
418 * TODO: unroll
419 */
420 http_message *new;
421 char *serialized_data;
422 size_t serialized_length;
423
424 http_message_serialize(msg, &serialized_data, &serialized_length);
425 new = http_message_parse(serialized_data, serialized_length);
426 efree(serialized_data);
427 return new;
428 }
429
430 PHP_HTTP_API void _http_message_dtor(http_message *message)
431 {
432 if (message) {
433 zend_hash_destroy(&message->hdrs);
434 phpstr_dtor(PHPSTR(message));
435 if (HTTP_MSG_TYPE(REQUEST, message)) {
436 if (message->info.request.method) {
437 efree(message->info.request.method);
438 message->info.request.method = NULL;
439 }
440 if (message->info.request.URI) {
441 efree(message->info.request.URI);
442 message->info.request.URI = NULL;
443 }
444 }
445 }
446 }
447
448 PHP_HTTP_API void _http_message_free(http_message *message)
449 {
450 if (message) {
451 if (message->parent) {
452 http_message_free(message->parent);
453 message->parent = NULL;
454 }
455 http_message_dtor(message);
456 efree(message);
457 }
458 }
459
460 /*
461 * Local variables:
462 * tab-width: 4
463 * c-basic-offset: 4
464 * End:
465 * vim600: noet sw=4 ts=4 fdm=marker
466 * vim<600: noet sw=4 ts=4
467 */
468