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.
Description
This PR introduces a new "Response Writer Safety" feature to address potential panics that can arise when the router or its middlewares make assumptions about the capabilities of custom
http.ResponseWriter
implementations.Background
Certain middlewares or handlers, like
http.TimeoutHandler
, wrap the originalhttp.ResponseWriter
and might return a new writer that omits certain interfaces, such ashttp.Flusher
. Routers likeFox
,Gin
, andEcho
traditionally assume these interfaces to be present. The absence of these interfaces can lead to undefined behavior or even cause a panic under specific circumstances.For example, the following implementation with
httputil.ReverseProxy
will result in a panic forFox
,Echo
andGin
.When
httputil.ReverseProxy
checks if the provided writer implementshttp.Flusher
and the check succeeds (because it use the writer from, say,Echo
orGin
), it will attempt to use it (based on the number of bytes already written). But since the underlying writer (fromhttp.TimeoutHandler
) doesn't actually support this, a panic occurs.Introduction of the
WithWriterSafety
Option:This PR introduces an option called
WithWriterSafety
to ensure more predictable behavior when working with differenthttp.ResponseWriter
implementations.Behavior
Safe Mode (
WithWriterSafety
Enabled):The router derives the protocol from the request's
ProtoMajor
and conducts explicit type assertions on the providedhttp.ResponseWriter
. This ensures compatibility across various writer implementations and safeguards against unpredictable outcomes.Optimistic Mode (Default Behavior without
WithWriterSafety
):The router operates under an optimistic assumption that the provided
http.ResponseWriter
fully supports the necessary interfaces for the request's protocol. Specifically, for HTTP/1.x requests, the writer should implementhttp.Flusher
,http.Hijacker
, andio.ReaderFrom
. For HTTP/2 requests, the writer should supporthttp.Flusher
andhttp.Pusher
.Example
Peformance
By default, the router operates in a mode optimized for performance, which may potentially be less safe. Benchmarks show a performance drop of approximately 30% when switching to Safe Mode due to the multiple interface assertions ensuring safety. Nonetheless, even in this mode, Fox remains as performant as Gin and Echo in most scenarios. As the Fox ecosystem's middleware ensures the necessary interfaces are satisfied, the more performant mode has been chosen as the default. However, for added safety, users can opt-in to the safer mode.