Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Response building logic into separate function #193

Merged
merged 2 commits into from
Oct 26, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions src/Httpful/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,33 +201,11 @@ public function send()

$result = curl_exec($this->_ch);

if ($result === false) {
if ($curlErrorNumber = curl_errno($this->_ch)) {
$curlErrorString = curl_error($this->_ch);
$this->_error($curlErrorString);
throw new ConnectionErrorException('Unable to connect to "'.$this->uri.'": ' . $curlErrorNumber . ' ' . $curlErrorString);
}

$this->_error('Unable to connect to "'.$this->uri.'".');
throw new ConnectionErrorException('Unable to connect to "'.$this->uri.'".');
}

$info = curl_getinfo($this->_ch);

// Remove the "HTTP/1.x 200 Connection established" string and any other headers added by proxy
$proxy_regex = "/HTTP\/1\.[01] 200 Connection established.*?\r\n\r\n/si";
if ($this->hasProxy() && preg_match($proxy_regex, $result)) {
$result = preg_replace($proxy_regex, '', $result);
}

$response = explode("\r\n\r\n", $result, 2 + $info['redirect_count']);

$body = array_pop($response);
$headers = array_pop($response);
$response = $this->buildResponse($result);

curl_close($this->_ch);

return new Response($body, $headers, $this, $info);
return $response;
}
public function sendIt()
{
Expand Down Expand Up @@ -1038,6 +1016,38 @@ public function buildUserAgent()
return $user_agent;
}

/**
* Takes a curl result and generates a Response from it
* @return Response
*/
public function buildResponse($result) {
if ($result === false) {
if ($curlErrorNumber = curl_errno($this->_ch)) {
$curlErrorString = curl_error($this->_ch);
$this->_error($curlErrorString);
throw new ConnectionErrorException('Unable to connect to "'.$this->uri.'": ' . $curlErrorNumber . ' ' . $curlErrorString);
}

$this->_error('Unable to connect to "'.$this->uri.'".');
throw new ConnectionErrorException('Unable to connect to "'.$this->uri.'".');
}

$info = curl_getinfo($this->_ch);

// Remove the "HTTP/1.x 200 Connection established" string and any other headers added by proxy
$proxy_regex = "/HTTP\/1\.[01] 200 Connection established.*?\r\n\r\n/si";
if ($this->hasProxy() && preg_match($proxy_regex, $result)) {
$result = preg_replace($proxy_regex, '', $result);
}

$response = explode("\r\n\r\n", $result, 2 + $info['redirect_count']);

$body = array_pop($response);
$headers = array_pop($response);

return new Response($body, $headers, $this, $info);
}

/**
* Semi-reluctantly added this as a way to add in curl opts
* that are not otherwise accessible from the rest of the API.
Expand Down