Browse Source

improve not found response (#3121)

fatedier 2 years ago
parent
commit
6a71d71e58
3 changed files with 10 additions and 9 deletions
  1. 0 1
      .golangci.yml
  2. 1 1
      pkg/util/vhost/http.go
  3. 9 7
      pkg/util/vhost/resource.go

+ 0 - 1
.golangci.yml

@@ -48,7 +48,6 @@ linters:
   - unconvert
   - unparam
   - gci
-  - bodyclose
   - gosec
   - asciicheck
   - prealloc

+ 1 - 1
pkg/util/vhost/http.go

@@ -251,7 +251,7 @@ func (rp *HTTPReverseProxy) connectHandler(rw http.ResponseWriter, req *http.Req
 
 	remote, err := rp.CreateConnection(domain, url, routeByHTTPUser, remoteAddr)
 	if err != nil {
-		http.Error(rw, "Failed", http.StatusBadRequest)
+		_ = notFoundResponse().Write(client)
 		client.Close()
 		return
 	}

+ 9 - 7
pkg/util/vhost/resource.go

@@ -72,14 +72,16 @@ func notFoundResponse() *http.Response {
 	header.Set("server", "frp/"+version.Full())
 	header.Set("Content-Type", "text/html")
 
+	content := getNotFoundPageContent()
 	res := &http.Response{
-		Status:     "Not Found",
-		StatusCode: 404,
-		Proto:      "HTTP/1.0",
-		ProtoMajor: 1,
-		ProtoMinor: 0,
-		Header:     header,
-		Body:       io.NopCloser(bytes.NewReader(getNotFoundPageContent())),
+		Status:        "Not Found",
+		StatusCode:    404,
+		Proto:         "HTTP/1.1",
+		ProtoMajor:    1,
+		ProtoMinor:    1,
+		Header:        header,
+		Body:          io.NopCloser(bytes.NewReader(content)),
+		ContentLength: int64(len(content)),
 	}
 	return res
 }