diff --git a/application/controller/controller.go b/application/controller/controller.go index fa40045..92e70b3 100644 --- a/application/controller/controller.go +++ b/application/controller/controller.go @@ -76,14 +76,9 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/socket": err = serveController(h.socketCtl, w, r, clientLogger) - case "/index.html": - fallthrough - case "/error.html": - err = ErrNotFound - default: - if len(r.URL.Path) > 0 { - err = serveStaticData( + if len(r.URL.Path) > 0 && strings.ToUpper(r.Method) == "GET" { + err = serveStaticCacheData( r.URL.Path[1:], staticFileExt(r.URL.Path[1:]), w, diff --git a/application/controller/failure.go b/application/controller/failure.go index 0a1231d..c7e9d1c 100644 --- a/application/controller/failure.go +++ b/application/controller/failure.go @@ -29,7 +29,5 @@ func serveFailure( r *http.Request, l log.Logger, ) error { - w.WriteHeader(err.Code()) - - return serveStaticPage("error.html", ".html", w, r, l) + return serveStaticPage("error.html", ".html", err.Code(), w, r, l) } diff --git a/application/controller/home.go b/application/controller/home.go index 00daebd..dbe0a0a 100644 --- a/application/controller/home.go +++ b/application/controller/home.go @@ -29,5 +29,5 @@ type home struct { } func (h home) Get(w http.ResponseWriter, r *http.Request, l log.Logger) error { - return serveStaticPage("index.html", ".html", w, r, l) + return serveStaticCachePage("index.html", ".html", w, r, l) } diff --git a/application/controller/static.go b/application/controller/static.go index 8087fea..8e89720 100644 --- a/application/controller/static.go +++ b/application/controller/static.go @@ -20,6 +20,7 @@ package controller import ( "mime" "net/http" + "strconv" "strings" "time" @@ -48,7 +49,7 @@ func staticFileExt(fileName string) string { return strings.ToLower(fileName[extIdx:]) } -func serveStaticData( +func serveStaticCacheData( dataName string, fileExt string, w http.ResponseWriter, @@ -59,20 +60,16 @@ func serveStaticData( return ErrNotFound } - return serveStaticPage(dataName, fileExt, w, r, l) + return serveStaticCachePage(dataName, fileExt, w, r, l) } -func serveStaticPage( +func serveStaticCachePage( dataName string, fileExt string, w http.ResponseWriter, r *http.Request, l log.Logger, ) error { - if strings.ToUpper(r.Method) != "GET" { - return ErrControllerNotImplemented - } - d, dFound := staticPages[dataName] if !dFound { @@ -81,11 +78,13 @@ func serveStaticPage( selectedData := d.data selectedDataHash := d.dataHash + selectedLength := len(d.data) compressEnabled := false if clientSupportGZIP(r) && d.hasCompressed() { selectedData = d.compressd selectedDataHash = d.compressdHash + selectedLength = len(d.compressd) compressEnabled = true @@ -123,7 +122,59 @@ func serveStaticPage( w.Header().Add("Content-Encoding", "gzip") } - _, wErr := w.Write([]byte(selectedData)) + w.Header().Add("Content-Length", + strconv.FormatInt(int64(selectedLength), 10)) + + _, wErr := w.Write(selectedData) + + return wErr +} + +func serveStaticPage( + dataName string, + fileExt string, + code int, + w http.ResponseWriter, + r *http.Request, + l log.Logger, +) error { + d, dFound := staticPages[dataName] + + if !dFound { + return ErrNotFound + } + + selectedData := d.data + selectedLength := len(d.data) + compressEnabled := false + + if clientSupportGZIP(r) && d.hasCompressed() { + selectedData = d.compressd + selectedLength = len(d.compressd) + + compressEnabled = true + + w.Header().Add("Vary", "Accept-Encoding") + } + + mimeType := mime.TypeByExtension(fileExt) + + if len(mimeType) > 0 { + w.Header().Add("Content-Type", mimeType) + } else { + w.Header().Add("Content-Type", "application/binary") + } + + if compressEnabled { + w.Header().Add("Content-Encoding", "gzip") + } + + w.Header().Add("Content-Length", + strconv.FormatInt(int64(selectedLength), 10)) + + w.WriteHeader(code) + + _, wErr := w.Write(selectedData) return wErr } diff --git a/ui/error.html b/ui/error.html new file mode 100644 index 0000000..ca21579 --- /dev/null +++ b/ui/error.html @@ -0,0 +1,39 @@ + + + +
+ +