Skip to content

Commit 032749a

Browse files
committed
fix(serverCompress): strip MIME option before checking suffix
1 parent 86a9e63 commit 032749a

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/serverCompress/util.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ func isCompressibleType(contentType string) bool {
1717
return true
1818
}
1919

20+
sepIndex := strings.IndexByte(contentType, ';')
21+
if sepIndex > 0 {
22+
contentType = contentType[:sepIndex]
23+
}
24+
2025
// "image/svg+xml", "application/xhtml+xml", ...
2126
if strings.HasSuffix(contentType, "+xml") {
2227
return true
2328
}
2429

25-
sepIndex := strings.IndexByte(contentType, ';')
26-
if sepIndex > 0 {
27-
contentType = contentType[:sepIndex]
28-
}
2930
return util.Contains(compressibleTypes, contentType)
3031
}

src/serverCompress/util_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package serverCompress
2+
3+
import "testing"
4+
5+
func TestIsCompressibleType(t *testing.T) {
6+
if !isCompressibleType("text/plain") {
7+
t.Error()
8+
}
9+
if !isCompressibleType("text/plain; charset=utf-8") {
10+
t.Error()
11+
}
12+
13+
if !isCompressibleType("image/svg+xml") {
14+
t.Error()
15+
}
16+
if !isCompressibleType("image/svg+xml; charset=utf-8") {
17+
t.Error()
18+
}
19+
if isCompressibleType("image/png") {
20+
t.Error()
21+
}
22+
if isCompressibleType("image/png; foo=bar") {
23+
t.Error()
24+
}
25+
26+
if !isCompressibleType("application/javascript") {
27+
t.Error()
28+
}
29+
if !isCompressibleType("application/javascript; charset=utf-8") {
30+
t.Error()
31+
}
32+
}

0 commit comments

Comments
 (0)