2016-11-29 19:26:36 +03:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package public
|
|
|
|
|
2017-01-29 01:14:56 +03:00
|
|
|
import (
|
2018-02-04 01:37:05 +03:00
|
|
|
"net/http"
|
2021-05-30 13:25:11 +03:00
|
|
|
"os"
|
2017-01-29 01:14:56 +03:00
|
|
|
"path"
|
2018-02-04 01:37:05 +03:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-01-29 01:14:56 +03:00
|
|
|
|
2020-11-18 01:44:52 +03:00
|
|
|
"code.gitea.io/gitea/modules/httpcache"
|
2021-05-30 13:25:11 +03:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-01-29 01:14:56 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2021-01-29 18:35:30 +03:00
|
|
|
// Options represents the available options to configure the handler.
|
2016-11-29 19:26:36 +03:00
|
|
|
type Options struct {
|
|
|
|
Directory string
|
2020-11-18 01:44:52 +03:00
|
|
|
Prefix string
|
2021-05-30 13:25:11 +03:00
|
|
|
CorsHandler func(http.Handler) http.Handler
|
2016-11-29 19:26:36 +03:00
|
|
|
}
|
2017-01-29 01:14:56 +03:00
|
|
|
|
2021-05-30 13:25:11 +03:00
|
|
|
// AssetsHandler implements the static handler for serving custom or original assets.
|
|
|
|
func AssetsHandler(opts *Options) func(next http.Handler) http.Handler {
|
|
|
|
var custPath = filepath.Join(setting.CustomPath, "public")
|
|
|
|
if !filepath.IsAbs(custPath) {
|
|
|
|
custPath = filepath.Join(setting.AppWorkPath, custPath)
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(opts.Directory) {
|
|
|
|
opts.Directory = filepath.Join(setting.AppWorkPath, opts.Directory)
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(opts.Prefix, "/") {
|
|
|
|
opts.Prefix += "/"
|
2018-02-04 01:37:05 +03:00
|
|
|
}
|
|
|
|
|
2021-05-30 13:25:11 +03:00
|
|
|
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
|
|
|
|
}
|
2018-02-04 01:37:05 +03:00
|
|
|
|
2021-05-30 13:25:11 +03:00
|
|
|
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
|
|
|
|
}
|
2018-02-04 01:37:05 +03:00
|
|
|
|
2021-05-30 13:25:11 +03:00
|
|
|
// custom files
|
|
|
|
if opts.handle(resp, req, http.Dir(custPath), file) {
|
|
|
|
return
|
2020-11-13 15:51:07 +03:00
|
|
|
}
|
2018-02-04 01:37:05 +03:00
|
|
|
|
2021-05-30 13:25:11 +03:00
|
|
|
// internal files
|
|
|
|
if opts.handle(resp, req, fileSystem(opts.Directory), file) {
|
|
|
|
return
|
2020-11-13 15:51:07 +03:00
|
|
|
}
|
2021-05-30 13:25:11 +03:00
|
|
|
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
2020-11-13 15:51:07 +03:00
|
|
|
})
|
2018-02-04 01:37:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 07:25:17 +03:00
|
|
|
// parseAcceptEncoding parse Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 as compress methods
|
|
|
|
func parseAcceptEncoding(val string) map[string]bool {
|
|
|
|
parts := strings.Split(val, ";")
|
|
|
|
var types = make(map[string]bool)
|
|
|
|
for _, v := range strings.Split(parts[0], ",") {
|
|
|
|
types[strings.TrimSpace(v)] = true
|
|
|
|
}
|
|
|
|
return types
|
|
|
|
}
|
|
|
|
|
2021-05-30 13:25:11 +03:00
|
|
|
func (opts *Options) handle(w http.ResponseWriter, req *http.Request, fs http.FileSystem, file string) bool {
|
|
|
|
// use clean to keep the file is a valid path with no . or ..
|
|
|
|
f, err := fs.Open(path.Clean(file))
|
2018-02-04 01:37:05 +03:00
|
|
|
if err != nil {
|
2021-05-30 13:25:11 +03:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
2020-04-19 00:01:06 +03:00
|
|
|
}
|
2021-05-30 13:25:11 +03:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
log.Error("[Static] Open %q failed: %v", file, err)
|
|
|
|
return true
|
2018-02-04 01:37:05 +03:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
2021-05-30 13:25:11 +03:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
log.Error("[Static] %q exists, but fails to open: %v", file, err)
|
2018-02-04 01:37:05 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to serve index file
|
|
|
|
if fi.IsDir() {
|
2021-05-30 13:25:11 +03:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return true
|
2018-02-04 01:37:05 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 17:49:26 +03:00
|
|
|
if httpcache.HandleFileETagCache(req, w, fi) {
|
2020-11-18 01:44:52 +03:00
|
|
|
return true
|
2018-02-04 01:37:05 +03:00
|
|
|
}
|
|
|
|
|
2021-05-30 13:25:11 +03:00
|
|
|
serveContent(w, req, fi, fi.ModTime(), f)
|
2018-02-04 01:37:05 +03:00
|
|
|
return true
|
|
|
|
}
|