Repository navigation
Conversation
Accept's rejection responses had no test that checked the status, headers and body together. Pinning them for every rejection before the 101 lets a change to how the response is written prove that the default output stays the same.
Accept always wrote a plain text http.Error response when it rejected a handshake, so servers with a structured error format had to write a second response, which net/http appends to Accept's body and logs as a superfluous WriteHeader call. ErrorHandler lets the caller write that response instead, with the same signature as gorilla's Upgrader.Error; nil keeps the previous output. Refs #545
mafredri
force-pushed
the
mafredri/accept-error-handler
branch
from
October 30, 2026 15:18
ff05d69 to
f154711
Compare
This branch has not been deployed
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.
Acceptwrites a plain texthttp.Errorresponse when it rejects a handshake request. A server that needs a structured error body, such as RFC 9457 JSON (#545), cannot replace that response. A second write afterAcceptreturns only appends to the plain text body: the status andtext/plaincontent type stayAccept's, and net/http logs a superfluousWriteHeadercall.AcceptOptions.ErrorHandler func(w http.ResponseWriter, r *http.Request, status int, err error)writes that response instead. It receives the statusAcceptchose and the errorAcceptreturns, before wrapping.w.Header()already holds the headersAcceptset, such asSec-WebSocket-Version: 13for an unsupported version. It is called for every rejection before the 101 response: request validation, origin, and aResponseWriterwithouthttp.Hijacker. It is not called when hijacking fails, becauseAccepthas already written 101 Switching Protocols. A handler that writes nothing leaves net/http's default 200 OK with an empty body.A nil
ErrorHandlerkeeps the previous response. TheAcceptdoc now says not to write towafterAcceptreturns an error.Tests cover the handler for all 11 rejections. A separate test pins the default status, headers and body for all 11 and passes on master. For four rejections, the raw responses match master byte for byte, excluding
Date.Fixes #545
Decision log
r, so one handler shared across requests can use it, for exampler.Context(). Allow customize how HTTP errors are rendered whenwebsocket.Acceptfails to upgrade #545 discussed capturing the request in a closure instead, which does not work for shared options.websocket.Acceptfails to upgrade #545, Support disabling error writing via AcceptOptions #152).OriginPatternsentry the handler receives "Forbidden", asAcceptreturns today; the pattern error is still only logged.http.Hijacker, the handler receives the descriptive error, while the nil default body stays "Not Implemented".recoveraround the handler: a panic behaves like any other handler panic in net/http.AcceptOptionsstub so shared code compiles.