From 906ddc39a03c1505972099bc9967f8fdb3e7f34c Mon Sep 17 00:00:00 2001 From: Jon Williams Date: Thu, 19 Dec 2024 14:12:17 -0500 Subject: [PATCH] Changed ExtractContentType to use mime.ParseMediaType. Fixes #91. --- helpers/operation_utilities.go | 25 +++++-------------------- helpers/operation_utilities_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/helpers/operation_utilities.go b/helpers/operation_utilities.go index 1347093..a4ceadd 100644 --- a/helpers/operation_utilities.go +++ b/helpers/operation_utilities.go @@ -4,8 +4,8 @@ package helpers import ( + "mime" "net/http" - "strings" "github.com/pb33f/libopenapi/datamodel/high/v3" ) @@ -38,23 +38,8 @@ func ExtractOperation(request *http.Request, item *v3.PathItem) *v3.Operation { // of the request.The second (optional) argument is the charset of the request. The third (optional) // argument is the boundary of the type (only used with forms really). func ExtractContentType(contentType string) (string, string, string) { - var charset, boundary string - if strings.ContainsRune(contentType, ';') { - segs := strings.Split(contentType, SemiColon) - contentType = strings.TrimSpace(segs[0]) - for _, v := range segs[1:] { - kv := strings.Split(v, Equals) - if len(kv) == 2 { - if strings.TrimSpace(strings.ToLower(kv[0])) == Charset { - charset = strings.TrimSpace(kv[1]) - } - if strings.TrimSpace(strings.ToLower(kv[0])) == Boundary { - boundary = strings.TrimSpace(kv[1]) - } - } - } - } else { - contentType = strings.TrimSpace(contentType) - } - return contentType, charset, boundary + // mime.ParseMediaType: "If there is an error parsing the optional parameter, + // the media type will be returned along with the error ErrInvalidMediaParameter." + ct, params, _ := mime.ParseMediaType(contentType) + return ct, params["charset"], params["boundary"] } diff --git a/helpers/operation_utilities_test.go b/helpers/operation_utilities_test.go index 2a3ef53..9d1f01f 100644 --- a/helpers/operation_utilities_test.go +++ b/helpers/operation_utilities_test.go @@ -4,6 +4,7 @@ package helpers import ( + "mime" "net/http" "testing" @@ -89,4 +90,25 @@ func TestExtractContentType(t *testing.T) { require.Equal(t, "application/xml", contentType) require.Empty(t, charset) require.Empty(t, boundary) + + // Content type with custom parameter + contentType, charset, boundary = ExtractContentType("text/html; version=2") + require.Equal(t, "text/html", contentType) + require.Empty(t, charset) + require.Empty(t, boundary) + + // Content type with custom parameter, charset, and boundary + contentType, charset, boundary = ExtractContentType("text/html; charset=UTF-8; version=2; boundary=myBoundary") + require.Equal(t, "text/html", contentType) + require.Equal(t, "UTF-8", charset) + require.Equal(t, "myBoundary", boundary) + + // mime.ParseMediaType returns an error, but ExtractContentType still returns the content type. + const ct = "text/plain;;" + _, _, err := mime.ParseMediaType(ct) + require.ErrorIs(t, err, mime.ErrInvalidMediaParameter) + contentType, charset, boundary = ExtractContentType(ct) + require.Equal(t, "text/plain", contentType) + require.Empty(t, charset) + require.Empty(t, boundary) }