]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - vendor/github.com/hashicorp/go-cleanhttp/handlers.go
Merge branch 'fix_read_test' of github.com:alexandreFre/terraform-provider-statuscake
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / go-cleanhttp / handlers.go
CommitLineData
15c0b25d
AP
1package cleanhttp
2
3import (
4 "net/http"
5 "strings"
6 "unicode"
7)
8
9// HandlerInput provides input options to cleanhttp's handlers
10type HandlerInput struct {
11 ErrStatus int
12}
13
14// PrintablePathCheckHandler is a middleware that ensures the request path
15// contains only printable runes.
16func PrintablePathCheckHandler(next http.Handler, input *HandlerInput) http.Handler {
17 // Nil-check on input to make it optional
18 if input == nil {
19 input = &HandlerInput{
20 ErrStatus: http.StatusBadRequest,
21 }
22 }
23
24 // Default to http.StatusBadRequest on error
25 if input.ErrStatus == 0 {
26 input.ErrStatus = http.StatusBadRequest
27 }
28
29 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
30 // Check URL path for non-printable characters
31 idx := strings.IndexFunc(r.URL.Path, func(c rune) bool {
32 return !unicode.IsPrint(c)
33 })
34
35 if idx != -1 {
36 w.WriteHeader(input.ErrStatus)
37 return
38 }
39
40 next.ServeHTTP(w, r)
41 return
42 })
43}