Repository navigation
feat: request header/raw body access and StaticRouter fixes #19
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
12 commits
Select commit
Hold shift + click to select a range
d151a48
test(request): cover header access from server globals and REST
abdul-kaioum 74f2f37
feat(request): read request headers via InteractsWithHeaders
abdul-kaioum 2898289
test(request): cover raw body access
abdul-kaioum 17a5fe3
feat(request): expose the raw body via content()
abdul-kaioum a18d570
test(request): require sanitized headers, method and content type
abdul-kaioum f2bc9d2
fix(request): sanitize headers, method and content type by default
abdul-kaioum 8ea28ab
test(router): cover static routing on real-world installs
abdul-kaioum 851a497
fix(router): make StaticRouter work on subdirectory installs
abdul-kaioum 2fe0f84
test(router): cover StaticRouter edge cases
abdul-kaioum 0d709ec
test(router): require deactivation to drop static rewrite rules
abdul-kaioum de9fc49
fix(router): clear persisted rules on deactivation instead of flushing
abdul-kaioum 1e755af
refactor(request): match content type prefix with str_starts_with
abdul-kaioum 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
|
|
||
| namespace BitApps\WPKit\Http\Request; | ||
|
|
||
| use WP_REST_Request; | ||
|
|
||
| /** | ||
| * Header access for a request: the hydrated WP_REST_Request when the host sets `$rest`, else the server globals. | ||
| */ | ||
| trait InteractsWithHeaders | ||
| { | ||
| /** | ||
| * CGI exposes these headers without the HTTP_ prefix. | ||
| */ | ||
| private static array $_unprefixedHeaders = ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5']; | ||
|
|
||
| /** | ||
| * Returns every request header, sanitized, keyed by canonical name (lowercase, underscores). | ||
| * | ||
| * @return array<string, string> | ||
| */ | ||
| public function headers() | ||
| { | ||
| return array_map('sanitize_text_field', $this->rawHeaders()); | ||
| } | ||
|
|
||
| /** | ||
| * Returns one sanitized header by any name style ("X-Token" or "x_token"), or the default when absent. | ||
| * | ||
| * @return null|string | ||
| */ | ||
| public function header(string $name, ?string $default = null) | ||
| { | ||
| $value = $this->rawHeader($name); | ||
|
|
||
| return $value === null ? $default : sanitize_text_field($value); | ||
| } | ||
|
|
||
| /** | ||
| * Returns one header exactly as sent, for signature or token checks only; never output or store it unsanitized. | ||
| * | ||
| * @return null|string | ||
| */ | ||
| public function rawHeader(string $name, ?string $default = null) | ||
| { | ||
| return $this->rawHeaders()[self::canonicalHeaderName($name)] ?? $default; | ||
| } | ||
|
|
||
| /** | ||
| * Reports whether the request carries the header. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function hasHeader(string $name) | ||
| { | ||
| return \array_key_exists(self::canonicalHeaderName($name), $this->rawHeaders()); | ||
| } | ||
|
|
||
| /** | ||
| * Collects unsanitized headers from the REST request when hydrated, else from the server globals. | ||
| * | ||
| * @return array<string, string> | ||
| */ | ||
| private function rawHeaders(): array | ||
| { | ||
| if (isset($this->rest) && $this->rest instanceof WP_REST_Request) { | ||
| return array_map(static fn ($values) => implode(', ', (array) $values), (array) $this->rest->get_headers()); | ||
| } | ||
|
|
||
| return $this->serverHeaders(); | ||
| } | ||
|
|
||
| /** | ||
| * Collects headers from $_SERVER, which WordPress slashes. | ||
| * | ||
| * @return array<string, string> | ||
| */ | ||
| private function serverHeaders(): array | ||
| { | ||
| $headers = []; | ||
| foreach ($_SERVER as $key => $value) { | ||
| if (!\is_string($value)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (str_starts_with($key, 'HTTP_')) { | ||
| $headers[self::canonicalHeaderName(substr($key, 5))] = wp_unslash($value); | ||
| } elseif ($value !== '' && \in_array($key, self::$_unprefixedHeaders, true)) { | ||
| // FastCGI passes CONTENT_* as '' when the request has no body. | ||
| $headers[self::canonicalHeaderName($key)] = wp_unslash($value); | ||
| } | ||
| } | ||
|
|
||
| return $headers; | ||
| } | ||
|
|
||
| /** | ||
| * Normalizes a header name the way WP_REST_Request does. | ||
| */ | ||
| private static function canonicalHeaderName(string $name): string | ||
| { | ||
| return str_replace('-', '_', strtolower($name)); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.