diff options
author | KN4CK3R <KN4CK3R@users.noreply.github.com> | 2021-04-12 16:49:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-12 10:49:26 -0400 |
commit | a35a5b225c6f49a91459ae3e9e97d0ad6a7c16bd (patch) | |
tree | 0f31bd0eb9ef1972c5049fc173db974b46e41fe3 /routers/repo/download.go | |
parent | 6d2866f20c7ba725a02d8ecdd42810291ef4f97c (diff) | |
download | gitea-a35a5b225c6f49a91459ae3e9e97d0ad6a7c16bd.tar.gz gitea-a35a5b225c6f49a91459ae3e9e97d0ad6a7c16bd.zip |
Add ETag header (#15370)
* Add ETag header.
* Comply with RFC 7232.
* Moved logic into httpcache.go
* Changed name.
* Lint
* Implemented If-None-Match list.
* Fixed missing header on *
* Removed weak etag support.
* Removed * support.
* Added unit test.
* Lint
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'routers/repo/download.go')
-rw-r--r-- | routers/repo/download.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/routers/repo/download.go b/routers/repo/download.go index 63a9ca47d7..1eedec8cb1 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" ) @@ -31,6 +32,7 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader) } ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400") + if size >= 0 { ctx.Resp.Header().Set("Content-Length", fmt.Sprintf("%d", size)) } else { @@ -71,6 +73,10 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader) // ServeBlob download a git.Blob func ServeBlob(ctx *context.Context, blob *git.Blob) error { + if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`) { + return nil + } + dataRc, err := blob.DataAsync() if err != nil { return err @@ -86,6 +92,10 @@ func ServeBlob(ctx *context.Context, blob *git.Blob) error { // 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()+`"`) { + return nil + } + dataRc, err := blob.DataAsync() if err != nil { return err @@ -102,6 +112,9 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error { if meta == nil { return ServeBlob(ctx, blob) } + if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+pointer.Oid+`"`) { + return nil + } lfsDataRc, err := lfs.ReadMetaObject(meta.Pointer) if err != nil { return err |