Skip to content
New issue

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

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

Already on GitHub? # to your account

Set the LastModified header for raw files #18356

Merged
merged 12 commits into from
May 9, 2022
Merged
9 changes: 7 additions & 2 deletions modules/httpcache/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,23 @@ func generateETag(fi os.FileInfo) string {

// HandleTimeCache handles time-based caching for a HTTP request
func HandleTimeCache(req *http.Request, w http.ResponseWriter, fi os.FileInfo) (handled bool) {
return HandleGenericTimeCache(req, w, fi.ModTime())
}

// HandleGenericTimeCache handles time-based caching for a HTTP request
func HandleGenericTimeCache(req *http.Request, w http.ResponseWriter, lastModified time.Time) (handled bool) {
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)

ifModifiedSince := req.Header.Get("If-Modified-Since")
if ifModifiedSince != "" {
t, err := time.Parse(http.TimeFormat, ifModifiedSince)
if err == nil && fi.ModTime().Unix() <= t.Unix() {
if err == nil && lastModified.Unix() <= t.Unix() {
w.WriteHeader(http.StatusNotModified)
return true
}
}

w.Header().Set("Last-Modified", fi.ModTime().Format(http.TimeFormat))
w.Header().Set("Last-Modified", lastModified.Format(http.TimeFormat))
return false
}

Expand Down
58 changes: 42 additions & 16 deletions routers/web/repo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
package repo

import (
"path"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/httpcache"
Expand Down Expand Up @@ -79,34 +82,57 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error {
return common.ServeBlob(ctx, blob)
}

func getBlobForEntry(ctx *context.Context) *git.Blob {
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
if err != nil {
ctx.ServerError("GetBlobByPath", err)
return nil
}

if entry.IsDir() || entry.IsSubModule() {
ctx.NotFound("GetBlobByPath", nil)
return nil
}

var c *git.LastCommitCache
if setting.CacheService.LastCommit.Enabled && ctx.Repo.CommitsCount >= setting.CacheService.LastCommit.CommitsCount {
c = git.NewLastCommitCache(ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, setting.LastCommitCacheTTLSeconds, cache.GetCache())
}

info, _, err := git.Entries([]*git.TreeEntry{entry}).GetCommitsInfo(ctx, ctx.Repo.Commit, path.Dir("/" + ctx.Repo.TreePath)[1:], c)
if err != nil {
ctx.ServerError("GetCommitsInfo", err)
return nil
}

if len(info) == 1 && httpcache.HandleGenericTimeCache(ctx.Req, ctx.Resp, info[0].Commit.Committer.When) {
// Not Modified
return nil
}

return entry.Blob()
}

// SingleDownload download a file by repos path
func SingleDownload(ctx *context.Context) {
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetBlobByPath", nil)
} else {
ctx.ServerError("GetBlobByPath", err)
}
blob := getBlobForEntry(ctx)
if blob == nil {
return
}
if err = common.ServeBlob(ctx, blob); err != nil {

if err := common.ServeBlob(ctx, blob); err != nil {
ctx.ServerError("ServeBlob", err)
}
}

// SingleDownloadOrLFS download a file by repos path redirecting to LFS if necessary
func SingleDownloadOrLFS(ctx *context.Context) {
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetBlobByPath", nil)
} else {
ctx.ServerError("GetBlobByPath", err)
}
blob := getBlobForEntry(ctx)
if blob == nil {
return
}
if err = ServeBlobOrLFS(ctx, blob); err != nil {

if err := ServeBlobOrLFS(ctx, blob); err != nil {
ctx.ServerError("ServeBlobOrLFS", err)
}
}
Expand Down