Add isSafe and isIdempotent to HTTPRequest.Method#129
Open
tkshsbcue wants to merge 1 commit into
Open
Conversation
RFC 9110 classifies request methods by two properties that callers frequently need: whether a method is "safe" (read-only semantics) and whether it is "idempotent" (repeating the request has the same effect as making it once). These properties drive common decisions such as whether a failed request may be retried automatically or whether a response may be cached. The type previously carried an internal `isSafe` helper that was never referenced anywhere, and it also misclassified TRACE, which RFC 9110 defines as safe. Replace it with public `isSafe` and `isIdempotent` computed properties: - `isSafe` is true for GET, HEAD, OPTIONS, TRACE (and the QUERY draft method that the type already models). - `isIdempotent` is true for PUT and DELETE in addition to every safe method. The properties are derived from the method token, so they work for methods constructed from arbitrary strings as well as the provided static members, and unknown methods are conservatively reported as neither safe nor idempotent. https://www.rfc-editor.org/rfc/rfc9110.html#name-safe-methods https://www.rfc-editor.org/rfc/rfc9110.html#name-idempotent-methods
Author
|
@guoye-zhang hope you can review my PR |
Contributor
|
This API addition will require more discussions. I'm not certain that exposing safe and idempotent as read-only properties is correct. HTTP method is extensible and this prevents users from defining new idempotent methods. However, adding a new stored property is also tricky since it will complicate equality and coding, and the info is not sent over the wire. Should we allow someone to set POST as idempotent? For now I think the decision of what's safe/idempotent should be made by the HTTP loader. |
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.
Motivation
RFC 9110 classifies request methods by two properties that HTTP clients and servers frequently need:
These drive everyday decisions like "may I automatically retry this failed request?" and "is this response cacheable?". Today a user has to hand-maintain their own set of method names to answer them.
What this does
HTTPRequest.Methodalready carried an internalisSafehelper, but it was:TRACE, which RFC 9110 defines as safe.This replaces it with two public, documented computed properties:
trueforisSafeGET,HEAD,OPTIONS,TRACE(and theQUERYdraft method the type already models)isIdempotentPUTandDELETEBecause the classification is derived from the method token, it works for methods built from arbitrary strings (
HTTPRequest.Method("GET")) as well as the static members. Unknown methods are conservatively reported as neither safe nor idempotent.Testing
Adds
testMethodSafetyandtestMethodSafetyFromStringcovering the safe, idempotent-but-unsafe, and neither buckets, plus classification of methods constructed from strings. Full suite (22 tests) passes.Notes
isSafewasinternaland unused, so nothing in the package depended on it.References: