- use zend_hash_find() instead of FOREACH in http_match_request_header() (don't ask)
authorMichael Wallner <mike@php.net>
Wed, 9 Nov 2005 16:37:57 +0000 (16:37 +0000)
committerMichael Wallner <mike@php.net>
Wed, 9 Nov 2005 16:37:57 +0000 (16:37 +0000)
- add test

http_headers_api.c
tests/match_request_header_001.phpt [new file with mode: 0644]

index b61646d34c4ac84cd7965d651fe6149f49fe8393..4c3c5025aa67d27f21665ca8b51953859d3e4589 100644 (file)
@@ -474,27 +474,19 @@ 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;
+       char *name;
+       uint name_len = strlen(header);
        zend_bool result = 0;
        HashTable headers;
-       HashPosition pos;
+       zval **data;
 
-       name = pretty_key(estrdup(header), strlen(header), 1, 1);
+       name = pretty_key(estrndup(header, name_len), name_len, 1, 1);
        zend_hash_init(&headers, 0, NULL, ZVAL_PTR_DTOR, 0);
        http_get_request_headers_ex(&headers, 1);
 
-       FOREACH_HASH_KEY(pos, &headers, key, idx) {
-               if (key && (!strcmp(key, name))) {
-                       zval **data;
-
-                       if (SUCCESS == zend_hash_get_current_data_ex(&headers, (void **) &data, &pos)) {
-                               result = (match_case ? strcmp(Z_STRVAL_PP(data), value) : strcasecmp(Z_STRVAL_PP(data), value)) ? 0 : 1;
-                       }
-                       break;
-               }
+       if (SUCCESS == zend_hash_find(&headers, name, name_len+1, (void **) &data)) {
+               result = (match_case ? strcmp(Z_STRVAL_PP(data), value) : strcasecmp(Z_STRVAL_PP(data), value)) ? 0 : 1;
        }
-
        zend_hash_destroy(&headers);
        efree(name);
 
diff --git a/tests/match_request_header_001.phpt b/tests/match_request_header_001.phpt
new file mode 100644 (file)
index 0000000..2d0f47e
--- /dev/null
@@ -0,0 +1,22 @@
+--TEST--
+http_match_request_header()
+--SKIPIF--
+<?php
+include 'skip.inc';
+?>
+--ENV--
+HTTP_FOO=bar
+--FILE--
+<?php
+echo "-TEST\n";
+var_dump(http_match_request_header("Foo", "bar", 1));
+var_dump(http_match_request_header("fOO", "BAR", 0));
+var_dump(http_match_request_header("foo", "BAR", 1));
+echo "Done\n";
+?>
+--EXPECTF--
+%sTEST
+bool(true)
+bool(true)
+bool(false)
+Done