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