Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #20 from CodeIncHQ/2.x
Browse files Browse the repository at this point in the history
v2.0.2
  • Loading branch information
Joan Fabrégat authored Oct 8, 2018
2 parents 7d5079e + 7b04fd2 commit 4598f7d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 89 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codeinc/psr7-responses",
"version": "2.0.1",
"version": "2.0.2",
"description": "A collection of PSR-7 responses",
"homepage": "https://github.com/CodeIncHQ/Psr7Responses",
"type": "library",
Expand All @@ -15,7 +15,8 @@
},
"require-dev": {
"phpunit/phpunit": "^7",
"codeinc/psr7-response-sender": "^1.1"
"codeinc/psr7-response-sender": "^1.1",
"guzzlehttp/guzzle": "^6"
},
"autoload": {
"psr-4": {"CodeInc\\Psr7Responses\\": "src/"}
Expand All @@ -35,7 +36,8 @@
"codeinc/psr7-response-sender": "A library to send PSR-7 responses to the web browser",
"codeinc/psr15-middlewares": "Provides a collection of PSR15 middlewares",
"hansott/psr7-cookies": "Adds cookies into a PSR7 response",
"micheh/psr7-cache": "Adds cache HTTP headers to responses"
"micheh/psr7-cache": "Adds cache HTTP headers to responses",
"guzzlehttp/guzzle": "Required in order to use HttpProxyResponse"
},
"config": {
"preferred-install": {
Expand Down
103 changes: 62 additions & 41 deletions src/HttpProxyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
declare(strict_types=1);
namespace CodeInc\Psr7Responses;
use CodeInc\Psr7Responses\Tests\HttpProxyResponseTest;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Stream;
use function GuzzleHttp\Psr7\stream_for;
use Psr\Http\Message\ResponseInterface;


/**
Expand All @@ -39,7 +39,20 @@
*/
class HttpProxyResponse extends Response
{
protected const IMPORT_HEADERS = [
/**
* @var string
*/
private $remoteUrl;

/**
* @var ResponseInterface
*/
private $response;

/**
* @var string[]
*/
private $acceptableResponseHeaders = [
'content-type',
'content-length',
'content-disposition',
Expand All @@ -51,22 +64,15 @@ class HttpProxyResponse extends Response
'cache-control'
];

/**
* @var string
*/
private $remoteUrl;

/**
* ProxyResponse constructor.
*
* @param string $remoteUrl
* @param int $code
* @param string $reasonPhrase
* @param array $headers
* @param string $version
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function __construct(string $remoteUrl, int $code = 200, string $reasonPhrase = '',
array $headers = [], string $version = '1.1')
public function __construct(string $remoteUrl, array $headers = [], string $version = '1.1')
{
if (!filter_var($remoteUrl, FILTER_VALIDATE_URL)) {
throw new ResponseException(
Expand All @@ -77,53 +83,68 @@ public function __construct(string $remoteUrl, int $code = 200, string $reasonPh
$this->remoteUrl = $remoteUrl;

parent::__construct(
$code,
$this->importHttpHeader($headers),
$this->getStream(),
$this->getResponse()->getStatusCode(),
$this->getResponseHeaders() + $headers,
$this->getResponse()->getBody(),
$version,
$reasonPhrase
$this->getResponse()->getReasonPhrase()
);
}

/**
* Returns the remote URL.
* Returns the HTTP response.
*
* @return string
* @return ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getRemoteUrl():string
public function getResponse():ResponseInterface
{
return $this->remoteUrl;
if (!$this->response) {
$this->response = (new Client())->request('GET', $this->remoteUrl);
}
return $this->response;
}

/**
* @return Stream
* @throws ResponseException
* Returns all the imported headers from the HTTP response.
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private function getStream():Stream
public function getResponseHeaders():array
{
$context = stream_context_create(['http' => ['method' => 'GET']]);
if (($f = fopen($this->remoteUrl, 'r', false, $context)) === false) {
throw new ResponseException(
sprintf("Unable to open the URL %s", $this->remoteUrl),
$this
);
$headers = [];
foreach ($this->getResponse()->getHeaders() as $header => $values) {
if (in_array(strtolower($header), $this->acceptableResponseHeaders)) {
$headers[$header] = $values;
}
}
return stream_for($f);
return $headers;
}

/**
* @param array $headers
* @return array
* @return string[]
*/
private function importHttpHeader(array $headers):array
public function getAcceptableResponseHeaders():array
{
foreach ($http_response_header as $header) {
if (preg_match('/^([\\w-]+): +(.+)$/ui', $header, $matches)) {
if (in_array(strtolower($matches[1]), self::IMPORT_HEADERS)) {
$headers[$matches[1]] = $matches[2];
}
}
}
return $headers;
return $this->acceptableResponseHeaders;
}

/**
* @param string[] $acceptableResponseHeaders
*/
public function setAcceptableResponseHeaders(array $acceptableResponseHeaders):void
{
$this->acceptableResponseHeaders = $acceptableResponseHeaders;
}

/**
* Returns the remote URL.
*
* @return string
*/
public function getRemoteUrl():string
{
return $this->remoteUrl;
}
}
45 changes: 0 additions & 45 deletions tests/DebugResponseTest.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/ErrorResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
*/
class ErrorResponseTest extends AbstractResponseTestCase
{
/**
* @throws \ReflectionException
*/
public function test():void
{
$response = new ErrorResponse(new FakeException('Test'));
Expand Down

0 comments on commit 4598f7d

Please sign in to comment.