Cleaned up responsewriter and request pointer handling, and added return values when missing after 404 responses

This commit is contained in:
Seednode 2022-10-03 21:35:02 -05:00
parent 919d1c2e8b
commit 84038440af
3 changed files with 15 additions and 10 deletions

View File

@ -211,8 +211,6 @@ func prepareDirectory(directory []string) []string {
_, last := filepath.Split(directory[len(directory)-1]) _, last := filepath.Split(directory[len(directory)-1])
last = cleanFilename(last) last = cleanFilename(last)
fmt.Printf("Comparing %v to %v\n", first, last)
if first == last { if first == last {
d := append([]string{}, directory[0]) d := append([]string{}, directory[0])
return d return d

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.12.4" var Version = "0.12.5"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)

View File

@ -24,7 +24,7 @@ func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
const LOGDATE string = "2006-01-02T15:04:05.000000000-07:00" const LOGDATE string = "2006-01-02T15:04:05.000-07:00"
const PREFIX string = "/src" const PREFIX string = "/src"
func stripQueryParam(inUrl string) (string, error) { func stripQueryParam(inUrl string) (string, error) {
@ -48,7 +48,7 @@ func refererToUri(referer string) string {
return "/" + parts[3] return "/" + parts[3]
} }
func serveHtml(w http.ResponseWriter, r http.Request, filePath string) error { func serveHtml(w http.ResponseWriter, r *http.Request, filePath string) error {
fileName := filepath.Base(filePath) fileName := filepath.Base(filePath)
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
@ -75,7 +75,7 @@ func serveHtml(w http.ResponseWriter, r http.Request, filePath string) error {
return nil return nil
} }
func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) error { func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) error {
strippedUrl, err := stripQueryParam(r.URL.Path) strippedUrl, err := stripQueryParam(r.URL.Path)
if err != nil { if err != nil {
return err return err
@ -89,7 +89,7 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro
filePath := strings.TrimPrefix(prefixedFilePath, PREFIX) filePath := strings.TrimPrefix(prefixedFilePath, PREFIX)
if !pathIsValid(filePath, paths) { if !pathIsValid(filePath, paths) {
http.NotFound(w, &r) http.NotFound(w, r)
} }
exists, err := fileExists(filePath) exists, err := fileExists(filePath)
@ -98,7 +98,9 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro
} }
if !exists { if !exists {
http.NotFound(w, &r) http.NotFound(w, r)
return nil
} }
var startTime time.Time var startTime time.Time
@ -123,7 +125,7 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro
func serveStaticFileHandler(paths []string) appHandler { func serveStaticFileHandler(paths []string) appHandler {
return func(w http.ResponseWriter, r *http.Request) error { return func(w http.ResponseWriter, r *http.Request) error {
err := serveStaticFile(w, *r, paths) err := serveStaticFile(w, r, paths)
if err != nil { if err != nil {
return err return err
} }
@ -159,6 +161,7 @@ func serveHtmlHandler(paths []string) appHandler {
switch { switch {
case err != nil && err == ErrNoImagesFound: case err != nil && err == ErrNoImagesFound:
http.NotFound(w, r) http.NotFound(w, r)
return nil return nil
case err != nil: case err != nil:
return err return err
@ -176,6 +179,7 @@ func serveHtmlHandler(paths []string) appHandler {
filePath, err := pickFile(paths, filter, successive) filePath, err := pickFile(paths, filter, successive)
if err != nil && err == ErrNoImagesFound { if err != nil && err == ErrNoImagesFound {
http.NotFound(w, r) http.NotFound(w, r)
return nil return nil
} else if err != nil { } else if err != nil {
return err return err
@ -192,6 +196,7 @@ func serveHtmlHandler(paths []string) appHandler {
filePath, err := pickFile(paths, filter, successive) filePath, err := pickFile(paths, filter, successive)
if err != nil && err == ErrNoImagesFound { if err != nil && err == ErrNoImagesFound {
http.NotFound(w, r) http.NotFound(w, r)
return nil return nil
} else if err != nil { } else if err != nil {
return err return err
@ -208,9 +213,11 @@ func serveHtmlHandler(paths []string) appHandler {
} }
if !image { if !image {
http.NotFound(w, r) http.NotFound(w, r)
return nil
} }
err = serveHtml(w, *r, filePath) err = serveHtml(w, r, filePath)
if err != nil { if err != nil {
return err return err
} }