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"
33 /* {{{ static int http_sort_q(const void *, const void *) */
34 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
37 zval result
, *first
, *second
;
42 first
= *((zval
**) f
->pData
);
43 second
= *((zval
**) s
->pData
);
45 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
48 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
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
)
55 zval
*zaccept
, zdelim
, zarray
, zentries
, **zentry
, **zsupp
;
56 char *q_ptr
= NULL
, *key
= NULL
;
61 HTTP_GSC(zaccept
, entry
, estrdup(def
));
64 array_init(&zentries
);
66 Z_STRVAL(zdelim
) = ",";
69 php_explode(&zdelim
, zaccept
, &zarray
, -1);
71 FOREACH_HASH_VAL(Z_ARRVAL(zarray
), zentry
) {
72 if (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';')) {
73 qual
= strtod(q_ptr
+ 3, NULL
);
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
);
89 zend_hash_sort(Z_ARRVAL(zentries
), zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
91 FOREACH_HASH_KEY(Z_ARRVAL(zentries
), key
, idx
) {
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
)
109 long begin
= -1, end
= -1, *ptr
;
111 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
112 range
= Z_STRVAL_P(zrange
);
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=");
122 range
+= sizeof("bytes=") - 1;
125 switch (c
= *(range
++))
131 case '1': case '2': case '3':
132 case '4': case '5': case '6':
133 case '7': case '8': case '9':
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
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".
158 /* IE - ignore for now */
165 /* validate ranges */
174 /* "0-0" or overflow */
175 if (end
== -10 || length
<= (size_t) end
) {
183 /* "-", "-0" or overflow */
184 if (end
== -1 || end
== -10 || length
<= (size_t) end
) {
187 begin
= length
- end
;
202 if (length
<= (size_t) begin
) {
210 if ( (length
<= (size_t) begin
) ||
211 (length
<= (size_t) end
) ||
222 MAKE_STD_ZVAL(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
);
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
)
247 const char *colon
= NULL
, *line
= NULL
, *begin
= header
, *crlfcrlf
= NULL
;
251 Z_ARRVAL(array
) = headers
;
253 if (crlfcrlf
= strstr(header
, HTTP_CRLF HTTP_CRLF
)) {
254 header_len
= crlfcrlf
- header
+ lenof(HTTP_CRLF
);
256 header_len
= strlen(header
) + 1;
260 if (header_len
< 2 || !strchr(header
, ':')) {
261 http_error(E_WARNING
, HTTP_E_PARSE
, "Cannot parse too short or malformed HTTP headers");
267 while (header_len
>= (size_t) (line
- begin
)) {
273 --value_len
; /* we don't have CR so value length is one char less */
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.")))) {
280 func(header
, &headers
, callback_data TSRMLS_CC
);
281 Z_ARRVAL(array
) = headers
;
285 /* "header: value" pair */
289 if (header
!= colon
) {
290 zval
**previous
= NULL
;
292 int keylen
= colon
- header
;
293 char *key
= estrndup(header
, keylen
);
296 key
= pretty_key(key
, keylen
, 1, 1);
299 value_len
+= line
- colon
- 1;
301 /* skip leading ws */
302 while (isspace(*(++colon
))) --value_len
;
303 /* skip trailing ws */
304 while (isspace(colon
[value_len
- 1])) --value_len
;
307 value
= estrndup(colon
, value_len
);
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
);
319 add_next_index_stringl(*previous
, value
, value_len
, 0);
321 add_assoc_stringl(&array
, key
, value
, value_len
, 0);
328 header
+= line
- header
;
343 PHP_HTTP_API
void _http_parse_headers_default_callback(const char *http_line
, HashTable
**headers
, void **cb_data TSRMLS_DC
)
348 Z_ARRVAL(array
) = *headers
;
350 if (crlf
= strstr(http_line
, HTTP_CRLF
)) {
351 line_length
= crlf
- http_line
;
353 line_length
= strlen(http_line
);
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);
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
);
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);
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
)
379 Z_ARRVAL(array
) = headers
;
381 FOREACH_HASH_KEY(HTTP_SERVER_VARS
, key
, idx
) {
382 if (key
&& !strncmp(key
, "HTTP_", 5)) {
387 key
= pretty_key(key
, strlen(key
), 1, 1);
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);
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
)
401 char *name
, *key
= NULL
;
403 zend_bool result
= 0;
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);
410 FOREACH_HASH_KEY(&headers
, key
, idx
) {
411 if (key
&& (!strcmp(key
, name
))) {
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;
421 zend_hash_destroy(&headers
);
434 * vim600: noet sw=4 ts=4 fdm=marker
435 * vim<600: noet sw=4 ts=4