mirror of
https://github.com/jech/galene.git
synced 2024-11-23 00:55:58 +01:00
Support custom 404 pages.
This commit is contained in:
parent
517d7edbc8
commit
243e2e8823
1 changed files with 41 additions and 4 deletions
45
webserver.go
45
webserver.go
|
@ -60,13 +60,50 @@ func mungeHeader(w http.ResponseWriter) {
|
||||||
"connect-src ws: wss: 'self'; img-src data: 'self'; default-src 'self'")
|
"connect-src ws: wss: 'self'; img-src data: 'self'; default-src 'self'")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mungeResponseWriter redirects 404 replies to our custom 404 page.
|
||||||
|
type mungeResponseWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
active bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *mungeResponseWriter) WriteHeader(status int) {
|
||||||
|
if status == http.StatusNotFound {
|
||||||
|
w.active = true
|
||||||
|
notFound(w.ResponseWriter)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.ResponseWriter.WriteHeader(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *mungeResponseWriter) Write(p []byte) (int, error) {
|
||||||
|
if w.active {
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
return w.ResponseWriter.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func notFound(w http.ResponseWriter) {
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
|
||||||
|
f, err := os.Open(path.Join(staticRoot, "404.html"))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(w, "<p>Not found</p>")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
io.Copy(w, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// mungeHandler adds our custom headers and redirects 404 replies
|
||||||
type mungeHandler struct {
|
type mungeHandler struct {
|
||||||
h http.Handler
|
h http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h mungeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h mungeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
mungeHeader(w)
|
mungeHeader(w)
|
||||||
h.h.ServeHTTP(w, r)
|
h.h.ServeHTTP(&mungeResponseWriter{ResponseWriter: w}, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseGroupName(path string) string {
|
func parseGroupName(path string) string {
|
||||||
|
@ -89,14 +126,14 @@ func groupHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
mungeHeader(w)
|
mungeHeader(w)
|
||||||
name := parseGroupName(r.URL.Path)
|
name := parseGroupName(r.URL.Path)
|
||||||
if name == "" {
|
if name == "" {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
g, err := addGroup(name, nil)
|
g, err := addGroup(name, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
} else {
|
} else {
|
||||||
log.Println("addGroup: %v", err)
|
log.Println("addGroup: %v", err)
|
||||||
http.Error(w, "Internal server error",
|
http.Error(w, "Internal server error",
|
||||||
|
@ -279,7 +316,7 @@ func recordingsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
f, err := os.Open(filepath.Join(recordingsDir, pth))
|
f, err := os.Open(filepath.Join(recordingsDir, pth))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, "server error",
|
http.Error(w, "server error",
|
||||||
http.StatusInternalServerError)
|
http.StatusInternalServerError)
|
||||||
|
|
Loading…
Reference in a new issue