Repository navigation
fix: commit response on Flush, pass through 1xx, and clear stale error headers - #3243
Merged
Merged
Conversation
…r headers Response.Flush now commits an uncommitted response through WriteHeader, so Before hooks run, Committed is accurate, and the error handler no longer writes a body after the headers were already sent. Response.WriteHeader forwards informational status codes (1xx except 101) without committing the response, so a handler can send 103 Early Hints and still set its final status. DefaultHTTPErrorHandler removes Content-Length before writing its body, and removes Content-Encoding when no middleware wraps the response. Fixes #3240 Fixes #3241 Fixes #3242 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Both DefaultHTTPErrorHandler and ProblemDetailsHTTPErrorHandler now clear the headers that described the failed response (Content-Encoding, Content-Length, Content-Type, Content-Disposition, Content-Range, ETag, Last-Modified), whatever wraps the writer. - Response.Flush panics before committing when the writer cannot flush, so Recover can still send a 500. - headResponseWriter, delayedStatusWriter and the Gzip writer forward 1xx codes instead of treating them as the final status. - delayedStatusWriter.Flush sends its delayed status before flushing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ppers - Restore DefaultHTTPErrorHandler's doc comment, which the helper had displaced. - Keep Content-Range, which a 416 error response carries. - supportsFlush now follows Unwrap to the innermost writer, so a wrapper that defines Flush (such as delayedStatusWriter over Response) no longer commits before a flush that will panic. The check runs only before the first commit. - Close the test transport's idle connections. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ookup - Error handlers now clear only Content-Encoding, Content-Length and Content-Disposition. Content-Type and validators such as ETag can be meant for the error response (a vendor media type, a 304), so they are kept as before. - supportsFlush follows the same lookup as http.ResponseController and only looks through *Response, whose Flush just delegates. A wrapper that flushes itself is no longer rejected because the writer it wraps cannot flush. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #3240, #3241 and #3242. All three were found while reviewing #3239.
#3240:
Response.Flushcommits the responseFlushnow commits throughWriteHeaderwith the current status (default200), the same wayWritedoes.Beforehooks run,Committedis accurate, and the error handler no longer writes a body after headers were sent.Flushpanics before committing, soRecovercan still send a 500. The check uses the same writer lookup ashttp.ResponseController, except that it looks through*Response.delayedStatusWriter.Flush(used byc.JSON) now sends its pending status before flushing, soc.JSON(201, …)with a serializer that flushes no longer sends200.#3241: 1xx status codes
Response.WriteHeadersends 100–199 (except101 Switching Protocols) straight to the underlying writer. It doesn't runBeforehooks or changeStatus/Committed, so a handler can send103 Early Hintsand then its final status.headResponseWriter),delayedStatusWriterand the Gzip writer pass 1xx codes through too, instead of storing them as the final status.#3242: stale headers on error responses
DefaultHTTPErrorHandlerandProblemDetailsHTTPErrorHandlernow removeContent-Encoding,Content-LengthandContent-Dispositionbefore writing the error body. These headers always describe the body the handler meant to send, not the error body. Before, an error afterc.Attachmentdownloaded the error JSON as the attachment.Content-Type,Content-Rangeand validators (ETag,Last-Modified) are kept, because they can be meant for the error response (a vendor media type, a 416, a 304).Behavior changes
Content-Encodingbefore the body is written and compresses everything sent through it will lose that header on error responses written while it is active (for example byRequestLoggerwithHandleError: true). Echo's ownGzipsets the header only when it starts compressing, so it is not affected.HTTPErrorHandlers are unchanged. They still need to clear these headers themselves.Testing
go test -race ./...passes andmake lintis clean.🤖 Generated with Claude Code