Skip to content

Commit

Permalink
Allow any upper-case ASCII word to be an HTTP method (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
topliceanu authored and tsenart committed Jan 29, 2017
1 parent 89f25bb commit 06c3677
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,10 @@ func NewLazyTargeter(src io.Reader, body []byte, hdr http.Header) Targeter {
if len(tokens) < 2 {
return fmt.Errorf("bad target: %s", line)
}
switch tokens[0] {
case "HEAD", "GET", "PUT", "POST", "PATCH", "OPTIONS", "DELETE":
tgt.Method = tokens[0]
default:
if !startsWithHTTPMethod(line) {
return fmt.Errorf("bad method: %s", tokens[0])
}
tgt.Method = tokens[0]
if _, err = url.ParseRequestURI(tokens[1]); err != nil {
return fmt.Errorf("bad URL: %s", tokens[1])
}
Expand Down Expand Up @@ -171,8 +169,10 @@ func NewLazyTargeter(src io.Reader, body []byte, hdr http.Header) Targeter {
}
}

var httpMethodChecker = regexp.MustCompile("^(HEAD|GET|PUT|POST|PATCH|OPTIONS|DELETE) ")
var httpMethodChecker = regexp.MustCompile("^[A-Z]+\\s")

// A line starts with an http method when the first word is uppercase ascii
// followed by a space.
func startsWithHTTPMethod(t string) bool {
return httpMethodChecker.MatchString(t)
}
Expand Down
12 changes: 11 additions & 1 deletion lib/targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ func TestNewLazyTargeter(t *testing.T) {
t.Parallel()

for want, def := range map[error]string{
errors.New("bad method"): "DO_WORK http://:6000",
errors.New("bad method"): "DOwork http://:6000",
errors.New("bad target"): "GET",
errors.New("bad method"): "SET http://:6060",
errors.New("bad URL"): "GET foobar",
errors.New("bad body"): `
GET http://:6060
Expand Down Expand Up @@ -142,6 +143,9 @@ func TestNewLazyTargeter(t *testing.T) {
POST http://foobar.org/fnord/2
Authorization: x67890
@`, bodyf.Name(),
`
SUBSCRIBE http://foobar.org/sub`,
)

src := bytes.NewBufferString(strings.TrimSpace(targets))
Expand Down Expand Up @@ -186,6 +190,12 @@ func TestNewLazyTargeter(t *testing.T) {
"Content-Type": []string{"text/plain"},
},
},
{
Method: "SUBSCRIBE",
URL: "http://foobar.org/sub",
Body: []byte{},
Header: http.Header{"Content-Type": []string{"text/plain"}},
},
} {
var got Target
if err := read(&got); err != nil {
Expand Down

0 comments on commit 06c3677

Please sign in to comment.