negotiation test & fixes
authorMichael Wallner <mike@php.net>
Mon, 23 Jan 2012 15:18:28 +0000 (15:18 +0000)
committerMichael Wallner <mike@php.net>
Mon, 23 Jan 2012 15:18:28 +0000 (15:18 +0000)
php_http_env.c
php_http_env.h
php_http_negotiate.c
tests/negotiate001.phpt [new file with mode: 0644]

index f7df9d04783d2454928483f6104e7b18dbb04132..a0232d201e174141fb0061cf7e8a46040e137735 100644 (file)
@@ -575,6 +575,11 @@ PHP_HTTP_BEGIN_ARGS(negotiateCharset, 1)
        PHP_HTTP_ARG_VAL(result_array, 1)
 PHP_HTTP_END_ARGS;
 
+PHP_HTTP_BEGIN_ARGS(negotiateEncoding, 1)
+       PHP_HTTP_ARG_VAL(supported, 0)
+       PHP_HTTP_ARG_VAL(result_array, 1)
+PHP_HTTP_END_ARGS;
+
 PHP_HTTP_BEGIN_ARGS(negotiate, 2)
        PHP_HTTP_ARG_VAL(value, 0)
        PHP_HTTP_ARG_VAL(supported, 0)
@@ -602,6 +607,7 @@ zend_function_entry php_http_env_method_entry[] = {
 
        PHP_HTTP_ENV_ME(negotiateLanguage)
        PHP_HTTP_ENV_ME(negotiateContentType)
+       PHP_HTTP_ENV_ME(negotiateEncoding)
        PHP_HTTP_ENV_ME(negotiateCharset)
        PHP_HTTP_ENV_ME(negotiate)
 
@@ -747,6 +753,22 @@ PHP_METHOD(HttpEnv, negotiateCharset)
        }
 }
 
+PHP_METHOD(HttpEnv, negotiateEncoding)
+{
+       HashTable *supported;
+       zval *rs_array = NULL;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
+               if (rs_array) {
+                       zval_dtor(rs_array);
+                       array_init(rs_array);
+               }
+               PHP_HTTP_DO_NEGOTIATE(encoding, supported, rs_array);
+       } else {
+               RETURN_FALSE;
+       }
+}
+
 PHP_METHOD(HttpEnv, negotiateContentType)
 {
        HashTable *supported;
index 62e22cd1babbc0bdfd9605407833f78ba72891ef..64cc70202afa1a87e87e2e5bb0daf87dfe6d6815 100644 (file)
@@ -81,6 +81,7 @@ PHP_METHOD(HttpEnv, setResponseHeader);
 PHP_METHOD(HttpEnv, setResponseCode);
 PHP_METHOD(HttpEnv, negotiateLanguage);
 PHP_METHOD(HttpEnv, negotiateCharset);
+PHP_METHOD(HttpEnv, negotiateEncoding);
 PHP_METHOD(HttpEnv, negotiateContentType);
 PHP_METHOD(HttpEnv, negotiate);
 PHP_METHOD(HttpEnv, persistentHandlesStat);
index aeb95211d2ed7a180de6407bbd647b6df40d48da..69c73934b02e9dbec810236bd0a5b99bed8895d4 100644 (file)
@@ -74,6 +74,11 @@ PHP_HTTP_API HashTable *php_http_negotiate(const char *value_str, size_t value_l
 
                                q = Z_DVAL_P(tmp);
                                zval_ptr_dtor(&tmp);
+
+                               if (!q) {
+                                       STR_FREE(key.str);
+                                       continue;
+                               }
                        } else {
                                q = 1.0 - ++i / 100.0;
                        }
diff --git a/tests/negotiate001.phpt b/tests/negotiate001.phpt
new file mode 100644 (file)
index 0000000..b71efe0
--- /dev/null
@@ -0,0 +1,111 @@
+--TEST--
+negotiate
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--ENV--
+HTTP_ACCEPT=text/html,text/plain,text/xml;q=0.1,*/*;q=0
+HTTP_ACCEPT_CHARSET=utf-8,iso-8859-1;q=0.8,iso-8859-15;q=0
+HTTP_ACCEPT_ENCODING=gzip,deflate;q=0
+HTTP_ACCEPT_LANGUAGE=de-DE,de-AT;q=0.9,en;q=0.8,fr;q=0
+--FILE--
+CONTENT TYPE
+
+<?php
+$ct = http\Env::negotiateContentType(array("text/html","text/xml","text/json"), $ctr);
+echo "$ct: "; print_r($ctr);
+$ct = http\Env::negotiateContentType(array("text/xml","text/json"), $ctr);
+echo "$ct: "; print_r($ctr);
+$ct = http\Env::negotiateContentType(array("text/json"), $ctr);
+echo "$ct: "; print_r($ctr);
+?>
+
+CHARSET
+
+<?php
+$cs = http\Env::negotiateCharset(array("utf-8", "iso-8859-1", "iso-8859-15"), $csr);
+echo "$cs: "; print_r($csr);
+$cs = http\Env::negotiateCharset(array("iso-8859-1", "iso-8859-15"), $csr);
+echo "$cs: "; print_r($csr);
+$cs = http\Env::negotiateCharset(array("utf-16", "iso-8859-15", "iso-8859-2"), $csr);
+echo "$cs: "; print_r($csr);
+?>
+
+ENCODING
+
+<?php
+$ce = http\Env::negotiateEncoding(array("gzip", "deflate", "sdch"), $cer);
+echo "$ce: "; print_r($cer);
+$ce = http\Env::negotiateEncoding(array("", "sdch"), $cer);
+echo "$ce: "; print_r($cer);
+?>
+
+LANGUAGE
+
+<?php
+$ln = http\Env::negotiateLanguage(array("de", "en", "fr"), $lnr);
+echo "$ln: "; print_r($lnr);
+$ln = http\Env::negotiateLanguage(array("de-DE", "de-AT", "en"), $lnr);
+echo "$ln: "; print_r($lnr);
+$ln = http\Env::negotiateLanguage(array("nl", "fr", "en"), $lnr);
+echo "$ln: "; print_r($lnr);
+?>
+DONE
+--EXPECT--
+CONTENT TYPE
+
+text/html: Array
+(
+    [text/html] => 0.99
+    [text/xml] => 0.1
+)
+text/xml: Array
+(
+    [text/xml] => 0.1
+)
+text/json: Array
+(
+)
+
+CHARSET
+
+utf-8: Array
+(
+    [utf-8] => 0.99
+    [iso-8859-1] => 0.8
+)
+iso-8859-1: Array
+(
+    [iso-8859-1] => 0.8
+)
+utf-16: Array
+(
+)
+
+ENCODING
+
+gzip: Array
+(
+    [gzip] => 0.99
+)
+: Array
+(
+)
+
+LANGUAGE
+
+de: Array
+(
+    [de] => 0.899
+    [en] => 0.8
+)
+de-DE: Array
+(
+    [de-DE] => 0.99
+    [de-AT] => 0.9
+    [en] => 0.8
+)
+en: Array
+(
+    [en] => 0.8
+)
+DONE