Skip to content

Commit 7875a89

Browse files
committed
Correctly handle 100 Continue HTTP status
When the server returns 100 Continue, the full HTTP response will be in $response->body. By re-executing the parse on that body until the response is no longer 100, we'll get the real status.
1 parent 3ae5912 commit 7875a89

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/CodeClimate/Bundle/TestReporterBundle/ApiClient.php

+25-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function __construct()
1010
$this->apiHost = "https://codeclimate.com";
1111

1212
if (isset($_SERVER["CODECLIMATE_API_HOST"])) {
13-
$this->apiHost = $_SERVER["CODECLIMATE_API_HOST"];
13+
$this->apiHost = $_SERVER["CODECLIMATE_API_HOST"];
1414
}
1515

1616
}
@@ -19,29 +19,37 @@ public function send($json)
1919
{
2020
$ch = curl_init();
2121
curl_setopt_array($ch, array(
22-
CURLOPT_CUSTOMREQUEST => 'POST',
23-
CURLOPT_URL => $this->apiHost.'/test_reports',
24-
CURLOPT_USERAGENT => 'Code Climate (PHP Test Reporter v'.Version::VERSION.')',
25-
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
26-
CURLOPT_HEADER => true,
27-
CURLOPT_RETURNTRANSFER => true,
28-
CURLOPT_POSTFIELDS => (string)$json,
22+
CURLOPT_CUSTOMREQUEST => 'POST',
23+
CURLOPT_URL => $this->apiHost.'/test_reports',
24+
CURLOPT_USERAGENT => 'Code Climate (PHP Test Reporter v'.Version::VERSION.')',
25+
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
26+
CURLOPT_HEADER => true,
27+
CURLOPT_RETURNTRANSFER => true,
28+
CURLOPT_POSTFIELDS => (string)$json,
2929
));
3030

3131
$response = new \stdClass;
3232
if ($raw_response = curl_exec($ch)) {
33-
list($response->headers, $response->body) = explode("\r\n\r\n", $raw_response, 2);
34-
$response->headers = explode("\r\n", $response->headers);
35-
list(, $response->code, $response->message) = explode(' ', $response->headers[0], 3);
36-
}
37-
else {
38-
$response->code = -curl_errno($ch);
39-
$response->message = curl_error($ch);
40-
$response->headers = array();
41-
$response->body = NULL;
33+
$this->buildResponse($response, $raw_response);
34+
35+
while ($response->code == 100) { // Continue
36+
$this->buildResponse($response, $response->body);
37+
}
38+
} else {
39+
$response->code = -curl_errno($ch);
40+
$response->message = curl_error($ch);
41+
$response->headers = array();
42+
$response->body = NULL;
4243
}
4344
curl_close($ch);
4445

4546
return $response;
4647
}
48+
49+
private function buildResponse($response, $body)
50+
{
51+
list($response->headers, $response->body) = explode("\r\n\r\n", $body, 2);
52+
$response->headers = explode("\r\n", $response->headers);
53+
list(, $response->code, $response->message) = explode(' ', $response->headers[0], 3);
54+
}
4755
}

0 commit comments

Comments
 (0)