Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Content-Length header to HEAD requests #14542

Merged
merged 9 commits into from
Feb 5, 2021
2 changes: 1 addition & 1 deletion routers/repo/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func GetAttachment(ctx *context.Context) {
return
}

if err = ServeData(ctx, attach.Name, fr); err != nil {
if err = ServeData(ctx, attach.Name, attach.Size, fr); err != nil {
ctx.ServerError("ServeData", err)
return
}
Expand Down
11 changes: 8 additions & 3 deletions routers/repo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

// ServeData download file from io.Reader
func ServeData(ctx *context.Context, name string, reader io.Reader) error {
func ServeData(ctx *context.Context, name string, size int64, reader io.Reader) error {
buf := make([]byte, 1024)
n, err := reader.Read(buf)
if err != nil && err != io.EOF {
Expand All @@ -31,6 +31,11 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error {
}

ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400")
if size >= 0 {
ctx.Resp.Header().Set("Content-Length", fmt.Sprintf("%d", size))
} else {
log.Error("ServeData called to serve data: %s with size < 0: %d", name, size)
}
name = path.Base(name)

// Google Chrome dislike commas in filenames, so let's change it to a space
Expand Down Expand Up @@ -76,7 +81,7 @@ func ServeBlob(ctx *context.Context, blob *git.Blob) error {
}
}()

return ServeData(ctx, ctx.Repo.TreePath, dataRc)
return ServeData(ctx, ctx.Repo.TreePath, blob.Size(), dataRc)
}

// ServeBlobOrLFS download a git.Blob redirecting to LFS if necessary
Expand Down Expand Up @@ -105,7 +110,7 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error {
log.Error("ServeBlobOrLFS: Close: %v", err)
}
}()
return ServeData(ctx, ctx.Repo.TreePath, lfsDataRc)
return ServeData(ctx, ctx.Repo.TreePath, meta.Size, lfsDataRc)
}

return ServeBlob(ctx, blob)
Expand Down