Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove doc tag from content type value #2

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion mediatype/mediatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
// * Newline characters
func Parse(ctype string) (mtype string, params map[string]string, invalidParams []string, err error) {
mtype, params, err = mime.ParseMediaType(
fixNewlines(fixUnescapedQuotes(fixUnquotedSpecials(fixMangledMediaType(ctype, ';')))))
fixNewlines(fixUnescapedQuotes(fixUnquotedSpecials(fixMangledMediaType(removeTag(ctype), ';')))))
dmotylev marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if err.Error() == "mime: no media type" {
return "", nil, nil, nil
Expand Down Expand Up @@ -484,3 +484,12 @@ func fixNewlines(value string) string {
value = strings.ReplaceAll(value, "\r", "")
return value
}

func removeTag(value string) string {
// check if ctype contains <!DOCTYPE tag and remove it
dtIdx := strings.Index(value, "<!DOCTYPE")
if dtIdx > -1 {
return value[0:dtIdx]
}
return value
}
6 changes: 6 additions & 0 deletions mediatype/mediatype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ func TestParseMediaType(t *testing.T) {
mtype: "application/octet-stream",
params: map[string]string{"name": "Outlook-Logo Desc"},
},
{
label: "ignore doctype",
input: `text/html; charset="iso-8859-1"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`,
mtype: "text/html",
params: map[string]string{"charset": "iso-8859-1"},
},
}
for _, tc := range testCases {
t.Run(tc.label, func(t *testing.T) {
Expand Down