- print doesn't like commas
[m6w6/ext-http] / http_headers_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "ext/standard/php_string.h"
24 #include "ext/standard/url.h"
25
26 #include "php_http.h"
27 #include "php_http_std_defs.h"
28 #include "php_http_api.h"
29 #include "php_http_headers_api.h"
30 #include "php_http_info_api.h"
31
32 #include <ctype.h>
33
34 ZEND_EXTERN_MODULE_GLOBALS(http);
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_q(char *, HashTable *, char *) */
56 PHP_HTTP_API char *_http_negotiate_q(const char *entry, const HashTable *supported, const char *def TSRMLS_DC)
57 {
58 zval *zaccept, zdelim, zarray, zentries, **zentry, **zsupp;
59 char *q_ptr = NULL, *key = NULL;
60 int i = 0;
61 ulong idx = 0;
62 double qual;
63
64 HTTP_GSC(zaccept, entry, estrdup(def));
65
66 array_init(&zarray);
67 array_init(&zentries);
68
69 Z_STRVAL(zdelim) = ",";
70 Z_STRLEN(zdelim) = 1;
71
72 php_explode(&zdelim, zaccept, &zarray, -1);
73
74 FOREACH_HASH_VAL(Z_ARRVAL(zarray), zentry) {
75 if (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';')) {
76 qual = strtod(q_ptr + 3, NULL);
77 *q_ptr = 0;
78 q_ptr = NULL;
79 } else {
80 qual = 1000.0 - i++;
81 }
82 /* TODO: support primaries only, too */
83 FOREACH_HASH_VAL((HashTable *)supported, zsupp) {
84 if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
85 add_assoc_double(&zentries, Z_STRVAL_PP(zsupp), qual);
86 break;
87 }
88 }
89 }
90 zval_dtor(&zarray);
91
92 zend_hash_sort(Z_ARRVAL(zentries), zend_qsort, http_sort_q, 0 TSRMLS_CC);
93
94 FOREACH_HASH_KEY(Z_ARRVAL(zentries), key, idx) {
95 if (key) {
96 key = estrdup(key);
97 zval_dtor(&zentries);
98 return key;
99 }
100 }
101 zval_dtor(&zentries);
102
103 return estrdup(def);
104 }
105 /* }}} */
106
107 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
108 PHP_HTTP_API http_range_status _http_get_request_ranges(HashTable *ranges, size_t length TSRMLS_DC)
109 {
110 zval *zrange;
111 char *range, c;
112 long begin = -1, end = -1, *ptr;
113
114 HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
115 range = Z_STRVAL_P(zrange);
116
117 if (strncmp(range, "bytes=", sizeof("bytes=") - 1)) {
118 return RANGE_NO;
119 }
120
121 ptr = &begin;
122 range += sizeof("bytes=") - 1;
123
124 do {
125 switch (c = *(range++))
126 {
127 case '0':
128 /* allow 000... - shall we? */
129 if (*ptr != -10) {
130 *ptr *= 10;
131 }
132 break;
133
134 case '1': case '2': case '3':
135 case '4': case '5': case '6':
136 case '7': case '8': case '9':
137 /*
138 * If the value of the pointer is already set (non-negative)
139 * then multiply its value by ten and add the current value,
140 * else initialise the pointers value with the current value
141 * --
142 * This let us recognize empty fields when validating the
143 * ranges, i.e. a "-10" for begin and "12345" for the end
144 * was the following range request: "Range: bytes=0-12345";
145 * While a "-1" for begin and "12345" for the end would
146 * have been: "Range: bytes=-12345".
147 */
148 if (*ptr > 0) {
149 *ptr *= 10;
150 *ptr += c - '0';
151 } else {
152 *ptr = c - '0';
153 }
154 break;
155
156 case '-':
157 ptr = &end;
158 break;
159
160 case ' ':
161 /* IE - ignore for now */
162 break;
163
164 case 0:
165 case ',':
166
167 if (length) {
168 /* validate ranges */
169 switch (begin)
170 {
171 /* "0-12345" */
172 case -10:
173 /* "0-" */
174 if (end == -1) {
175 return RANGE_NO;
176 }
177 /* "0-0" or overflow */
178 if (end == -10 || length <= (size_t) end) {
179 return RANGE_ERR;
180 }
181 begin = 0;
182 break;
183
184 /* "-12345" */
185 case -1:
186 /* "-", "-0" or overflow */
187 if (end == -1 || end == -10 || length <= (size_t) end) {
188 return RANGE_ERR;
189 }
190 begin = length - end;
191 end = length - 1;
192 break;
193
194 /* "12345-(xxx)" */
195 default:
196 switch (end)
197 {
198 /* "12345-0" */
199 case -10:
200 return RANGE_ERR;
201 break;
202
203 /* "12345-" */
204 case -1:
205 if (length <= (size_t) begin) {
206 return RANGE_ERR;
207 }
208 end = length - 1;
209 break;
210
211 /* "12345-67890" */
212 default:
213 if ( (length <= (size_t) begin) ||
214 (length <= (size_t) end) ||
215 (end < begin)) {
216 return RANGE_ERR;
217 }
218 break;
219 }
220 break;
221 }
222 }
223 {
224 zval *zentry;
225 MAKE_STD_ZVAL(zentry);
226 array_init(zentry);
227 add_index_long(zentry, 0, begin);
228 add_index_long(zentry, 1, end);
229 zend_hash_next_index_insert(ranges, &zentry, sizeof(zval *), NULL);
230
231 begin = -1;
232 end = -1;
233 ptr = &begin;
234 }
235 break;
236
237 default:
238 return RANGE_NO;
239 break;
240 }
241 } while (c != 0);
242
243 return RANGE_OK;
244 }
245 /* }}} */
246
247 /* {{{ STATUS http_parse_headers(char *, HashTable *, zend_bool) */
248 PHP_HTTP_API STATUS _http_parse_headers_ex(const char *header, HashTable *headers, zend_bool prettify,
249 http_info_callback callback_func, void **callback_data TSRMLS_DC)
250 {
251 const char *colon = NULL, *line = NULL, *begin = header;
252 const char *body = http_locate_body(header);
253 size_t header_len;
254 zval array;
255
256 Z_ARRVAL(array) = headers;
257 if (body) {
258 header_len = body - header;
259 } else {
260 header_len = strlen(header) + 1;
261 }
262 line = header;
263
264 while (header_len >= (size_t) (line - begin)) {
265 int value_len = 0;
266
267 switch (*line++)
268 {
269 case ':':
270 if (!colon) {
271 colon = line - 1;
272 }
273 break;
274
275 case 0:
276 --value_len; /* we don't have CR so value length is one char less */
277 case '\n':
278 if ((!(*line - 1)) || ((*line != ' ') && (*line != '\t'))) {
279 http_info i;
280
281 /* response/request line */
282 if (SUCCESS == http_info_parse(header, &i)) {
283 callback_func(callback_data, &headers, &i TSRMLS_CC);
284 http_info_dtor(&i);
285 Z_ARRVAL(array) = headers;
286 } else
287
288 /* "header: value" pair */
289 if (colon) {
290
291 /* skip empty key */
292 if (header != colon) {
293 zval **previous = NULL;
294 char *value;
295 int keylen = colon - header;
296 char *key = estrndup(header, keylen);
297
298 if (prettify) {
299 key = pretty_key(key, keylen, 1, 1);
300 }
301
302 value_len += line - colon - 1;
303
304 /* skip leading ws */
305 while (isspace(*(++colon))) --value_len;
306 /* skip trailing ws */
307 while (isspace(colon[value_len - 1])) --value_len;
308
309 if (value_len > 0) {
310 value = estrndup(colon, value_len);
311 } else {
312 value = estrdup("");
313 value_len = 0;
314 }
315
316 /* if we already have got such a header make an array of those */
317 if (SUCCESS == zend_hash_find(headers, key, keylen + 1, (void **) &previous)) {
318 /* convert to array */
319 if (Z_TYPE_PP(previous) != IS_ARRAY) {
320 convert_to_array(*previous);
321 }
322 add_next_index_stringl(*previous, value, value_len, 0);
323 } else {
324 add_assoc_stringl(&array, key, value, value_len, 0);
325 }
326 efree(key);
327 }
328 }
329 colon = NULL;
330 value_len = 0;
331 header += line - header;
332 }
333 break;
334 }
335 }
336 return SUCCESS;
337 }
338 /* }}} */
339
340 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
341 PHP_HTTP_API void _http_get_request_headers_ex(HashTable *headers, zend_bool prettify TSRMLS_DC)
342 {
343 char *key = NULL;
344 ulong idx = 0;
345 zval array, **hsv;
346
347 Z_ARRVAL(array) = headers;
348
349 if (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &hsv)) {
350 FOREACH_KEY(*hsv, key, idx) {
351 if (key && !strncmp(key, "HTTP_", 5)) {
352 zval **header;
353
354 key += 5;
355 if (prettify) {
356 key = pretty_key(key, strlen(key), 1, 1);
357 }
358
359 zend_hash_get_current_data(Z_ARRVAL_PP(hsv), (void **) &header);
360 add_assoc_stringl(&array, key, Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
361 key = NULL;
362 }
363 }
364 }
365 }
366 /* }}} */
367
368 /* {{{ zend_bool http_match_request_header(char *, char *) */
369 PHP_HTTP_API zend_bool _http_match_request_header_ex(const char *header, const char *value, zend_bool match_case TSRMLS_DC)
370 {
371 char *name, *key = NULL;
372 ulong idx;
373 zend_bool result = 0;
374 HashTable headers;
375
376 name = pretty_key(estrdup(header), strlen(header), 1, 1);
377 zend_hash_init(&headers, 0, NULL, ZVAL_PTR_DTOR, 0);
378 http_get_request_headers_ex(&headers, 1);
379
380 FOREACH_HASH_KEY(&headers, key, idx) {
381 if (key && (!strcmp(key, name))) {
382 zval **data;
383
384 if (SUCCESS == zend_hash_get_current_data(&headers, (void **) &data)) {
385 result = (match_case ? strcmp(Z_STRVAL_PP(data), value) : strcasecmp(Z_STRVAL_PP(data), value)) ? 0 : 1;
386 }
387 break;
388 }
389 }
390
391 zend_hash_destroy(&headers);
392 efree(name);
393
394 return result;
395 }
396 /* }}} */
397
398
399 /*
400 * Local variables:
401 * tab-width: 4
402 * c-basic-offset: 4
403 * End:
404 * vim600: noet sw=4 ts=4 fdm=marker
405 * vim<600: noet sw=4 ts=4
406 */
407