Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 5 additions & 20 deletions helpers/operation_utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package helpers

import (
"mime"
"net/http"
"strings"

"github.com/pb33f/libopenapi/datamodel/high/v3"
)
Expand Down Expand Up @@ -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"]
}
22 changes: 22 additions & 0 deletions helpers/operation_utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package helpers

import (
"mime"
"net/http"
"testing"

Expand Down Expand Up @@ -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)
}
Loading