Fix #27: Allow case-insensitive access to request headers and response headers

This commit is contained in:
Zach Borboa
2014-03-14 01:27:06 -07:00
parent 875f636931
commit 6c9709af59
3 changed files with 98 additions and 1 deletions
+35 -1
View File
@@ -204,7 +204,7 @@ class Curl {
private function _parseHeaders($raw_headers) {
$raw_headers = preg_split('/\r\n/', $raw_headers, null, PREG_SPLIT_NO_EMPTY);
$http_headers = array();
$http_headers = new CaseInsensitiveArray();
for ($i = 1; $i < count($raw_headers); $i++) {
list($key, $value) = explode(':', $raw_headers[$i], 2);
@@ -305,6 +305,40 @@ class Curl {
}
}
class CaseInsensitiveArray implements ArrayAccess, Countable {
private $container = array();
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->container[] = $value;
}
else {
$index = array_search(strtolower($offset), array_keys(array_change_key_case($this->container, CASE_LOWER)));
if (!($index === false)) {
unset($this->container[array_keys($this->container)[$index]]);
}
$this->container[$offset] = $value;
}
}
public function offsetExists($offset) {
return array_key_exists(strtolower($offset), array_change_key_case($this->container, CASE_LOWER));
}
public function offsetUnset($offset) {
unset($this->container[$offset]);
}
public function offsetGet($offset) {
$index = array_search(strtolower($offset), array_keys(array_change_key_case($this->container, CASE_LOWER)));
return $index === false ? null : array_values($this->container)[$index];
}
public function count() {
return count($this->container);
}
}
function is_array_assoc($array) {
return (bool)count(array_filter(array_keys($array), 'is_string'));
}