From 55afaa87da5e4e3a061d0b8875df55c58784e433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 9 Apr 2023 19:31:28 +0800 Subject: [PATCH] Fix write http status after response sent --- transport/v2raygrpclite/server.go | 4 +++- transport/v2rayhttp/server.go | 8 +++++--- transport/v2raywebsocket/server.go | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/transport/v2raygrpclite/server.go b/transport/v2raygrpclite/server.go index a9c57fdd..c3c62232 100644 --- a/transport/v2raygrpclite/server.go +++ b/transport/v2raygrpclite/server.go @@ -93,7 +93,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)) } diff --git a/transport/v2rayhttp/server.go b/transport/v2rayhttp/server.go index 95288631..bc81c50b 100644 --- a/transport/v2rayhttp/server.go +++ b/transport/v2rayhttp/server.go @@ -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)) } diff --git a/transport/v2raywebsocket/server.go b/transport/v2raywebsocket/server.go index 6d542572..b669bdd1 100644 --- a/transport/v2raywebsocket/server.go +++ b/transport/v2raywebsocket/server.go @@ -96,7 +96,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 @@ -116,7 +116,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)) }