From 74c406d2e8a73540d9df9b888a17c86b0bb7fc2c Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 6 Jun 2005 11:31:38 +0000 Subject: [PATCH] - add http_match_request_header() --- http.c | 1 + http_functions.c | 20 +++++++++++++++++++- http_headers_api.c | 30 ++++++++++++++++++++++++++++++ php_http.h | 1 + php_http_headers_api.h | 6 +++++- 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/http.c b/http.c index ce1a398..a09fa61 100644 --- a/http.c +++ b/http.c @@ -114,6 +114,7 @@ function_entry http_functions[] = { PHP_FE(http_split_response, NULL) PHP_FE(http_parse_headers, NULL) PHP_FE(http_get_request_headers, NULL) + PHP_FE(http_match_request_header, NULL) #ifdef HTTP_HAVE_CURL PHP_FE(http_get, http_request_info_ref_3) PHP_FE(http_head, http_request_info_ref_3) diff --git a/http_functions.c b/http_functions.c index 4e88f42..fa9cf9c 100644 --- a/http_functions.c +++ b/http_functions.c @@ -652,6 +652,7 @@ PHP_FUNCTION(http_parse_headers) /* {{{ proto array http_get_request_headers(void) * + * Get a list of incoming HTTP headers. */ PHP_FUNCTION(http_get_request_headers) { @@ -662,6 +663,24 @@ PHP_FUNCTION(http_get_request_headers) } /* }}} */ +/* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false]) + * + * Match an incoming HTTP header. + */ +PHP_FUNCTION(http_match_request_header) +{ + char *header, *value; + int header_len, value_len; + zend_bool match_case = 0, result = 0; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &header, &header_len, &value, &value_len, &match_case)) { + RETURN_FALSE; + } + + RETURN_BOOL(http_match_request_header_ex(header, value, match_case)); +} +/* }}} */ + /* {{{ HAVE_CURL */ #ifdef HTTP_HAVE_CURL @@ -825,7 +844,6 @@ PHP_FUNCTION(http_post_data) } else { RETVAL_FALSE; } - http_request_body_dtor(&body); } /* }}} */ diff --git a/http_headers_api.c b/http_headers_api.c index 7d82452..81181e5 100644 --- a/http_headers_api.c +++ b/http_headers_api.c @@ -464,6 +464,36 @@ PHP_HTTP_API void _http_get_request_headers_ex(HashTable *headers, zend_bool pre } /* }}} */ +/* {{{ zend_bool http_match_request_header(char *, char *) */ +PHP_HTTP_API zend_bool _http_match_request_header_ex(const char *header, const char *value, zend_bool match_case TSRMLS_DC) +{ + char *name, *key = NULL; + ulong idx; + zend_bool result = 0; + HashTable headers; + + name = pretty_key(estrdup(header), strlen(header), 1, 1); + zend_hash_init(&headers, 0, NULL, ZVAL_PTR_DTOR, 0); + http_get_request_headers_ex(&headers, 1); + + FOREACH_HASH_KEY(&headers, key, idx) { + if (key && (!strcmp(key, name))) { + zval **data; + + if (SUCCESS == zend_hash_get_current_data(&headers, (void **) &data)) { + result = (match_case ? strcmp(Z_STRVAL_PP(data), value) : strcasecmp(Z_STRVAL_PP(data), value)) ? 0 : 1; + } + break; + } + } + + zend_hash_destroy(&headers); + efree(name); + + return result; +} +/* }}} */ + /* * Local variables: diff --git a/php_http.h b/php_http.h index 27e8fc0..467507e 100644 --- a/php_http.h +++ b/php_http.h @@ -104,6 +104,7 @@ PHP_FUNCTION(http_chunked_decode); PHP_FUNCTION(http_split_response); PHP_FUNCTION(http_parse_headers); PHP_FUNCTION(http_get_request_headers); +PHP_FUNCTION(http_match_request_header); #ifdef HTTP_HAVE_CURL PHP_FUNCTION(http_get); PHP_FUNCTION(http_head); diff --git a/php_http_headers_api.h b/php_http_headers_api.h index ae3748a..6d6319a 100644 --- a/php_http_headers_api.h +++ b/php_http_headers_api.h @@ -40,7 +40,7 @@ PHP_HTTP_API STATUS _http_parse_headers_ex(const char *header, HashTable *header PHP_HTTP_API STATUS _http_parse_cookie(const char *cookie, HashTable *values TSRMLS_DC); #define http_get_request_headers(h) _http_get_request_headers_ex(Z_ARRVAL_P(h), 1 TSRMLS_CC) -#define http_get_request_headers_ex(h, p) _http_get_request_headers_ex((h), (s) TSRMLS_CC) +#define http_get_request_headers_ex(h, p) _http_get_request_headers_ex((h), (p) TSRMLS_CC) PHP_HTTP_API void _http_get_request_headers_ex(HashTable *headers, zend_bool prettify TSRMLS_DC); #define http_negotiate_language(zsupported, def) http_negotiate_language_ex(Z_ARRVAL_P(zsupported), (def)) @@ -53,6 +53,10 @@ PHP_HTTP_API char *_http_negotiate_q(const char *entry, const HashTable *support #define http_get_request_ranges(r, l) _http_get_request_ranges((r), (l) TSRMLS_CC) PHP_HTTP_API http_range_status _http_get_request_ranges(HashTable *ranges, size_t length TSRMLS_DC); +#define http_match_request_header(h, v) http_match_request_header_ex((h), (v), 0) +#define http_match_request_header_ex(h, v, c) _http_match_request_header_ex((h), (v), (c) TSRMLS_CC) +PHP_HTTP_API zend_bool _http_match_request_header_ex(const char *header, const char *value, zend_bool match_case TSRMLS_DC); + #endif /* -- 2.30.2