Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve precompressed static file responses https://github.com/siyuan-note/siyuan/issues/18951

Danieldcb811a88f7b
diff --git a/kernel/go.mod b/kernel/go.mod
index 4aa3d1b9a6..bb9c58a754 100644
--- a/kernel/go.mod
+++ b/kernel/go.mod
@@ -33,7 +33,7 @@ require (
github.com/fsnotify/fsnotify v1.10.1
github.com/gabriel-vasile/mimetype v1.4.15
github.com/gen2brain/h265 v0.1.1
- github.com/gin-contrib/gzip v1.2.6
+ github.com/gin-contrib/gzip v1.2.3
github.com/gin-contrib/sessions v1.1.0
github.com/gin-contrib/sse v1.1.1
github.com/gin-gonic/gin v1.12.0
diff --git a/kernel/go.sum b/kernel/go.sum
index 17f955a65a..c774613315 100644
--- a/kernel/go.sum
+++ b/kernel/go.sum
@@ -181,8 +181,8 @@ github.com/gen2brain/h265 v0.1.1 h1:S+EtCOYeOZ4UQpwzXKCSv37fQ546y4eHHnXye1L236s=
github.com/gen2brain/h265 v0.1.1/go.mod h1:kefpLxLhGpNCUG/q/aabJcD6GqB9Nhet3fNi9xX9Wzw=
github.com/gigawattio/window v0.0.0-20180317192513-0f5467e35573 h1:u8AQ9bPa9oC+8/A/jlWouakhIvkFfuxgIIRjiy8av7I=
github.com/gigawattio/window v0.0.0-20180317192513-0f5467e35573/go.mod h1:eBvb3i++NHDH4Ugo9qCvMw8t0mTSctaEa5blJbWcNxs=
-github.com/gin-contrib/gzip v1.2.6 h1:OtN8DplD5DNZCSLAnQ5HxRkD2qZ5VU+JhOrcfJrcRvg=
-github.com/gin-contrib/gzip v1.2.6/go.mod h1:BQy8/+JApnRjAVUplSGZiVtD2k8GmIE2e9rYu/hLzzU=
+github.com/gin-contrib/gzip v1.2.3 h1:dAhT722RuEG330ce2agAs75z7yB+NKvX/ZM1r8w0u2U=
+github.com/gin-contrib/gzip v1.2.3/go.mod h1:ad72i4Bzmaypk8M762gNXa2wkxxjbz0icRNnuLJ9a/c=
github.com/gin-contrib/sessions v1.1.0 h1:00mhHfNEGF5sP2fwxa98aRqj1FOJdL6IkR86n2hOiBo=
github.com/gin-contrib/sessions v1.1.0/go.mod h1:TyYZDIs6qCQg2SOoYPgMT9pAkmZceVNEJMcv5qbIy60=
github.com/gin-contrib/sse v1.1.1 h1:uGYpNwTacv5R68bSGMapo62iLTRa9l5zxGCps4hK6ko=
diff --git a/kernel/server/serve.go b/kernel/server/serve.go
index 7928466626..6cef57e4a3 100644
--- a/kernel/server/serve.go
+++ b/kernel/server/serve.go
@@ -152,9 +152,7 @@ func Serve(fastMode bool, cookieKey string) {
model.Activity, // 记录用户活动时间,用于 AutoFixIndex 的空闲判断
corsMiddleware(), // 后端服务支持 CORS 预检请求验证 https://github.com/siyuan-note/siyuan/pull/5593
jwtMiddleware, // 解析 JWT https://github.com/siyuan-note/siyuan/issues/11364
- gzip.Gzip(gzip.DefaultCompression,
- gzip.WithExcludedExtensions([]string{".pdf", ".mp3", ".wav", ".ogg", ".mov", ".weba", ".mkv", ".mp4", ".webm", ".flac"}),
- gzip.WithExcludedPathsRegexs([]string{`(?i)\.hei[cf]$`})),
+ gzipMiddleware(),
)
sessionStore = cookie.NewStore([]byte(cookieKey))
@@ -312,6 +310,12 @@ func Serve(fastMode bool, cookieKey string) {
}
}
+func gzipMiddleware() gin.HandlerFunc {
+ return gzip.Gzip(gzip.DefaultCompression,
+ gzip.WithExcludedExtensions([]string{".pdf", ".mp3", ".wav", ".ogg", ".mov", ".weba", ".mkv", ".mp4", ".webm", ".flac", ".gz"}),
+ gzip.WithExcludedPathsRegexs([]string{`(?i)\.hei[cf]$`}))
+}
+
func rewritePortJSON(pid, port string) {
portJSON := filepath.Join(util.HomeDir, ".config", "siyuan", "port.json")
pidPorts := map[string]string{}
diff --git a/kernel/server/serve_static_test.go b/kernel/server/serve_static_test.go
index 3c7277e4ef..769e56b9c1 100644
--- a/kernel/server/serve_static_test.go
+++ b/kernel/server/serve_static_test.go
@@ -17,6 +17,10 @@
package server
import (
+ "bytes"
+ "compress/gzip"
+ "io"
+ "math/rand"
"net/http"
"net/http/httptest"
"os"
@@ -71,6 +75,70 @@ func TestRegisterStaticFileHandlers(t *testing.T) {
}
}
+func TestGzipMiddlewareServesPrecompressedStaticFile(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ source := make([]byte, 128*1024)
+ if _, err := rand.New(rand.NewSource(1)).Read(source); err != nil {
+ t.Fatal(err)
+ }
+
+ var compressed bytes.Buffer
+ writer := gzip.NewWriter(&compressed)
+ if _, err := writer.Write(source); err != nil {
+ t.Fatal(err)
+ }
+ if err := writer.Close(); err != nil {
+ t.Fatal(err)
+ }
+ if compressed.Len() <= 32*1024 {
+ t.Fatalf("compressed test file is too small: %d", compressed.Len())
+ }
+
+ filePath := filepath.Join(t.TempDir(), "data.gz")
+ if err := os.WriteFile(filePath, compressed.Bytes(), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ engine := gin.New()
+ engine.Use(gzipMiddleware())
+ for _, requestPath := range []string{"/data.gz", "/data.dump"} {
+ engine.GET(requestPath, func(c *gin.Context) {
+ http.ServeFile(c.Writer, c.Request, filePath)
+ })
+ }
+
+ for _, requestPath := range []string{"/data.gz", "/data.dump"} {
+ t.Run(requestPath, func(t *testing.T) {
+ request := httptest.NewRequest(http.MethodGet, requestPath, nil)
+ request.Header.Set("Accept-Encoding", "gzip")
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, request)
+ if recorder.Code != http.StatusOK {
+ t.Fatalf("unexpected status: %d", recorder.Code)
+ }
+
+ body := recorder.Body.Bytes()
+ if recorder.Header().Get("Content-Encoding") == "gzip" {
+ reader, err := gzip.NewReader(bytes.NewReader(body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ body, err = io.ReadAll(reader)
+ if err != nil {
+ _ = reader.Close()
+ t.Fatal(err)
+ }
+ if err = reader.Close(); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if !bytes.Equal(body, compressed.Bytes()) {
+ t.Fatalf("response differs from the static file: got %d bytes, want %d", len(body), compressed.Len())
+ }
+ })
+ }
+}
+
func TestStaticFileNestedSymlinkEscape(t *testing.T) {
gin.SetMode(gin.TestMode)
root, outside := t.TempDir(), t.TempDir()