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
40 /* {{{ static int http_sort_q(const void *, const void *) */
41 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
44 zval result
, *first
, *second
;
49 first
= *((zval
**) f
->pData
);
50 second
= *((zval
**) s
->pData
);
52 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
55 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
59 /* {{{ char *http_negotiate_language_func */
60 char *_http_negotiate_language_func(const char *test
, double *quality
, HashTable
*supported TSRMLS_DC
)
63 const char *dash_test
;
65 FOREACH_HASH_VAL(supported
, value
) {
67 fprintf(stderr
, "strcasecmp('%s', '%s')\n", Z_STRVAL_PP(value
), test
);
69 if (!strcasecmp(Z_STRVAL_PP(value
), test
)) {
70 return Z_STRVAL_PP(value
);
74 /* no distinct match found, so try primaries */
75 if (dash_test
= strchr(test
, '-')) {
76 FOREACH_HASH_VAL(supported
, value
) {
77 int len
= dash_test
- test
;
79 fprintf(stderr
, "strncascmp('%s', '%s', %d)\n", Z_STRVAL_PP(value
), test
, len
);
81 if ( (!strncasecmp(Z_STRVAL_PP(value
), test
, len
)) &&
82 ( (Z_STRVAL_PP(value
)[len
] == '\0') ||
83 (Z_STRVAL_PP(value
)[len
] == '-'))) {
85 return Z_STRVAL_PP(value
);
94 /* {{{ char *http_negotiate_charset_func */
95 char *_http_negotiate_charset_func(const char *test
, double *quality
, HashTable
*supported TSRMLS_DC
)
99 FOREACH_HASH_VAL(supported
, value
) {
101 fprintf(stderr
, "strcasecmp('%s', '%s')\n", Z_STRVAL_PP(value
), test
);
103 if (!strcasecmp(Z_STRVAL_PP(value
), test
)) {
104 return Z_STRVAL_PP(value
);
112 /* {{{ HashTable *http_negotiate_q(const char *, HashTable *, negotiate_func_t) */
113 PHP_HTTP_API HashTable
*_http_negotiate_q(const char *header
, HashTable
*supported
, negotiate_func_t neg TSRMLS_DC
)
116 HashTable
*result
= NULL
;
119 fprintf(stderr
, "Reading header %s: ", header
);
121 HTTP_GSC(accept
, header
, NULL
);
123 fprintf(stderr
, "%s\n", Z_STRVAL_P(accept
));
126 if (Z_STRLEN_P(accept
)) {
131 ZVAL_STRINGL(&ex_del
, ",", 1, 0);
134 php_explode(&ex_del
, accept
, &ex_arr
, -1);
136 if (zend_hash_num_elements(Z_ARRVAL(ex_arr
)) > 0) {
143 FOREACH_HASH_VAL(Z_ARRVAL(ex_arr
), entry
) {
145 char *selected
, *identifier
;
146 const char *separator
;
149 fprintf(stderr
, "Checking %s\n", Z_STRVAL_PP(entry
));
152 if (separator
= strchr(Z_STRVAL_PP(entry
), ';')) {
153 const char *ptr
= separator
;
157 } while ((*ptr
== ' ') || (*ptr
== 'q') || (*ptr
== '='));
159 quality
= strtod(ptr
, NULL
);
160 identifier
= estrndup(Z_STRVAL_PP(entry
), separator
- Z_STRVAL_PP(entry
));
162 quality
= 1000.0 - i
++;
163 identifier
= estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
166 if (selected
= neg(identifier
, &quality
, supported TSRMLS_CC
)) {
167 /* don't overwrite previously set with higher quality */
168 if (!zend_hash_exists(Z_ARRVAL(array
), selected
, strlen(selected
) + 1)) {
169 add_assoc_double(&array
, selected
, quality
);
176 result
= Z_ARRVAL(array
);
177 zend_hash_sort(result
, zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
187 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
188 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
, size_t length TSRMLS_DC
)
192 long begin
= -1, end
= -1, *ptr
;
194 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
195 range
= Z_STRVAL_P(zrange
);
197 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
202 range
+= sizeof("bytes=") - 1;
205 switch (c
= *(range
++))
208 /* allow 000... - shall we? */
214 case '1': case '2': case '3':
215 case '4': case '5': case '6':
216 case '7': case '8': case '9':
218 * If the value of the pointer is already set (non-negative)
219 * then multiply its value by ten and add the current value,
220 * else initialise the pointers value with the current value
222 * This let us recognize empty fields when validating the
223 * ranges, i.e. a "-10" for begin and "12345" for the end
224 * was the following range request: "Range: bytes=0-12345";
225 * While a "-1" for begin and "12345" for the end would
226 * have been: "Range: bytes=-12345".
241 /* IE - ignore for now */
248 /* validate ranges */
257 /* "0-0" or overflow */
258 if (end
== -10 || length
<= (size_t) end
) {
266 /* "-", "-0" or overflow */
267 if (end
== -1 || end
== -10 || length
<= (size_t) end
) {
270 begin
= length
- end
;
285 if (length
<= (size_t) begin
) {
293 if ( (length
<= (size_t) begin
) ||
294 (length
<= (size_t) end
) ||
305 MAKE_STD_ZVAL(zentry
);
307 add_index_long(zentry
, 0, begin
);
308 add_index_long(zentry
, 1, end
);
309 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
327 /* {{{ STATUS http_parse_headers(char *, HashTable *, zend_bool) */
328 PHP_HTTP_API STATUS
_http_parse_headers_ex(const char *header
, HashTable
*headers
, zend_bool prettify
,
329 http_info_callback callback_func
, void **callback_data TSRMLS_DC
)
331 const char *colon
= NULL
, *line
= NULL
, *begin
= header
;
332 const char *body
= http_locate_body(header
);
336 Z_ARRVAL(array
) = headers
;
338 header_len
= body
- header
;
340 header_len
= strlen(header
) + 1;
344 while (header_len
>= (size_t) (line
- begin
)) {
356 --value_len
; /* we don't have CR so value length is one char less */
358 if ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t'))) {
361 /* response/request line */
362 if (SUCCESS
== http_info_parse(header
, &i
)) {
363 callback_func(callback_data
, &headers
, &i TSRMLS_CC
);
365 Z_ARRVAL(array
) = headers
;
368 /* "header: value" pair */
372 if (header
!= colon
) {
373 zval
**previous
= NULL
;
375 int keylen
= colon
- header
;
376 char *key
= estrndup(header
, keylen
);
379 key
= pretty_key(key
, keylen
, 1, 1);
382 value_len
+= line
- colon
- 1;
384 /* skip leading ws */
385 while (isspace(*(++colon
))) --value_len
;
386 /* skip trailing ws */
387 while (isspace(colon
[value_len
- 1])) --value_len
;
390 value
= estrndup(colon
, value_len
);
396 /* if we already have got such a header make an array of those */
397 if (SUCCESS
== zend_hash_find(headers
, key
, keylen
+ 1, (void **) &previous
)) {
398 /* convert to array */
399 if (Z_TYPE_PP(previous
) != IS_ARRAY
) {
400 convert_to_array(*previous
);
402 add_next_index_stringl(*previous
, value
, value_len
, 0);
404 add_assoc_stringl(&array
, key
, value
, value_len
, 0);
411 header
+= line
- header
;
420 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
421 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
427 Z_ARRVAL(array
) = headers
;
429 if (SUCCESS
== zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void **) &hsv
)) {
430 FOREACH_KEY(*hsv
, key
, idx
) {
431 if (key
&& !strncmp(key
, "HTTP_", 5)) {
436 key
= pretty_key(key
, strlen(key
), 1, 1);
439 zend_hash_get_current_data(Z_ARRVAL_PP(hsv
), (void **) &header
);
440 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
448 /* {{{ zend_bool http_match_request_header(char *, char *) */
449 PHP_HTTP_API zend_bool
_http_match_request_header_ex(const char *header
, const char *value
, zend_bool match_case TSRMLS_DC
)
451 char *name
, *key
= NULL
;
453 zend_bool result
= 0;
456 name
= pretty_key(estrdup(header
), strlen(header
), 1, 1);
457 zend_hash_init(&headers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
458 http_get_request_headers_ex(&headers
, 1);
460 FOREACH_HASH_KEY(&headers
, key
, idx
) {
461 if (key
&& (!strcmp(key
, name
))) {
464 if (SUCCESS
== zend_hash_get_current_data(&headers
, (void **) &data
)) {
465 result
= (match_case
? strcmp(Z_STRVAL_PP(data
), value
) : strcasecmp(Z_STRVAL_PP(data
), value
)) ? 0 : 1;
471 zend_hash_destroy(&headers
);
484 * vim600: noet sw=4 ts=4 fdm=marker
485 * vim<600: noet sw=4 ts=4