2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
23 #include "ext/standard/php_string.h"
24 #include "ext/standard/url.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"
34 ZEND_EXTERN_MODULE_GLOBALS(http
);
37 # define HTTP_DBG_NEG 0
41 PHP_MINIT_FUNCTION(http_headers
)
43 HTTP_LONG_CONSTANT("HTTP_REDIRECT", HTTP_REDIRECT
);
44 HTTP_LONG_CONSTANT("HTTP_REDIRECT_PERM", HTTP_REDIRECT_PERM
);
45 HTTP_LONG_CONSTANT("HTTP_REDIRECT_POST", HTTP_REDIRECT_POST
);
46 HTTP_LONG_CONSTANT("HTTP_REDIRECT_TEMP", HTTP_REDIRECT_TEMP
);
52 /* {{{ static int http_sort_q(const void *, const void *) */
53 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
56 zval result
, *first
, *second
;
61 first
= *((zval
**) f
->pData
);
62 second
= *((zval
**) s
->pData
);
64 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
67 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
71 /* {{{ char *http_negotiate_language_func */
72 char *_http_negotiate_language_func(const char *test
, double *quality
, HashTable
*supported TSRMLS_DC
)
75 const char *dash_test
;
77 FOREACH_HASH_VAL(supported
, value
) {
79 fprintf(stderr
, "strcasecmp('%s', '%s')\n", Z_STRVAL_PP(value
), test
);
81 if (!strcasecmp(Z_STRVAL_PP(value
), test
)) {
82 return Z_STRVAL_PP(value
);
86 /* no distinct match found, so try primaries */
87 if (dash_test
= strchr(test
, '-')) {
88 FOREACH_HASH_VAL(supported
, value
) {
89 int len
= dash_test
- test
;
91 fprintf(stderr
, "strncascmp('%s', '%s', %d)\n", Z_STRVAL_PP(value
), test
, len
);
93 if ( (!strncasecmp(Z_STRVAL_PP(value
), test
, len
)) &&
94 ( (Z_STRVAL_PP(value
)[len
] == '\0') ||
95 (Z_STRVAL_PP(value
)[len
] == '-'))) {
97 return Z_STRVAL_PP(value
);
106 /* {{{ char *http_negotiate_default_func */
107 char *_http_negotiate_default_func(const char *test
, double *quality
, HashTable
*supported TSRMLS_DC
)
111 FOREACH_HASH_VAL(supported
, value
) {
113 fprintf(stderr
, "strcasecmp('%s', '%s')\n", Z_STRVAL_PP(value
), test
);
115 if (!strcasecmp(Z_STRVAL_PP(value
), test
)) {
116 return Z_STRVAL_PP(value
);
124 /* {{{ HashTable *http_negotiate_q(const char *, HashTable *, negotiate_func_t) */
125 PHP_HTTP_API HashTable
*_http_negotiate_q(const char *header
, HashTable
*supported
, negotiate_func_t neg TSRMLS_DC
)
128 HashTable
*result
= NULL
;
131 fprintf(stderr
, "Reading header %s: ", header
);
133 HTTP_GSC(accept
, header
, NULL
);
135 fprintf(stderr
, "%s\n", Z_STRVAL_P(accept
));
138 if (Z_STRLEN_P(accept
)) {
143 ZVAL_STRINGL(&ex_del
, ",", 1, 0);
146 php_explode(&ex_del
, accept
, &ex_arr
, -1);
148 if (zend_hash_num_elements(Z_ARRVAL(ex_arr
)) > 0) {
155 FOREACH_HASH_VAL(Z_ARRVAL(ex_arr
), entry
) {
157 char *selected
, *identifier
;
158 const char *separator
;
161 fprintf(stderr
, "Checking %s\n", Z_STRVAL_PP(entry
));
164 if (separator
= strchr(Z_STRVAL_PP(entry
), ';')) {
165 const char *ptr
= separator
;
167 while (*++ptr
&& !isdigit(*ptr
));
170 identifier
= estrndup(Z_STRVAL_PP(entry
), separator
- Z_STRVAL_PP(entry
));
172 quality
= 1000.0 - i
++;
173 identifier
= estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
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
);
186 result
= Z_ARRVAL(array
);
187 zend_hash_sort(result
, zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
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
)
202 long begin
= -1, end
= -1, *ptr
;
204 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
205 range
= Z_STRVAL_P(zrange
);
207 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
212 range
+= sizeof("bytes=") - 1;
215 switch (c
= *(range
++))
218 /* allow 000... - shall we? */
224 case '1': case '2': case '3':
225 case '4': case '5': case '6':
226 case '7': case '8': case '9':
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
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".
251 /* IE - ignore for now */
258 /* validate ranges */
267 /* "0-0" or overflow */
268 if (end
== -10 || length
<= (size_t) end
) {
276 /* "-", "-0" or overflow */
277 if (end
== -1 || end
== -10 || length
<= (size_t) end
) {
280 begin
= length
- end
;
295 if (length
<= (size_t) begin
) {
303 if ( (length
<= (size_t) begin
) ||
304 (length
<= (size_t) end
) ||
315 MAKE_STD_ZVAL(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
);
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
)
341 const char *colon
= NULL
, *line
= NULL
, *begin
= header
;
342 const char *body
= http_locate_body(header
);
346 INIT_ZARR(array
, headers
);
349 header_len
= body
- header
;
351 header_len
= strlen(header
) + 1;
355 while (header_len
>= (size_t) (line
- begin
)) {
357 /* note: valgrind may choke on that -- should be safe though */
367 --value_len
; /* we don't have CR so value length is one char less */
369 if ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t'))) {
372 /* response/request line */
373 if (SUCCESS
== http_info_parse(header
, &i
)) {
374 callback_func(callback_data
, &headers
, &i TSRMLS_CC
);
376 Z_ARRVAL(array
) = headers
;
379 /* "header: value" pair */
383 if (header
!= colon
) {
384 zval
**previous
= NULL
;
386 int keylen
= colon
- header
;
387 char *key
= estrndup(header
, keylen
);
390 key
= pretty_key(key
, keylen
, 1, 1);
393 value_len
+= line
- colon
- 1;
395 /* skip leading ws */
396 while (isspace(*(++colon
))) --value_len
;
397 /* skip trailing ws */
398 while (isspace(colon
[value_len
- 1])) --value_len
;
401 value
= estrndup(colon
, value_len
);
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
);
413 add_next_index_stringl(*previous
, value
, value_len
, 0);
415 add_assoc_stringl(&array
, key
, value
, value_len
, 0);
422 header
+= line
- header
;
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
)
438 Z_ARRVAL(array
) = headers
;
440 if (SUCCESS
== zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void **) &hsv
)) {
441 FOREACH_KEY(*hsv
, key
, idx
) {
442 if (key
&& !strncmp(key
, "HTTP_", 5)) {
447 key
= pretty_key(key
, strlen(key
), 1, 1);
450 zend_hash_get_current_data(Z_ARRVAL_PP(hsv
), (void **) &header
);
451 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
459 /* {{{ zend_bool http_match_request_header(char *, char *) */
460 PHP_HTTP_API zend_bool
_http_match_request_header_ex(const char *header
, const char *value
, zend_bool match_case TSRMLS_DC
)
462 char *name
, *key
= NULL
;
464 zend_bool result
= 0;
467 name
= pretty_key(estrdup(header
), strlen(header
), 1, 1);
468 zend_hash_init(&headers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
469 http_get_request_headers_ex(&headers
, 1);
471 FOREACH_HASH_KEY(&headers
, key
, idx
) {
472 if (key
&& (!strcmp(key
, name
))) {
475 if (SUCCESS
== zend_hash_get_current_data(&headers
, (void **) &data
)) {
476 result
= (match_case
? strcmp(Z_STRVAL_PP(data
), value
) : strcasecmp(Z_STRVAL_PP(data
), value
)) ? 0 : 1;
482 zend_hash_destroy(&headers
);
495 * vim600: noet sw=4 ts=4 fdm=marker
496 * vim<600: noet sw=4 ts=4