Remove invalid code

This commit is contained in:
世界 2023-09-28 15:51:33 +08:00
parent 2f1b2199c5
commit e26096085e
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
7 changed files with 20 additions and 58 deletions

View file

@ -85,15 +85,15 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
}
host := request.Host
if len(s.host) > 0 && !common.Contains(s.host, host) {
s.fallbackRequest(request.Context(), writer, request, http.StatusBadRequest, E.New("bad host: ", host))
s.invalidRequest(writer, request, http.StatusBadRequest, E.New("bad host: ", host))
return
}
if !strings.HasPrefix(request.URL.Path, s.path) {
s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path))
s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path))
return
}
if request.Method != s.method {
s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad method: ", request.Method))
s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad method: ", request.Method))
return
}
@ -113,7 +113,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
requestBody = buf.NewSize(contentLength)
_, err := requestBody.ReadFullFrom(request.Body, contentLength)
if err != nil {
s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "read request"))
s.invalidRequest(writer, request, 0, E.Cause(err, "read request"))
return
}
}
@ -121,14 +121,15 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
writer.(http.Flusher).Flush()
conn, reader, err := h.Hijack()
if err != nil {
s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "hijack conn"))
s.invalidRequest(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, 0, E.Cause(err, "read cache"))
conn.Close()
s.invalidRequest(writer, request, 0, E.Cause(err, "read cache"))
return
}
conn = bufio.NewCachedConn(conn, cache)
@ -148,18 +149,11 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
}
}
func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter, request *http.Request, statusCode int, err error) {
conn := NewHTTPConn(request.Body, writer)
fErr := s.handler.FallbackConnection(ctx, &conn, M.Metadata{})
if fErr == nil {
return
} else if fErr == os.ErrInvalid {
fErr = nil
}
func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) {
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))
s.handler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
}
func (s *Server) Serve(listener net.Listener) error {