Skip to content

Commit ddf5a83

Browse files
committed
fixed removing handled header parts from response header
1 parent 836f07a commit ddf5a83

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/Client/Adapter/Curl.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,26 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
421421
throw new AdapterException\RuntimeException("Error in cURL request: " . curl_error($this->curl));
422422
}
423423

424+
// separating header from body because it is dangerous to accidentially replace strings in the body
425+
$responseHeaderSize = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
426+
$responseHeaders = substr($this->response, 0, $responseHeaderSize);
427+
424428
// cURL automatically decodes chunked-messages, this means we have to disallow the Zend\Http\Response to do it again
425-
if (stripos($this->response, "Transfer-Encoding: chunked\r\n")) {
426-
$this->response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $this->response);
427-
}
429+
$responseHeaders = preg_replace("/Transfer-Encoding:\s*chunked\\r\\n/", "", $responseHeaders);
428430

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

438+
// cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string:
439+
$responseHeaders = preg_replace("/HTTP\/1.0\s*200\s*Connection\s*established\\r\\n\\r\\n/", "", $responseHeaders);
440+
441+
// replace old header with new, cleaned up, header
442+
$this->response = substr_replace($this->response, $responseHeaders, 0, $responseHeaderSize);
443+
437444
// Eliminate multiple HTTP responses.
438445
do {
439446
$parts = preg_split('|(?:\r?\n){2}|m', $this->response, 2);
@@ -445,11 +452,6 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
445452
}
446453
} while ($again);
447454

448-
// cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string:
449-
if (stripos($this->response, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) {
450-
$this->response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $this->response);
451-
}
452-
453455
return $request;
454456
}
455457

0 commit comments

Comments
 (0)