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