Skip to content

Commit

Permalink
Merge pull request #193 from griffy/master
Browse files Browse the repository at this point in the history
Move Response building logic into separate function
  • Loading branch information
Nate Good committed Oct 26, 2015
2 parents 89513cd + 2070848 commit ae8a698
Showing 1 changed file with 34 additions and 24 deletions.
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

0 comments on commit ae8a698

Please sign in to comment.