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

check Content-Encoding header value #66

Merged
merged 1 commit into from
Feb 6, 2024
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
26 changes: 23 additions & 3 deletions ridgenative.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func (rw *responseWriter) encodeBody() string {
}

if typ := rw.header.Get("Content-Type"); typ != "" {
rw.isBinary = isBinary(typ)
rw.isBinary = isBinary(rw.header)
} else {
rw.detectContentType()
}
Expand All @@ -356,11 +356,27 @@ func (rw *responseWriter) encodeBody() string {
func (rw *responseWriter) detectContentType() {
contentType := http.DetectContentType(rw.w.Bytes())
rw.header.Set("Content-Type", contentType)
rw.isBinary = isBinary(contentType)
rw.isBinary = isBinary(rw.header)
}

// assume text/*, application/json, application/javascript, application/xml, */*+json, */*+xml as text
func isBinary(contentType string) bool {
func isBinary(headers http.Header) bool {
contentEncoding := headers.Values("Content-Encoding")
if len(contentEncoding) > 0 {
// typically, gzip, deflate, br, etc.
// these compressed encodings are not text, they are binary.
return true
}

// custom content encoding for Lambda
// it is compatible with AWS Lambda Rust Runtime.
// https://github.com/awslabs/aws-lambda-rust-runtime/blob/11d5669542d38ba2e1b5aa2d64b8e445d93718ea/lambda-http/src/response.rs#L303-L307
lambdaContentEncoding := headers.Get("X-Lambda-Http-Content-Encoding")
if lambdaContentEncoding == "text" {
return false
}

contentType := headers.Get("Content-Type")
i := strings.Index(contentType, ";")
if i == -1 {
i = len(contentType)
Expand All @@ -372,6 +388,7 @@ func isBinary(contentType string) bool {
}
mainType := mediaType[:i]

// common text mime types
if strings.EqualFold(mainType, "text") {
return false
}
Expand All @@ -388,6 +405,7 @@ func isBinary(contentType string) bool {
return false
}

// custom text mime types, such as application/*+json, application/*+xml
i = strings.LastIndex(mediaType, "+")
if i == -1 {
i = 0
Expand All @@ -402,6 +420,8 @@ func isBinary(contentType string) bool {
if strings.EqualFold(suffix, "+xml") {
return false
}

// assume it's binary
return true
}

Expand Down
145 changes: 125 additions & 20 deletions ridgenative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,31 +952,136 @@ func TestLambdaHandlerStreaming(t *testing.T) {

func TestIsBinary(t *testing.T) {
tests := []struct {
contentType string
want bool
header http.Header
want bool
}{
{"text/html", false},
{"text/plain", false},
{"text/xml", false},
{"application/json", false},
{"application/javascript", false},
{"application/yaml", false},
{"application/xml", false},
{"application/vnd.foo+json", false},
{"application/vnd.foo+yaml", false},
{"application/vnd.foo+xml", false},
{"application/vnd.foo+xml ; charset=utf8", false},
{"application/octet-stream", true},
{"image/jpeg", true},
{"audio/mpeg", true},
{"unknown-content-type", true},
{"", true},
// text/html but encoded as gzip
{
header: http.Header{
"Content-Type": []string{"text/html; charset=utf-8"},
"Content-Encoding": []string{"gzip"},
},
want: true,
},

// custom content encoding for Lambda
{
header: http.Header{
"Content-Type": []string{"image/svg"},
"X-Lambda-Http-Content-Encoding": []string{"text"},
},
want: false,
},

// text/*
{
header: http.Header{
"Content-Type": []string{"text/html"},
},
want: false,
},
{
header: http.Header{
"Content-Type": []string{"text/plain"},
},
want: false,
},
{
header: http.Header{
"Content-Type": []string{"text/xml"},
},
want: false,
},

// data formats that are encoded as text
{
header: http.Header{
"Content-Type": []string{"application/json"},
},
want: false,
},
{
header: http.Header{
"Content-Type": []string{"application/javascript"},
},
want: false,
},
{
header: http.Header{
"Content-Type": []string{"application/yaml"},
},
want: false,
},
{
header: http.Header{
"Content-Type": []string{"application/xml"},
},
want: false,
},

// custom media types that are encoded as text
{
header: http.Header{
"Content-Type": []string{"application/foo+json"},
},
want: false,
},
{
header: http.Header{
"Content-Type": []string{"application/foo+yaml"},
},
want: false,
},
{
header: http.Header{
"Content-Type": []string{"application/foo+xml"},
},
want: false,
},
{
header: http.Header{
"Content-Type": []string{"application/foo+xml ; charset=utf8"},
},
want: false,
},

// common binary formats
{
header: http.Header{
"Content-Type": []string{"application/octet-stream"},
},
want: true,
},
{
header: http.Header{
"Content-Type": []string{"image/jpeg"},
},
want: true,
},
{
header: http.Header{
"Content-Type": []string{"audio/mpeg"},
},
want: true,
},
{
header: http.Header{
"Content-Type": []string{"unknown-content-type"},
},
want: true,
},
{
header: http.Header{
"Content-Type": []string{""},
},
want: true,
},
}

for _, tt := range tests {
got := isBinary(tt.contentType)
got := isBinary(tt.header)
if got != tt.want {
t.Errorf("isBinary(%q) = %v, want %v", tt.contentType, got, tt.want)
t.Errorf("isBinary(%v) = %v, want %v", tt.header, got, tt.want)
}
}
}
Loading