release 1.7.5
[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-2010, 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_z(zval *, HashTable *, negotiate_func_t) */
111 PHP_HTTP_API HashTable *_http_negotiate_z(zval *value, HashTable *supported, negotiate_func_t neg TSRMLS_DC)
112 {
113 zval *accept = http_zsep(IS_STRING, value);
114 HashTable *result = NULL;
115
116 if (Z_STRLEN_P(accept)) {
117 zval ex_arr, ex_del;
118
119 INIT_PZVAL(&ex_del);
120 INIT_PZVAL(&ex_arr);
121 ZVAL_STRINGL(&ex_del, ",", 1, 0);
122 array_init(&ex_arr);
123
124 php_explode(&ex_del, accept, &ex_arr, INT_MAX);
125
126 if (zend_hash_num_elements(Z_ARRVAL(ex_arr)) > 0) {
127 int i = 0;
128 HashPosition pos;
129 zval **entry, array;
130
131 INIT_PZVAL(&array);
132 array_init(&array);
133
134 FOREACH_HASH_VAL(pos, Z_ARRVAL(ex_arr), entry) {
135 int ident_len;
136 double quality;
137 char *selected, *identifier, *freeme;
138 const char *separator;
139
140 #if HTTP_DBG_NEG
141 fprintf(stderr, "Checking %s\n", Z_STRVAL_PP(entry));
142 #endif
143
144 if ((separator = strchr(Z_STRVAL_PP(entry), ';'))) {
145 const char *ptr = separator;
146
147 while (*++ptr && !HTTP_IS_CTYPE(digit, *ptr) && '.' != *ptr);
148
149 quality = zend_strtod(ptr, NULL);
150 identifier = estrndup(Z_STRVAL_PP(entry), ident_len = separator - Z_STRVAL_PP(entry));
151 } else {
152 quality = 1000.0 - i++;
153 identifier = estrndup(Z_STRVAL_PP(entry), ident_len = Z_STRLEN_PP(entry));
154 }
155 freeme = identifier;
156
157 while (HTTP_IS_CTYPE(space, *identifier)) {
158 ++identifier;
159 --ident_len;
160 }
161 while (ident_len && HTTP_IS_CTYPE(space, identifier[ident_len - 1])) {
162 identifier[--ident_len] = '\0';
163 }
164
165 if ((selected = neg(identifier, &quality, supported TSRMLS_CC))) {
166 /* don't overwrite previously set with higher quality */
167 if (!zend_hash_exists(Z_ARRVAL(array), selected, strlen(selected) + 1)) {
168 add_assoc_double(&array, selected, quality);
169 }
170 }
171
172 efree(freeme);
173 }
174
175 result = Z_ARRVAL(array);
176 zend_hash_sort(result, zend_qsort, http_sort_q, 0 TSRMLS_CC);
177 }
178
179 zval_dtor(&ex_arr);
180 }
181
182 zval_ptr_dtor(&accept);
183
184 return result;
185 }
186 /* }}} */
187
188 /* {{{ HashTable *http_negotiate_q(const char *, HashTable *, negotiate_func_t) */
189 PHP_HTTP_API HashTable *_http_negotiate_q(const char *header, HashTable *supported, negotiate_func_t neg TSRMLS_DC)
190 {
191 zval *accept;
192
193 #if HTTP_DBG_NEG
194 fprintf(stderr, "Reading header %s: ", header);
195 #endif
196 if (!(accept = http_get_server_var(header, 1))) {
197 return NULL;
198 }
199 #if HTTP_DBG_NEG
200 fprintf(stderr, "%s\n", Z_STRVAL_P(accept));
201 #endif
202
203 return http_negotiate_z(accept, supported, neg);
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 if ( !(zrange = http_get_server_var("HTTP_RANGE", 1)) ||
215 (size_t) Z_STRLEN_P(zrange) < lenof("bytes=") || strncmp(Z_STRVAL_P(zrange), "bytes=", lenof("bytes="))) {
216 return RANGE_NO;
217 }
218 range = Z_STRVAL_P(zrange) + lenof("bytes=");
219 ptr = &begin;
220
221 do {
222 switch (c = *(range++)) {
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 break;
258
259 case 0:
260 case ',':
261
262 if (length) {
263 /* validate ranges */
264 switch (begin) {
265 /* "0-12345" */
266 case -10:
267 switch (end) {
268 /* "0-" */
269 case -1:
270 return RANGE_NO;
271
272 /* "0-0" */
273 case -10:
274 end = 0;
275 break;
276
277 default:
278 if (length <= (size_t) end) {
279 return RANGE_ERR;
280 }
281 break;
282 }
283 begin = 0;
284 break;
285
286 /* "-12345" */
287 case -1:
288 /* "-", "-0" or overflow */
289 if (end == -1 || end == -10 || length <= (size_t) end) {
290 return RANGE_ERR;
291 }
292 begin = length - end;
293 end = length - 1;
294 break;
295
296 /* "12345-(xxx)" */
297 default:
298 switch (end) {
299 /* "12345-0" */
300 case -10:
301 return RANGE_ERR;
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 }
340 } while (c != 0);
341
342 return RANGE_OK;
343 }
344 /* }}} */
345
346 /* {{{ STATUS http_parse_headers(char *, HashTable *, zend_bool) */
347 PHP_HTTP_API STATUS _http_parse_headers_ex(const char *header, HashTable *headers, zend_bool prettify,
348 http_info_callback callback_func, void **callback_data TSRMLS_DC)
349 {
350 const char *colon = NULL, *line = NULL;
351 zval array;
352
353 INIT_ZARR(array, headers);
354
355 /* skip leading ws */
356 while (HTTP_IS_CTYPE(space, *header)) ++header;
357 line = header;
358
359 #define MORE_HEADERS (*(line-1) && !(*(line-1) == '\n' && (*line == '\n' || *line == '\r')))
360 do {
361 int value_len = 0;
362
363 switch (*line++) {
364 case ':':
365 if (!colon) {
366 colon = line - 1;
367 }
368 break;
369
370 case 0:
371 --value_len; /* we don't have CR so value length is one char less */
372 case '\n':
373 if ((!*(line - 1)) || ((*line != ' ') && (*line != '\t'))) {
374 http_info i;
375
376 if (SUCCESS == http_info_parse(header, &i)) {
377 /* response/request line */
378 callback_func(callback_data, &headers, &i TSRMLS_CC);
379 http_info_dtor(&i);
380 Z_ARRVAL(array) = headers;
381 } else if (colon) {
382 /* "header: value" pair */
383 if (header != colon) {
384 int keylen = colon - header;
385 const char *key = header;
386
387 /* skip leading ws */
388 while (keylen && HTTP_IS_CTYPE(space, *key)) --keylen, ++key;
389 /* skip trailing ws */
390 while (keylen && HTTP_IS_CTYPE(space, key[keylen - 1])) --keylen;
391
392 if (keylen > 0) {
393 zval **previous = NULL;
394 char *value;
395 char *keydup = estrndup(key, keylen);
396
397 if (prettify) {
398 keydup = pretty_key(keydup, keylen, 1, 1);
399 }
400
401 value_len += line - colon - 1;
402
403 /* skip leading ws */
404 while (HTTP_IS_CTYPE(space, *(++colon))) --value_len;
405 /* skip trailing ws */
406 while (HTTP_IS_CTYPE(space, colon[value_len - 1])) --value_len;
407
408 if (value_len > 0) {
409 value = estrndup(colon, value_len);
410 } else {
411 value = estrdup("");
412 value_len = 0;
413 }
414
415 /* if we already have got such a header make an array of those */
416 if (SUCCESS == zend_hash_find(headers, keydup, keylen + 1, (void *) &previous)) {
417 /* convert to array */
418 if (Z_TYPE_PP(previous) != IS_ARRAY) {
419 convert_to_array(*previous);
420 }
421 add_next_index_stringl(*previous, value, value_len, 0);
422 } else {
423 add_assoc_stringl(&array, keydup, value, value_len, 0);
424 }
425 efree(keydup);
426 } else {
427 /* empty key (" : ...") */
428 return FAILURE;
429 }
430 } else {
431 /* empty key (": ...") */
432 return FAILURE;
433 }
434 } else if (MORE_HEADERS) {
435 /* a line without a colon */
436 return FAILURE;
437 }
438 colon = NULL;
439 value_len = 0;
440 header += line - header;
441 }
442 break;
443 }
444 } while (MORE_HEADERS);
445
446 return SUCCESS;
447 }
448 /* }}} */
449
450 /* {{{ void http_get_request_headers(HashTable *) */
451 PHP_HTTP_API void _http_get_request_headers(HashTable *headers TSRMLS_DC)
452 {
453 HashKey key = initHashKey(0);
454 zval **hsv, **header;
455 HashPosition pos;
456
457 if (!HTTP_G->request.headers) {
458 ALLOC_HASHTABLE(HTTP_G->request.headers);
459 zend_hash_init(HTTP_G->request.headers, 0, NULL, ZVAL_PTR_DTOR, 0);
460
461 #ifdef ZEND_ENGINE_2
462 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
463 #endif
464
465 if (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv) && Z_TYPE_PP(hsv) == IS_ARRAY) {
466 FOREACH_KEY(pos, *hsv, key) {
467 if (key.type == HASH_KEY_IS_STRING && key.len > 6 && !strncmp(key.str, "HTTP_", 5)) {
468 key.len -= 5;
469 key.str = pretty_key(estrndup(key.str + 5, key.len - 1), key.len - 1, 1, 1);
470
471 zend_hash_get_current_data_ex(Z_ARRVAL_PP(hsv), (void *) &header, &pos);
472 ZVAL_ADDREF(*header);
473 zend_hash_add(HTTP_G->request.headers, key.str, key.len, (void *) header, sizeof(zval *), NULL);
474
475 efree(key.str);
476 }
477 }
478 }
479 }
480
481 if (headers) {
482 zend_hash_copy(headers, HTTP_G->request.headers, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
483 }
484 }
485 /* }}} */
486
487 /* {{{ STATUS http_get_response_headers(HashTable *) */
488 PHP_HTTP_API STATUS _http_get_response_headers(HashTable *headers_ht TSRMLS_DC)
489 {
490 STATUS status;
491 phpstr headers;
492
493 phpstr_init(&headers);
494 zend_llist_apply_with_argument(&SG(sapi_headers).headers, http_grab_response_headers, &headers TSRMLS_CC);
495 phpstr_fix(&headers);
496
497 status = http_parse_headers_ex(PHPSTR_VAL(&headers), headers_ht, 1);
498 phpstr_dtor(&headers);
499
500 return status;
501 }
502 /* }}} */
503
504 /* {{{ zend_bool http_match_request_header(char *, char *) */
505 PHP_HTTP_API zend_bool _http_match_request_header_ex(const char *header, const char *value, zend_bool match_case TSRMLS_DC)
506 {
507 char *name;
508 uint name_len = strlen(header);
509 zend_bool result = 0;
510 zval **data, *zvalue;
511
512 http_get_request_headers(NULL);
513 name = pretty_key(estrndup(header, name_len), name_len, 1, 1);
514 if (SUCCESS == zend_hash_find(HTTP_G->request.headers, name, name_len+1, (void *) &data)) {
515 zvalue = http_zsep(IS_STRING, *data);
516 result = (match_case ? strcmp(Z_STRVAL_P(zvalue), value) : strcasecmp(Z_STRVAL_P(zvalue), value)) ? 0 : 1;
517 zval_ptr_dtor(&zvalue);
518 }
519 efree(name);
520
521 return result;
522 }
523 /* }}} */
524
525
526 /*
527 * Local variables:
528 * tab-width: 4
529 * c-basic-offset: 4
530 * End:
531 * vim600: noet sw=4 ts=4 fdm=marker
532 * vim<600: noet sw=4 ts=4
533 */
534