-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add OutgoingRequestInterface #6698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a7c40b4
feat: add OutgoingRequestInterface and OutgoingRequest class
kenjis ed1b0fd
fix: use OutgoingRequestInterface and OutgoingRequest class
kenjis 1184383
refactor: CURLRequest extends OutgoingRequest
kenjis 75cd06e
docs: fix PHPDocs
kenjis b29a4d3
refactor: run rector
kenjis d3b0c7f
docs: BaseController depends on RequestInterface
kenjis 3a448e8
docs: add comments
kenjis eaa73f1
docs: add changelog
kenjis 583689c
docs: add note for Interface Changes
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\HTTP; | ||
|
|
||
| /** | ||
| * Representation of an outgoing, client-side request. | ||
| */ | ||
| class OutgoingRequest extends Message implements OutgoingRequestInterface | ||
| { | ||
| /** | ||
| * Request method. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $method; | ||
|
|
||
| /** | ||
| * A URI instance. | ||
| * | ||
| * @var URI|null | ||
| */ | ||
| protected $uri; | ||
|
|
||
| /** | ||
| * @param string $method HTTP method | ||
| * @param string|null $body | ||
| */ | ||
| public function __construct( | ||
| string $method, | ||
| ?URI $uri = null, | ||
| array $headers = [], | ||
| $body = null, | ||
| string $version = '1.1' | ||
| ) { | ||
| $this->method = $method; | ||
| $this->uri = $uri; | ||
|
|
||
| foreach ($headers as $header => $value) { | ||
| $this->setHeader($header, $value); | ||
| } | ||
|
|
||
| $this->body = $body; | ||
| $this->protocolVersion = $version; | ||
|
|
||
| if (! $this->hasHeader('Host') && $this->uri->getHost() !== '') { | ||
| $this->setHeader('Host', $this->getHostFromUri($this->uri)); | ||
| } | ||
| } | ||
|
|
||
| private function getHostFromUri(URI $uri): string | ||
| { | ||
| $host = $uri->getHost(); | ||
|
|
||
| return $host . ($uri->getPort() ? ':' . $uri->getPort() : ''); | ||
| } | ||
|
|
||
| /** | ||
| * Get the request method. | ||
| * | ||
| * @param bool $upper Whether to return in upper or lower case. | ||
| * | ||
| * @deprecated The $upper functionality will be removed and this will revert to its PSR-7 equivalent | ||
| */ | ||
| public function getMethod(bool $upper = false): string | ||
| { | ||
| return ($upper) ? strtoupper($this->method) : strtolower($this->method); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the request method. Used when spoofing the request. | ||
| * | ||
| * @return $this | ||
| * | ||
| * @deprecated Use withMethod() instead for immutability | ||
| */ | ||
| public function setMethod(string $method) | ||
| { | ||
| $this->method = $method; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns an instance with the specified method. | ||
| * | ||
| * @param string $method | ||
| * | ||
| * @return static | ||
| */ | ||
| public function withMethod($method) | ||
| { | ||
| $request = clone $this; | ||
| $request->method = $method; | ||
|
|
||
| return $request; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the URI instance. | ||
| * | ||
| * @return URI|null | ||
| */ | ||
| public function getUri() | ||
| { | ||
| return $this->uri; | ||
| } | ||
|
|
||
| /** | ||
| * Returns an instance with the provided URI. | ||
| * | ||
| * @param URI $uri New request URI to use. | ||
| * @param bool $preserveHost Preserve the original state of the Host header. | ||
| * | ||
| * @return static | ||
| */ | ||
| public function withUri(URI $uri, $preserveHost = false) | ||
| { | ||
| $request = clone $this; | ||
| $request->uri = $uri; | ||
|
|
||
| if ($preserveHost) { | ||
| if ($this->isHostHeaderMissingOrEmpty() && $uri->getHost() !== '') { | ||
| $request->setHeader('Host', $this->getHostFromUri($uri)); | ||
|
|
||
| return $request; | ||
| } | ||
|
|
||
| if ($this->isHostHeaderMissingOrEmpty() && $uri->getHost() === '') { | ||
| return $request; | ||
| } | ||
|
|
||
| if (! $this->isHostHeaderMissingOrEmpty()) { | ||
| return $request; | ||
| } | ||
| } | ||
|
|
||
| if ($uri->getHost() !== '') { | ||
| $request->setHeader('Host', $this->getHostFromUri($uri)); | ||
| } | ||
|
|
||
| return $request; | ||
| } | ||
|
|
||
| private function isHostHeaderMissingOrEmpty(): bool | ||
MGatner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (! $this->hasHeader('Host')) { | ||
| return true; | ||
| } | ||
|
|
||
| return $this->header('Host')->getValue() === ''; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\HTTP; | ||
|
|
||
| use InvalidArgumentException; | ||
|
|
||
| /** | ||
| * Representation of an outgoing, client-side request. | ||
| * | ||
| * Corresponds to Psr7\RequestInterface. | ||
| */ | ||
| interface OutgoingRequestInterface extends MessageInterface | ||
MGatner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /** | ||
| * Get the request method. | ||
| * An extension of PSR-7's getMethod to allow casing. | ||
| * | ||
| * @param bool $upper Whether to return in upper or lower case. | ||
| * | ||
| * @deprecated The $upper functionality will be removed and this will revert to its PSR-7 equivalent | ||
| */ | ||
| public function getMethod(bool $upper = false): string; | ||
|
|
||
| /** | ||
| * Return an instance with the provided HTTP method. | ||
| * | ||
| * While HTTP method names are typically all uppercase characters, HTTP | ||
| * method names are case-sensitive and thus implementations SHOULD NOT | ||
| * modify the given string. | ||
| * | ||
| * This method MUST be implemented in such a way as to retain the | ||
| * immutability of the message, and MUST return an instance that has the | ||
| * changed request method. | ||
| * | ||
| * @param string $method Case-sensitive method. | ||
| * | ||
| * @return static | ||
| * | ||
| * @throws InvalidArgumentException for invalid HTTP methods. | ||
| */ | ||
| public function withMethod($method); | ||
|
|
||
| /** | ||
| * Retrieves the URI instance. | ||
| * | ||
| * @see http://tools.ietf.org/html/rfc3986#section-4.3 | ||
| * | ||
| * @return URI | ||
| */ | ||
| public function getUri(); | ||
|
|
||
| /** | ||
| * Returns an instance with the provided URI. | ||
| * | ||
| * This method MUST update the Host header of the returned request by | ||
| * default if the URI contains a host component. If the URI does not | ||
| * contain a host component, any pre-existing Host header MUST be carried | ||
| * over to the returned request. | ||
| * | ||
| * You can opt-in to preserving the original state of the Host header by | ||
| * setting `$preserveHost` to `true`. When `$preserveHost` is set to | ||
| * `true`, this method interacts with the Host header in the following ways: | ||
| * | ||
| * - If the Host header is missing or empty, and the new URI contains | ||
| * a host component, this method MUST update the Host header in the returned | ||
| * request. | ||
| * - If the Host header is missing or empty, and the new URI does not contain a | ||
| * host component, this method MUST NOT update the Host header in the returned | ||
| * request. | ||
| * - If a Host header is present and non-empty, this method MUST NOT update | ||
| * the Host header in the returned request. | ||
| * | ||
| * This method MUST be implemented in such a way as to retain the | ||
| * immutability of the message, and MUST return an instance that has the | ||
| * new UriInterface instance. | ||
| * | ||
| * @see http://tools.ietf.org/html/rfc3986#section-4.3 | ||
| * | ||
| * @param URI $uri New request URI to use. | ||
| * @param bool $preserveHost Preserve the original state of the Host header. | ||
| * | ||
| * @return static | ||
| */ | ||
| public function withUri(URI $uri, $preserveHost = false); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.