Skip to content

Commit

Permalink
Add Content-Length header to HEAD requests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
faridtsl committed Feb 1, 2021
1 parent a918863 commit 46122fe
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions routers/repo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package repo
import (
"fmt"
"io"
"io/ioutil"
"path"
"strings"

Expand All @@ -21,16 +22,15 @@ import (

// ServeData download file from io.Reader
func ServeData(ctx *context.Context, name string, reader io.Reader) error {
buf := make([]byte, 1024)
n, err := reader.Read(buf)
content, err := ioutil.ReadAll(reader)
if err != nil && err != io.EOF {
return err
}
if n >= 0 {
buf = buf[:n]
}
length := len(content)
buf := content[:1024]

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

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

_, err = ctx.Resp.Write(buf)
if err != nil {
return err
}
_, err = io.Copy(ctx.Resp, reader)
_, err = ctx.Resp.Write(content)
return err
}

Expand Down

0 comments on commit 46122fe

Please sign in to comment.