- fix some gcc warnings
[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
31 #include <ctype.h>
32
33 /* {{{ static int http_sort_q(const void *, const void *) */
34 static int http_sort_q(const void *a, const void *b TSRMLS_DC)
35 {
36 Bucket *f, *s;
37 zval result, *first, *second;
38
39 f = *((Bucket **) a);
40 s = *((Bucket **) b);
41
42 first = *((zval **) f->pData);
43 second= *((zval **) s->pData);
44
45 if (numeric_compare_function(&result, first, second TSRMLS_CC) != SUCCESS) {
46 return 0;
47 }
48 return (Z_LVAL(result) > 0 ? -1 : (Z_LVAL(result) < 0 ? 1 : 0));
49 }
50 /* }}} */
51
52 /* {{{ char *http_negotiate_q(char *, HashTable *, char *) */
53 PHP_HTTP_API char *_http_negotiate_q(const char *entry, const HashTable *supported, const char *def TSRMLS_DC)
54 {
55 zval *zaccept, zdelim, zarray, zentries, **zentry, **zsupp;
56 char *q_ptr = NULL, *key = NULL;
57 int i = 0;
58 ulong idx = 0;
59 double qual;
60
61 HTTP_GSC(zaccept, entry, estrdup(def));
62
63 array_init(&zarray);
64 array_init(&zentries);
65
66 Z_STRVAL(zdelim) = ",";
67 Z_STRLEN(zdelim) = 1;
68
69 php_explode(&zdelim, zaccept, &zarray, -1);
70
71 FOREACH_HASH_VAL(Z_ARRVAL(zarray), zentry) {
72 if (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';')) {
73 qual = strtod(q_ptr + 3, NULL);
74 *q_ptr = 0;
75 q_ptr = NULL;
76 } else {
77 qual = 1000.0 - i++;
78 }
79 /* TODO: support primaries only, too */
80 FOREACH_HASH_VAL((HashTable *)supported, zsupp) {
81 if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
82 add_assoc_double(&zentries, Z_STRVAL_PP(zsupp), qual);
83 break;
84 }
85 }
86 }
87 zval_dtor(&zarray);
88
89 zend_hash_sort(Z_ARRVAL(zentries), zend_qsort, http_sort_q, 0 TSRMLS_CC);
90
91 FOREACH_HASH_KEY(Z_ARRVAL(zentries), key, idx) {
92 if (key) {
93 key = estrdup(key);
94 zval_dtor(&zentries);
95 return key;
96 }
97 }
98 zval_dtor(&zentries);
99
100 return estrdup(def);
101 }
102 /* }}} */
103
104 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
105 PHP_HTTP_API http_range_status _http_get_request_ranges(HashTable *ranges, size_t length TSRMLS_DC)
106 {
107 zval *zrange;
108 char *range, c;
109 long begin = -1, end = -1, *ptr;
110
111 HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
112 range = Z_STRVAL_P(zrange);
113
114 if (strncmp(range, "bytes=", sizeof("bytes=") - 1)) {
115 /* should we really issue a notice for a client misbehaviour?
116 http_error(E_NOTICE, HTTP_E_HEADER, "Range header misses bytes=");
117 */
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 *ptr *= 10;
129 break;
130
131 case '1': case '2': case '3':
132 case '4': case '5': case '6':
133 case '7': case '8': case '9':
134 /*
135 * If the value of the pointer is already set (non-negative)
136 * then multiply its value by ten and add the current value,
137 * else initialise the pointers value with the current value
138 * --
139 * This let us recognize empty fields when validating the
140 * ranges, i.e. a "-10" for begin and "12345" for the end
141 * was the following range request: "Range: bytes=0-12345";
142 * While a "-1" for begin and "12345" for the end would
143 * have been: "Range: bytes=-12345".
144 */
145 if (*ptr > 0) {
146 *ptr *= 10;
147 *ptr += c - '0';
148 } else {
149 *ptr = c - '0';
150 }
151 break;
152
153 case '-':
154 ptr = &end;
155 break;
156
157 case ' ':
158 /* IE - ignore for now */
159 break;
160
161 case 0:
162 case ',':
163
164 if (length) {
165 /* validate ranges */
166 switch (begin)
167 {
168 /* "0-12345" */
169 case -10:
170 /* "0-" */
171 if (end == -1) {
172 return RANGE_NO;
173 }
174 /* "0-0" or overflow */
175 if (end == -10 || length <= (size_t) end) {
176 return RANGE_ERR;
177 }
178 begin = 0;
179 break;
180
181 /* "-12345" */
182 case -1:
183 /* "-", "-0" or overflow */
184 if (end == -1 || end == -10 || length <= (size_t) end) {
185 return RANGE_ERR;
186 }
187 begin = length - end;
188 end = length - 1;
189 break;
190
191 /* "12345-(xxx)" */
192 default:
193 switch (end)
194 {
195 /* "12345-0" */
196 case -10:
197 return RANGE_ERR;
198 break;
199
200 /* "12345-" */
201 case -1:
202 if (length <= (size_t) begin) {
203 return RANGE_ERR;
204 }
205 end = length - 1;
206 break;
207
208 /* "12345-67890" */
209 default:
210 if ( (length <= (size_t) begin) ||
211 (length <= (size_t) end) ||
212 (end < begin)) {
213 return RANGE_ERR;
214 }
215 break;
216 }
217 break;
218 }
219 }
220 {
221 zval *zentry;
222 MAKE_STD_ZVAL(zentry);
223 array_init(zentry);
224 add_index_long(zentry, 0, begin);
225 add_index_long(zentry, 1, end);
226 zend_hash_next_index_insert(ranges, &zentry, sizeof(zval *), NULL);
227
228 begin = -1;
229 end = -1;
230 ptr = &begin;
231 }
232 break;
233
234 default:
235 return RANGE_NO;
236 break;
237 }
238 } while (c != 0);
239
240 return RANGE_OK;
241 }
242 /* }}} */
243
244 /* {{{ STATUS http_parse_headers(char *, HashTable *, zend_bool) */
245 PHP_HTTP_API STATUS _http_parse_headers_ex(const char *header, HashTable *headers, zend_bool prettify, http_parse_headers_callback_t func, void **callback_data TSRMLS_DC)
246 {
247 const char *colon = NULL, *line = NULL, *begin = header, *crlfcrlf = NULL;
248 size_t header_len;
249 zval array;
250
251 Z_ARRVAL(array) = headers;
252
253 if (crlfcrlf = strstr(header, HTTP_CRLF HTTP_CRLF)) {
254 header_len = crlfcrlf - header + lenof(HTTP_CRLF);
255 } else {
256 header_len = strlen(header) + 1;
257 }
258
259
260 if (header_len < 2 || !strchr(header, ':')) {
261 http_error(E_WARNING, HTTP_E_PARSE, "Cannot parse too short or malformed HTTP headers");
262 return FAILURE;
263 }
264
265 line = header;
266
267 while (header_len >= (size_t) (line - begin)) {
268 int value_len = 0;
269
270 switch (*line++)
271 {
272 case 0:
273 --value_len; /* we don't have CR so value length is one char less */
274 case '\n':
275 if ((!(*line - 1)) || ((*line != ' ') && (*line != '\t'))) {
276 /* response/request line */
277 if ( (!strncmp(header, "HTTP/1.", lenof("HTTP/1."))) ||
278 (!strncmp(line - lenof("HTTP/1.x" HTTP_CRLF) + value_len, "HTTP/1.", lenof("HTTP/1.")))) {
279 if (func) {
280 func(header, &headers, callback_data TSRMLS_CC);
281 Z_ARRVAL(array) = headers;
282 }
283 } else
284
285 /* "header: value" pair */
286 if (colon) {
287
288 /* skip empty key */
289 if (header != colon) {
290 zval **previous = NULL;
291 char *value;
292 int keylen = colon - header;
293 char *key = estrndup(header, keylen);
294
295 if (prettify) {
296 key = pretty_key(key, keylen, 1, 1);
297 }
298
299 value_len += line - colon - 1;
300
301 /* skip leading ws */
302 while (isspace(*(++colon))) --value_len;
303 /* skip trailing ws */
304 while (isspace(colon[value_len - 1])) --value_len;
305
306 if (value_len > 0) {
307 value = estrndup(colon, value_len);
308 } else {
309 value = estrdup("");
310 value_len = 0;
311 }
312
313 /* if we already have got such a header make an array of those */
314 if (SUCCESS == zend_hash_find(headers, key, keylen + 1, (void **) &previous)) {
315 /* convert to array */
316 if (Z_TYPE_PP(previous) != IS_ARRAY) {
317 convert_to_array(*previous);
318 }
319 add_next_index_stringl(*previous, value, value_len, 0);
320 } else {
321 add_assoc_stringl(&array, key, value, value_len, 0);
322 }
323 efree(key);
324 }
325 }
326 colon = NULL;
327 value_len = 0;
328 header += line - header;
329 }
330 break;
331
332 case ':':
333 if (!colon) {
334 colon = line - 1;
335 }
336 break;
337 }
338 }
339 return SUCCESS;
340 }
341 /* }}} */
342
343 PHP_HTTP_API void _http_parse_headers_default_callback(const char *http_line, HashTable **headers, void **cb_data TSRMLS_DC)
344 {
345 zval array;
346 char *crlf = NULL;
347 size_t line_length;
348 Z_ARRVAL(array) = *headers;
349
350 if (crlf = strstr(http_line, HTTP_CRLF)) {
351 line_length = crlf - http_line;
352 } else {
353 line_length = strlen(http_line);
354 }
355
356 /* response */
357 if (!strncmp(http_line, "HTTP/1.", lenof("HTTP/1."))) {
358 char *status = estrndup(http_line + lenof("HTTP/1.x "), line_length - lenof("HTTP/1.x "));
359 add_assoc_stringl(&array, "Response Status", status, line_length - lenof("HTTP/1.x "), 0);
360 } else
361 /* request */
362 if (!strncmp(http_line + line_length - lenof("HTTP/1.x"), "HTTP/1.", lenof("HTTP/1."))) {
363 char *sep = strchr(http_line, ' ');
364 char *url = estrndup(sep + 1, strstr(sep, "HTTP/1.") - sep + 1 + 1);
365 char *met = estrndup(http_line, sep - http_line);
366
367 add_assoc_stringl(&array, "Request Method", met, sep - http_line, 0);
368 add_assoc_stringl(&array, "Request Uri", url, strstr(sep, "HTTP/1.") - sep + 1 + 1, 0);
369 }
370 }
371
372 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
373 PHP_HTTP_API void _http_get_request_headers_ex(HashTable *headers, zend_bool prettify TSRMLS_DC)
374 {
375 char *key = NULL;
376 ulong idx = 0;
377 zval array;
378
379 Z_ARRVAL(array) = headers;
380
381 FOREACH_HASH_KEY(HTTP_SERVER_VARS, key, idx) {
382 if (key && !strncmp(key, "HTTP_", 5)) {
383 zval **header;
384
385 key += 5;
386 if (prettify) {
387 key = pretty_key(key, strlen(key), 1, 1);
388 }
389
390 zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
391 add_assoc_stringl(&array, key, Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
392 key = NULL;
393 }
394 }
395 }
396 /* }}} */
397
398 /* {{{ zend_bool http_match_request_header(char *, char *) */
399 PHP_HTTP_API zend_bool _http_match_request_header_ex(const char *header, const char *value, zend_bool match_case TSRMLS_DC)
400 {
401 char *name, *key = NULL;
402 ulong idx;
403 zend_bool result = 0;
404 HashTable headers;
405
406 name = pretty_key(estrdup(header), strlen(header), 1, 1);
407 zend_hash_init(&headers, 0, NULL, ZVAL_PTR_DTOR, 0);
408 http_get_request_headers_ex(&headers, 1);
409
410 FOREACH_HASH_KEY(&headers, key, idx) {
411 if (key && (!strcmp(key, name))) {
412 zval **data;
413
414 if (SUCCESS == zend_hash_get_current_data(&headers, (void **) &data)) {
415 result = (match_case ? strcmp(Z_STRVAL_PP(data), value) : strcasecmp(Z_STRVAL_PP(data), value)) ? 0 : 1;
416 }
417 break;
418 }
419 }
420
421 zend_hash_destroy(&headers);
422 efree(name);
423
424 return result;
425 }
426 /* }}} */
427
428
429 /*
430 * Local variables:
431 * tab-width: 4
432 * c-basic-offset: 4
433 * End:
434 * vim600: noet sw=4 ts=4 fdm=marker
435 * vim<600: noet sw=4 ts=4
436 */
437