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