-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_server.go
45 lines (37 loc) · 1.02 KB
/
static_server.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
41
42
43
44
45
package web_container_proxy
import (
"errors"
"net/http"
)
type StaticServer struct {
handler http.Handler
hasCustom404 bool
custom404 string
}
func (server *StaticServer) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
handler := StaticHandler{}
handler.request = request
handler.ResponseWriter = writer
handler.server = server
server.handler.ServeHTTP(&handler, request)
}
type StaticHandler struct {
http.ResponseWriter
request *http.Request
server *StaticServer
is404 bool
}
func (handler *StaticHandler) WriteHeader(n int) {
if n == http.StatusNotFound && handler.server.hasCustom404 {
handler.ResponseWriter.Header().Set("Content-Type", "text/html")
http.ServeFile(handler.ResponseWriter, handler.request, handler.server.custom404)
return
}
handler.ResponseWriter.WriteHeader(n)
}
func (handler *StaticHandler) Write(b []byte) (int, error) {
if handler.is404 {
return 0, errors.New("404 status written, will serve custom 404 page")
}
return handler.ResponseWriter.Write(b)
}