Skip to content

Commit

Permalink
feat: remove tspecials from type/subtype
Browse files Browse the repository at this point in the history
  • Loading branch information
milankonir committed Jan 3, 2024
1 parent ed0394e commit ead80ac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
10 changes: 10 additions & 0 deletions mediatype/mediatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func fixMangledMediaType(mtype string, sep rune) string {
// The content type is completely missing. Put in a placeholder.
p = ctPlaceholder
}
// Remove invalid characters (specials)
p = removeTypeSpecials(p)
// Check for missing token after slash.
if strings.HasSuffix(p, "/") {
switch p {
Expand Down Expand Up @@ -525,3 +527,11 @@ loop:

return value
}

func removeTypeSpecials(value string) string {
for _, r := range []string{"(", ")", "<", ">", "@", ",", ":", "\\", "\"", "[", "]", "?", "="} {
value = strings.ReplaceAll(value, r, "")
}

return value
}

Check failure on line 537 in mediatype/mediatype.go

View workflow job for this annotation

GitHub Actions / golangci

File is not `gofmt`-ed with `-s` (gofmt)

Check failure on line 537 in mediatype/mediatype.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x build

syntax error: unexpected var after top level declaration
10 changes: 8 additions & 2 deletions mediatype/mediatype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func TestFixMangledMediaType(t *testing.T) {
},
{
// Removes empty parameters in the middle
input: `Content-Type: text/html; =""; charset=""`,
input: `text/html; =""; charset=""`,
sep: ';',
want: `Content-Type: text/html; charset=""`,
want: `text/html; charset=""`,
},
{
input: "application/octet-stream;=?UTF-8?B?bmFtZT0iw7DCn8KUwoo=?=You've got a new voice miss call.msg",
Expand Down Expand Up @@ -133,6 +133,12 @@ func TestFixMangledMediaType(t *testing.T) {
sep: ';',
want: `application/pdf; name=1337.pdf`,
},
// invalid media type characters
{
input: `text/html>`,
sep: ';',
want: `text/html`,
},
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/parts/empty-ctype-bad-content.raw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Content-Type: multipart/alternative; boundary="----=_Part_10541_215446765.98298273965074084"

------=_Part_10541_215446765.98298273965074084
Content-Type: <html>
Content-Type:

Use the View Invoices function or locate the account on the List of Accounts table.<br>
<br>
Expand Down

0 comments on commit ead80ac

Please sign in to comment.