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