3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
namespace CodeIgniter\HTTP;
17
* Representation of an outgoing, client-side request.
19
* @see \CodeIgniter\HTTP\OutgoingRequestTest
21
class OutgoingRequest extends Message implements OutgoingRequestInterface
38
* @param string $method HTTP method
39
* @param string|null $body
41
public function __construct(
46
string $version = '1.1'
48
$this->method = $method;
51
foreach ($headers as $header => $value) {
52
$this->setHeader($header, $value);
56
$this->protocolVersion = $version;
58
if (! $this->hasHeader('Host') && $this->uri->getHost() !== '') {
59
$this->setHeader('Host', $this->getHostFromUri($this->uri));
63
private function getHostFromUri(URI $uri): string
65
$host = $uri->getHost();
67
return $host . ($uri->getPort() ? ':' . $uri->getPort() : '');
71
* Retrieves the HTTP method of the request.
73
* @return string Returns the request method (always uppercase)
75
public function getMethod(): string
81
* Sets the request method. Used when spoofing the request.
85
* @deprecated Use withMethod() instead for immutability
87
public function setMethod(string $method)
89
$this->method = $method;
95
* Returns an instance with the specified method.
97
* @param string $method
101
public function withMethod($method)
103
$request = clone $this;
104
$request->method = $method;
110
* Retrieves the URI instance.
114
public function getUri()
120
* Returns an instance with the provided URI.
122
* @param URI $uri New request URI to use.
123
* @param bool $preserveHost Preserve the original state of the Host header.
127
public function withUri(URI $uri, $preserveHost = false)
129
$request = clone $this;
130
$request->uri = $uri;
133
if ($this->isHostHeaderMissingOrEmpty() && $uri->getHost() !== '') {
134
$request->setHeader('Host', $this->getHostFromUri($uri));
139
if ($this->isHostHeaderMissingOrEmpty() && $uri->getHost() === '') {
143
if (! $this->isHostHeaderMissingOrEmpty()) {
148
if ($uri->getHost() !== '') {
149
$request->setHeader('Host', $this->getHostFromUri($uri));
155
private function isHostHeaderMissingOrEmpty(): bool
157
if (! $this->hasHeader('Host')) {
161
return $this->header('Host')->getValue() === '';