Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Commit

Permalink
http 400 on invalid requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbowes committed Jul 31, 2013
1 parent b38d9ca commit 7b73de2
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ func shift(s []string) ([]string, string) {
return s[1:], s[0]
}

func invalidRequest(w http.ResponseWriter, r *http.Request) {
log.Println("bad request", r.URL.String())
http.Error(w, "bad request", 400)
}

func buckle(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")

if len(parts) != 3 {
// error
invalidRequest(w, r)
return
}

imageName := wsReplacer.Replace(parts[2])
Expand Down Expand Up @@ -48,15 +54,20 @@ func buckle(w http.ResponseWriter, r *http.Request) {
}

if len(newParts) != 3 {
// error
invalidRequest(w, r)
return
}

if !strings.HasSuffix(newParts[2], ".png") {
// error
invalidRequest(w, r)
return
}

c := Colors[newParts[2][0:len(newParts[2])-4]]
// validate
c, ok := Colors[newParts[2][0:len(newParts[2])-4]]
if !ok {
invalidRequest(w, r)
return
}

d := Data{newParts[0], newParts[1], c}
makePngShield(w, d)
Expand Down

0 comments on commit 7b73de2

Please sign in to comment.