Skip to content

Commit

Permalink
Merge branch 'curl-transport' of https://github.com/haydenyoung/jooml…
Browse files Browse the repository at this point in the history
…a-cms into curl-transport
  • Loading branch information
haydenyoung committed Aug 11, 2017
2 parents c1d93d8 + 6a4e20d commit 4e82acd
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions libraries/src/Http/Transport/CurlTransport.php
Expand Up @@ -188,6 +188,14 @@ public function request($method, Uri $uri, $data = null, array $headers = null,
foreach ($this->options->get('transport.curl', array()) as $key => $value)
{
$options[$key] = $value;

if ($key == CURLOPT_FILE && (bool) $value)
{
$this->headers = '';

$options[CURLOPT_HEADER] = false;
$options[CURLOPT_HEADERFUNCTION] = array($this, 'getHeaders');
}
}

// Authentification, if needed
Expand All @@ -203,8 +211,8 @@ public function request($method, Uri $uri, $data = null, array $headers = null,
// Execute the request and close the connection.
$content = curl_exec($ch);

// Check if the content is a string. If it is not, it must be an error.
if (!is_string($content))
// Check if the content is a string or is boolean true. If it is not, it must be an error.
if (!is_string($content) && !(bool) $content)
{
$message = curl_error($ch);

Expand All @@ -223,6 +231,12 @@ public function request($method, Uri $uri, $data = null, array $headers = null,
// Close the connection.
curl_close($ch);

// If headers are set append to the content.
if (isset($this->headers))
{
$content = $this->headers . (is_bool($content) ? "" : $content);
}

$response = $this->getResponse($content, $info);

// Manually follow redirects if server doesn't allow to follow location using curl
Expand Down Expand Up @@ -367,4 +381,19 @@ private function redirectsAllowed()

return false;
}

/**
* Gets each header returned by cURL, appending it to the headers string.
*
* @param resource $ch The cURL resource.
* @param string $header The header string.
*
* @return int The length of the header.
*/
private function getHeaders($ch, $header)
{
$this->headers .= $header;

return strlen($header);
}
}

0 comments on commit 4e82acd

Please sign in to comment.