Skip to content

Commit e73e0cd

Browse files
committed
feat: Brotli encoding support
1 parent 59ea8b4 commit e73e0cd

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

main.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type ServerSettings struct {
4747
ExtScriptTypes []string `json:"extScriptTypes"`
4848
ExtIndexTypes []string `json:"extIndexTypes"`
4949
ExtGzippeddTypes []string `json:"extGzippedTypes"`
50+
ExtBrotliTypes []string `json:"extBrotliTypes"`
5051
ExtMimeTypes map[string]string `json:"extMimeTypes"`
5152
}
5253

@@ -163,8 +164,10 @@ func setContentType(r *http.Request, resp *http.Response) {
163164

164165
resp.Header.Del("Content-Type")
165166

166-
rext := strings.ToLower(filepath.Ext(resp.Header.Get("ZIPSVR_FILENAME")))
167-
ext := strings.ToLower(filepath.Ext(strings.TrimSpace(r.URL.Path)))
167+
fnameHeader := resp.Header.Get("ZIPSVR_FILENAME")
168+
fname := strings.TrimSpace(r.URL.Path)
169+
rext := strings.ToLower(filepath.Ext(fnameHeader))
170+
ext := strings.ToLower(filepath.Ext(fname))
168171
mime := ""
169172

170173
// If the request already has an extension, fetch the mime via extension
@@ -181,6 +184,22 @@ func setContentType(r *http.Request, resp *http.Response) {
181184
break // String found, no need to continue iterating
182185
}
183186
}
187+
188+
for _, element := range serverSettings.ExtBrotliTypes {
189+
if element == e {
190+
resp.Header.Set("Content-Encoding", "br")
191+
// Try and use the previous ending for content type, if exists
192+
prevExt := strings.TrimSuffix(strings.ToLower(fname), "."+e)
193+
if prevExt != "" {
194+
prevMime := serverSettings.ExtMimeTypes[prevExt[1:]]
195+
if prevMime != "" {
196+
resp.Header.Set("Content-Type", prevMime)
197+
mime = prevMime
198+
}
199+
}
200+
break;
201+
}
202+
}
184203
}
185204
}
186205

@@ -198,6 +217,22 @@ func setContentType(r *http.Request, resp *http.Response) {
198217
break // String found, no need to continue iterating
199218
}
200219
}
220+
221+
for _, element := range serverSettings.ExtBrotliTypes {
222+
if element == e {
223+
resp.Header.Set("Content-Encoding", "br")
224+
// Try and use the previous ending for content type, if exists
225+
prevExt := strings.TrimSuffix(strings.ToLower(fnameHeader), "."+e)
226+
if prevExt != "" {
227+
prevMime := serverSettings.ExtMimeTypes[prevExt[1:]]
228+
if prevMime != "" {
229+
resp.Header.Set("Content-Type", prevMime)
230+
mime = prevMime
231+
}
232+
}
233+
break;
234+
}
235+
}
201236
}
202237
}
203238

proxySettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
"extGzippedTypes": [
4545
"svgz"
4646
],
47+
"extBrotliTypes": [
48+
"br"
49+
],
4750
"extMimeTypes": {
4851
"aab": "application/x-authorware-bin",
4952
"aam": "application/x-authorware-map",

0 commit comments

Comments
 (0)