Skip to content
Snippets Groups Projects
Commit 1fd01c91 authored by Marcin Kolanko's avatar Marcin Kolanko
Browse files

fixed issue with headers

parent c5dab20b
No related branches found
No related tags found
1 merge request!3Devel
Pipeline #5051 failed
This commit is part of merge request !3. Comments created here will be created in the context of that merge request.
<?php <?php
namespace WPDesk\HttpClient\Curl; namespace iFirmaVendor\WPDesk\HttpClient\Curl;
use WPDesk\HttpClient\HttpClient; use iFirmaVendor\WPDesk\HttpClient\HttpClient;
use WPDesk\HttpClient\HttpClientResponse; use iFirmaVendor\WPDesk\HttpClient\HttpClientResponse;
use WPDesk\HttpClient\HttpClientRequestException; use iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException;
class CurlClient implements \iFirmaVendor\WPDesk\HttpClient\HttpClient
class CurlClient implements HttpClient
{ {
/** @var resource */ /** @var resource */
private $curlResource; private $curlResource;
/** @var string|null|boolean */ /** @var string|null|boolean */
private $rawResponse; private $rawResponse;
/** @var int */ /** @var int */
private $httpResponseCode; private $httpResponseCode;
/** @var array */
private $headers = array();
/** /**
* @param string $url * @param string $url
* @param string $body * @param string $body
...@@ -29,7 +27,6 @@ class CurlClient implements HttpClient ...@@ -29,7 +27,6 @@ class CurlClient implements HttpClient
{ {
return $this->send($url, 'GET', $body, $headers, $timeOut); return $this->send($url, 'GET', $body, $headers, $timeOut);
} }
/** /**
* @param string $url * @param string $url
* @param string $method * @param string $method
...@@ -47,23 +44,19 @@ class CurlClient implements HttpClient ...@@ -47,23 +44,19 @@ class CurlClient implements HttpClient
$this->sendRequest(); $this->sendRequest();
$this->throwExceptionIfError(); $this->throwExceptionIfError();
$this->closeConnection(); $this->closeConnection();
return $this->prepareResponse(); return $this->prepareResponse();
} catch (\Exception $e) { } catch (\Exception $e) {
$this->closeConnection(); $this->closeConnection();
if ($e instanceof HttpClientRequestException) { if ($e instanceof \iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException) {
throw $e; throw $e;
} }
throw CurlExceptionFactory::createDefaultException($e->getMessage(), $e->getCode(), $e); throw \iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory::createDefaultException($e->getMessage(), $e->getCode(), $e);
} }
} }
private function initResource() private function initResource()
{ {
$this->curlResource = curl_init(); $this->curlResource = \curl_init();
} }
/** /**
* Opens a new curl connection. * Opens a new curl connection.
* *
...@@ -75,25 +68,31 @@ class CurlClient implements HttpClient ...@@ -75,25 +68,31 @@ class CurlClient implements HttpClient
*/ */
private function prepareConnection($url, $method, $body, array $headers, $timeOut) private function prepareConnection($url, $method, $body, array $headers, $timeOut)
{ {
$options = [ $options = [
CURLOPT_CUSTOMREQUEST => $method, \CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $this->compileRequestHeaders($headers), \CURLOPT_HTTPHEADER => $this->compileRequestHeaders($headers),
CURLOPT_URL => $url, \CURLOPT_URL => $url,
CURLOPT_CONNECTTIMEOUT => 10, \CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => $timeOut, \CURLOPT_TIMEOUT => $timeOut,
CURLOPT_RETURNTRANSFER => true, // Return response as string \CURLOPT_RETURNTRANSFER => \true,
CURLOPT_HEADER => true, // Enable header processing // Return response as string
CURLOPT_SSL_VERIFYHOST => 2, \CURLOPT_HEADER => \false,
CURLOPT_SSL_VERIFYPEER => true, // Enable header processing
//CURLOPT_CAINFO => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem', \CURLOPT_SSL_VERIFYHOST => 2,
\CURLOPT_SSL_VERIFYPEER => \true,
\CURLOPT_HEADERFUNCTION => function ( $curl, $header ) {
$len = strlen( $header );
$this->headers[] = trim( $header );
return $len;
}
]; ];
if ($method !== "GET") { if ($method !== "GET") {
$options[CURLOPT_POSTFIELDS] = $body; $options[\CURLOPT_POSTFIELDS] = $body;
} }
\curl_setopt_array($this->curlResource, $options);
curl_setopt_array($this->curlResource, $options);
} }
/** /**
* Compiles the request headers into a curl-friendly format. * Compiles the request headers into a curl-friendly format.
* *
...@@ -109,48 +108,41 @@ class CurlClient implements HttpClient ...@@ -109,48 +108,41 @@ class CurlClient implements HttpClient
} }
return $return; return $return;
} }
/** /**
* Send the request and get the raw response from curl * Send the request and get the raw response from curl
*/ */
private function sendRequest() private function sendRequest()
{ {
$this->rawResponse = curl_exec($this->curlResource); $this->rawResponse = \curl_exec($this->curlResource);
$this->httpResponseCode = $this->getHttpResponseCode(); $this->httpResponseCode = $this->getHttpResponseCode();
} }
/** @return int */ /** @return int */
private function getHttpResponseCode() private function getHttpResponseCode()
{ {
return intval(curl_getinfo($this->curlResource, CURLINFO_HTTP_CODE)); return \intval(\curl_getinfo($this->curlResource, \CURLINFO_HTTP_CODE));
} }
private function throwExceptionIfError() private function throwExceptionIfError()
{ {
$errorNumber = curl_errno($this->curlResource); $errorNumber = \curl_errno($this->curlResource);
if ($errorNumber === 0) { if ($errorNumber === 0) {
return; return;
} }
$errorMessage = \curl_error($this->curlResource);
$errorMessage = curl_error($this->curlResource); throw \iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory::createCurlException($errorMessage, $errorNumber);
throw CurlExceptionFactory::createCurlException($errorMessage, $errorNumber);
} }
/** /**
* Closes an existing curl connection * Closes an existing curl connection
*/ */
private function closeConnection() private function closeConnection()
{ {
curl_close($this->curlResource); \curl_close($this->curlResource);
$this->curlResource = null; $this->curlResource = null;
} }
private function prepareResponse() private function prepareResponse()
{ {
list($rawHeaders, $rawBody) = $this->extractResponseHeadersAndBody(); list($rawHeaders, $rawBody) = $this->extractResponseHeadersAndBody();
return new HttpClientResponse($rawHeaders, $rawBody, $this->httpResponseCode); return new \iFirmaVendor\WPDesk\HttpClient\HttpClientResponse($rawHeaders, $rawBody, $this->httpResponseCode);
} }
/** /**
* Extracts the headers and the body into a two-part array * Extracts the headers and the body into a two-part array
* *
...@@ -158,12 +150,10 @@ class CurlClient implements HttpClient ...@@ -158,12 +150,10 @@ class CurlClient implements HttpClient
*/ */
private function extractResponseHeadersAndBody() private function extractResponseHeadersAndBody()
{ {
$parts = explode("\r\n\r\n", $this->rawResponse, 2 ); $rawBody = \trim( $this->rawResponse );
$rawBody = array_pop($parts); $rawHeaders = \trim(implode( "\r\n",$this->headers ));
$rawHeaders = implode("\r\n\r\n", $parts); return [ $rawHeaders, $rawBody ];
return [trim($rawHeaders), trim($rawBody)];
} }
/** /**
* @param string $url * @param string $url
* @param string $body * @param string $body
...@@ -176,7 +166,6 @@ class CurlClient implements HttpClient ...@@ -176,7 +166,6 @@ class CurlClient implements HttpClient
{ {
return $this->send($url, 'POST', $body, $headers, $timeOut); return $this->send($url, 'POST', $body, $headers, $timeOut);
} }
/** /**
* @param string $url * @param string $url
* @param string $body * @param string $body
...@@ -189,7 +178,6 @@ class CurlClient implements HttpClient ...@@ -189,7 +178,6 @@ class CurlClient implements HttpClient
{ {
return $this->send($url, 'DELETE', $body, $headers, $timeOut); return $this->send($url, 'DELETE', $body, $headers, $timeOut);
} }
/** /**
* @param string $url * @param string $url
* @param string $body * @param string $body
...@@ -202,5 +190,4 @@ class CurlClient implements HttpClient ...@@ -202,5 +190,4 @@ class CurlClient implements HttpClient
{ {
return $this->send($url, 'PUT', $body, $headers, $timeOut); return $this->send($url, 'PUT', $body, $headers, $timeOut);
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment