blob: 38714af80fe540589c45cb22d1dc03c374417e0e (
plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
<?php
namespace GuzzleHttp\Message;
use GuzzleHttp\Collection;
use GuzzleHttp\Event\HasEmitterTrait;
use GuzzleHttp\Subscriber\Prepare;
use GuzzleHttp\Url;
/**
* HTTP request class to send requests
*/
class Request extends AbstractMessage implements RequestInterface
{
use HasEmitterTrait;
/** @var Url HTTP Url */
private $url;
/** @var string HTTP method */
private $method;
/** @var Collection Transfer options */
private $transferOptions;
/**
* @param string $method HTTP method
* @param string|Url $url HTTP URL to connect to. The URI scheme,
* host header, and URI are parsed from the full URL. If query string
* parameters are present they will be parsed as well.
* @param array|Collection $headers HTTP headers
* @param mixed $body Body to send with the request
* @param array $options Array of options to use with the request
* - emitter: Event emitter to use with the request
*/
public function __construct(
$method,
$url,
$headers = [],
$body = null,
array $options = []
) {
$this->setUrl($url);
$this->method = strtoupper($method);
$this->handleOptions($options);
$this->transferOptions = new Collection($options);
$this->addPrepareEvent();
if ($body !== null) {
$this->setBody($body);
}
if ($headers) {
foreach ($headers as $key => $value) {
$this->addHeader($key, $value);
}
}
}
public function __clone()
{
if ($this->emitter) {
$this->emitter = clone $this->emitter;
}
$this->transferOptions = clone $this->transferOptions;
$this->url = clone $this->url;
}
public function setUrl($url)
{
$this->url = $url instanceof Url ? $url : Url::fromString($url);
$this->updateHostHeaderFromUrl();
}
public function getUrl()
{
return (string) $this->url;
}
public function setQuery($query)
{
$this->url->setQuery($query);
}
public function getQuery()
{
return $this->url->getQuery();
}
public function setMethod($method)
{
$this->method = strtoupper($method);
}
public function getMethod()
{
return $this->method;
}
public function getScheme()
{
return $this->url->getScheme();
}
public function setScheme($scheme)
{
$this->url->setScheme($scheme);
}
public function getPort()
{
return $this->url->getPort();
}
public function setPort($port)
{
$this->url->setPort($port);
$this->updateHostHeaderFromUrl();
}
public function getHost()
{
return $this->url->getHost();
}
public function setHost($host)
{
$this->url->setHost($host);
$this->updateHostHeaderFromUrl();
}
public function getPath()
{
return '/' . ltrim($this->url->getPath(), '/');
}
public function setPath($path)
{
$this->url->setPath($path);
}
public function getResource()
{
$resource = $this->getPath();
if ($query = (string) $this->url->getQuery()) {
$resource .= '?' . $query;
}
return $resource;
}
public function getConfig()
{
return $this->transferOptions;
}
protected function handleOptions(array &$options)
{
parent::handleOptions($options);
// Use a custom emitter if one is specified, and remove it from
// options that are exposed through getConfig()
if (isset($options['emitter'])) {
$this->emitter = $options['emitter'];
unset($options['emitter']);
}
}
/**
* Adds a subscriber that ensures a request's body is prepared before
* sending.
*/
private function addPrepareEvent()
{
static $subscriber;
if (!$subscriber) {
$subscriber = new Prepare();
}
$this->getEmitter()->attach($subscriber);
}
private function updateHostHeaderFromUrl()
{
$port = $this->url->getPort();
$scheme = $this->url->getScheme();
if ($host = $this->url->getHost()) {
if (($port == 80 && $scheme == 'http') ||
($port == 443 && $scheme == 'https')
) {
$this->setHeader('Host', $host);
} else {
$this->setHeader('Host', "{$host}:{$port}");
}
}
}
}
|