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