From c09d870b409feb4a445161894e7cfc0d5f009c54 Mon Sep 17 00:00:00 2001 From: yequari Date: Sun, 30 Jul 2023 01:06:38 -0700 Subject: [PATCH] clean referer URLs --- cmd/web/handlers.go | 9 ++++++--- cmd/web/helpers.go | 5 ++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 91199bc..5dc9566 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -83,8 +83,9 @@ func (app *application) siteEntryCreate(w http.ResponseWriter, r *http.Request) func (app *application) nextSiteEntry(w http.ResponseWriter, r *http.Request) { ref := app.cleanUrl(r.Referer()) - refSite, err := app.siteEntries.GetByUrl(ref) - app.infoLog.Printf("REFERER %s\n", ref) + cleanUrl := app.cleanUrl(ref) + app.infoLog.Printf("REFERER %s. CLEAN %s\n", ref, cleanUrl) + refSite, err := app.siteEntries.GetByUrl(cleanUrl) if err != nil { app.serverError(w, err) return @@ -102,7 +103,9 @@ func (app *application) nextSiteEntry(w http.ResponseWriter, r *http.Request) { func (app *application) prevSiteEntry(w http.ResponseWriter, r *http.Request) { ref := app.cleanUrl(r.Referer()) - refSite, err := app.siteEntries.GetByUrl(ref) + cleanUrl := app.cleanUrl(ref) + app.infoLog.Printf("REFERER %s. CLEAN %s\n", ref, cleanUrl) + refSite, err := app.siteEntries.GetByUrl(cleanUrl) if err != nil { app.serverError(w, err) return diff --git a/cmd/web/helpers.go b/cmd/web/helpers.go index 25d2b22..8d38d5f 100644 --- a/cmd/web/helpers.go +++ b/cmd/web/helpers.go @@ -25,10 +25,9 @@ func (app *application) notFound(w http.ResponseWriter) { } func (app *application) cleanUrl(url string) string { - s, _ := strings.CutPrefix(url, "http://") - s, _ = strings.CutPrefix(s, "https://") + s := strings.TrimPrefix(url, "http://") + s = strings.TrimPrefix(s, "https://") s = path.Base(path.Clean(s)) - s = "http://" + s return s }