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