mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-10-22 20:07:56 +00:00
Fix #27: Allow case-insensitive access to request headers and response headers
This commit is contained in:
+35
-1
@@ -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'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user