-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Enable caching on assets and avatars #3376
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eb16186
Enable caching on assets and avatars
thehowl 4e66d26
Only set avatar in user BeforeUpdate when there is no avatar set
thehowl 1543ddc
add error checking after stat
thehowl dbc536e
gofmt
thehowl 54926d7
Change cache time for avatars to an hour
thehowl 79fadd1
Merge branch 'master' into fix-3323
lafriks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,13 @@ | |
package public | ||
|
||
import ( | ||
"encoding/base64" | ||
"log" | ||
"net/http" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
"gopkg.in/macaron.v1" | ||
|
@@ -19,15 +25,135 @@ import ( | |
// Options represents the available options to configure the macaron handler. | ||
type Options struct { | ||
Directory string | ||
IndexFile string | ||
SkipLogging bool | ||
// if set to true, will enable caching. Expires header will also be set to | ||
// expire after the defined time. | ||
ExpiresAfter time.Duration | ||
FileSystem http.FileSystem | ||
Prefix string | ||
} | ||
|
||
// Custom implements the macaron static handler for serving custom assets. | ||
func Custom(opts *Options) macaron.Handler { | ||
return macaron.Static( | ||
path.Join(setting.CustomPath, "public"), | ||
macaron.StaticOptions{ | ||
SkipLogging: opts.SkipLogging, | ||
}, | ||
) | ||
return opts.staticHandler(path.Join(setting.CustomPath, "public")) | ||
} | ||
|
||
// staticFileSystem implements http.FileSystem interface. | ||
type staticFileSystem struct { | ||
dir *http.Dir | ||
} | ||
|
||
func newStaticFileSystem(directory string) staticFileSystem { | ||
if !filepath.IsAbs(directory) { | ||
directory = filepath.Join(macaron.Root, directory) | ||
} | ||
dir := http.Dir(directory) | ||
return staticFileSystem{&dir} | ||
} | ||
|
||
func (fs staticFileSystem) Open(name string) (http.File, error) { | ||
return fs.dir.Open(name) | ||
} | ||
|
||
// StaticHandler sets up a new middleware for serving static files in the | ||
func StaticHandler(dir string, opts *Options) macaron.Handler { | ||
return opts.staticHandler(dir) | ||
} | ||
|
||
func (opts *Options) staticHandler(dir string) macaron.Handler { | ||
// Defaults | ||
if len(opts.IndexFile) == 0 { | ||
opts.IndexFile = "index.html" | ||
} | ||
// Normalize the prefix if provided | ||
if opts.Prefix != "" { | ||
// Ensure we have a leading '/' | ||
if opts.Prefix[0] != '/' { | ||
opts.Prefix = "/" + opts.Prefix | ||
} | ||
// Remove any trailing '/' | ||
opts.Prefix = strings.TrimRight(opts.Prefix, "/") | ||
} | ||
if opts.FileSystem == nil { | ||
opts.FileSystem = newStaticFileSystem(dir) | ||
} | ||
|
||
return func(ctx *macaron.Context, log *log.Logger) { | ||
opts.handle(ctx, log, opts) | ||
} | ||
} | ||
|
||
func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options) bool { | ||
if ctx.Req.Method != "GET" && ctx.Req.Method != "HEAD" { | ||
return false | ||
} | ||
|
||
file := ctx.Req.URL.Path | ||
// if we have a prefix, filter requests by stripping the prefix | ||
if opt.Prefix != "" { | ||
if !strings.HasPrefix(file, opt.Prefix) { | ||
return false | ||
} | ||
file = file[len(opt.Prefix):] | ||
if file != "" && file[0] != '/' { | ||
return false | ||
} | ||
} | ||
|
||
f, err := opt.FileSystem.Open(file) | ||
if err != nil { | ||
return false | ||
} | ||
defer f.Close() | ||
|
||
fi, err := f.Stat() | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should at least log error in such case |
||
log.Printf("[Static] %q exists, but fails to open: %v", file, err) | ||
return true | ||
} | ||
|
||
// Try to serve index file | ||
if fi.IsDir() { | ||
// Redirect if missing trailing slash. | ||
if !strings.HasSuffix(ctx.Req.URL.Path, "/") { | ||
http.Redirect(ctx.Resp, ctx.Req.Request, ctx.Req.URL.Path+"/", http.StatusFound) | ||
return true | ||
} | ||
|
||
f, err = opt.FileSystem.Open(file) | ||
if err != nil { | ||
return false // Discard error. | ||
} | ||
defer f.Close() | ||
|
||
fi, err = f.Stat() | ||
if err != nil || fi.IsDir() { | ||
return true | ||
} | ||
} | ||
|
||
if !opt.SkipLogging { | ||
log.Println("[Static] Serving " + file) | ||
} | ||
|
||
// Add an Expires header to the static content | ||
if opt.ExpiresAfter > 0 { | ||
ctx.Resp.Header().Set("Expires", time.Now().Add(opt.ExpiresAfter).UTC().Format(http.TimeFormat)) | ||
tag := GenerateETag(string(fi.Size()), fi.Name(), fi.ModTime().UTC().Format(http.TimeFormat)) | ||
ctx.Resp.Header().Set("ETag", tag) | ||
if ctx.Req.Header.Get("If-None-Match") == tag { | ||
ctx.Resp.WriteHeader(304) | ||
return false | ||
} | ||
} | ||
|
||
http.ServeContent(ctx.Resp, ctx.Req.Request, file, fi.ModTime(), f) | ||
return true | ||
} | ||
|
||
// GenerateETag generates an ETag based on size, filename and file modification time | ||
func GenerateETag(fileSize, fileName, modTime string) string { | ||
etag := fileSize + fileName + modTime | ||
return base64.StdEncoding.EncodeToString([]byte(etag)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of staticHandler has been copied over from parts of macaron. The handle function was modified to use ExpireAfter instead of the Expires function, and also now it has proper support for ETags (by reading the req header
If-None-Match
).