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