- support for proprietary Unless-Modified-Since header (eq If-Unmodified-Since)
[m6w6/ext-http] / http_headers_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 "ext/standard/php_string.h"
24 #include "ext/standard/url.h"
25
26 #include "php_http.h"
27 #include "php_http_std_defs.h"
28 #include "php_http_api.h"
29 #include "php_http_headers_api.h"
30
31 #include <ctype.h>
32
33 ZEND_EXTERN_MODULE_GLOBALS(http);
34
35 /* {{{ static int http_sort_q(const void *, const void *) */
36 static int http_sort_q(const void *a, const void *b TSRMLS_DC)
37 {
38 Bucket *f, *s;
39 zval result, *first, *second;
40
41 f = *((Bucket **) a);
42 s = *((Bucket **) b);
43
44 first = *((zval **) f->pData);
45 second= *((zval **) s->pData);
46
47 if (numeric_compare_function(&result, first, second TSRMLS_CC) != SUCCESS) {
48 return 0;
49 }
50 return (Z_LVAL(result) > 0 ? -1 : (Z_LVAL(result) < 0 ? 1 : 0));
51 }
52 /* }}} */
53
54 /* {{{ char *http_negotiate_q(char *, HashTable *, char *) */
55 PHP_HTTP_API char *_http_negotiate_q(const char *entry, const HashTable *supported, const char *def TSRMLS_DC)
56 {
57 zval *zaccept, zdelim, zarray, zentries, **zentry, **zsupp;
58 char *q_ptr = NULL, *key = NULL;
59 int i = 0;
60 ulong idx = 0;
61 double qual;
62
63 HTTP_GSC(zaccept, entry, estrdup(def));
64
65 array_init(&zarray);
66 array_init(&zentries);
67
68 Z_STRVAL(zdelim) = ",";
69 Z_STRLEN(zdelim) = 1;
70
71 php_explode(&zdelim, zaccept, &zarray, -1);
72
73 FOREACH_HASH_VAL(Z_ARRVAL(zarray), zentry) {
74 if (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';')) {
75 qual = strtod(q_ptr + 3, NULL);
76 *q_ptr = 0;
77 q_ptr = NULL;
78 } else {
79 qual = 1000.0 - i++;
80 }
81 /* TODO: support primaries only, too */
82 FOREACH_HASH_VAL((HashTable *)supported, zsupp) {
83 if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
84 add_assoc_double(&zentries, Z_STRVAL_PP(zsupp), qual);
85 break;
86 }
87 }
88 }
89 zval_dtor(&zarray);
90
91 zend_hash_sort(Z_ARRVAL(zentries), zend_qsort, http_sort_q, 0 TSRMLS_CC);
92
93 FOREACH_HASH_KEY(Z_ARRVAL(zentries), key, idx) {
94 if (key) {
95 key = estrdup(key);
96 zval_dtor(&zentries);
97 return key;
98 }
99 }
100 zval_dtor(&zentries);
101
102 return estrdup(def);
103 }
104 /* }}} */
105
106 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
107 PHP_HTTP_API http_range_status _http_get_request_ranges(HashTable *ranges, size_t length TSRMLS_DC)
108 {
109 zval *zrange;
110 char *range, c;
111 long begin = -1, end = -1, *ptr;
112
113 HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
114 range = Z_STRVAL_P(zrange);
115
116 if (strncmp(range, "bytes=", sizeof("bytes=") - 1)) {
117 return RANGE_NO;
118 }
119
120 ptr = &begin;
121 range += sizeof("bytes=") - 1;
122
123 do {
124 switch (c = *(range++))
125 {
126 case '0':
127 *ptr *= 10;
128 break;
129
130 case '1': case '2': case '3':
131 case '4': case '5': case '6':
132 case '7': case '8': case '9':
133 /*
134 * If the value of the pointer is already set (non-negative)
135 * then multiply its value by ten and add the current value,
136 * else initialise the pointers value with the current value
137 * --
138 * This let us recognize empty fields when validating the
139 * ranges, i.e. a "-10" for begin and "12345" for the end
140 * was the following range request: "Range: bytes=0-12345";
141 * While a "-1" for begin and "12345" for the end would
142 * have been: "Range: bytes=-12345".
143 */
144 if (*ptr > 0) {
145 *ptr *= 10;
146 *ptr += c - '0';
147 } else {
148 *ptr = c - '0';
149 }
150 break;
151
152 case '-':
153 ptr = &end;
154 break;
155
156 case ' ':
157 /* IE - ignore for now */
158 break;
159
160 case 0:
161 case ',':
162
163 if (length) {
164 /* validate ranges */
165 switch (begin)
166 {
167 /* "0-12345" */
168 case -10:
169 /* "0-" */
170 if (end == -1) {
171 return RANGE_NO;
172 }
173 /* "0-0" or overflow */
174 if (end == -10 || length <= (size_t) end) {
175 return RANGE_ERR;
176 }
177 begin = 0;
178 break;
179
180 /* "-12345" */
181 case -1:
182 /* "-", "-0" or overflow */
183 if (end == -1 || end == -10 || length <= (size_t) end) {
184 return RANGE_ERR;
185 }
186 begin = length - end;
187 end = length - 1;
188 break;
189
190 /* "12345-(xxx)" */
191 default:
192 switch (end)
193 {
194 /* "12345-0" */
195 case -10:
196 return RANGE_ERR;
197 break;
198
199 /* "12345-" */
200 case -1:
201 if (length <= (size_t) begin) {
202 return RANGE_ERR;
203 }
204 end = length - 1;
205 break;
206
207 /* "12345-67890" */
208 default:
209 if ( (length <= (size_t) begin) ||
210 (length <= (size_t) end) ||
211 (end < begin)) {
212 return RANGE_ERR;
213 }
214 break;
215 }
216 break;
217 }
218 }
219 {
220 zval *zentry;
221 MAKE_STD_ZVAL(zentry);
222 array_init(zentry);
223 add_index_long(zentry, 0, begin);
224 add_index_long(zentry, 1, end);
225 zend_hash_next_index_insert(ranges, &zentry, sizeof(zval *), NULL);
226
227 begin = -1;
228 end = -1;
229 ptr = &begin;
230 }
231 break;
232
233 default:
234 return RANGE_NO;
235 break;
236 }
237 } while (c != 0);
238
239 return RANGE_OK;
240 }
241 /* }}} */
242
243 /* {{{ STATUS http_parse_headers(char *, HashTable *, zend_bool) */
244 PHP_HTTP_API STATUS _http_parse_headers_ex(const char *header, HashTable *headers, zend_bool prettify, http_parse_headers_callback_t func, void **callback_data TSRMLS_DC)
245 {
246 const char *colon = NULL, *line = NULL, *begin = header, *crlfcrlf = NULL;
247 size_t header_len;
248 zval array;
249
250 Z_ARRVAL(array) = headers;
251
252 if (crlfcrlf = strstr(header, HTTP_CRLF HTTP_CRLF)) {
253 header_len = crlfcrlf - header + lenof(HTTP_CRLF);
254 } else {
255 header_len = strlen(header) + 1;
256 }
257
258
259 if (header_len < 2 || !strchr(header, ':')) {
260 http_error(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Cannot parse too short or malformed HTTP headers");
261 return FAILURE;
262 }
263
264 line = header;
265
266 while (header_len >= (size_t) (line - begin)) {
267 int value_len = 0;
268
269 switch (*line++)
270 {
271 case 0:
272 --value_len; /* we don't have CR so value length is one char less */
273 case '\n':
274 if ((!(*line - 1)) || ((*line != ' ') && (*line != '\t'))) {
275 /* response/request line */
276 if ( (!strncmp(header, "HTTP/1.", lenof("HTTP/1."))) ||
277 (!strncmp(line - lenof("HTTP/1.x" HTTP_CRLF) + value_len, "HTTP/1.", lenof("HTTP/1.")))) {
278 if (func) {
279 func(header, &headers, callback_data TSRMLS_CC);
280 Z_ARRVAL(array) = headers;
281 }
282 } else
283
284 /* "header: value" pair */
285 if (colon) {
286
287 /* skip empty key */
288 if (header != colon) {
289 zval **previous = NULL;
290 char *value;
291 int keylen = colon - header;
292 char *key = estrndup(header, keylen);
293
294 if (prettify) {
295 key = pretty_key(key, keylen, 1, 1);
296 }
297
298 value_len += line - colon - 1;
299
300 /* skip leading ws */
301 while (isspace(*(++colon))) --value_len;
302 /* skip trailing ws */
303 while (isspace(colon[value_len - 1])) --value_len;
304
305 if (value_len > 0) {
306 value = estrndup(colon, value_len);
307 } else {
308 value = estrdup("");
309 value_len = 0;
310 }
311
312 /* if we already have got such a header make an array of those */
313 if (SUCCESS == zend_hash_find(headers, key, keylen + 1, (void **) &previous)) {
314 /* convert to array */
315 if (Z_TYPE_PP(previous) != IS_ARRAY) {
316 convert_to_array(*previous);
317 }
318 add_next_index_stringl(*previous, value, value_len, 0);
319 } else {
320 add_assoc_stringl(&array, key, value, value_len, 0);
321 }
322 efree(key);
323 }
324 }
325 colon = NULL;
326 value_len = 0;
327 header += line - header;
328 }
329 break;
330
331 case ':':
332 if (!colon) {
333 colon = line - 1;
334 }
335 break;
336 }
337 }
338 return SUCCESS;
339 }
340 /* }}} */
341
342 PHP_HTTP_API void _http_parse_headers_default_callback(const char *http_line, HashTable **headers, void **cb_data TSRMLS_DC)
343 {
344 zval array;
345 char *crlf = NULL;
346 size_t line_length;
347 Z_ARRVAL(array) = *headers;
348
349 if (crlf = strstr(http_line, HTTP_CRLF)) {
350 line_length = crlf - http_line;
351 } else {
352 line_length = strlen(http_line);
353 }
354
355 /* response */
356 if (!strncmp(http_line, "HTTP/1.", lenof("HTTP/1."))) {
357 char *status = estrndup(http_line + lenof("HTTP/1.x "), line_length - lenof("HTTP/1.x "));
358 add_assoc_stringl(&array, "Response Status", status, line_length - lenof("HTTP/1.x "), 0);
359 } else
360 /* request */
361 if (!strncmp(http_line + line_length - lenof("HTTP/1.x"), "HTTP/1.", lenof("HTTP/1."))) {
362 char *sep = strchr(http_line, ' ');
363 char *url = estrndup(sep + 1, strstr(sep, "HTTP/1.") - sep + 1 + 1);
364 char *met = estrndup(http_line, sep - http_line);
365
366 add_assoc_stringl(&array, "Request Method", met, sep - http_line, 0);
367 add_assoc_stringl(&array, "Request Uri", url, strstr(sep, "HTTP/1.") - sep + 1 + 1, 0);
368 }
369 }
370
371 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
372 PHP_HTTP_API void _http_get_request_headers_ex(HashTable *headers, zend_bool prettify TSRMLS_DC)
373 {
374 char *key = NULL;
375 ulong idx = 0;
376 zval array;
377
378 Z_ARRVAL(array) = headers;
379
380 FOREACH_HASH_KEY(HTTP_SERVER_VARS, key, idx) {
381 if (key && !strncmp(key, "HTTP_", 5)) {
382 zval **header;
383
384 key += 5;
385 if (prettify) {
386 key = pretty_key(key, strlen(key), 1, 1);
387 }
388
389 zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
390 add_assoc_stringl(&array, key, Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
391 key = NULL;
392 }
393 }
394 }
395 /* }}} */
396
397 /* {{{ zend_bool http_match_request_header(char *, char *) */
398 PHP_HTTP_API zend_bool _http_match_request_header_ex(const char *header, const char *value, zend_bool match_case TSRMLS_DC)
399 {
400 char *name, *key = NULL;
401 ulong idx;
402 zend_bool result = 0;
403 HashTable headers;
404
405 name = pretty_key(estrdup(header), strlen(header), 1, 1);
406 zend_hash_init(&headers, 0, NULL, ZVAL_PTR_DTOR, 0);
407 http_get_request_headers_ex(&headers, 1);
408
409 FOREACH_HASH_KEY(&headers, key, idx) {
410 if (key && (!strcmp(key, name))) {
411 zval **data;
412
413 if (SUCCESS == zend_hash_get_current_data(&headers, (void **) &data)) {
414 result = (match_case ? strcmp(Z_STRVAL_PP(data), value) : strcasecmp(Z_STRVAL_PP(data), value)) ? 0 : 1;
415 }
416 break;
417 }
418 }
419
420 zend_hash_destroy(&headers);
421 efree(name);
422
423 return result;
424 }
425 /* }}} */
426
427
428 /*
429 * Local variables:
430 * tab-width: 4
431 * c-basic-offset: 4
432 * End:
433 * vim600: noet sw=4 ts=4 fdm=marker
434 * vim<600: noet sw=4 ts=4
435 */
436