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 +----------------------------------------------------------------------+
27 #include "php_http_std_defs.h"
28 #include "php_http_api.h"
29 #include "php_http_headers_api.h"
31 /* {{{ static int http_sort_q(const void *, const void *) */
32 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
35 zval result
, *first
, *second
;
40 first
= *((zval
**) f
->pData
);
41 second
= *((zval
**) s
->pData
);
43 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
46 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
50 /* {{{ char *http_negotiate_q(char *, HashTable *, char *) */
51 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const HashTable
*supported
, const char *def TSRMLS_DC
)
53 zval
*zaccept
, zdelim
, zarray
, zentries
, **zentry
, **zsupp
;
54 char *q_ptr
= NULL
, *key
= NULL
;
59 HTTP_GSC(zaccept
, entry
, estrdup(def
));
62 array_init(&zentries
);
64 Z_STRVAL(zdelim
) = ",";
67 php_explode(&zdelim
, zaccept
, &zarray
, -1);
69 FOREACH_HASH_VAL(Z_ARRVAL(zarray
), zentry
) {
70 if (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';')) {
71 qual
= strtod(q_ptr
+ 3, NULL
);
77 FOREACH_HASH_VAL((HashTable
*)supported
, zsupp
) {
78 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
79 add_assoc_double(&zentries
, Z_STRVAL_PP(zsupp
), qual
);
86 zend_hash_sort(Z_ARRVAL(zentries
), zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
88 FOREACH_HASH_KEY(Z_ARRVAL(zentries
), key
, idx
) {
101 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
102 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
, size_t length TSRMLS_DC
)
106 long begin
= -1, end
= -1, *ptr
;
108 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
109 range
= Z_STRVAL_P(zrange
);
111 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
112 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
117 range
+= sizeof("bytes=") - 1;
120 switch (c
= *(range
++))
126 case '1': case '2': case '3':
127 case '4': case '5': case '6':
128 case '7': case '8': case '9':
130 * If the value of the pointer is already set (non-negative)
131 * then multiply its value by ten and add the current value,
132 * else initialise the pointers value with the current value
134 * This let us recognize empty fields when validating the
135 * ranges, i.e. a "-10" for begin and "12345" for the end
136 * was the following range request: "Range: bytes=0-12345";
137 * While a "-1" for begin and "12345" for the end would
138 * have been: "Range: bytes=-12345".
153 /* IE - ignore for now */
160 /* validate ranges */
169 /* "0-0" or overflow */
170 if (end
== -10 || length
<= end
) {
178 /* "-", "-0" or overflow */
179 if (end
== -1 || end
== -10 || length
<= end
) {
182 begin
= length
- end
;
197 if (length
<= begin
) {
205 if ( (length
<= begin
) ||
217 MAKE_STD_ZVAL(zentry
);
219 add_index_long(zentry
, 0, begin
);
220 add_index_long(zentry
, 1, end
);
221 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
239 /* {{{ STATUS http_parse_headers(char *, size_t, HashTable *, zend_bool) */
240 PHP_HTTP_API STATUS
_http_parse_headers_ex(const char *header
, size_t header_len
,
241 HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
243 const char *colon
= NULL
, *line
= NULL
, *begin
= header
;
246 Z_ARRVAL(array
) = headers
;
248 if (header_len
< 2) {
253 if (!strncmp(header
, "HTTP/1.", 7)) {
254 char *end
= strstr(header
, HTTP_CRLF
);
255 size_t len
= end
- (header
+ lenof("HTTP/1.x "));
256 char *val
= estrndup(header
+ lenof("HTTP/1.x "), len
);
258 add_assoc_stringl(&array
, "Status", val
, len
, 0);
264 while (header_len
>= (line
- begin
)) {
270 --value_len
; /* we don't have CR so value length is one char less */
272 if (colon
&& ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t')))) {
275 if (header
!= colon
) {
276 zval
**previous
= NULL
;
277 char *value
= empty_string
;
278 int keylen
= colon
- header
;
279 char *key
= estrndup(header
, keylen
);
282 key
= pretty_key(key
, keylen
, 1, 1);
285 value_len
+= line
- colon
- 1;
287 /* skip leading ws */
288 while (isspace(*(++colon
))) --value_len
;
289 /* skip trailing ws */
290 while (isspace(colon
[value_len
- 1])) --value_len
;
293 value
= estrndup(colon
, value_len
);
298 /* if we already have got such a header make an array of those */
299 if (SUCCESS
== zend_hash_find(headers
, key
, keylen
+ 1, (void **) &previous
)) {
300 /* already an array? - just add */
301 if (Z_TYPE_PP(previous
) == IS_ARRAY
) {
302 add_next_index_stringl(*previous
, value
, value_len
, 0);
304 /* create the array */
306 MAKE_STD_ZVAL(new_array
);
307 array_init(new_array
);
309 add_next_index_stringl(new_array
, Z_STRVAL_PP(previous
), Z_STRLEN_PP(previous
), 1);
310 add_next_index_stringl(new_array
, value
, value_len
, 0);
311 add_assoc_zval(&array
, key
, new_array
);
316 add_assoc_stringl(&array
, key
, value
, value_len
, 0);
323 header
+= line
- header
;
339 PHP_HTTP_API STATUS
_http_parse_cookie(const char *cookie
, HashTable
*values TSRMLS_DC
)
341 const char *key
= cookie
, *val
= NULL
;
342 int vallen
= 0, keylen
= 0, done
= 0;
345 Z_ARRVAL(array
) = values
;
347 if (!(val
= strchr(cookie
, '='))) {
351 #define HTTP_COOKIE_VAL(array, k, str, len) \
353 const char *encoded = str; \
354 char *decoded = NULL; \
355 int decoded_len = 0, encoded_len = len; \
356 decoded = estrndup(encoded, encoded_len); \
357 decoded_len = php_url_decode(decoded, encoded_len); \
358 add_assoc_stringl(array, k, decoded, decoded_len, 0); \
360 #define HTTP_COOKIE_FIXKEY() \
362 while (isspace(*key)) ++key; \
363 keylen = val - key; \
364 while (isspace(key[keylen - 1])) --keylen; \
366 #define HTTP_COOKIE_FIXVAL() \
369 while (isspace(*val)) ++val; \
370 vallen = key - val; \
371 while (isspace(val[vallen - 1])) --vallen; \
374 HTTP_COOKIE_FIXKEY();
375 HTTP_COOKIE_VAL(&array
, "name", key
, keylen
);
377 /* just a name=value cookie */
378 if (!(key
= strchr(val
, ';'))) {
379 key
= val
+ strlen(val
);
380 HTTP_COOKIE_FIXVAL();
381 HTTP_COOKIE_VAL(&array
, "value", val
, vallen
);
383 /* additional info appended */
387 HTTP_COOKIE_FIXVAL();
388 HTTP_COOKIE_VAL(&array
, "value", val
, vallen
);
391 if (!(val
= strchr(key
, '='))) {
395 HTTP_COOKIE_FIXKEY();
396 keydup
= estrndup(key
, keylen
);
397 if (!(key
= strchr(val
, ';'))) {
399 key
= val
+ strlen(val
);
401 HTTP_COOKIE_FIXVAL();
402 HTTP_COOKIE_VAL(&array
, keydup
, val
, vallen
);
410 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
411 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
417 Z_ARRVAL(array
) = headers
;
419 FOREACH_HASH_KEY(HTTP_SERVER_VARS
, key
, idx
) {
420 if (key
&& !strncmp(key
, "HTTP_", 5)) {
425 key
= pretty_key(key
, strlen(key
), 1, 1);
428 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
429 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
442 * vim600: noet sw=4 ts=4 fdm=marker
443 * vim<600: noet sw=4 ts=4