Added HTTP 500 response on all failed requests, instead of not sending any response
This commit is contained in:
parent
7ca86c19fd
commit
40ed9889f9
|
@ -11,7 +11,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
Version string = "0.52.2"
|
||||
Version string = "0.53.0"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
64
cmd/web.go
64
cmd/web.go
|
@ -12,7 +12,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
|
@ -356,7 +355,7 @@ func (s *ServeStats) Export(path string) error {
|
|||
|
||||
err = enc.Encode(&stats)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -429,7 +428,30 @@ func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func serverError() func(http.ResponseWriter, *http.Request, interface{}) {
|
||||
func serverError(w http.ResponseWriter, r *http.Request) {
|
||||
startTime := time.Now()
|
||||
|
||||
if verbose {
|
||||
fmt.Printf("%s | Invalid request for %s from %s\n",
|
||||
startTime.Format(LogDate),
|
||||
r.URL.Path,
|
||||
r.RemoteAddr,
|
||||
)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
|
||||
var htmlBody strings.Builder
|
||||
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
|
||||
htmlBody.WriteString(`<style>a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;}</style>`)
|
||||
htmlBody.WriteString(`<title>Server Error</title></head>`)
|
||||
htmlBody.WriteString(`<body><a href="/">500 Internal Server Error</a></body></html>`)
|
||||
|
||||
io.WriteString(w, gohtml.Format(htmlBody.String()))
|
||||
}
|
||||
|
||||
func serverErrorHandler() func(http.ResponseWriter, *http.Request, interface{}) {
|
||||
return func(w http.ResponseWriter, r *http.Request, i interface{}) {
|
||||
startTime := time.Now()
|
||||
|
||||
|
@ -620,7 +642,11 @@ func serveStats(args []string, stats *ServeStats) httprouter.Handle {
|
|||
|
||||
first, last, err := stats.GetHistory()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
most := stats.GetMostServed()
|
||||
|
@ -633,6 +659,8 @@ func serveStats(args []string, stats *ServeStats) httprouter.Handle {
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -723,6 +751,10 @@ func serveDebugJson(args []string, index *Index) httprouter.Handle {
|
|||
|
||||
response, err := json.MarshalIndent(indexDump, "", " ")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -747,6 +779,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -754,6 +788,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -767,6 +803,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -782,6 +820,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -811,6 +851,8 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -832,6 +874,8 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -857,6 +901,8 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle
|
|||
case err != nil:
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -891,6 +937,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
|
@ -903,6 +951,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -919,6 +969,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -966,6 +1018,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
|
|||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
||||
serverError(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -1031,7 +1085,7 @@ func ServePage(args []string) error {
|
|||
|
||||
mux := httprouter.New()
|
||||
|
||||
mux.PanicHandler = serverError()
|
||||
mux.PanicHandler = serverErrorHandler()
|
||||
|
||||
if cache {
|
||||
skipIndex := false
|
||||
|
|
Loading…
Reference in New Issue