Added HTTP 500 response on all failed requests, instead of not sending any response

This commit is contained in:
Seednode 2023-06-20 10:24:12 -05:00
parent 7ca86c19fd
commit 40ed9889f9
2 changed files with 60 additions and 6 deletions

View File

@ -11,7 +11,7 @@ import (
) )
const ( const (
Version string = "0.52.2" Version string = "0.53.0"
) )
var ( var (

View File

@ -12,7 +12,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"math/rand" "math/rand"
"net" "net"
"net/http" "net/http"
@ -356,7 +355,7 @@ func (s *ServeStats) Export(path string) error {
err = enc.Encode(&stats) err = enc.Encode(&stats)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
return nil return nil
@ -429,7 +428,30 @@ func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
return nil 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{}) { return func(w http.ResponseWriter, r *http.Request, i interface{}) {
startTime := time.Now() startTime := time.Now()
@ -620,7 +642,11 @@ func serveStats(args []string, stats *ServeStats) httprouter.Handle {
first, last, err := stats.GetHistory() first, last, err := stats.GetHistory()
if err != nil { if err != nil {
log.Fatal(err) fmt.Println(err)
serverError(w, r)
return
} }
most := stats.GetMostServed() most := stats.GetMostServed()
@ -633,6 +659,8 @@ func serveStats(args []string, stats *ServeStats) httprouter.Handle {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
@ -723,6 +751,10 @@ func serveDebugJson(args []string, index *Index) httprouter.Handle {
response, err := json.MarshalIndent(indexDump, "", " ") response, err := json.MarshalIndent(indexDump, "", " ")
if err != nil { if err != nil {
fmt.Println(err)
serverError(w, r)
return return
} }
@ -747,6 +779,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
@ -754,6 +788,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
@ -767,6 +803,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
@ -782,6 +820,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
@ -811,6 +851,8 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
@ -832,6 +874,8 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
} }
@ -857,6 +901,8 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle
case err != nil: case err != nil:
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
} }
@ -891,6 +937,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
if !exists { if !exists {
@ -903,6 +951,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
@ -919,6 +969,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
} }
@ -966,6 +1018,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
serverError(w, r)
return return
} }
} }
@ -1031,7 +1085,7 @@ func ServePage(args []string) error {
mux := httprouter.New() mux := httprouter.New()
mux.PanicHandler = serverError() mux.PanicHandler = serverErrorHandler()
if cache { if cache {
skipIndex := false skipIndex := false