From 5986015358e5b7cf523122bbdb6831dccf7ca306 Mon Sep 17 00:00:00 2001 From: Horus3 Date: Mon, 11 May 2015 23:47:47 +0200 Subject: Some fixes, shows 404 page on directory not found. --- template.go | 166 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 81 insertions(+), 85 deletions(-) (limited to 'template.go') diff --git a/template.go b/template.go index c90b13c..017690d 100644 --- a/template.go +++ b/template.go @@ -4,7 +4,6 @@ import ( "html/template" "log" "net/http" - "net/http/httptest" "strings" ) @@ -14,44 +13,69 @@ type TemplateHandler struct { func (t *TemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Server", "uhttpd") + // We use our custom handler to prettify directories. Files are served via default. - if strings.HasSuffix(r.URL.Path, "/") { - r.Header.Del("If-Modified-Since") - rec := httptest.NewRecorder() + if !strings.HasSuffix(r.URL.Path, "/") { + t.handler.ServeHTTP(w, r) + return + } - defer rec.Body.Reset() + rec := NewRecorder() - // passing the recorder instead of the real ResponseWriter - t.handler.ServeHTTP(rec, r) + defer rec.Body.Reset() - // we copy the original headers first - for k, v := range rec.Header() { - w.Header()[k] = v - } + // passing the recorder instead of the real ResponseWriter + t.handler.ServeHTTP(rec, r) - // BUG: Sometimes we get a 0 byte response - if rec.Body.Len() == 0 { - log.Println("Error: Empty body. Requested:", r.URL.Path) - } + // let the standard lib handle all the caching + if rec.Code > 300 && rec.Code < 400 { + log.Println("Code: ", rec.Code) + t.handler.ServeHTTP(w, r) + return + } - // we serve a file instead of a html page - if !strings.Contains(w.Header().Get("Content-Type"), "text/html") { - w.Write(rec.Body.Bytes()) + // we copy the original headers first + for k, v := range rec.Header() { + w.Header()[k] = v + } + + // not found + if rec.Code == 404 { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(404) + tmpl := template.New("404") + tmpl, err := tmpl.Parse(get404()) + if err != nil { + log.Println(err.Error()) + w.WriteHeader(500) + w.Write([]byte(err.Error())) return } - data := rec.Body.String() - - // we serve the directoy page - if strings.HasPrefix(data, "
") {
- execTemplate(w, r, data)
- } else {
- w.Write(rec.Body.Bytes())
+ err = tmpl.Execute(w, struct{ URL string }{URL: r.URL.Path})
+ if err != nil {
+ log.Println(err.Error())
+ w.WriteHeader(500)
+ w.Write([]byte(err.Error()))
+ return
}
+ return
+ }
+ // we serve a file instead of a html page
+ if !strings.Contains(w.Header().Get("Content-Type"), "text/html") {
+ w.Write(rec.Body.Bytes())
return
+ }
+
+ data := rec.Body.String()
+
+ // we serve the directoy page
+ if strings.HasPrefix(data, "") {
+ execTemplate(w, r, data)
} else {
- t.handler.ServeHTTP(w, r)
+ w.Write(rec.Body.Bytes())
}
}
@@ -59,10 +83,9 @@ func execTemplate(w http.ResponseWriter, r *http.Request, data string) {
data = strings.Replace(data, "", "", 1)
data = strings.Replace(data, "
", "", 1)
data = strings.Replace(data, "", "
", -1)
- data = ".. (up a dir)
" + data
tmpl := template.New("page")
- tmpl, err := tmpl.Parse(renderTemplate())
+ tmpl, err := tmpl.Parse(getTemplate())
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
@@ -84,73 +107,25 @@ func NewTemplateHandler(handler http.Handler, allowedHost string) *TemplateHandl
return &TemplateHandler{handler: handler}
}
-func renderTemplate() string {
- html := `
+func getTemplate() string {
+ return `
{{.URL}}
-
+
@@ -171,6 +146,27 @@ div.icon div.chimney {
`
+}
- return html
+func get404() string {
+ return `
+
+
+
+ Not Found - {{.URL}}
+
+
+
+
+
+
+
+
+`
}
--
cgit v1.2.3