- fix requests for the first byte only
[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-2007, 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 switch (end) {
257 /* "0-" */
258 case -1:
259 return RANGE_NO;
260
261 /* "0-0" */
262 case -10:
263 end = 0;
264 break;
265
266 default:
267 if (length <= (size_t) end) {
268 return RANGE_ERR;
269 }
270 break;
271 }
272 begin = 0;
273 break;
274
275 /* "-12345" */
276 case -1:
277 /* "-", "-0" or overflow */
278 if (end == -1 || end == -10 || length <= (size_t) end) {
279 return RANGE_ERR;
280 }
281 begin = length - end;
282 end = length - 1;
283 break;
284
285 /* "12345-(xxx)" */
286 default:
287 switch (end) {
288 /* "12345-0" */
289 case -10:
290 return RANGE_ERR;
291
292 /* "12345-" */
293 case -1:
294 if (length <= (size_t) begin) {
295 return RANGE_ERR;
296 }
297 end = length - 1;
298 break;
299
300 /* "12345-67890" */
301 default:
302 if ( (length <= (size_t) begin) ||
303 (length <= (size_t) end) ||
304 (end < begin)) {
305 return RANGE_ERR;
306 }
307 break;
308 }
309 break;
310 }
311 }
312 {
313 zval *zentry;
314 MAKE_STD_ZVAL(zentry);
315 array_init(zentry);
316 add_index_long(zentry, 0, begin);
317 add_index_long(zentry, 1, end);
318 zend_hash_next_index_insert(ranges, &zentry, sizeof(zval *), NULL);
319
320 begin = -1;
321 end = -1;
322 ptr = &begin;
323 }
324 break;
325
326 default:
327 return RANGE_NO;
328 }
329 } while (c != 0);
330
331 return RANGE_OK;
332 }
333 /* }}} */
334
335 /* {{{ STATUS http_parse_headers(char *, HashTable *, zend_bool) */
336 PHP_HTTP_API STATUS _http_parse_headers_ex(const char *header, HashTable *headers, zend_bool prettify,
337 http_info_callback callback_func, void **callback_data TSRMLS_DC)
338 {
339 const char *colon = NULL, *line = NULL;
340 zval array;
341
342 INIT_ZARR(array, headers);
343
344 /* skip leading ws */
345 while (HTTP_IS_CTYPE(space, *header)) ++header;
346 line = header;
347
348 #define MORE_HEADERS (*(line-1) && !(*(line-1) == '\n' && (*line == '\n' || *line == '\r')))
349 do {
350 int value_len = 0;
351
352 switch (*line++) {
353 case ':':
354 if (!colon) {
355 colon = line - 1;
356 }
357 break;
358
359 case 0:
360 --value_len; /* we don't have CR so value length is one char less */
361 case '\n':
362 if ((!*(line - 1)) || ((*line != ' ') && (*line != '\t'))) {
363 http_info i;
364
365 if (SUCCESS == http_info_parse(header, &i)) {
366 /* response/request line */
367 callback_func(callback_data, &headers, &i TSRMLS_CC);
368 http_info_dtor(&i);
369 Z_ARRVAL(array) = headers;
370 } else if (colon) {
371 /* "header: value" pair */
372 if (header != colon) {
373 int keylen = colon - header;
374 const char *key = header;
375
376 /* skip leading ws */
377 while (keylen && HTTP_IS_CTYPE(space, *key)) --keylen, ++key;
378 /* skip trailing ws */
379 while (keylen && HTTP_IS_CTYPE(space, key[keylen - 1])) --keylen;
380
381 if (keylen > 0) {
382 zval **previous = NULL;
383 char *value;
384 char *keydup = estrndup(key, keylen);
385
386 if (prettify) {
387 keydup = pretty_key(keydup, keylen, 1, 1);
388 }
389
390 value_len += line - colon - 1;
391
392 /* skip leading ws */
393 while (HTTP_IS_CTYPE(space, *(++colon))) --value_len;
394 /* skip trailing ws */
395 while (HTTP_IS_CTYPE(space, colon[value_len - 1])) --value_len;
396
397 if (value_len > 0) {
398 value = estrndup(colon, value_len);
399 } else {
400 value = estrdup("");
401 value_len = 0;
402 }
403
404 /* if we already have got such a header make an array of those */
405 if (SUCCESS == zend_hash_find(headers, keydup, keylen + 1, (void *) &previous)) {
406 /* convert to array */
407 if (Z_TYPE_PP(previous) != IS_ARRAY) {
408 convert_to_array(*previous);
409 }
410 add_next_index_stringl(*previous, value, value_len, 0);
411 } else {
412 add_assoc_stringl(&array, keydup, value, value_len, 0);
413 }
414 efree(keydup);
415 } else {
416 /* empty key (" : ...") */
417 return FAILURE;
418 }
419 } else {
420 /* empty key (": ...") */
421 return FAILURE;
422 }
423 } else if (MORE_HEADERS) {
424 /* a line without a colon */
425 return FAILURE;
426 }
427 colon = NULL;
428 value_len = 0;
429 header += line - header;
430 }
431 break;
432 }
433 } while (MORE_HEADERS);
434
435 return SUCCESS;
436 }
437 /* }}} */
438
439 /* {{{ void http_get_request_headers(HashTable *) */
440 PHP_HTTP_API void _http_get_request_headers(HashTable *headers TSRMLS_DC)
441 {
442 HashKey key = initHashKey(0);
443 zval **hsv, **header;
444 HashPosition pos;
445
446 if (!HTTP_G->request.headers) {
447 ALLOC_HASHTABLE(HTTP_G->request.headers);
448 zend_hash_init(HTTP_G->request.headers, 0, NULL, ZVAL_PTR_DTOR, 0);
449
450 #ifdef ZEND_ENGINE_2
451 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
452 #endif
453
454 if (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv) && Z_TYPE_PP(hsv) == IS_ARRAY) {
455 FOREACH_KEY(pos, *hsv, key) {
456 if (key.type == HASH_KEY_IS_STRING && key.len > 6 && !strncmp(key.str, "HTTP_", 5)) {
457 key.len -= 5;
458 key.str = pretty_key(estrndup(key.str + 5, key.len - 1), key.len - 1, 1, 1);
459
460 zend_hash_get_current_data_ex(Z_ARRVAL_PP(hsv), (void *) &header, &pos);
461 ZVAL_ADDREF(*header);
462 zend_hash_add(HTTP_G->request.headers, key.str, key.len, (void *) header, sizeof(zval *), NULL);
463
464 efree(key.str);
465 }
466 }
467 }
468 }
469
470 if (headers) {
471 zend_hash_copy(headers, HTTP_G->request.headers, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
472 }
473 }
474 /* }}} */
475
476 /* {{{ STATUS http_get_response_headers(HashTable *) */
477 PHP_HTTP_API STATUS _http_get_response_headers(HashTable *headers_ht TSRMLS_DC)
478 {
479 STATUS status;
480 phpstr headers;
481
482 phpstr_init(&headers);
483 zend_llist_apply_with_argument(&SG(sapi_headers).headers, http_grab_response_headers, &headers TSRMLS_CC);
484 phpstr_fix(&headers);
485
486 status = http_parse_headers_ex(PHPSTR_VAL(&headers), headers_ht, 1);
487 phpstr_dtor(&headers);
488
489 return status;
490 }
491 /* }}} */
492
493 /* {{{ zend_bool http_match_request_header(char *, char *) */
494 PHP_HTTP_API zend_bool _http_match_request_header_ex(const char *header, const char *value, zend_bool match_case TSRMLS_DC)
495 {
496 char *name;
497 uint name_len = strlen(header);
498 zend_bool result = 0;
499 zval **data, *zvalue;
500
501 http_get_request_headers(NULL);
502 name = pretty_key(estrndup(header, name_len), name_len, 1, 1);
503 if (SUCCESS == zend_hash_find(HTTP_G->request.headers, name, name_len+1, (void *) &data)) {
504 zvalue = zval_copy(IS_STRING, *data);
505 result = (match_case ? strcmp(Z_STRVAL_P(zvalue), value) : strcasecmp(Z_STRVAL_P(zvalue), value)) ? 0 : 1;
506 zval_free(&zvalue);
507 }
508 efree(name);
509
510 return result;
511 }
512 /* }}} */
513
514
515 /*
516 * Local variables:
517 * tab-width: 4
518 * c-basic-offset: 4
519 * End:
520 * vim600: noet sw=4 ts=4 fdm=marker
521 * vim<600: noet sw=4 ts=4
522 */
523