Fix write http status after response sent

This commit is contained in:
世界 2023-04-09 19:31:28 +08:00
parent 50a7295360
commit 0d7aa19cd1
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 12 additions and 6 deletions

View file

@ -92,7 +92,9 @@ func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter
} else if fErr == os.ErrInvalid {
fErr = nil
}
writer.WriteHeader(statusCode)
if statusCode > 0 {
writer.WriteHeader(statusCode)
}
s.handler.NewError(request.Context(), E.Cause(E.Errors(err, E.Cause(fErr, "fallback connection")), "process connection from ", request.RemoteAddr))
}

View file

@ -110,14 +110,14 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if h, ok := writer.(http.Hijacker); ok {
conn, reader, err := h.Hijack()
if err != nil {
s.fallbackRequest(request.Context(), writer, request, http.StatusInternalServerError, E.Cause(err, "hijack conn"))
s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "hijack conn"))
return
}
if cacheLen := reader.Reader.Buffered(); cacheLen > 0 {
cache := buf.NewSize(cacheLen)
_, err = cache.ReadFullFrom(reader.Reader, cacheLen)
if err != nil {
s.fallbackRequest(request.Context(), writer, request, http.StatusInternalServerError, E.Cause(err, "read cache"))
s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "read cache"))
return
}
conn = bufio.NewCachedConn(conn, cache)
@ -141,7 +141,9 @@ func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter
} else if fErr == os.ErrInvalid {
fErr = nil
}
writer.WriteHeader(statusCode)
if statusCode > 0 {
writer.WriteHeader(statusCode)
}
s.handler.NewError(request.Context(), E.Cause(E.Errors(err, E.Cause(fErr, "fallback connection")), "process connection from ", request.RemoteAddr))
}

View file

@ -95,7 +95,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
}
wsConn, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
s.fallbackRequest(request.Context(), writer, request, http.StatusBadRequest, E.Cause(err, "upgrade websocket connection"))
s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "upgrade websocket connection"))
return
}
var metadata M.Metadata
@ -115,7 +115,9 @@ func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter
} else if fErr == os.ErrInvalid {
fErr = nil
}
writer.WriteHeader(statusCode)
if statusCode > 0 {
writer.WriteHeader(statusCode)
}
s.handler.NewError(request.Context(), E.Cause(E.Errors(err, E.Cause(fErr, "fallback connection")), "process connection from ", request.RemoteAddr))
}