2016-11-29 17:26:36 +01:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-11-29 17:26:36 +01:00
|
|
|
|
|
|
|
package public
|
|
|
|
|
2017-01-28 23:14:56 +01:00
|
|
|
import (
|
2018-02-03 23:37:05 +01:00
|
|
|
"net/http"
|
2021-05-30 12:25:11 +02:00
|
|
|
"os"
|
2017-01-28 23:14:56 +01:00
|
|
|
"path"
|
2018-02-03 23:37:05 +01:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-01-28 23:14:56 +01:00
|
|
|
|
2022-10-12 07:18:26 +02:00
|
|
|
"code.gitea.io/gitea/modules/container"
|
2020-11-17 23:44:52 +01:00
|
|
|
"code.gitea.io/gitea/modules/httpcache"
|
2021-05-30 12:25:11 +02:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-01-28 23:14:56 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2021-01-29 16:35:30 +01:00
|
|
|
// Options represents the available options to configure the handler.
|
2016-11-29 17:26:36 +01:00
|
|
|
type Options struct {
|
|
|
|
Directory string
|
2020-11-17 23:44:52 +01:00
|
|
|
Prefix string
|
2021-05-30 12:25:11 +02:00
|
|
|
CorsHandler func(http.Handler) http.Handler
|
2016-11-29 17:26:36 +01:00
|
|
|
}
|
2017-01-28 23:14:56 +01:00
|
|
|
|
2022-01-20 12:41:25 +01:00
|
|
|
// 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 {
|
2022-01-20 18:46:10 +01:00
|
|
|
custPath := filepath.Join(setting.CustomPath, "public")
|
2021-05-30 12:25:11 +02:00
|
|
|
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-03 23:37:05 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 12:41:25 +01:00
|
|
|
return func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != "GET" && req.Method != "HEAD" {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2018-02-03 23:37:05 +01:00
|
|
|
|
2022-01-20 12:41:25 +01: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
|
2021-05-30 12:25:11 +02:00
|
|
|
|
2022-01-20 12:41:25 +01:00
|
|
|
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-03 23:37:05 +01:00
|
|
|
|
2022-01-20 12:41:25 +01:00
|
|
|
// custom files
|
|
|
|
if opts.handle(resp, req, http.Dir(custPath), file) {
|
|
|
|
return
|
|
|
|
}
|
2018-02-03 23:37:05 +01:00
|
|
|
|
2022-01-20 12:41:25 +01:00
|
|
|
// internal files
|
|
|
|
if opts.handle(resp, req, fileSystem(opts.Directory), file) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-30 12:25:11 +02:00
|
|
|
|
2022-01-20 12:41:25 +01:00
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
2018-02-03 23:37:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 05:25:17 +01:00
|
|
|
// parseAcceptEncoding parse Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 as compress methods
|
2022-10-12 07:18:26 +02:00
|
|
|
func parseAcceptEncoding(val string) container.Set[string] {
|
2020-12-24 05:25:17 +01:00
|
|
|
parts := strings.Split(val, ";")
|
2022-10-12 07:18:26 +02:00
|
|
|
types := make(container.Set[string])
|
2020-12-24 05:25:17 +01:00
|
|
|
for _, v := range strings.Split(parts[0], ",") {
|
2022-10-12 07:18:26 +02:00
|
|
|
types.Add(strings.TrimSpace(v))
|
2020-12-24 05:25:17 +01:00
|
|
|
}
|
|
|
|
return types
|
|
|
|
}
|
|
|
|
|
2022-01-23 13:19:49 +01:00
|
|
|
// setWellKnownContentType will set the Content-Type if the file is a well-known type.
|
|
|
|
// See the comments of detectWellKnownMimeType
|
|
|
|
func setWellKnownContentType(w http.ResponseWriter, file string) {
|
|
|
|
mimeType := detectWellKnownMimeType(filepath.Ext(file))
|
|
|
|
if mimeType != "" {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-30 12:25:11 +02: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-03 23:37:05 +01:00
|
|
|
if err != nil {
|
2021-05-30 12:25:11 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
2020-04-18 23:01:06 +02:00
|
|
|
}
|
2021-05-30 12:25:11 +02:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
log.Error("[Static] Open %q failed: %v", file, err)
|
|
|
|
return true
|
2018-02-03 23:37:05 +01:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
2021-05-30 12:25:11 +02:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
log.Error("[Static] %q exists, but fails to open: %v", file, err)
|
2018-02-03 23:37:05 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to serve index file
|
|
|
|
if fi.IsDir() {
|
2021-05-30 12:25:11 +02:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return true
|
2018-02-03 23:37:05 +01:00
|
|
|
}
|
|
|
|
|
2021-04-12 16:49:26 +02:00
|
|
|
if httpcache.HandleFileETagCache(req, w, fi) {
|
2020-11-17 23:44:52 +01:00
|
|
|
return true
|
2018-02-03 23:37:05 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 13:19:49 +01:00
|
|
|
setWellKnownContentType(w, file)
|
|
|
|
|
2021-05-30 12:25:11 +02:00
|
|
|
serveContent(w, req, fi, fi.ModTime(), f)
|
2018-02-03 23:37:05 +01:00
|
|
|
return true
|
|
|
|
}
|