Split SimplePie\File class into HTTP client and response#815
Merged
mblaney merged 16 commits intosimplepie:masterfrom May 30, 2023
Merged
Split SimplePie\File class into HTTP client and response#815mblaney merged 16 commits intosimplepie:masterfrom
mblaney merged 16 commits intosimplepie:masterfrom
Conversation
fddcf74 to
5c4652c
Compare
5c4652c to
feae95c
Compare
feae95c to
8b22f57
Compare
Art4
reviewed
Mar 20, 2023
a35dc7e to
90ed956
Compare
jtojnar
commented
Mar 20, 2023
4e67dec to
15cac44
Compare
jtojnar
commented
Mar 20, 2023
jtojnar
commented
Mar 20, 2023
db532eb to
1fc0248
Compare
1fc0248 to
f7d8ef1
Compare
Member
Author
|
Rebased. |
f7d8ef1 to
0d1047c
Compare
0d1047c to
deeac78
Compare
Art4
reviewed
May 21, 2023
There are correctness issues as well as disorganized commit history. The fixed changes are re-applied in the commits that follow.
Dividing a number may return a float, which will cause a `TypeError` once we add type hints. The value was `1` originally, when timeouts were introduced along with `SimplePie_File` in simplepie@5bf1814 We could consider hardcoding it again but we still want to keep it proportional to the master timeout to allow people with slow connection to increase it, as introduced in simplepie@fe7e333 In fact, reducing the timeout probably does not make sense in the first place. Even if we assume majority of cached resources will not get outdated, waiting for server to reply `HTTP 304 Not Modified` can still take time (domain name resolution can be particularly slow). And if it turns out that the cached version is outdated, the web server will try to send an updated version. Having too low timeout can mean the request will be terminated, only to be started again with the original timeout. The timeout reduction was presumably done to prevent fetching a cached resource from an unresponsive server without `force_cache_fallback` from essentially doubling the timeout due to re-running the failed request. Let’s use the original timeout and prevent repeat requests instead.
- Drop unused variable - Add white space for clarity
PSR-7 `MessageInterface` expects values of each header is always a list. But concatenating multiple instances of the same header like we do now is a lossy operation so we cannot confidently split them. To allow us to use PSR-7-like semantics internally, let’s add PSR-7 mode to the HTTP parser that will avoid the imploding.
`Client` is sort of like `ClientInterface` from PSR-18 but the `request()` method expects request parameters as discrete arguments rather than wrapped in PSR-7 `RequestInterface` like `ClientInterface::sendRequest()` does. `Response` is a pared-down `ResponseInterface` from PSR-7. There are also other notable differences from PSR-7: - Method names use snake_case. - The class also provides the URI of the final HTTP request after redirects through `get_final_requested_uri()` and permanent URI of the requested resource through `get_permanent_uri()` - Body is returned as a string rather than stream in `get_body_content()`. - `get_headers()` does not have to preserve the exact case in which headers were originally specified. https://www.php-fig.org/psr/psr-7/ https://www.php-fig.org/psr/psr-18/ Acked-by: Jan Tojnar <jtojnar@gmail.com>
Implementing the PSR-7 inspired `Response` interface gets us one step closer to pluggable HTTP implementation. Most of the complexity comes from the fact that PSR-7 `MessageInterface` expects values of each header to always be a list, while `File` stored each as a single strings. But we cannot change the type of the `$headers` property without breaking backwards compatibility (especially for `File` subclasses that modify the headers), and we cannot just confidently split the header lines in the methods, since the concatenating multiple instances of the same header like `HTTP\Parser` currently does is a lossy operation. As a result, we have to check if `$headers` property changed every time we access the internal header state. Co-authored-by: Jan Tojnar <jtojnar@gmail.com>
Mostly extract repeat use of values from `File` object into local variables. This will make the changes in the next commit cleaner. Acked-by: Jan Tojnar <jtojnar@gmail.com>
f110ea7 to
4a56b21
Compare
jtojnar
commented
May 21, 2023
Member
Author
jtojnar
left a comment
There was a problem hiding this comment.
Thanks for the review, fixed.
faf8181 to
573e67d
Compare
Art4
reviewed
May 22, 2023
| return $iri->get_uri(); | ||
| } | ||
|
|
||
| public static function is_remote_uri(string $uri): bool |
Contributor
There was a problem hiding this comment.
We should add a note at the CHANGELOG.md about this new method.
Member
Author
There was a problem hiding this comment.
Good point. I have decided to mark it as internal instead to avoid increasing the API surface and having to deprecate it later.
Art4
reviewed
May 22, 2023
Acked-by: Jan Tojnar <jtojnar@gmail.com>
Introduces a trivial `Client` that just creates a `File` object. Acked-by: Jan Tojnar <jtojnar@gmail.com>
Acked-by: Jan Tojnar <jtojnar@gmail.com>
Acked-by: Jan Tojnar <jtojnar@gmail.com>
Acked-by: Jan Tojnar <jtojnar@gmail.com>
Methods should be used instead.
Acked-by: Jan Tojnar <jtojnar@gmail.com>
573e67d to
7a48ae6
Compare
Art4
approved these changes
May 22, 2023
Contributor
|
@mblaney This is ready for merge. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is #774, with few clean-ups for easier reviewing commit-by-commit (commits squashed or split to ideally fit into my head, added some implementation notes into commit messages).
The main differences as outlined in #774 (review):
Response::get_requested_uri()toResponse::get_final_requested_uri()so it is clearer what this does.Response::get_headers()to preserve header capitalization, removed mention of that from the comment.HTTP\Parserto produce PSR-7 compatible headers, using$parsed_headersinternally inFileas $774 did initially, and recomputing$parsed_headerswhen$headerschange (which should not really happen outside of tests but better safe than sorry).Misc::is_remote_uri().SimplePie::get_http_client()factory instead of side-channel signalling through$this->timeout.Locator.Full diff (will be cleaner once dependencies are merged).
Depends on:
Should be mergable without squashing (e.g. rebase).