diff --git a/internal/web/handlers/gist/download.go b/internal/web/handlers/gist/download.go index 3370ba3..0c5607f 100644 --- a/internal/web/handlers/gist/download.go +++ b/internal/web/handlers/gist/download.go @@ -36,6 +36,7 @@ func RawFile(ctx *context.Context) error { } ctx.Response().Header().Set("Content-Disposition", "inline; filename=\""+url.PathEscape(file.Filename)+"\"") + ctx.Response().Header().Set("Content-Length", strconv.Itoa(int(file.Size))) ctx.Response().Header().Set("X-Content-Type-Options", "nosniff") return ctx.PlainText(200, file.Content) } @@ -53,7 +54,7 @@ func DownloadFile(ctx *context.Context) error { ctx.Response().Header().Set("Content-Type", file.MimeType.Header()) ctx.Response().Header().Set("Content-Disposition", "attachment; filename=\""+url.PathEscape(file.Filename)+"\"") - ctx.Response().Header().Set("Content-Length", strconv.Itoa(len(file.Content))) + ctx.Response().Header().Set("Content-Length", strconv.Itoa(int(file.Size))) ctx.Response().Header().Set("X-Content-Type-Options", "nosniff") _, err = ctx.Response().Write([]byte(file.Content)) if err != nil { diff --git a/internal/web/handlers/gist/download_test.go b/internal/web/handlers/gist/download_test.go index 597d09e..497c06e 100644 --- a/internal/web/handlers/gist/download_test.go +++ b/internal/web/handlers/gist/download_test.go @@ -65,6 +65,7 @@ func TestRawFile(t *testing.T) { resp := s.Request(t, "GET", "/"+username+"/"+identifier+"/raw/HEAD/file.txt", nil, 200) require.Equal(t, `inline; filename="file.txt"`, resp.Header.Get("Content-Disposition")) + require.Equal(t, "11", resp.Header.Get("Content-Length")) require.Equal(t, "nosniff", resp.Header.Get("X-Content-Type-Options")) require.Contains(t, resp.Header.Get("Content-Type"), "text/plain") diff --git a/internal/web/server/router.go b/internal/web/server/router.go index 0fa6d34..d25aa74 100644 --- a/internal/web/server/router.go +++ b/internal/web/server/router.go @@ -194,7 +194,9 @@ func (s *Server) registerRoutes() { sC.POST("/archive", gist.ToggleArchive, logged, writePermission) sC.POST("/delete", gist.DeleteGist, logged, writePermission) sC.GET("/raw/:revision/:file", gist.RawFile) + sC.HEAD("/raw/:revision/:file", gist.RawFile) sC.GET("/download/:revision/:file", gist.DownloadFile) + sC.HEAD("/download/:revision/:file", gist.DownloadFile) sC.GET("/edit", gist.Edit, logged, writePermission, notArchived) sC.POST("/edit", gist.ProcessCreate, logged, writePermission, notArchived) sC.POST("/like", gist.Like, logged) @@ -259,6 +261,10 @@ func (r *Router) GET(path string, h Handler, m ...Middleware) { r.Group.GET(path, chain(h, m...).toEchoHandler()) } +func (r *Router) HEAD(path string, h Handler, m ...Middleware) { + r.Group.HEAD(path, chain(h, m...).toEchoHandler()) +} + func (r *Router) POST(path string, h Handler, m ...Middleware) { r.Group.POST(path, chain(h, m...).toEchoHandler()) }