1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\Test; |
13
|
|
|
|
14
|
|
|
use Http\Discovery\HttpClientDiscovery; |
15
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
16
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
17
|
|
|
use Http\Client\HttpClient as PhpHttpClient; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
use Psr\Http\Message\UriInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Provides a very simple way to fetch HTTP responses, auto-creating a client. |
24
|
|
|
*/ |
25
|
|
|
class HttpClient |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* HTTP client for requests to the application. |
29
|
|
|
* |
30
|
|
|
* @var PhpHttpClient |
31
|
|
|
*/ |
32
|
|
|
private $httpClient; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $hostname; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $port; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $hostname Default hostname if not specified in the URL |
46
|
|
|
* @param string $port Default port if not specified in the URL |
47
|
|
|
*/ |
48
|
33 |
|
public function __construct($hostname, $port) |
49
|
|
|
{ |
50
|
33 |
|
$this->hostname = $hostname; |
51
|
33 |
|
$this->port = $port; |
52
|
33 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get HTTP response from your application. |
56
|
|
|
* |
57
|
|
|
* @param string $uri HTTP URI |
58
|
|
|
* @param array $headers HTTP headers |
59
|
|
|
* @param string $method HTTP method |
60
|
|
|
* |
61
|
|
|
* @return ResponseInterface |
62
|
|
|
*/ |
63
|
33 |
|
public function getResponse($uri, array $headers, $method) |
64
|
|
|
{ |
65
|
33 |
|
$request = $this->createRequest($method, $uri, $headers); |
66
|
|
|
|
67
|
33 |
|
return $this->sendRequest($request); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Send HTTP request to your application. |
72
|
|
|
* |
73
|
|
|
* @param RequestInterface $request |
74
|
|
|
* |
75
|
|
|
* @return ResponseInterface |
76
|
|
|
*/ |
77
|
33 |
|
public function sendRequest(RequestInterface $request) |
78
|
|
|
{ |
79
|
|
|
// Close connections to make sure invalidation (PURGE/BAN) requests will |
80
|
|
|
// not interfere with content (GET) requests. |
81
|
33 |
|
$request = $request->withHeader('Connection', 'Close'); |
82
|
|
|
|
83
|
33 |
|
return $this->getHttpClient()->sendRequest($request); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get HTTP client for your application. |
88
|
|
|
* |
89
|
|
|
* @return PhpHttpClient |
90
|
|
|
*/ |
91
|
33 |
|
private function getHttpClient() |
92
|
|
|
{ |
93
|
33 |
|
if ($this->httpClient === null) { |
94
|
33 |
|
$this->httpClient = HttpClientDiscovery::find(); |
95
|
33 |
|
} |
96
|
|
|
|
97
|
33 |
|
return $this->httpClient; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create a request. |
102
|
|
|
* |
103
|
|
|
* @param string $method |
104
|
|
|
* @param string $uri |
105
|
|
|
* @param array $headers |
106
|
|
|
* |
107
|
|
|
* @return RequestInterface |
108
|
|
|
* |
109
|
|
|
* @throws \Exception |
110
|
|
|
*/ |
111
|
33 |
|
private function createRequest($method, $uri, $headers) |
112
|
|
|
{ |
113
|
33 |
|
$uri = $this->createUri($uri); |
114
|
33 |
|
if ($uri->getHost() === '') { |
115
|
|
|
// Add base URI host |
116
|
33 |
|
$uri = $uri->withHost($this->hostname); |
117
|
33 |
|
} |
118
|
|
|
|
119
|
33 |
|
if (!$uri->getPort()) { |
|
|
|
|
120
|
33 |
|
$uri = $uri->withPort($this->port); |
121
|
33 |
|
} |
122
|
|
|
|
123
|
33 |
|
if ($uri->getScheme() === '') { |
124
|
33 |
|
$uri = $uri->withScheme('http'); |
125
|
33 |
|
} |
126
|
|
|
|
127
|
33 |
|
return MessageFactoryDiscovery::find()->createRequest( |
128
|
33 |
|
$method, |
129
|
33 |
|
$uri, |
130
|
|
|
$headers |
131
|
33 |
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Create PSR-7 URI object from URI string. |
136
|
|
|
* |
137
|
|
|
* @param string $uriString |
138
|
|
|
* |
139
|
|
|
* @return UriInterface |
140
|
|
|
*/ |
141
|
33 |
|
private function createUri($uriString) |
142
|
|
|
{ |
143
|
33 |
|
return UriFactoryDiscovery::find()->createUri($uriString); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: