Skip to content

Commit f0c4198

Browse files
committed
Forward-port of PR php-vcr#294
[Curl] Add \r\n to HTTP status and headers to mimic what curl does
1 parent 2c3cf0a commit f0c4198

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/VCR/Util/CurlHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function handleOutput(Response $response, array $curlOptions, $ch)
5959
if (isset($curlOptions[CURLOPT_HEADERFUNCTION])) {
6060
$headerList = array(HttpUtil::formatAsStatusString($response));
6161
$headerList = array_merge($headerList, HttpUtil::formatHeadersForCurl($response->getHeaders()));
62-
$headerList[] = '';
62+
$headerList[] = "\r\n";
6363
foreach ($headerList as $header) {
6464
call_user_func($curlOptions[CURLOPT_HEADERFUNCTION], $ch, $header);
6565
}

src/VCR/Util/HttpUtil.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ public static function parseRawHeader($rawHeader)
9191
* Returns a list of headers from a key/value paired array.
9292
*
9393
* @param array $headers Headers as key/value pairs.
94-
* @return array List of headers ['Content-Type: text/html', '...'].
94+
* @return array List of headers ["Content-Type: text/html\r\n", '...'].
9595
*/
9696
public static function formatHeadersForCurl(array $headers)
9797
{
9898
$curlHeaders = array();
9999

100100
foreach ($headers as $key => $value) {
101-
$curlHeaders[] = $key . ': ' . $value;
101+
$curlHeaders[] = $key . ': ' . $value . "\r\n";
102102
}
103103

104104
return $curlHeaders;
@@ -108,25 +108,30 @@ public static function formatHeadersForCurl(array $headers)
108108
* Returns a HTTP status line from specified response.
109109
*
110110
* @param Response $response
111+
*
111112
* @return string HTTP status line.
112113
*/
113114
public static function formatAsStatusString(Response $response)
114115
{
115116
return 'HTTP/' . $response->getHttpVersion()
116-
. ' ' . $response->getStatusCode()
117-
. ' ' . $response->getStatusMessage();
117+
. ' ' . $response->getStatusCode()
118+
. ' ' . $response->getStatusMessage()
119+
. "\r\n";
118120
}
119121

120122
/**
121123
* Returns a HTTP status line with headers from specified response.
122124
*
123125
* @param Response $response
126+
*
124127
* @return string HTTP status line.
125128
*/
126129
public static function formatAsStatusWithHeadersString(Response $response)
127130
{
128131
$headers = self::formatHeadersForCurl($response->getHeaders());
129132
array_unshift($headers, self::formatAsStatusString($response));
130-
return join("\r\n", $headers) . "\r\n\r\n";
133+
134+
// Only one \r\n, as self::formatHeadersForCurl() returns an empty line as the last header already;
135+
return join('', $headers) . "\r\n";
131136
}
132137
}

tests/VCR/Util/CurlHelperTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ public function testHandleOutputHeaderFunction()
252252
CurlHelper::handleOutput($response, $curlOptions, curl_init());
253253

254254
$expected = array(
255-
'HTTP/1.1 200 OK',
256-
'Content-Length: 0',
257-
''
255+
"HTTP/1.1 200 OK\r\n",
256+
"Content-Length: 0\r\n",
257+
"\r\n"
258258
);
259259
$this->assertEquals($expected, $actualHeaders);
260260
}

0 commit comments

Comments
 (0)