diff options
author | Garry Filakhtov <[email protected]> | 2019-11-25 10:52:41 +1100 |
---|---|---|
committer | Garry Filakhtov <[email protected]> | 2019-11-25 10:52:41 +1100 |
commit | 8e16501e074b85ef5c5225ed6ff7d5f9856232ef (patch) | |
tree | 947ff9adf98c89b8d4924f6eddf568fa7e66c8bf | |
parent | 8f93aa61da6e66cbd13de96ef53e476556d391a7 (diff) |
Replace regular expression with strings.Split()
Originally, a regular expression was used to split and trim multiple
comma-separated header names from "Trailer" header. This commit replaces
regular expression with strings.Split() to break string into parts and
then trims spaces and tabs from individual header names.
-rw-r--r-- | service/http/response.go | 6 | ||||
-rw-r--r-- | service/http/response_test.go | 17 |
2 files changed, 20 insertions, 3 deletions
diff --git a/service/http/response.go b/service/http/response.go index 8dda87c7..166ced82 100644 --- a/service/http/response.go +++ b/service/http/response.go @@ -4,7 +4,7 @@ import ( "encoding/json" "io" "net/http" - "regexp" + "strings" "github.com/spiral/roadrunner" ) @@ -84,9 +84,9 @@ func handleTrailers(h map[string][]string) map[string][]string { return h } - zp := regexp.MustCompile(` *, *`) for _, tr := range trailers { - for _, n := range zp.Split(tr, -1) { + for _, n := range strings.Split(tr, ",") { + n = strings.Trim(n, "\t ") if v, ok := h[n]; ok { h["Trailer:"+n] = v diff --git a/service/http/response_test.go b/service/http/response_test.go index 7ba9d8d4..ad524567 100644 --- a/service/http/response_test.go +++ b/service/http/response_test.go @@ -132,3 +132,20 @@ func TestWrite_HandlesTrailers(t *testing.T) { assert.Equal(t, "test", w.h.Get("Trailer:foo")) assert.Equal(t, "demo", w.h.Get("Trailer:bar")) } + +func TestWrite_HandlesHandlesWhitespacesInTrailer(t *testing.T) { + r, err := NewResponse(&roadrunner.Payload{ + Context: []byte( + `{"headers":{"trailer":["foo\t,bar , baz"],"foo":["a"],"bar":["b"],"baz":["c"]},"status": 200}`), + }) + + assert.NoError(t, err) + assert.NotNil(t, r) + + w := &testWriter{h: http.Header(make(map[string][]string))} + assert.NoError(t, r.Write(w)) + + assert.Equal(t, "a", w.h.Get("Trailer:foo")) + assert.Equal(t, "b", w.h.Get("Trailer:bar")) + assert.Equal(t, "c", w.h.Get("Trailer:baz")) +} |