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