2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
20 #include "ext/standard/php_string.h"
21 #include "ext/standard/url.h"
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_headers_api.h"
27 #include "php_http_info_api.h"
31 ZEND_EXTERN_MODULE_GLOBALS(http
);
34 # define HTTP_DBG_NEG 0
38 PHP_MINIT_FUNCTION(http_headers
)
40 HTTP_LONG_CONSTANT("HTTP_REDIRECT", HTTP_REDIRECT
);
41 HTTP_LONG_CONSTANT("HTTP_REDIRECT_PERM", HTTP_REDIRECT_PERM
);
42 HTTP_LONG_CONSTANT("HTTP_REDIRECT_POST", HTTP_REDIRECT_POST
);
43 HTTP_LONG_CONSTANT("HTTP_REDIRECT_TEMP", HTTP_REDIRECT_TEMP
);
49 /* {{{ static int http_sort_q(const void *, const void *) */
50 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
53 zval result
, *first
, *second
;
58 first
= *((zval
**) f
->pData
);
59 second
= *((zval
**) s
->pData
);
61 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
64 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
68 /* {{{ char *http_negotiate_language_func */
69 char *_http_negotiate_language_func(const char *test
, double *quality
, HashTable
*supported TSRMLS_DC
)
72 const char *dash_test
;
74 FOREACH_HASH_VAL(supported
, value
) {
76 fprintf(stderr
, "strcasecmp('%s', '%s')\n", Z_STRVAL_PP(value
), test
);
78 if (!strcasecmp(Z_STRVAL_PP(value
), test
)) {
79 return Z_STRVAL_PP(value
);
83 /* no distinct match found, so try primaries */
84 if (dash_test
= strchr(test
, '-')) {
85 FOREACH_HASH_VAL(supported
, value
) {
86 int len
= dash_test
- test
;
88 fprintf(stderr
, "strncascmp('%s', '%s', %d)\n", Z_STRVAL_PP(value
), test
, len
);
90 if ( (!strncasecmp(Z_STRVAL_PP(value
), test
, len
)) &&
91 ( (Z_STRVAL_PP(value
)[len
] == '\0') ||
92 (Z_STRVAL_PP(value
)[len
] == '-'))) {
94 return Z_STRVAL_PP(value
);
103 /* {{{ char *http_negotiate_default_func */
104 char *_http_negotiate_default_func(const char *test
, double *quality
, HashTable
*supported TSRMLS_DC
)
108 FOREACH_HASH_VAL(supported
, value
) {
110 fprintf(stderr
, "strcasecmp('%s', '%s')\n", Z_STRVAL_PP(value
), test
);
112 if (!strcasecmp(Z_STRVAL_PP(value
), test
)) {
113 return Z_STRVAL_PP(value
);
121 /* {{{ HashTable *http_negotiate_q(const char *, HashTable *, negotiate_func_t) */
122 PHP_HTTP_API HashTable
*_http_negotiate_q(const char *header
, HashTable
*supported
, negotiate_func_t neg TSRMLS_DC
)
125 HashTable
*result
= NULL
;
128 fprintf(stderr
, "Reading header %s: ", header
);
130 HTTP_GSC(accept
, header
, NULL
);
132 fprintf(stderr
, "%s\n", Z_STRVAL_P(accept
));
135 if (Z_STRLEN_P(accept
)) {
140 ZVAL_STRINGL(&ex_del
, ",", 1, 0);
143 php_explode(&ex_del
, accept
, &ex_arr
, -1);
145 if (zend_hash_num_elements(Z_ARRVAL(ex_arr
)) > 0) {
152 FOREACH_HASH_VAL(Z_ARRVAL(ex_arr
), entry
) {
154 char *selected
, *identifier
;
155 const char *separator
;
158 fprintf(stderr
, "Checking %s\n", Z_STRVAL_PP(entry
));
161 if (separator
= strchr(Z_STRVAL_PP(entry
), ';')) {
162 const char *ptr
= separator
;
164 while (*++ptr
&& !isdigit(*ptr
));
167 identifier
= estrndup(Z_STRVAL_PP(entry
), separator
- Z_STRVAL_PP(entry
));
169 quality
= 1000.0 - i
++;
170 identifier
= estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
173 if (selected
= neg(identifier
, &quality
, supported TSRMLS_CC
)) {
174 /* don't overwrite previously set with higher quality */
175 if (!zend_hash_exists(Z_ARRVAL(array
), selected
, strlen(selected
) + 1)) {
176 add_assoc_double(&array
, selected
, quality
);
183 result
= Z_ARRVAL(array
);
184 zend_hash_sort(result
, zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
194 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
195 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
, size_t length TSRMLS_DC
)
199 long begin
= -1, end
= -1, *ptr
;
201 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
202 range
= Z_STRVAL_P(zrange
);
204 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
209 range
+= sizeof("bytes=") - 1;
212 switch (c
= *(range
++))
215 /* allow 000... - shall we? */
221 case '1': case '2': case '3':
222 case '4': case '5': case '6':
223 case '7': case '8': case '9':
225 * If the value of the pointer is already set (non-negative)
226 * then multiply its value by ten and add the current value,
227 * else initialise the pointers value with the current value
229 * This let us recognize empty fields when validating the
230 * ranges, i.e. a "-10" for begin and "12345" for the end
231 * was the following range request: "Range: bytes=0-12345";
232 * While a "-1" for begin and "12345" for the end would
233 * have been: "Range: bytes=-12345".
248 /* IE - ignore for now */
255 /* validate ranges */
264 /* "0-0" or overflow */
265 if (end
== -10 || length
<= (size_t) end
) {
273 /* "-", "-0" or overflow */
274 if (end
== -1 || end
== -10 || length
<= (size_t) end
) {
277 begin
= length
- end
;
292 if (length
<= (size_t) begin
) {
300 if ( (length
<= (size_t) begin
) ||
301 (length
<= (size_t) end
) ||
312 MAKE_STD_ZVAL(zentry
);
314 add_index_long(zentry
, 0, begin
);
315 add_index_long(zentry
, 1, end
);
316 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
334 /* {{{ STATUS http_parse_headers(char *, HashTable *, zend_bool) */
335 PHP_HTTP_API STATUS
_http_parse_headers_ex(const char *header
, HashTable
*headers
, zend_bool prettify
,
336 http_info_callback callback_func
, void **callback_data TSRMLS_DC
)
338 const char *colon
= NULL
, *line
= NULL
, *begin
= header
;
339 const char *body
= http_locate_body(header
);
343 INIT_ZARR(array
, headers
);
346 header_len
= body
- header
;
348 header_len
= strlen(header
) + 1;
352 while (header_len
>= (size_t) (line
- begin
)) {
354 /* note: valgrind may choke on that -- should be safe though */
364 --value_len
; /* we don't have CR so value length is one char less */
366 if ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t'))) {
369 /* response/request line */
370 if (SUCCESS
== http_info_parse(header
, &i
)) {
371 callback_func(callback_data
, &headers
, &i TSRMLS_CC
);
373 Z_ARRVAL(array
) = headers
;
376 /* "header: value" pair */
380 if (header
!= colon
) {
381 zval
**previous
= NULL
;
383 int keylen
= colon
- header
;
384 char *key
= estrndup(header
, keylen
);
387 key
= pretty_key(key
, keylen
, 1, 1);
390 value_len
+= line
- colon
- 1;
392 /* skip leading ws */
393 while (isspace(*(++colon
))) --value_len
;
394 /* skip trailing ws */
395 while (isspace(colon
[value_len
- 1])) --value_len
;
398 value
= estrndup(colon
, value_len
);
404 /* if we already have got such a header make an array of those */
405 if (SUCCESS
== zend_hash_find(headers
, key
, keylen
+ 1, (void **) &previous
)) {
406 /* convert to array */
407 if (Z_TYPE_PP(previous
) != IS_ARRAY
) {
408 convert_to_array(*previous
);
410 add_next_index_stringl(*previous
, value
, value_len
, 0);
412 add_assoc_stringl(&array
, key
, value
, value_len
, 0);
419 header
+= line
- header
;
428 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
429 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
435 Z_ARRVAL(array
) = headers
;
437 if (SUCCESS
== zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void **) &hsv
)) {
438 FOREACH_KEY(*hsv
, key
, idx
) {
439 if (key
&& !strncmp(key
, "HTTP_", 5)) {
444 key
= pretty_key(key
, strlen(key
), 1, 1);
447 zend_hash_get_current_data(Z_ARRVAL_PP(hsv
), (void **) &header
);
448 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
456 /* {{{ zend_bool http_match_request_header(char *, char *) */
457 PHP_HTTP_API zend_bool
_http_match_request_header_ex(const char *header
, const char *value
, zend_bool match_case TSRMLS_DC
)
459 char *name
, *key
= NULL
;
461 zend_bool result
= 0;
464 name
= pretty_key(estrdup(header
), strlen(header
), 1, 1);
465 zend_hash_init(&headers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
466 http_get_request_headers_ex(&headers
, 1);
468 FOREACH_HASH_KEY(&headers
, key
, idx
) {
469 if (key
&& (!strcmp(key
, name
))) {
472 if (SUCCESS
== zend_hash_get_current_data(&headers
, (void **) &data
)) {
473 result
= (match_case
? strcmp(Z_STRVAL_PP(data
), value
) : strcasecmp(Z_STRVAL_PP(data
), value
)) ? 0 : 1;
479 zend_hash_destroy(&headers
);
492 * vim600: noet sw=4 ts=4 fdm=marker
493 * vim<600: noet sw=4 ts=4