summaryrefslogtreecommitdiffstats
path: root/modules/public
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2022-01-20 19:41:25 +0800
committerGitHub <noreply@github.com>2022-01-20 19:41:25 +0800
commit5bf8d5445e3e3ec6b2be156654dcb233d78a570e (patch)
tree9a426fd03b619947b88931465c1a645540b94bfb /modules/public
parentbbd30787d3d33fe66896c7e758e6922be459e252 (diff)
downloadgitea-5bf8d5445e3e3ec6b2be156654dcb233d78a570e.tar.gz
gitea-5bf8d5445e3e3ec6b2be156654dcb233d78a570e.zip
Refactor Router Logger (#17308)
Make router logger more friendly, show the related function name/file/line. [BREAKING] This PR substantially changes the logging format of the router logger. If you use this logging for monitoring e.g. fail2ban you will need to update this to match the new format.
Diffstat (limited to 'modules/public')
-rw-r--r--modules/public/public.go89
1 files changed, 43 insertions, 46 deletions
diff --git a/modules/public/public.go b/modules/public/public.go
index a58709d86f..f4a769f27a 100644
--- a/modules/public/public.go
+++ b/modules/public/public.go
@@ -23,8 +23,11 @@ type Options struct {
CorsHandler func(http.Handler) http.Handler
}
-// AssetsHandler implements the static handler for serving custom or original assets.
-func AssetsHandler(opts *Options) func(next http.Handler) http.Handler {
+// AssetsURLPathPrefix is the path prefix for static asset files
+const AssetsURLPathPrefix = "/assets/"
+
+// AssetsHandlerFunc implements the static handler for serving custom or original assets.
+func AssetsHandlerFunc(opts *Options) http.HandlerFunc {
var custPath = filepath.Join(setting.CustomPath, "public")
if !filepath.IsAbs(custPath) {
custPath = filepath.Join(setting.AppWorkPath, custPath)
@@ -36,52 +39,46 @@ func AssetsHandler(opts *Options) func(next http.Handler) http.Handler {
opts.Prefix += "/"
}
- return func(next http.Handler) http.Handler {
- return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
- if !strings.HasPrefix(req.URL.Path, opts.Prefix) {
- next.ServeHTTP(resp, req)
- return
- }
- if req.Method != "GET" && req.Method != "HEAD" {
- resp.WriteHeader(http.StatusNotFound)
- return
- }
-
- file := req.URL.Path
- file = file[len(opts.Prefix):]
- if len(file) == 0 {
- resp.WriteHeader(http.StatusNotFound)
- return
- }
- if strings.Contains(file, "\\") {
- resp.WriteHeader(http.StatusBadRequest)
- return
- }
- file = "/" + file
-
- var written bool
- if opts.CorsHandler != nil {
- written = true
- opts.CorsHandler(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
- written = false
- })).ServeHTTP(resp, req)
- }
- if written {
- return
- }
-
- // custom files
- if opts.handle(resp, req, http.Dir(custPath), file) {
- return
- }
-
- // internal files
- if opts.handle(resp, req, fileSystem(opts.Directory), file) {
- return
- }
+ return func(resp http.ResponseWriter, req *http.Request) {
+ if req.Method != "GET" && req.Method != "HEAD" {
+ resp.WriteHeader(http.StatusNotFound)
+ return
+ }
+ file := req.URL.Path
+ file = file[len(opts.Prefix):]
+ if len(file) == 0 {
resp.WriteHeader(http.StatusNotFound)
- })
+ return
+ }
+ if strings.Contains(file, "\\") {
+ resp.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ file = "/" + file
+
+ var written bool
+ if opts.CorsHandler != nil {
+ written = true
+ opts.CorsHandler(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
+ written = false
+ })).ServeHTTP(resp, req)
+ }
+ if written {
+ return
+ }
+
+ // custom files
+ if opts.handle(resp, req, http.Dir(custPath), file) {
+ return
+ }
+
+ // internal files
+ if opts.handle(resp, req, fileSystem(opts.Directory), file) {
+ return
+ }
+
+ resp.WriteHeader(http.StatusNotFound)
}
}