Skip to content

Commit 46122fe

Browse files
committed
Add Content-Length header to HEAD requests
This change adds the header Content-Length to HEAD HTTP requests. The previous behaviour was blocking some Windows executables (i.e bitsadmin.exe) from downloading files hosted in Gitea. This along with PR go-gitea#14541, makes the web server compliant with HTTP RFC 2616 which states "The methods GET and HEAD MUST be supported by all general-purpose servers" and "The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response." This should also respond to issues go-gitea#8030 and go-gitea#14532.
1 parent a918863 commit 46122fe

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

routers/repo/download.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package repo
88
import (
99
"fmt"
1010
"io"
11+
"io/ioutil"
1112
"path"
1213
"strings"
1314

@@ -21,16 +22,15 @@ import (
2122

2223
// ServeData download file from io.Reader
2324
func ServeData(ctx *context.Context, name string, reader io.Reader) error {
24-
buf := make([]byte, 1024)
25-
n, err := reader.Read(buf)
25+
content, err := ioutil.ReadAll(reader)
2626
if err != nil && err != io.EOF {
2727
return err
2828
}
29-
if n >= 0 {
30-
buf = buf[:n]
31-
}
29+
length := len(content)
30+
buf := content[:1024]
3231

3332
ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400")
33+
ctx.Resp.Header().Set("Content-Length", fmt.Sprintf("%d", length))
3434
name = path.Base(name)
3535

3636
// Google Chrome dislike commas in filenames, so let's change it to a space
@@ -56,11 +56,7 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error {
5656
ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
5757
}
5858

59-
_, err = ctx.Resp.Write(buf)
60-
if err != nil {
61-
return err
62-
}
63-
_, err = io.Copy(ctx.Resp, reader)
59+
_, err = ctx.Resp.Write(content)
6460
return err
6561
}
6662

0 commit comments

Comments
 (0)