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

Set the LastModified header for raw files #18356

Merged
merged 12 commits into from
May 9, 2022
Merged
39 changes: 37 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 Expand Up @@ -85,3 +90,33 @@ func checkIfNoneMatchIsValid(req *http.Request, etag string) bool {
}
return false
}

// HandleGenericETagTimeCache handles ETag-based caching with Last-Modified caching for a HTTP request.
// It returns true if the request was handled.
func HandleGenericETagTimeCache(req *http.Request, w http.ResponseWriter, etag string, lastModified time.Time) (handled bool) {
if len(etag) > 0 {
w.Header().Set("Etag", etag)
}
if !lastModified.IsZero() {
w.Header().Set("Last-Modified", lastModified.Format(http.TimeFormat))
}

if len(etag) > 0 {
if checkIfNoneMatchIsValid(req, etag) {
w.WriteHeader(http.StatusNotModified)
return true
}
}
if !lastModified.IsZero() {
ifModifiedSince := req.Header.Get("If-Modified-Since")
if ifModifiedSince != "" {
t, err := time.Parse(http.TimeFormat, ifModifiedSince)
if err == nil && lastModified.Unix() <= t.Unix() {
w.WriteHeader(http.StatusNotModified)
return true
}
}
}
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
return false
}
44 changes: 36 additions & 8 deletions routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (
"encoding/base64"
"fmt"
"net/http"
"path"
"time"

"code.gitea.io/gitea/models"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/common"
Expand Down Expand Up @@ -62,6 +65,14 @@ func GetRawFile(ctx *context.APIContext) {
return
}

blob, lastModified := getBlobForEntry(ctx)

if err := common.ServeBlob(ctx.Context, blob, lastModified); err != nil {
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
}
}

func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, lastModified time.Time) {
commit := ctx.Repo.Commit

if ref := ctx.FormTrim("ref"); len(ref) > 0 {
Expand All @@ -77,18 +88,35 @@ func GetRawFile(ctx *context.APIContext) {
}
}

blob, err := commit.GetBlobByPath(ctx.Repo.TreePath)
entry, err := commit.GetTreeEntryByPath(ctx.Repo.TreePath)
if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err)
}
ctx.Error(http.StatusInternalServerError, "GetTreeEntryByPath", err)
return
}
if err = common.ServeBlob(ctx.Context, blob); err != nil {
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)

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

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.Error(http.StatusInternalServerError, "GetCommitsInfo", err)
return
}

if len(info) == 1 {
// Not Modified
lastModified = info[0].Commit.Committer.When
}
blob = entry.Blob()

return
}

// GetArchive get archive of a repository
Expand Down
5 changes: 3 additions & 2 deletions routers/common/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path"
"path/filepath"
"strings"
"time"

"code.gitea.io/gitea/modules/charset"
"code.gitea.io/gitea/modules/context"
Expand All @@ -22,8 +23,8 @@ import (
)

// ServeBlob download a git.Blob
func ServeBlob(ctx *context.Context, blob *git.Blob) error {
if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`) {
func ServeBlob(ctx *context.Context, blob *git.Blob, lastModified time.Time) error {
if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
return nil
}

Expand Down
72 changes: 50 additions & 22 deletions routers/web/repo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
package repo

import (
"path"
"time"

"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 All @@ -18,8 +22,8 @@ import (
)

// ServeBlobOrLFS download a git.Blob redirecting to LFS if necessary
func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error {
if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`) {
func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob, lastModified time.Time) error {
if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
return nil
}

Expand All @@ -45,7 +49,7 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error {
log.Error("ServeBlobOrLFS: Close: %v", err)
}
closed = true
return common.ServeBlob(ctx, blob)
return common.ServeBlob(ctx, blob, lastModified)
}
if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+pointer.Oid+`"`) {
return nil
Expand Down Expand Up @@ -76,37 +80,61 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error {
}
closed = true

return common.ServeBlob(ctx, blob)
return common.ServeBlob(ctx, blob, lastModified)
}

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

if entry.IsDir() || entry.IsSubModule() {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
ctx.NotFound("getBlobForEntry", nil)
return
}

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
}

if len(info) == 1 {
// Not Modified
lastModified = info[0].Commit.Committer.When
}
blob = entry.Blob()

return
}

// 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, lastModified := getBlobForEntry(ctx)
if blob == nil {
return
}
if err = common.ServeBlob(ctx, blob); err != nil {

if err := common.ServeBlob(ctx, blob, lastModified); 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, lastModified := getBlobForEntry(ctx)
if blob == nil {
return
}
if err = ServeBlobOrLFS(ctx, blob); err != nil {

if err := ServeBlobOrLFS(ctx, blob, lastModified); err != nil {
ctx.ServerError("ServeBlobOrLFS", err)
}
}
Expand All @@ -122,7 +150,7 @@ func DownloadByID(ctx *context.Context) {
}
return
}
if err = common.ServeBlob(ctx, blob); err != nil {
if err = common.ServeBlob(ctx, blob, time.Time{}); err != nil {
6543 marked this conversation as resolved.
Show resolved Hide resolved
ctx.ServerError("ServeBlob", err)
}
}
Expand All @@ -138,7 +166,7 @@ func DownloadByIDOrLFS(ctx *context.Context) {
}
return
}
if err = ServeBlobOrLFS(ctx, blob); err != nil {
if err = ServeBlobOrLFS(ctx, blob, time.Time{}); err != nil {
ctx.ServerError("ServeBlob", err)
}
}
3 changes: 2 additions & 1 deletion routers/web/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"path/filepath"
"strings"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/unit"
Expand Down Expand Up @@ -612,7 +613,7 @@ func WikiRaw(ctx *context.Context) {
}

if entry != nil {
if err = common.ServeBlob(ctx, entry.Blob()); err != nil {
if err = common.ServeBlob(ctx, entry.Blob(), time.Time{}); err != nil {
ctx.ServerError("ServeBlob", err)
}
return
Expand Down