Skip to content

Commit

Permalink
Better error message for /bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
sharat87 committed Dec 2, 2023
1 parent d16469a commit 5a93f6c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func GetRoutes() []Route {
"/ip(\\.(?P<format>txt|json))?": handleIp,

"/b(ase)?64(/(?P<encoded>.*))?": handleDecodeBase64,
"/bytes/(?P<size>\\d+)": handleRandomBytes,
"/bytes(/(?P<size>.+))?": handleRandomBytes,
"/delay/(?P<delay>[^/]+)": handleDelayedResponse,
"/drip(-(?P<mode>lines))?": handleDrip,
"/links/(?P<count>\\d+)(/(?P<offset>\\d+))?/?": handleLinks,
Expand Down Expand Up @@ -158,8 +158,17 @@ func handleDecodeBase64(ex *exchange.Exchange) {
}

func handleRandomBytes(ex *exchange.Exchange) {
sizeField := ex.Field("size")
if sizeField == "" {
ex.RespondBadRequest("specify size in bytes, example `/bytes/10`")
return
}
n, err := strconv.Atoi(sizeField)
if err != nil {
ex.RespondBadRequest("Invalid size: " + sizeField)
return
}
ex.ResponseWriter.Header().Set("content-type", "application/octet-stream")
n, _ := strconv.Atoi(ex.Field("size"))
ex.ResponseWriter.Header().Set("content-length", fmt.Sprint(n))
ex.WriteBytes(util.RandomBytes(n))
}
Expand Down

0 comments on commit 5a93f6c

Please sign in to comment.