Skip to content

Commit 4c08d24

Browse files
authored
Implement support for the host prefix in operations (#1514)
* Implement support for the host prefix in operations This is needed to have a working implementation of LocationService. * Keep the original query string when possible
1 parent 1663e33 commit 4c08d24

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- Support for LocationService
8+
- Support for hostPrefix in requests
89

910
## 1.19.0
1011

src/Request.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ final class Request
3333
*/
3434
private $body;
3535

36+
/**
37+
* @var string|null
38+
*/
39+
private $queryString;
40+
3641
/**
3742
* @var array<string, string>
3843
*/
@@ -43,6 +48,11 @@ final class Request
4348
*/
4449
private $endpoint;
4550

51+
/**
52+
* @var string
53+
*/
54+
private $hostPrefix;
55+
4656
/**
4757
* @var array{scheme: string, host: string, port: int|null}|null
4858
*/
@@ -52,7 +62,7 @@ final class Request
5262
* @param array<string, string> $query
5363
* @param array<string, string> $headers
5464
*/
55-
public function __construct(string $method, string $uri, array $query, array $headers, RequestStream $body)
65+
public function __construct(string $method, string $uri, array $query, array $headers, RequestStream $body, string $hostPrefix = '')
5666
{
5767
$this->method = $method;
5868
$this->uri = $uri;
@@ -62,6 +72,7 @@ public function __construct(string $method, string $uri, array $query, array $he
6272
}
6373
$this->body = $body;
6474
$this->query = $query;
75+
$this->hostPrefix = $hostPrefix;
6576
$this->endpoint = '';
6677
}
6778

@@ -126,12 +137,14 @@ public function hasQueryAttribute(string $name): bool
126137
public function removeQueryAttribute(string $name): void
127138
{
128139
unset($this->query[$name]);
140+
$this->queryString = null;
129141
$this->endpoint = '';
130142
}
131143

132144
public function setQueryAttribute(string $name, string $value): void
133145
{
134146
$this->query[$name] = $value;
147+
$this->queryString = null;
135148
$this->endpoint = '';
136149
}
137150

@@ -148,35 +161,55 @@ public function getQuery(): array
148161
return $this->query;
149162
}
150163

164+
public function getHostPrefix(): string
165+
{
166+
return $this->hostPrefix;
167+
}
168+
169+
public function setHostPrefix(string $hostPrefix): void
170+
{
171+
$this->hostPrefix = $hostPrefix;
172+
$this->endpoint = '';
173+
}
174+
151175
public function getEndpoint(): string
152176
{
153177
if (empty($this->endpoint)) {
154178
if (null === $this->parsed) {
155179
throw new LogicException('Request::$endpoint must be set before using it.');
156180
}
157181

158-
$this->endpoint = $this->parsed['scheme'] . '://' . $this->parsed['host'] . (isset($this->parsed['port']) ? ':' . $this->parsed['port'] : '') . $this->uri . ($this->query ? (false === strpos($this->uri, '?') ? '?' : '&') . http_build_query($this->query, '', '&', \PHP_QUERY_RFC3986) : '');
182+
$this->endpoint = $this->parsed['scheme'] . '://' . $this->hostPrefix . $this->parsed['host'] . (isset($this->parsed['port']) ? ':' . $this->parsed['port'] : '') . $this->uri . ($this->query ? (false === strpos($this->uri, '?') ? '?' : '&') . $this->getQueryString() : '');
159183
}
160184

161185
return $this->endpoint;
162186
}
163187

164188
public function setEndpoint(string $endpoint): void
165189
{
166-
if (!empty($this->endpoint)) {
190+
if (null !== $this->parsed) {
167191
throw new LogicException('Request::$endpoint cannot be changed after it has a value.');
168192
}
169193

170-
$this->endpoint = $endpoint;
171-
$parsed = parse_url($this->endpoint);
194+
$parsed = parse_url($endpoint);
172195

173196
if (false === $parsed || !isset($parsed['scheme'], $parsed['host'])) {
174197
throw new InvalidArgument(sprintf('The endpoint "%s" is invalid.', $endpoint));
175198
}
176199

177200
$this->parsed = ['scheme' => $parsed['scheme'], 'host' => $parsed['host'], 'port' => $parsed['port'] ?? null];
178201

202+
$this->queryString = $parsed['query'] ?? '';
179203
parse_str($parsed['query'] ?? '', $this->query);
180204
$this->uri = $parsed['path'] ?? '/';
181205
}
206+
207+
private function getQueryString(): string
208+
{
209+
if (null === $this->queryString) {
210+
$this->queryString = http_build_query($this->query, '', '&', \PHP_QUERY_RFC3986);
211+
}
212+
213+
return $this->queryString;
214+
}
182215
}

tests/Unit/Signer/SignerV4Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ private function parseRequest(string $req): Request
156156

157157
$req = new Request($method, '/', [], $headers, StringStream::create(implode("\n", $lines)));
158158
$req->setEndpoint('https://' . $headers['Host'] . $path);
159+
// Ensure that the memoized property is filled, so that comparison works consistently.
160+
$req->getEndpoint();
159161

160162
return $req;
161163
}

0 commit comments

Comments
 (0)