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

Allow content type middleware #2576

Closed
wants to merge 4 commits into from
Closed
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
56 changes: 56 additions & 0 deletions middleware/allow_content_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package middleware

import (
"net/http"
"strings"

"github.com/labstack/echo/v4"
)

// generateAcceptHeaderString takes in a list of allowed content types and generates
// a string that can be used in the Accept part of an HTTP header
func generateAcceptHeaderString(allowedContentTypes map[string]struct{}) string {
acceptString := ""
i := 0
for mimeType := range allowedContentTypes {
acceptString += mimeType
if i != len(allowedContentTypes)-1 {
acceptString += ", "
}
i += 1
}
return acceptString
}

// AllowContentType returns an AllowContentType middleware
//
// It requries at least one content type to be passed in as an argument
func AllowContentType(contentTypes ...string) echo.MiddlewareFunc {
if len(contentTypes) == 0 {
panic("echo: allow-content middleware requires at least one content type")
}
allowedContentTypes := make(map[string]struct{}, len(contentTypes))
for _, ctype := range contentTypes {
allowedContentTypes[strings.TrimSpace(strings.ToLower(ctype))] = struct{}{}
}

return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// Add allowed types to Accept header to tell client what data types are allowed
c.Response().Header().Add("Accept", generateAcceptHeaderString(allowedContentTypes))

if c.Request().ContentLength == 0 {
// skip check for empty content body
return next(c)
}
s := strings.ToLower(strings.TrimSpace(c.Request().Header.Get("Content-Type")))
if i := strings.Index(s, ";"); i > -1 {
s = s[0:i]
}
if _, ok := allowedContentTypes[s]; ok {
return next(c)
}
return echo.NewHTTPError(http.StatusUnsupportedMediaType)
}
}
}
36 changes: 36 additions & 0 deletions middleware/allow_content_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package middleware

import (
"bytes"
"net/http"
"net/http/httptest"
"testing"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

func TestAllowContentType(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", bytes.NewReader([]byte("Hello World!")))
res := httptest.NewRecorder()

h := AllowContentType("application/json", "text/plain")(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
})

// Test valid content type
req.Header.Add("Content-Type", "application/json")

c := e.NewContext(req, res)
assert.NoError(t, h(c))

// Test invalid content type
req.Header.Add("Content-Type", "text/html")
c = e.NewContext(req, res)
assert.NoError(t, h(c))

// Test Accept header
accept := c.Response().Header().Get("Accept")
assert.Equal(t, "application/json, text/plain", accept)
}