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 +----------------------------------------------------------------------+
25 #include "ext/standard/php_string.h"
26 #include "ext/standard/url.h"
29 #include "php_http_std_defs.h"
30 #include "php_http_api.h"
31 #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 FOREACH_HASH_VAL((HashTable
*)supported
, zsupp
) {
80 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
81 add_assoc_double(&zentries
, Z_STRVAL_PP(zsupp
), qual
);
88 zend_hash_sort(Z_ARRVAL(zentries
), zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
90 FOREACH_HASH_KEY(Z_ARRVAL(zentries
), key
, idx
) {
103 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
104 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
, size_t length TSRMLS_DC
)
108 long begin
= -1, end
= -1, *ptr
;
110 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
111 range
= Z_STRVAL_P(zrange
);
113 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
114 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
119 range
+= sizeof("bytes=") - 1;
122 switch (c
= *(range
++))
128 case '1': case '2': case '3':
129 case '4': case '5': case '6':
130 case '7': case '8': case '9':
132 * If the value of the pointer is already set (non-negative)
133 * then multiply its value by ten and add the current value,
134 * else initialise the pointers value with the current value
136 * This let us recognize empty fields when validating the
137 * ranges, i.e. a "-10" for begin and "12345" for the end
138 * was the following range request: "Range: bytes=0-12345";
139 * While a "-1" for begin and "12345" for the end would
140 * have been: "Range: bytes=-12345".
155 /* IE - ignore for now */
162 /* validate ranges */
171 /* "0-0" or overflow */
172 if (end
== -10 || length
<= end
) {
180 /* "-", "-0" or overflow */
181 if (end
== -1 || end
== -10 || length
<= end
) {
184 begin
= length
- end
;
199 if (length
<= begin
) {
207 if ( (length
<= begin
) ||
219 MAKE_STD_ZVAL(zentry
);
221 add_index_long(zentry
, 0, begin
);
222 add_index_long(zentry
, 1, end
);
223 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
241 /* {{{ STATUS http_parse_headers(char *, size_t, HashTable *, zend_bool) */
242 PHP_HTTP_API STATUS
_http_parse_headers_ex(char *header
, size_t header_len
,
243 HashTable
*headers
, zend_bool prettify
,
244 http_parse_headers_callback_t func
, void **callback_data TSRMLS_DC
)
246 char *colon
= NULL
, *line
= NULL
, *begin
= header
;
249 Z_ARRVAL(array
) = headers
;
251 if (header_len
< 2) {
257 while (header_len
>= (line
- begin
)) {
263 --value_len
; /* we don't have CR so value length is one char less */
265 if ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t'))) {
266 /* response/request line */
267 if ( (!strncmp(header
, "HTTP/1.", lenof("HTTP/1."))) ||
268 (!strncmp(line
- lenof("HTTP/1.x\r") + value_len
, "HTTP/1.", lenof("HTTP/1.")))) {
270 func(callback_data
, header
, line
- header
+ value_len
, &headers TSRMLS_CC
);
271 Z_ARRVAL(array
) = headers
;
275 /* "header: value" pair */
279 if (header
!= colon
) {
280 zval
**previous
= NULL
;
282 int keylen
= colon
- header
;
283 char *key
= estrndup(header
, keylen
);
286 key
= pretty_key(key
, keylen
, 1, 1);
289 value_len
+= line
- colon
- 1;
291 /* skip leading ws */
292 while (isspace(*(++colon
))) --value_len
;
293 /* skip trailing ws */
294 while (isspace(colon
[value_len
- 1])) --value_len
;
297 value
= estrndup(colon
, value_len
);
303 /* if we already have got such a header make an array of those */
304 if (SUCCESS
== zend_hash_find(headers
, key
, keylen
+ 1, (void **) &previous
)) {
305 /* already an array? - just add */
306 if (Z_TYPE_PP(previous
) == IS_ARRAY
) {
307 add_next_index_stringl(*previous
, value
, value_len
, 0);
309 /* create the array */
311 MAKE_STD_ZVAL(new_array
);
312 array_init(new_array
);
314 add_next_index_stringl(new_array
, Z_STRVAL_PP(previous
), Z_STRLEN_PP(previous
), 1);
315 add_next_index_stringl(new_array
, value
, value_len
, 0);
316 add_assoc_zval(&array
, key
, new_array
);
321 add_assoc_stringl(&array
, key
, value
, value_len
, 0);
328 header
+= line
- header
;
343 PHP_HTTP_API
void _http_parse_headers_default_callback(void **cb_data
, char *http_line
, size_t line_length
, HashTable
**headers TSRMLS_DC
)
346 Z_ARRVAL(array
) = *headers
;
349 if (!strncmp(http_line
, "HTTP/1.", lenof("HTTP/1."))) {
350 add_assoc_stringl(&array
, "Response Status", http_line
+ lenof("HTTP/1.x "), line_length
- lenof("HTTP/1.x \r\n"), 1);
353 if (!strncmp(http_line
+ line_length
- lenof("HTTP/1.x\r\n"), "HTTP/1.", lenof("HTTP/1."))) {
354 char *sep
= strchr(http_line
, ' ');
356 add_assoc_stringl(&array
, "Request Method", http_line
, sep
- http_line
, 1);
357 add_assoc_stringl(&array
, "Request Uri", sep
+ 1, strstr(sep
, "HTTP/1.") - sep
+ 1 + 1, 1);
362 PHP_HTTP_API STATUS
_http_parse_cookie(const char *cookie
, HashTable
*values TSRMLS_DC
)
364 const char *key
= cookie
, *val
= NULL
;
365 int vallen
= 0, keylen
= 0, done
= 0;
368 Z_ARRVAL(array
) = values
;
370 if (!(val
= strchr(cookie
, '='))) {
374 #define HTTP_COOKIE_VAL(array, k, str, len) \
376 const char *encoded = str; \
377 char *decoded = NULL; \
378 int decoded_len = 0, encoded_len = len; \
379 decoded = estrndup(encoded, encoded_len); \
380 decoded_len = php_url_decode(decoded, encoded_len); \
381 add_assoc_stringl(array, k, decoded, decoded_len, 0); \
383 #define HTTP_COOKIE_FIXKEY() \
385 while (isspace(*key)) ++key; \
386 keylen = val - key; \
387 while (isspace(key[keylen - 1])) --keylen; \
389 #define HTTP_COOKIE_FIXVAL() \
392 while (isspace(*val)) ++val; \
393 vallen = key - val; \
394 while (isspace(val[vallen - 1])) --vallen; \
397 HTTP_COOKIE_FIXKEY();
398 HTTP_COOKIE_VAL(&array
, "name", key
, keylen
);
400 /* just a name=value cookie */
401 if (!(key
= strchr(val
, ';'))) {
402 key
= val
+ strlen(val
);
403 HTTP_COOKIE_FIXVAL();
404 HTTP_COOKIE_VAL(&array
, "value", val
, vallen
);
406 /* additional info appended */
410 HTTP_COOKIE_FIXVAL();
411 HTTP_COOKIE_VAL(&array
, "value", val
, vallen
);
414 if (!(val
= strchr(key
, '='))) {
418 HTTP_COOKIE_FIXKEY();
419 keydup
= estrndup(key
, keylen
);
420 if (!(key
= strchr(val
, ';'))) {
422 key
= val
+ strlen(val
);
424 HTTP_COOKIE_FIXVAL();
425 HTTP_COOKIE_VAL(&array
, keydup
, val
, vallen
);
433 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
434 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
440 Z_ARRVAL(array
) = headers
;
442 FOREACH_HASH_KEY(HTTP_SERVER_VARS
, key
, idx
) {
443 if (key
&& !strncmp(key
, "HTTP_", 5)) {
448 key
= pretty_key(key
, strlen(key
), 1, 1);
451 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
452 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
465 * vim600: noet sw=4 ts=4 fdm=marker
466 * vim<600: noet sw=4 ts=4