Skip to content

Commit 3972f97

Browse files
trungbradfitz
authored andcommitted
net/http, net/textproto: add Header.Values, MIMEHeader.Values methods
Fixes #34799 Change-Id: I134b2717fa90c8955902e7eeaaf8510dcc28340e Reviewed-on: https://go-review.googlesource.com/c/go/+/200760 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent f38a510 commit 3972f97

File tree

4 files changed

+78
-40
lines changed

4 files changed

+78
-40
lines changed

src/net/http/header.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,21 @@ func (h Header) Set(key, value string) {
4040
// Get gets the first value associated with the given key. If
4141
// there are no values associated with the key, Get returns "".
4242
// It is case insensitive; textproto.CanonicalMIMEHeaderKey is
43-
// used to canonicalize the provided key. To access multiple
44-
// values of a key, or to use non-canonical keys, access the
45-
// map directly.
43+
// used to canonicalize the provided key. To use non-canonical keys,
44+
// access the map directly.
4645
func (h Header) Get(key string) string {
4746
return textproto.MIMEHeader(h).Get(key)
4847
}
4948

49+
// Values returns all values associated with the given key.
50+
// It is case insensitive; textproto.CanonicalMIMEHeaderKey is
51+
// used to canonicalize the provided key. To use non-canonical
52+
// keys, access the map directly.
53+
// The returned slice is not a copy.
54+
func (h Header) Values(key string) []string {
55+
return textproto.MIMEHeader(h).Values(key)
56+
}
57+
5058
// get is like Get, but key must already be in CanonicalHeaderKey form.
5159
func (h Header) get(key string) string {
5260
if v := h[key]; len(v) > 0 {

src/net/textproto/header.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ func (h MIMEHeader) Set(key, value string) {
2626
// It is case insensitive; CanonicalMIMEHeaderKey is used
2727
// to canonicalize the provided key.
2828
// If there are no values associated with the key, Get returns "".
29-
// To access multiple values of a key, or to use non-canonical keys,
30-
// access the map directly.
29+
// To use non-canonical keys, access the map directly.
3130
func (h MIMEHeader) Get(key string) string {
3231
if h == nil {
3332
return ""
@@ -39,6 +38,18 @@ func (h MIMEHeader) Get(key string) string {
3938
return v[0]
4039
}
4140

41+
// Values returns all values associated with the given key.
42+
// It is case insensitive; CanonicalMIMEHeaderKey is
43+
// used to canonicalize the provided key. To use non-canonical
44+
// keys, access the map directly.
45+
// The returned slice is not a copy.
46+
func (h MIMEHeader) Values(key string) []string {
47+
if h == nil {
48+
return nil
49+
}
50+
return h[CanonicalMIMEHeaderKey(key)]
51+
}
52+
4253
// Del deletes the values associated with key.
4354
func (h MIMEHeader) Del(key string) {
4455
delete(h, CanonicalMIMEHeaderKey(key))

src/net/textproto/header_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2010 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package textproto
6+
7+
import "testing"
8+
9+
type canonicalHeaderKeyTest struct {
10+
in, out string
11+
}
12+
13+
var canonicalHeaderKeyTests = []canonicalHeaderKeyTest{
14+
{"a-b-c", "A-B-C"},
15+
{"a-1-c", "A-1-C"},
16+
{"User-Agent", "User-Agent"},
17+
{"uSER-aGENT", "User-Agent"},
18+
{"user-agent", "User-Agent"},
19+
{"USER-AGENT", "User-Agent"},
20+
21+
// Other valid tchar bytes in tokens:
22+
{"foo-bar_baz", "Foo-Bar_baz"},
23+
{"foo-bar$baz", "Foo-Bar$baz"},
24+
{"foo-bar~baz", "Foo-Bar~baz"},
25+
{"foo-bar*baz", "Foo-Bar*baz"},
26+
27+
// Non-ASCII or anything with spaces or non-token chars is unchanged:
28+
{"üser-agenT", "üser-agenT"},
29+
{"a B", "a B"},
30+
31+
// This caused a panic due to mishandling of a space:
32+
{"C Ontent-Transfer-Encoding", "C Ontent-Transfer-Encoding"},
33+
{"foo bar", "foo bar"},
34+
}
35+
36+
func TestCanonicalMIMEHeaderKey(t *testing.T) {
37+
for _, tt := range canonicalHeaderKeyTests {
38+
if s := CanonicalMIMEHeaderKey(tt.in); s != tt.out {
39+
t.Errorf("CanonicalMIMEHeaderKey(%q) = %q, want %q", tt.in, s, tt.out)
40+
}
41+
}
42+
}
43+
44+
// Issue #34799 add a Header method to get multiple values []string, with canonicalized key
45+
func TestMIMEHeaderMultipleValues(t *testing.T) {
46+
testHeader := MIMEHeader{
47+
"Set-Cookie": {"cookie 1", "cookie 2"},
48+
}
49+
values := testHeader.Values("set-cookie")
50+
n := len(values)
51+
if n != 2 {
52+
t.Errorf("count: %d; want 2", n)
53+
}
54+
}

src/net/textproto/reader_test.go

-35
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,6 @@ import (
1313
"testing"
1414
)
1515

16-
type canonicalHeaderKeyTest struct {
17-
in, out string
18-
}
19-
20-
var canonicalHeaderKeyTests = []canonicalHeaderKeyTest{
21-
{"a-b-c", "A-B-C"},
22-
{"a-1-c", "A-1-C"},
23-
{"User-Agent", "User-Agent"},
24-
{"uSER-aGENT", "User-Agent"},
25-
{"user-agent", "User-Agent"},
26-
{"USER-AGENT", "User-Agent"},
27-
28-
// Other valid tchar bytes in tokens:
29-
{"foo-bar_baz", "Foo-Bar_baz"},
30-
{"foo-bar$baz", "Foo-Bar$baz"},
31-
{"foo-bar~baz", "Foo-Bar~baz"},
32-
{"foo-bar*baz", "Foo-Bar*baz"},
33-
34-
// Non-ASCII or anything with spaces or non-token chars is unchanged:
35-
{"üser-agenT", "üser-agenT"},
36-
{"a B", "a B"},
37-
38-
// This caused a panic due to mishandling of a space:
39-
{"C Ontent-Transfer-Encoding", "C Ontent-Transfer-Encoding"},
40-
{"foo bar", "foo bar"},
41-
}
42-
43-
func TestCanonicalMIMEHeaderKey(t *testing.T) {
44-
for _, tt := range canonicalHeaderKeyTests {
45-
if s := CanonicalMIMEHeaderKey(tt.in); s != tt.out {
46-
t.Errorf("CanonicalMIMEHeaderKey(%q) = %q, want %q", tt.in, s, tt.out)
47-
}
48-
}
49-
}
50-
5116
func reader(s string) *Reader {
5217
return NewReader(bufio.NewReader(strings.NewReader(s)))
5318
}

0 commit comments

Comments
 (0)