From 533c4ca56a90f21211060e8657d986b93f9feb79 Mon Sep 17 00:00:00 2001 From: Azareal Date: Fri, 12 Jul 2019 07:51:50 +1000 Subject: [PATCH] Fix more potential superfluous header errors. Make sure we early exit out of the phrase route after pushing a not modified header. --- routes.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/routes.go b/routes.go index 0adda4a6..d78f11e8 100644 --- a/routes.go +++ b/routes.go @@ -237,7 +237,7 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout } var plist map[string]string - var doneHead = false + var notModified = false var posLoop = func(positive string) c.RouteError { // ! Constrain it to a subset of phrases for now for _, item := range phraseWhitelist { @@ -250,9 +250,8 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout ok = true w.Header().Set("ETag", etag) match := r.Header.Get("If-None-Match") - if match != "" && !doneHead && strings.Contains(match, etag) { - w.WriteHeader(http.StatusNotModified) - doneHead = true + if match != "" && strings.Contains(match, etag) { + notModified = true return nil } } @@ -273,6 +272,9 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout if rerr != nil { return rerr } + if notModified { + break + } pPhrases, ok := phrases.GetTmplPhrasesByPrefix(positive) if !ok { return c.PreErrorJS("No such prefix", w, r) @@ -292,6 +294,10 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout } plist = pPhrases } + if notModified { + w.WriteHeader(http.StatusNotModified) + return nil + } for _, negation := range negations { for name, _ := range plist {