Skip to content

Commit 62f5228

Browse files
committed
ParseContentType now returns a blank media type if the input is blank or malformed
1 parent b230f03 commit 62f5228

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

header/contenttype.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ type ContentType struct {
2020
Params []KV
2121
}
2222

23+
// WithDefault returns "*/*" if ct has a blank media type.
24+
func (ct ContentType) WithDefault() ContentType {
25+
if ct.MediaType == "" {
26+
ct.MediaType = "*/*"
27+
}
28+
return ct
29+
}
30+
2331
func (ct ContentType) Split() (string, string) {
2432
t, s, _ := strings.Cut(ct.MediaType, "/")
2533
return t, s
@@ -99,27 +107,26 @@ func (ct ContentType) String() string {
99107
return buf.String()
100108
}
101109

102-
var starStar = ContentType{MediaType: "*/*"}
103-
104110
// ParseContentTypeFromHeaders gets the "Content-Type" header and returns
105-
// its parsed value.
111+
// its parsed value. An absent or malformed input yields a blank media type.
106112
func ParseContentTypeFromHeaders(hdrs http.Header) ContentType {
107113
cts := hdrs[headername.ContentType]
108114
if len(cts) == 0 {
109-
return starStar
115+
return ContentType{}
110116
}
111117
return ParseContentType(cts[0])
112118
}
113119

114120
// ParseContentType parses a content type value.
121+
// An absent or malformed input yields a blank media type.
115122
func ParseContentType(ct string) ContentType {
116123
if ct == "" {
117-
return starStar
124+
return ContentType{}
118125
}
119126

120127
mt, params, err := mime.ParseMediaType(ct)
121128
if err != nil {
122-
return starStar
129+
return ContentType{}
123130
}
124131

125132
var paramsKV []KV

header/contenttype_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ func TestParseContentTypeFromHeaders(t *testing.T) {
1414

1515
ct1 := ParseContentTypeFromHeaders(hdrs)
1616

17-
expect.Any(ct1).ToBe(t, ContentType{MediaType: "*/*"})
17+
expect.Any(ct1).ToBe(t, ContentType{MediaType: ""})
18+
expect.Any(ct1.WithDefault()).ToBe(t, ContentType{MediaType: "*/*"})
1819

1920
hdrs.Set(headername.ContentType, "text/plain")
2021

@@ -25,16 +26,16 @@ func TestParseContentTypeFromHeaders(t *testing.T) {
2526

2627
func TestParseContentType(t *testing.T) {
2728
// blank value is treated as star-star
28-
expect.Any(ParseContentType("")).ToBe(t, ContentType{MediaType: "*/*"})
29+
expect.Any(ParseContentType("")).ToBe(t, ContentType{MediaType: ""})
2930

3031
// illegal value is treated as star-star
31-
expect.Any(ParseContentType("/")).ToBe(t, ContentType{MediaType: "*/*"})
32+
expect.Any(ParseContentType("/")).ToBe(t, ContentType{MediaType: ""})
3233

3334
// illegal value is treated as star-star
34-
expect.Any(ParseContentType("/plain")).ToBe(t, ContentType{MediaType: "*/*"})
35+
expect.Any(ParseContentType("/plain")).ToBe(t, ContentType{MediaType: ""})
3536

3637
// error case handled silently
37-
expect.Any(ParseContentType("text/")).ToBe(t, ContentType{MediaType: "*/*"})
38+
expect.Any(ParseContentType("text/")).ToBe(t, ContentType{MediaType: ""})
3839

3940
expect.Any(ParseContentType("text/plain")).ToBe(t, ContentType{MediaType: "text/plain"})
4041

magefiles/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
package main
77

88
import (
9-
"github.com/magefile/mage/sh"
109
"log"
1110
"os"
11+
12+
"github.com/magefile/mage/sh"
1213
)
1314

1415
var Default = Build

offer/offer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Offer struct {
5151
// The correct behaviour is a 406 when no match can be made.
5252
func Of(processor Processor, contentType string) Offer {
5353
return Offer{
54-
ContentType: header.ParseContentType(contentType),
54+
ContentType: header.ParseContentType(contentType).WithDefault(),
5555
processor: processor,
5656
Langs: []string{"*"},
5757
data: make(map[string]datapkg.Data),

0 commit comments

Comments
 (0)