mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-07 11:28:48 +03:00
Encapsulate WriteSiteNotFound error
This commit is contained in:
parent
b63d9fdc68
commit
ea245b5af5
1 changed files with 14 additions and 1 deletions
|
@ -366,7 +366,7 @@ func (s *Server) serveHTTP(w http.ResponseWriter, r *http.Request) (int, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remoteHost = r.RemoteAddr
|
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)",
|
log.Printf("[INFO] %s - No such site at %s (Remote: %s, Referer: %s)",
|
||||||
hostname, s.Server.Addr, remoteHost, r.Header.Get("Referer"))
|
hostname, s.Server.Addr, remoteHost, r.Header.Get("Referer"))
|
||||||
return 0, nil
|
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)))
|
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
|
// WriteTextResponse writes body with code status to w. The body will
|
||||||
// be interpreted as plain text.
|
// be interpreted as plain text.
|
||||||
func WriteTextResponse(w http.ResponseWriter, status int, body string) {
|
func WriteTextResponse(w http.ResponseWriter, status int, body string) {
|
||||||
|
|
Loading…
Reference in a new issue