forked from DHowett/spectre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
404.go
40 lines (34 loc) · 931 Bytes
/
404.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"bytes"
"net/http"
)
type fourOhFourConsumerWriter struct {
http.ResponseWriter
statusCode int
tripped bool
}
func (w *fourOhFourConsumerWriter) WriteHeader(status int) {
w.statusCode = status
if status == http.StatusNotFound {
w.ResponseWriter.Header().Set("Content-Type", "text/html; charset=utf-8")
}
w.ResponseWriter.WriteHeader(status)
}
func (w *fourOhFourConsumerWriter) Write(p []byte) (int, error) {
if w.statusCode == http.StatusNotFound && bytes.Equal(p, []byte("404 page not found\n")) {
w.tripped = true
return len(p), nil
}
return w.ResponseWriter.Write(p)
}
type fourOhFourConsumerHandler struct {
http.Handler
}
func (h *fourOhFourConsumerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
writer := &fourOhFourConsumerWriter{ResponseWriter: w}
h.Handler.ServeHTTP(writer, r)
if writer.tripped {
RenderPage(writer.ResponseWriter, r, "404", nil)
}
}