aboutsummaryrefslogtreecommitdiffstats
path: root/modules/public
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2022-10-12 07:18:26 +0200
committerGitHub <noreply@github.com>2022-10-12 13:18:26 +0800
commit0e57ff7eee4ac71d923f970d15889ad4d50f97a9 (patch)
treee5a92c55af5366924bd40ae14b4bf12842239193 /modules/public
parente84558b0931309cf1f4f2767bc47296483b9b3e1 (diff)
downloadgitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.tar.gz
gitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.zip
Add generic set type (#21408)
This PR adds a generic set type to get rid of maps used as sets. Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules/public')
-rw-r--r--modules/public/public.go7
-rw-r--r--modules/public/public_test.go19
-rw-r--r--modules/public/serve_static.go2
3 files changed, 12 insertions, 16 deletions
diff --git a/modules/public/public.go b/modules/public/public.go
index 7804e945e7..ac1d80c860 100644
--- a/modules/public/public.go
+++ b/modules/public/public.go
@@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"
+ "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/httpcache"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
@@ -83,11 +84,11 @@ func AssetsHandlerFunc(opts *Options) http.HandlerFunc {
}
// parseAcceptEncoding parse Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 as compress methods
-func parseAcceptEncoding(val string) map[string]bool {
+func parseAcceptEncoding(val string) container.Set[string] {
parts := strings.Split(val, ";")
- types := make(map[string]bool)
+ types := make(container.Set[string])
for _, v := range strings.Split(parts[0], ",") {
- types[strings.TrimSpace(v)] = true
+ types.Add(strings.TrimSpace(v))
}
return types
}
diff --git a/modules/public/public_test.go b/modules/public/public_test.go
index 430e734564..8b58d6af33 100644
--- a/modules/public/public_test.go
+++ b/modules/public/public_test.go
@@ -7,28 +7,23 @@ package public
import (
"testing"
+ "code.gitea.io/gitea/modules/container"
+
"github.com/stretchr/testify/assert"
)
func TestParseAcceptEncoding(t *testing.T) {
kases := []struct {
Header string
- Expected map[string]bool
+ Expected container.Set[string]
}{
{
- Header: "deflate, gzip;q=1.0, *;q=0.5",
- Expected: map[string]bool{
- "deflate": true,
- "gzip": true,
- },
+ Header: "deflate, gzip;q=1.0, *;q=0.5",
+ Expected: container.SetOf("deflate", "gzip"),
},
{
- Header: " gzip, deflate, br",
- Expected: map[string]bool{
- "deflate": true,
- "gzip": true,
- "br": true,
- },
+ Header: " gzip, deflate, br",
+ Expected: container.SetOf("deflate", "gzip", "br"),
},
}
diff --git a/modules/public/serve_static.go b/modules/public/serve_static.go
index 9666880adf..10120bf85d 100644
--- a/modules/public/serve_static.go
+++ b/modules/public/serve_static.go
@@ -60,7 +60,7 @@ func AssetIsDir(name string) (bool, error) {
// serveContent serve http content
func serveContent(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) {
encodings := parseAcceptEncoding(req.Header.Get("Accept-Encoding"))
- if encodings["gzip"] {
+ if encodings.Contains("gzip") {
if cf, ok := fi.(*vfsgen۰CompressedFileInfo); ok {
rdGzip := bytes.NewReader(cf.GzipBytes())
// all static files are managed by Gitea, so we can make sure every file has the correct ext name