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