Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#6691 from kasitmp/fix-…
Browse files Browse the repository at this point in the history
…header-handling-in-curl-adapter

fixed removing handled header parts from response header
  • Loading branch information
weierophinney committed Mar 18, 2015
2 parents 4e9e50f + be27851 commit cbc9361
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/Client/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,19 +415,26 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
throw new AdapterException\RuntimeException("Error in cURL request: " . curl_error($this->curl));
}

// separating header from body because it is dangerous to accidentially replace strings in the body
$responseHeaderSize = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
$responseHeaders = substr($this->response, 0, $responseHeaderSize);

// cURL automatically decodes chunked-messages, this means we have to disallow the Zend\Http\Response to do it again
if (stripos($this->response, "Transfer-Encoding: chunked\r\n")) {
$this->response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $this->response);
}
$responseHeaders = preg_replace("/Transfer-Encoding:\s*chunked\\r\\n/", "", $responseHeaders);

// cURL can automatically handle content encoding; prevent double-decoding from occurring
if (isset($this->config['curloptions'][CURLOPT_ENCODING])
&& '' == $this->config['curloptions'][CURLOPT_ENCODING]
&& stripos($this->response, "Content-Encoding: gzip\r\n")
) {
$this->response = str_ireplace("Content-Encoding: gzip\r\n", '', $this->response);
$responseHeaders = preg_replace("/Content-Encoding:\s*gzip\\r\\n/", "", $responseHeaders);
}

// cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string:
$responseHeaders = preg_replace("/HTTP\/1.0\s*200\s*Connection\s*established\\r\\n\\r\\n/", "", $responseHeaders);

// replace old header with new, cleaned up, header
$this->response = substr_replace($this->response, $responseHeaders, 0, $responseHeaderSize);

// Eliminate multiple HTTP responses.
do {
$parts = preg_split('|(?:\r?\n){2}|m', $this->response, 2);
Expand All @@ -439,11 +446,6 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
}
} while ($again);

// cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string:
if (stripos($this->response, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) {
$this->response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $this->response);
}

return $request;
}

Expand Down

0 comments on commit cbc9361

Please sign in to comment.