From f7720575d51c010809ab201576aaa81b6e0e10ad Mon Sep 17 00:00:00 2001 From: Azareal Date: Fri, 12 Jul 2019 07:38:06 +1000 Subject: [PATCH] Fix the superfluous header errors. Shorten the query parameter for phrases. Move the cache control header in the phrase route down to avoid caching errors improperly. Fixes #53 --- public/init.js | 2 +- routes.go | 23 ++++++++++------------- routes/panel/users.go | 1 - templates/create_topic.html | 2 +- templates/header.html | 2 +- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/public/init.js b/public/init.js index de3c048a..c5517920 100644 --- a/public/init.js +++ b/public/init.js @@ -185,7 +185,7 @@ function initPhrases(loggedIn, panel = false) { } function fetchPhrases(plist) { - fetch("/api/phrases/?query="+plist, {cache: "no-cache"}) + fetch("/api/phrases/?q="+plist, {cache: "no-cache"}) .then((resp) => resp.json()) .then((data) => { console.log("loaded phrase endpoint data"); diff --git a/routes.go b/routes.go index 7df42919..0adda4a6 100644 --- a/routes.go +++ b/routes.go @@ -193,22 +193,18 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout // TODO: Don't make this too JSON dependent so that we can swap in newer more efficient formats h := w.Header() h.Set("Content-Type", "application/json") - h.Set("Cache-Control", cacheControlMaxAge) //Cache-Control: max-age=31536000 err := r.ParseForm() if err != nil { return c.PreErrorJS("Bad Form", w, r) } - query := r.FormValue("query") + query := r.FormValue("q") if query == "" { return c.PreErrorJS("No query provided", w, r) } - var negations []string - var positives []string - - queryBits := strings.Split(query, ",") - for _, queryBit := range queryBits { + var negations, positives []string + for _, queryBit := range strings.Split(query, ",") { queryBit = strings.TrimSpace(queryBit) if queryBit[0] == '!' && len(queryBit) > 1 { queryBit = strings.TrimPrefix(queryBit, "!") @@ -230,6 +226,7 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout if len(positives) == 0 { return c.PreErrorJS("You haven't requested any phrases", w, r) } + h.Set("Cache-Control", cacheControlMaxAge) //Cache-Control: max-age=31536000 var etag string _, ok := w.(c.GzipResponseWriter) @@ -240,6 +237,7 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout } var plist map[string]string + var doneHead = false var posLoop = func(positive string) c.RouteError { // ! Constrain it to a subset of phrases for now for _, item := range phraseWhitelist { @@ -251,11 +249,11 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout } else { ok = true w.Header().Set("ETag", etag) - if match := r.Header.Get("If-None-Match"); match != "" { - if strings.Contains(match, etag) { - w.WriteHeader(http.StatusNotModified) - return nil - } + match := r.Header.Get("If-None-Match") + if match != "" && !doneHead && strings.Contains(match, etag) { + w.WriteHeader(http.StatusNotModified) + doneHead = true + return nil } } break @@ -309,7 +307,6 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout return c.InternalError(err, w, r) } w.Write(jsonBytes) - return nil } diff --git a/routes/panel/users.go b/routes/panel/users.go index 84f47374..093f0023 100644 --- a/routes/panel/users.go +++ b/routes/panel/users.go @@ -48,7 +48,6 @@ func UsersEdit(w http.ResponseWriter, r *http.Request, user c.User, suid string) } else if err != nil { return c.InternalError(err, w, r) } - if targetUser.IsAdmin && !user.IsAdmin { return c.LocalError("Only administrators can edit the account of an administrator.", w, r, user) } diff --git a/templates/create_topic.html b/templates/create_topic.html index ed4de7d3..de8be758 100644 --- a/templates/create_topic.html +++ b/templates/create_topic.html @@ -8,7 +8,7 @@
diff --git a/templates/header.html b/templates/header.html index 84152c4d..a2489e3f 100644 --- a/templates/header.html +++ b/templates/header.html @@ -7,7 +7,7 @@ {{range .Header.PreScriptsAsync}} {{end}} - + {{range .Header.ScriptsAsync}} {{end}}