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
);
36 /* {{{ static int http_sort_q(const void *, const void *) */
37 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
40 zval result
, *first
, *second
;
45 first
= *((zval
**) f
->pData
);
46 second
= *((zval
**) s
->pData
);
48 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
51 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
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
)
58 zval
*zaccept
, zdelim
, zarray
, zentries
, **zentry
, **zsupp
;
59 char *q_ptr
= NULL
, *key
= NULL
;
64 HTTP_GSC(zaccept
, entry
, estrdup(def
));
67 array_init(&zentries
);
69 Z_STRVAL(zdelim
) = ",";
72 php_explode(&zdelim
, zaccept
, &zarray
, -1);
74 FOREACH_HASH_VAL(Z_ARRVAL(zarray
), zentry
) {
75 if (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';')) {
76 qual
= strtod(q_ptr
+ 3, NULL
);
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
);
92 zend_hash_sort(Z_ARRVAL(zentries
), zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
94 FOREACH_HASH_KEY(Z_ARRVAL(zentries
), key
, idx
) {
101 zval_dtor(&zentries
);
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
)
112 long begin
= -1, end
= -1, *ptr
;
114 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
115 range
= Z_STRVAL_P(zrange
);
117 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
122 range
+= sizeof("bytes=") - 1;
125 switch (c
= *(range
++))
128 /* allow 000... - shall we? */
134 case '1': case '2': case '3':
135 case '4': case '5': case '6':
136 case '7': case '8': case '9':
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
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".
161 /* IE - ignore for now */
168 /* validate ranges */
177 /* "0-0" or overflow */
178 if (end
== -10 || length
<= (size_t) end
) {
186 /* "-", "-0" or overflow */
187 if (end
== -1 || end
== -10 || length
<= (size_t) end
) {
190 begin
= length
- end
;
205 if (length
<= (size_t) begin
) {
213 if ( (length
<= (size_t) begin
) ||
214 (length
<= (size_t) end
) ||
225 MAKE_STD_ZVAL(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
);
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
)
251 const char *colon
= NULL
, *line
= NULL
, *begin
= header
;
252 const char *body
= http_locate_body(header
);
256 Z_ARRVAL(array
) = headers
;
257 header_len
= body
? body
- header
: strlen(header
) + 1;
260 while (header_len
>= (size_t) (line
- begin
)) {
272 --value_len
; /* we don't have CR so value length is one char less */
274 if ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t'))) {
277 /* response/request line */
278 if (SUCCESS
== http_info_parse(header
, &i
)) {
279 callback_func(callback_data
, &headers
, &i TSRMLS_CC
);
281 Z_ARRVAL(array
) = headers
;
284 /* "header: value" pair */
288 if (header
!= colon
) {
289 zval
**previous
= NULL
;
291 int keylen
= colon
- header
;
292 char *key
= estrndup(header
, keylen
);
295 key
= pretty_key(key
, keylen
, 1, 1);
298 value_len
+= line
- colon
- 1;
300 /* skip leading ws */
301 while (isspace(*(++colon
))) --value_len
;
302 /* skip trailing ws */
303 while (isspace(colon
[value_len
- 1])) --value_len
;
306 value
= estrndup(colon
, value_len
);
312 /* if we already have got such a header make an array of those */
313 if (SUCCESS
== zend_hash_find(headers
, key
, keylen
+ 1, (void **) &previous
)) {
314 /* convert to array */
315 if (Z_TYPE_PP(previous
) != IS_ARRAY
) {
316 convert_to_array(*previous
);
318 add_next_index_stringl(*previous
, value
, value_len
, 0);
320 add_assoc_stringl(&array
, key
, value
, value_len
, 0);
327 header
+= line
- header
;
336 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
337 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
343 Z_ARRVAL(array
) = headers
;
345 if (SUCCESS
== zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void **) &hsv
)) {
346 FOREACH_KEY(*hsv
, key
, idx
) {
347 if (key
&& !strncmp(key
, "HTTP_", 5)) {
352 key
= pretty_key(key
, strlen(key
), 1, 1);
355 zend_hash_get_current_data(Z_ARRVAL_PP(hsv
), (void **) &header
);
356 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
364 /* {{{ zend_bool http_match_request_header(char *, char *) */
365 PHP_HTTP_API zend_bool
_http_match_request_header_ex(const char *header
, const char *value
, zend_bool match_case TSRMLS_DC
)
367 char *name
, *key
= NULL
;
369 zend_bool result
= 0;
372 name
= pretty_key(estrdup(header
), strlen(header
), 1, 1);
373 zend_hash_init(&headers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
374 http_get_request_headers_ex(&headers
, 1);
376 FOREACH_HASH_KEY(&headers
, key
, idx
) {
377 if (key
&& (!strcmp(key
, name
))) {
380 if (SUCCESS
== zend_hash_get_current_data(&headers
, (void **) &data
)) {
381 result
= (match_case
? strcmp(Z_STRVAL_PP(data
), value
) : strcasecmp(Z_STRVAL_PP(data
), value
)) ? 0 : 1;
387 zend_hash_destroy(&headers
);
400 * vim600: noet sw=4 ts=4 fdm=marker
401 * vim<600: noet sw=4 ts=4