This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpProxyResponse.php
92 lines (85 loc) · 3.04 KB
/
HttpProxyResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. |
// | Visit https://www.codeinc.fr for more information about licensing. |
// +---------------------------------------------------------------------+
// | NOTICE: All information contained herein is, and remains the |
// | property of Code Inc. SAS. The intellectual and technical concepts |
// | contained herein are proprietary to Code Inc. SAS are protected by |
// | trade secret or copyright law. Dissemination of this information or |
// | reproduction of this material is strictly forbidden unless prior |
// | written permission is obtained from Code Inc. SAS. |
// +---------------------------------------------------------------------+
//
// Author: Joan Fabrégat <[email protected]>
// Date: 03/05/2018
// Time: 11:43
// Project: Psr7Responses
//
declare(strict_types=1);
namespace CodeInc\Psr7Responses;
use CodeInc\Psr7Responses\Tests\HttpProxyResponseTest;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\stream_for;
/**
* Class HttpProxyResponse
*
* @see HttpProxyResponseTest
* @package CodeInc\Psr7Responses
* @author Joan Fabrégat <[email protected]>
*/
class HttpProxyResponse extends Response
{
protected const IMPORT_HEADERS = [
'content-type',
'content-length',
'content-disposition',
'date',
'last-modified',
'etag',
'pragma',
'expires',
'cache-control'
];
/**
* ProxyResponse constructor.
*
* @param string $remoteUrl
* @param int $status
* @param array $headers
* @param string $version
* @param null|string $reason
* @throws ResponseException
*/
public function __construct(string $remoteUrl, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
{
// checking the URL
if (!filter_var($remoteUrl, FILTER_VALIDATE_URL)) {
throw new ResponseException(
sprintf("%s is not a valid URL", $remoteUrl),
$this
);
}
// downloading
$context = stream_context_create(['http' => ['method' => 'GET']]);
if (($f = fopen($remoteUrl, 'r', false, $context)) === false) {
throw new ResponseException(
sprintf("Unable to open the URL %s", $remoteUrl),
$this
);
}
// importing the headers
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];
}
}
}
parent::__construct($status, $headers, stream_for($f), $version, $reason);
}
}