-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: net/http: decide and document accepted HTTP methods in FileServer #59470
Comments
CC @neild |
I would still support a change to reject methods other than GET, HEAD, POST. This is the kind of protocol compliance change that would be good to use a GODEBUG to control and roll out. |
It's surprising that
On the other hand, the negative impact of responding to a DELETE request with the contents of a file seems low. I certainly agree that this is surprising, but "broken" may be a bit much. Perhaps this is a good motivation to consider whether we should have an easier way to register method-specific handlers on a
|
Method handling would be a more thorough solution indeed. Even the examples from the |
Whatever we do, we should consider ServeFile and ServeContent as well as FileServer. |
Change https://go.dev/cl/482876 mentions this issue: |
ServeFile and FileServer will respond to methods such as DELETE by serving the file contents. This is surprising, but we don't want to change it without some consideration. Add tests covering the current behavior. For #59470 Change-Id: Ib6a2594c5b2b7f380149fc1628f7204b308161e1 Reviewed-on: https://go-review.googlesource.com/c/go/+/482876 Run-TryBot: Damien Neil <dneil@google.com> Reviewed-by: Tatiana Bradley <tatianabradley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
I don't think ServeFile or ServeContent should be changed. I see these as lower-level functions, used to compose handlers (they are not themselves handlers). For example, as part of a handler that processes HTTP DELETE requests, you may want to return some static response from a file: FileServer is different. It returns a handler, and it might make sense to return a high-level handler that rejects methods such as DELETE. Or it could continue to be treated as a low-level primitive (more like ServeFile), that doesn't decide HTTP method policies itself. The documentation example for FileServer, Similar to @neild's ServeMux idea above, another option would be a wrapper handler (similar to http.TimeoutHandler, etc.), that is something like: func AllowMethods(h Handler, methods ...string) Then the FileServer example would become: And the ServeMux idea might be more flexible, because you could add different handlers for different methods (one for But if most people use FileServer as a high-level handler, it might be more convenient for them if it were changed to reject DELETEs, etc. (Though I don't see much harm in FileServer just ignoring the method, and serving files regardless. It follows the "be liberal in what you accept" robustness principle). |
That principle is not universally held. |
Now that the fix got reversed, the documentation should at least inform/warn about it's broken by default.
The text was updated successfully, but these errors were encountered: