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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
17 #include "ext/standard/url.h"
18 #include "ext/standard/php_string.h"
20 #include "php_http_api.h"
21 #include "php_http_headers_api.h"
24 # define HTTP_DBG_NEG 0
27 /* {{{ static int http_sort_q(const void *, const void *) */
28 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
31 zval result
, *first
, *second
;
36 first
= *((zval
**) f
->pData
);
37 second
= *((zval
**) s
->pData
);
39 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
42 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
46 /* {{{ char *http_negotiate_language_func */
47 char *_http_negotiate_language_func(const char *test
, double *quality
, HashTable
*supported TSRMLS_DC
)
51 const char *dash_test
;
53 FOREACH_HASH_VAL(pos
, supported
, value
) {
55 fprintf(stderr
, "strcasecmp('%s', '%s')\n", Z_STRVAL_PP(value
), test
);
57 if (!strcasecmp(Z_STRVAL_PP(value
), test
)) {
58 return Z_STRVAL_PP(value
);
62 /* no distinct match found, so try primaries */
63 if ((dash_test
= strchr(test
, '-'))) {
64 FOREACH_HASH_VAL(pos
, supported
, value
) {
65 int len
= dash_test
- test
;
67 fprintf(stderr
, "strncasecmp('%s', '%s', %d)\n", Z_STRVAL_PP(value
), test
, len
);
69 if ( (!strncasecmp(Z_STRVAL_PP(value
), test
, len
)) &&
70 ( (Z_STRVAL_PP(value
)[len
] == '\0') ||
71 (Z_STRVAL_PP(value
)[len
] == '-'))) {
73 return Z_STRVAL_PP(value
);
82 /* {{{ char *http_negotiate_default_func */
83 char *_http_negotiate_default_func(const char *test
, double *quality
, HashTable
*supported TSRMLS_DC
)
88 FOREACH_HASH_VAL(pos
, supported
, value
) {
90 fprintf(stderr
, "strcasecmp('%s', '%s')\n", Z_STRVAL_PP(value
), test
);
92 if (!strcasecmp(Z_STRVAL_PP(value
), test
)) {
93 return Z_STRVAL_PP(value
);
101 /* {{{ HashTable *http_negotiate_q(const char *, HashTable *, negotiate_func_t) */
102 PHP_HTTP_API HashTable
*_http_negotiate_q(const char *header
, HashTable
*supported
, negotiate_func_t neg TSRMLS_DC
)
105 HashTable
*result
= NULL
;
108 fprintf(stderr
, "Reading header %s: ", header
);
110 HTTP_GSC(accept
, header
, NULL
);
112 fprintf(stderr
, "%s\n", Z_STRVAL_P(accept
));
115 if (Z_STRLEN_P(accept
)) {
120 ZVAL_STRINGL(&ex_del
, ",", 1, 0);
123 php_explode(&ex_del
, accept
, &ex_arr
, -1);
125 if (zend_hash_num_elements(Z_ARRVAL(ex_arr
)) > 0) {
133 FOREACH_HASH_VAL(pos
, Z_ARRVAL(ex_arr
), entry
) {
136 char *selected
, *identifier
, *freeme
;
137 const char *separator
;
140 fprintf(stderr
, "Checking %s\n", Z_STRVAL_PP(entry
));
143 if ((separator
= strchr(Z_STRVAL_PP(entry
), ';'))) {
144 const char *ptr
= separator
;
146 while (*++ptr
&& !isdigit(*ptr
) && '.' != *ptr
);
149 identifier
= estrndup(Z_STRVAL_PP(entry
), ident_len
= separator
- Z_STRVAL_PP(entry
));
151 quality
= 1000.0 - i
++;
152 identifier
= estrndup(Z_STRVAL_PP(entry
), ident_len
= Z_STRLEN_PP(entry
));
156 while (isspace(*identifier
)) {
160 while (ident_len
&& isspace(identifier
[ident_len
- 1])) {
161 identifier
[--ident_len
] = '\0';
164 if ((selected
= neg(identifier
, &quality
, supported TSRMLS_CC
))) {
165 /* don't overwrite previously set with higher quality */
166 if (!zend_hash_exists(Z_ARRVAL(array
), selected
, strlen(selected
) + 1)) {
167 add_assoc_double(&array
, selected
, quality
);
174 result
= Z_ARRVAL(array
);
175 zend_hash_sort(result
, zend_qsort
, http_sort_q
, 0 TSRMLS_CC
);
185 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
186 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
, size_t length TSRMLS_DC
)
190 long begin
= -1, end
= -1, *ptr
;
192 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
193 range
= Z_STRVAL_P(zrange
);
195 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
200 range
+= sizeof("bytes=") - 1;
203 switch (c
= *(range
++))
206 /* allow 000... - shall we? */
212 case '1': case '2': case '3':
213 case '4': case '5': case '6':
214 case '7': case '8': case '9':
216 * If the value of the pointer is already set (non-negative)
217 * then multiply its value by ten and add the current value,
218 * else initialise the pointers value with the current value
220 * This let us recognize empty fields when validating the
221 * ranges, i.e. a "-10" for begin and "12345" for the end
222 * was the following range request: "Range: bytes=0-12345";
223 * While a "-1" for begin and "12345" for the end would
224 * have been: "Range: bytes=-12345".
239 /* IE - ignore for now */
246 /* validate ranges */
255 /* "0-0" or overflow */
256 if (end
== -10 || length
<= (size_t) end
) {
264 /* "-", "-0" or overflow */
265 if (end
== -1 || end
== -10 || length
<= (size_t) end
) {
268 begin
= length
- end
;
283 if (length
<= (size_t) begin
) {
291 if ( (length
<= (size_t) begin
) ||
292 (length
<= (size_t) end
) ||
303 MAKE_STD_ZVAL(zentry
);
305 add_index_long(zentry
, 0, begin
);
306 add_index_long(zentry
, 1, end
);
307 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
325 /* {{{ STATUS http_parse_headers(char *, HashTable *, zend_bool) */
326 PHP_HTTP_API STATUS
_http_parse_headers_ex(const char *header
, HashTable
*headers
, zend_bool prettify
,
327 http_info_callback callback_func
, void **callback_data TSRMLS_DC
)
329 const char *colon
= NULL
, *line
= NULL
, *begin
= header
;
330 const char *body
= http_locate_body(header
);
334 INIT_ZARR(array
, headers
);
337 header_len
= body
- header
;
339 header_len
= strlen(header
) + 1;
354 --value_len
; /* we don't have CR so value length is one char less */
356 if ((!*(line
- 1)) || ((*line
!= ' ') && (*line
!= '\t'))) {
359 /* response/request line */
360 if (SUCCESS
== http_info_parse(header
, &i
)) {
361 callback_func(callback_data
, &headers
, &i TSRMLS_CC
);
363 Z_ARRVAL(array
) = headers
;
366 /* "header: value" pair */
370 if (header
!= colon
) {
371 zval
**previous
= NULL
;
373 int keylen
= colon
- header
;
374 char *key
= estrndup(header
, keylen
);
377 key
= pretty_key(key
, keylen
, 1, 1);
380 value_len
+= line
- colon
- 1;
382 /* skip leading ws */
383 while (isspace(*(++colon
))) --value_len
;
384 /* skip trailing ws */
385 while (isspace(colon
[value_len
- 1])) --value_len
;
388 value
= estrndup(colon
, value_len
);
394 /* if we already have got such a header make an array of those */
395 if (SUCCESS
== zend_hash_find(headers
, key
, keylen
+ 1, (void **) &previous
)) {
396 /* convert to array */
397 if (Z_TYPE_PP(previous
) != IS_ARRAY
) {
398 convert_to_array(*previous
);
400 add_next_index_stringl(*previous
, value
, value_len
, 0);
402 add_assoc_stringl(&array
, key
, value
, value_len
, 0);
409 header
+= line
- header
;
413 } while (header_len
> (size_t) (line
- begin
));
419 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
420 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
428 Z_ARRVAL(array
) = headers
;
430 if (SUCCESS
== zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void **) &hsv
)) {
431 FOREACH_KEYLEN(pos
, *hsv
, key
, keylen
, idx
) {
432 if (key
&& keylen
> 6 && !strncmp(key
, "HTTP_", 5)) {
433 zval
**header
, *orig
;
438 key
= pretty_key(estrndup(key
, keylen
), keylen
, 1, 1);
441 zend_hash_get_current_data_ex(Z_ARRVAL_PP(hsv
), (void **) &header
, &pos
);
444 convert_to_string_ex(header
);
445 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
446 if (orig
!= *header
) {
447 zval_ptr_dtor(header
);
462 /* {{{ zend_bool http_match_request_header(char *, char *) */
463 PHP_HTTP_API zend_bool
_http_match_request_header_ex(const char *header
, const char *value
, zend_bool match_case TSRMLS_DC
)
466 uint name_len
= strlen(header
);
467 zend_bool result
= 0;
471 name
= pretty_key(estrndup(header
, name_len
), name_len
, 1, 1);
472 zend_hash_init(&headers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
473 http_get_request_headers_ex(&headers
, 1);
475 if (SUCCESS
== zend_hash_find(&headers
, name
, name_len
+1, (void **) &data
)) {
476 result
= (match_case
? strcmp(Z_STRVAL_PP(data
), value
) : strcasecmp(Z_STRVAL_PP(data
), value
)) ? 0 : 1;
478 zend_hash_destroy(&headers
);
491 * vim600: noet sw=4 ts=4 fdm=marker
492 * vim<600: noet sw=4 ts=4