Problem: http status codes use integer constants

Solution: replace http status codes with symbolic constants
This commit is contained in:
Peter Stuifzand 2021-11-20 21:32:19 +01:00
parent 9c8cec2c5b
commit fd3a246f0d
Signed by: peter
GPG Key ID: 374322D56E5209E8
5 changed files with 51 additions and 51 deletions

View File

@ -292,7 +292,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Println(err)
http.Error(w, fmt.Sprintf("Bad Request: %s", err.Error()), 400)
http.Error(w, fmt.Sprintf("Bad Request: %s", err.Error()), http.StatusBadRequest)
return
}
@ -317,10 +317,10 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if r.URL.Path == "/session/callback" {
c, err := r.Cookie("session")
if err == http.ErrNoCookie {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
} else if err != nil {
http.Error(w, "could not read cookie", 500)
http.Error(w, "could not read cookie", http.StatusInternalServerError)
return
}
@ -342,9 +342,9 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
saveSession(sessionVar, &sess, conn)
log.Printf("SESSION: %#v\n", sess)
if sess.NextURI != "" {
http.Redirect(w, r, sess.NextURI, 302)
http.Redirect(w, r, sess.NextURI, http.StatusFound)
} else {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
}
return
}
@ -352,7 +352,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if r.URL.Path == "/settings/channel" {
c, err := r.Cookie("session")
if err == http.ErrNoCookie {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
}
sessionVar := c.Value
@ -423,7 +423,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if r.URL.Path == "/logs" {
c, err := r.Cookie("session")
if err == http.ErrNoCookie {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
}
sessionVar := c.Value
@ -451,7 +451,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if r.URL.Path == "/settings" {
c, err := r.Cookie("session")
if err == http.ErrNoCookie {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
}
sessionVar := c.Value
@ -499,7 +499,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !isLoggedIn(h.Backend, &sess) {
sess.NextURI = r.URL.String()
saveSession(sessionVar, &sess, conn)
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
}
@ -563,7 +563,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/session" {
c, err := r.Cookie("session")
if err == http.ErrNoCookie {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
}
@ -574,7 +574,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
endpoints, err := getEndpoints(me)
if err != nil {
http.Error(w, fmt.Sprintf("Bad Request: %s, %s", err.Error(), me), 400)
http.Error(w, fmt.Sprintf("Bad Request: %s, %s", err.Error(), me), http.StatusBadRequest)
return
}
@ -584,7 +584,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sess, err := loadSession(sessionVar, conn)
if err != nil {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
}
@ -596,12 +596,12 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err = saveSession(sessionVar, &sess, conn)
if err != nil {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
}
authenticationURL := indieauth.CreateAuthenticationURL(*endpoints.AuthorizationEndpoint, endpoints.Me.String(), h.BaseURL, redirectURI, state)
http.Redirect(w, r, authenticationURL, 302)
http.Redirect(w, r, authenticationURL, http.StatusFound)
return
} else if r.URL.Path == "/session/logout" {
@ -654,7 +654,7 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
redirectURI.RawQuery = q.Encode()
log.Println(redirectURI)
http.Redirect(w, r, redirectURI.String(), 302)
http.Redirect(w, r, redirectURI.String(), http.StatusFound)
return
} else if r.URL.Path == "/auth/token" {
grantType := r.FormValue("grant_type")
@ -726,11 +726,11 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// }
// h.Backend.Settings[uid] = setting
http.Redirect(w, r, "/settings", 302)
http.Redirect(w, r, "/settings", http.StatusFound)
return
} else if r.URL.Path == "/refresh" {
h.Backend.RefreshFeeds()
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
}
}
@ -741,14 +741,14 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func httpSessionLogout(r *http.Request, w http.ResponseWriter, conn redis.Conn) {
c, err := r.Cookie("session")
if err == http.ErrNoCookie {
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
return
}
if err == nil {
sessionVar := c.Value
_, _ = conn.Do("DEL", "session:"+sessionVar)
}
http.Redirect(w, r, "/", 302)
http.Redirect(w, r, "/", http.StatusFound)
}
type parsedEndpoints struct {

View File

@ -51,13 +51,13 @@ func (h *incomingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
leaseSeconds, err := strconv.ParseInt(leaseStr, 10, 64)
if err != nil {
log.Printf("error in hub.lease_seconds format %q: %s", leaseSeconds, err)
http.Error(w, fmt.Sprintf("error in hub.lease_seconds format %q: %s", leaseSeconds, err), 400)
http.Error(w, fmt.Sprintf("error in hub.lease_seconds format %q: %s", leaseSeconds, err), http.StatusBadRequest)
return
}
err = h.Backend.FeedSetLeaseSeconds(feed, leaseSeconds)
if err != nil {
log.Printf("error in while setting hub.lease_seconds: %s", err)
http.Error(w, fmt.Sprintf("error in while setting hub.lease_seconds: %s", err), 400)
http.Error(w, fmt.Sprintf("error in while setting hub.lease_seconds: %s", err), http.StatusBadRequest)
return
}
}
@ -70,7 +70,7 @@ func (h *incomingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", 405)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
@ -78,7 +78,7 @@ func (h *incomingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
secret := h.Backend.GetSecret(feed)
if secret == "" {
log.Printf("missing secret for feed %d\n", feed)
http.Error(w, "Unknown", 400)
http.Error(w, "Unknown", http.StatusBadRequest)
return
}
@ -94,7 +94,7 @@ func (h *incomingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if sig != "" {
if err := websub.ValidateHubSignature(sig, feedContent, []byte(secret)); err != nil {
log.Printf("could not validate signature: %+v", err)
http.Error(w, fmt.Sprintf("could not validate signature: %s", err), 400)
http.Error(w, fmt.Sprintf("could not validate signature: %s", err), http.StatusBadRequest)
return
}
}
@ -102,7 +102,7 @@ func (h *incomingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ct := r.Header.Get("Content-Type")
err = h.Backend.UpdateFeed(h.Processor, feed, ct, bytes.NewBuffer(feedContent))
if err != nil {
http.Error(w, fmt.Sprintf("could not update feed: %s (%s)", ct, err), 400)
http.Error(w, fmt.Sprintf("could not update feed: %s (%s)", ct, err), http.StatusBadRequest)
return
}

View File

@ -86,13 +86,13 @@ func WithAuth(handler http.Handler, b *memoryBackend) http.Handler {
}
if !authorized {
log.Printf("Token could not be validated")
http.Error(w, "Can't validate token", 403)
http.Error(w, "Can't validate token", http.StatusForbidden)
return
}
if token.Me != b.Me { // FIXME: Me should be part of the request
log.Printf("Missing \"me\" in token response: %#v\n", token)
http.Error(w, "Wrong me", 403)
http.Error(w, "Wrong me", http.StatusForbidden)
return
}

View File

@ -40,7 +40,7 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, "bad request", 400)
http.Error(w, "bad request", http.StatusBadRequest)
return
}
@ -50,13 +50,13 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
channel, err = getChannelFromAuthorization(r, conn)
if err != nil {
log.Println(err)
http.Error(w, "unauthorized", 401)
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
// no channel is found
if channel == "" {
http.Error(w, "bad request, unknown channel", 400)
http.Error(w, "bad request, unknown channel", http.StatusBadRequest)
return
}
@ -64,7 +64,7 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
item, err := parseIncomingItem(r)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Printf("Item published: %s", item.Published)
@ -76,7 +76,7 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
newID, err := generateItemID(conn, channel)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
item.ID = newID
@ -95,13 +95,13 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err = json.NewEncoder(w).Encode(map[string]string{"ok": "1"}); err != nil {
log.Println(err)
http.Error(w, "internal server error", 500)
http.Error(w, "internal server error", http.StatusInternalServerError)
}
return
}
http.Error(w, "Method not allowed", 405)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
func generateItemID(conn redis.Conn, channel string) (string, error) {

View File

@ -36,7 +36,7 @@ func respondJSON(w http.ResponseWriter, value interface{}) {
w.Header().Add("Content-Type", OutputContentType)
err := jw.Encode(value)
if err != nil {
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
@ -70,7 +70,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
channels, err := h.backend.ChannelsGetList()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, map[string][]microsub.Channel{
@ -80,7 +80,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timeline, err := h.backend.TimelineGet(values.Get("before"), values.Get("after"), values.Get("channel"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, timeline)
@ -88,7 +88,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timeline, err := h.backend.PreviewURL(values.Get("url"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, timeline)
@ -97,7 +97,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
following, err := h.backend.FollowGetList(channel)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, map[string][]microsub.Feed{
@ -107,7 +107,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
events, err := h.backend.Events()
if err != nil {
log.Println(err)
http.Error(w, "could not start sse connection", 500)
http.Error(w, "could not start sse connection", http.StatusInternalServerError)
}
// Remove this client from the map of connected clients
@ -127,10 +127,10 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err = sse.WriteMessages(w, events)
if err != nil {
log.Println(err)
http.Error(w, "internal server error", 500)
http.Error(w, "internal server error", http.StatusInternalServerError)
}
} else {
http.Error(w, fmt.Sprintf("unknown action %s", action), 400)
http.Error(w, fmt.Sprintf("unknown action %s", action), http.StatusBadRequest)
return
}
return
@ -147,7 +147,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := h.backend.ChannelsDelete(uid)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, []string{})
@ -158,7 +158,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
channel, err := h.backend.ChannelsCreate(name)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, channel)
@ -166,7 +166,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
channel, err := h.backend.ChannelsUpdate(uid, name)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, channel)
@ -177,7 +177,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// h.HubIncomingBackend.CreateFeed(url, uid)
feed, err := h.backend.FollowURL(uid, url)
if err != nil {
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, feed)
@ -186,14 +186,14 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
url := values.Get("url")
err := h.backend.UnfollowURL(uid, url)
if err != nil {
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, []string{})
} else if action == "preview" {
timeline, err := h.backend.PreviewURL(values.Get("url"))
if err != nil {
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respondJSON(w, timeline)
@ -250,20 +250,20 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if len(markAsRead) > 0 {
err := h.backend.MarkRead(channel, markAsRead)
if err != nil {
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
log.Println("No uids specified for mark read")
}
} else {
http.Error(w, fmt.Sprintf("unknown method in timeline %s\n", method), 500)
http.Error(w, fmt.Sprintf("unknown method in timeline %s\n", method), http.StatusInternalServerError)
return
}
respondJSON(w, []string{})
} else {
http.Error(w, fmt.Sprintf("unknown action %s\n", action), 400)
http.Error(w, fmt.Sprintf("unknown action %s\n", action), http.StatusBadRequest)
}
return
}