summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMatt Holt <[email protected]>2017-07-25 13:33:25 -0600
committerGitHub <[email protected]>2017-07-25 13:33:25 -0600
commit1366a44639edb50e441ef4ffda49b33f9e7b0576 (patch)
treed8a399847ee5e2e1c7f0b0d195332c70f8d5178d
parentb63d9fdc6829f749257d348073c5288e151c4a89 (diff)
parentea245b5af59278d2f392fb806610691e6d0a65ca (diff)
downloadcaddy-1366a44639edb50e441ef4ffda49b33f9e7b0576.tar.gz
caddy-1366a44639edb50e441ef4ffda49b33f9e7b0576.zip
Merge pull request #1780 from sergeyfrolov/master
httpserver: Encapsulate WriteSiteNotFound error
-rw-r--r--caddyhttp/httpserver/server.go15
1 files changed, 14 insertions, 1 deletions
diff --git a/caddyhttp/httpserver/server.go b/caddyhttp/httpserver/server.go
index 3c2c0af9f..f4f22ab5a 100644
--- a/caddyhttp/httpserver/server.go
+++ b/caddyhttp/httpserver/server.go
@@ -366,7 +366,7 @@ func (s *Server) serveHTTP(w http.ResponseWriter, r *http.Request) (int, error)
if err != nil {
remoteHost = r.RemoteAddr
}
- WriteTextResponse(w, http.StatusNotFound, "No such site at "+s.Server.Addr)
+ WriteSiteNotFound(w, r) // don't add headers outside of this function
log.Printf("[INFO] %s - No such site at %s (Remote: %s, Referer: %s)",
hostname, s.Server.Addr, remoteHost, r.Header.Get("Referer"))
return 0, nil
@@ -490,6 +490,19 @@ func DefaultErrorFunc(w http.ResponseWriter, r *http.Request, status int) {
WriteTextResponse(w, status, fmt.Sprintf("%d %s\n", status, http.StatusText(status)))
}
+const httpStatusMisdirectedRequest = 421 // RFC 7540, 9.1.2
+
+// WriteSiteNotFound writes appropriate error code to w, signaling that
+// requested host is not served by Caddy on a given port.
+func WriteSiteNotFound(w http.ResponseWriter, r *http.Request) {
+ status := http.StatusNotFound
+ if r.ProtoMajor >= 2 {
+ // TODO: use http.StatusMisdirectedRequest when it gets defined
+ status = httpStatusMisdirectedRequest
+ }
+ WriteTextResponse(w, status, fmt.Sprintf("%d Site %s is not served on this interface\n", status, r.Host))
+}
+
// WriteTextResponse writes body with code status to w. The body will
// be interpreted as plain text.
func WriteTextResponse(w http.ResponseWriter, status int, body string) {