Use associative-style arrays for request headers and response headers

This commit is contained in:
Zach Borboa
2014-03-14 01:04:39 -07:00
parent fbec34fd6c
commit 875f636931
2 changed files with 24 additions and 12 deletions
+21 -2
View File
@@ -202,6 +202,25 @@ class Curl {
return $url . (empty($data) ? '' : '?' . http_build_query($data));
}
private function _parseHeaders($raw_headers) {
$raw_headers = preg_split('/\r\n/', $raw_headers, null, PREG_SPLIT_NO_EMPTY);
$http_headers = array();
for ($i = 1; $i < count($raw_headers); $i++) {
list($key, $value) = explode(':', $raw_headers[$i], 2);
$key = trim($key);
$value = trim($value);
if (array_key_exists($key, $http_headers)) {
$http_headers[$key] .= ',' . $value;
}
else {
$http_headers[$key] = $value;
}
}
return $http_headers;
}
private function _postfields($data) {
if (is_array($data)) {
if (is_array_multidim($data)) {
@@ -248,14 +267,14 @@ class Curl {
$ch->error = $ch->curl_error || $ch->http_error;
$ch->error_code = $ch->error ? ($ch->curl_error ? $ch->curl_error_code : $ch->http_status_code) : 0;
$ch->request_headers = preg_split('/\r\n/', curl_getinfo($ch->curl, CURLINFO_HEADER_OUT), null, PREG_SPLIT_NO_EMPTY);
$ch->request_headers = $this->_parseHeaders(curl_getinfo($ch->curl, CURLINFO_HEADER_OUT));
$ch->response_headers = '';
if (!(strpos($ch->response, "\r\n\r\n") === false)) {
list($response_header, $ch->response) = explode("\r\n\r\n", $ch->response, 2);
if ($response_header === 'HTTP/1.1 100 Continue') {
list($response_header, $ch->response) = explode("\r\n\r\n", $ch->response, 2);
}
$ch->response_headers = preg_split('/\r\n/', $response_header, null, PREG_SPLIT_NO_EMPTY);
$ch->response_headers = $this->_parseHeaders($response_header);
}
$ch->http_error_message = $ch->error ? (isset($ch->response_headers['0']) ? $ch->response_headers['0'] : '') : '';
+3 -10
View File
@@ -283,21 +283,14 @@ class CurlTest extends PHPUnit_Framework_TestCase {
public function testPostContentTypes() {
$test = new Test();
$test->server('server', 'POST', 'foo=bar');
$this->assertTrue(in_array(
'Content-Type: application/x-www-form-urlencoded',
$test->curl->request_headers
));
$this->assertEquals($test->curl->request_headers['Content-Type'], 'application/x-www-form-urlencoded');
$test = new Test();
$test->server('server', 'POST', array(
'foo' => 'bar',
));
$this->assertTrue(in_array(
'Expect: 100-continue',
$test->curl->request_headers
));
$content_type = preg_grep('/^Content-Type: multipart\/form-data; boundary=/',
$test->curl->request_headers);
$this->assertEquals($test->curl->request_headers['Expect'], '100-continue');
preg_match('/^multipart\/form-data; boundary=/', $test->curl->request_headers['Content-Type'], $content_type);
$this->assertTrue(!empty($content_type));
}