2020-11-13 18:21:07 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-11-13 18:21:07 +05:30
|
|
|
|
2021-06-09 05:03:54 +05:30
|
|
|
package web
|
2020-11-13 18:21:07 +05:30
|
|
|
|
|
|
|
import (
|
2022-08-28 15:13:25 +05:30
|
|
|
goctx "context"
|
2020-11-13 18:21:07 +05:30
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2021-01-20 07:17:43 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-11-18 04:14:52 +05:30
|
|
|
"code.gitea.io/gitea/modules/httpcache"
|
2020-11-13 18:21:07 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2023-03-08 17:47:39 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-30 14:25:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2022-01-20 17:11:25 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/routing"
|
2021-06-09 23:23:16 +05:30
|
|
|
"code.gitea.io/gitea/services/auth"
|
2020-11-13 18:21:07 +05:30
|
|
|
|
2021-01-05 18:35:40 +05:30
|
|
|
"gitea.com/go-chi/session"
|
2020-11-13 18:21:07 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func storageHandler(storageSetting setting.Storage, prefix string, objStore storage.ObjectStorage) func(next http.Handler) http.Handler {
|
2022-03-23 02:32:26 +05:30
|
|
|
prefix = strings.Trim(prefix, "/")
|
2022-01-20 17:11:25 +05:30
|
|
|
funcInfo := routing.GetFuncInfo(storageHandler, prefix)
|
2020-11-13 18:21:07 +05:30
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
if storageSetting.ServeDirect {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != "GET" && req.Method != "HEAD" {
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-23 02:32:26 +05:30
|
|
|
if !strings.HasPrefix(req.URL.Path, "/"+prefix+"/") {
|
2020-11-13 18:21:07 +05:30
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
2022-01-20 17:11:25 +05:30
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
2020-11-13 18:21:07 +05:30
|
|
|
|
2022-03-23 02:32:26 +05:30
|
|
|
rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
|
2023-03-22 01:32:49 +05:30
|
|
|
rPath = util.PathJoinRelX(rPath)
|
2022-03-23 02:32:26 +05:30
|
|
|
|
2020-11-13 18:21:07 +05:30
|
|
|
u, err := objStore.URL(rPath, path.Base(rPath))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
|
|
|
|
log.Warn("Unable to find %s %s", prefix, rPath)
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Error(w, "file not found", http.StatusNotFound)
|
2020-11-13 18:21:07 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Error("Error whilst getting URL for %s %s. Error: %v", prefix, rPath, err)
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Error(w, fmt.Sprintf("Error whilst getting URL for %s %s", prefix, rPath), http.StatusInternalServerError)
|
2020-11-13 18:21:07 +05:30
|
|
|
return
|
|
|
|
}
|
2022-03-23 02:32:26 +05:30
|
|
|
|
2020-11-13 18:21:07 +05:30
|
|
|
http.Redirect(
|
|
|
|
w,
|
|
|
|
req,
|
|
|
|
u.String(),
|
2022-05-19 20:50:34 +05:30
|
|
|
http.StatusTemporaryRedirect,
|
2020-11-13 18:21:07 +05:30
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != "GET" && req.Method != "HEAD" {
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-23 02:32:26 +05:30
|
|
|
if !strings.HasPrefix(req.URL.Path, "/"+prefix+"/") {
|
2020-11-13 18:21:07 +05:30
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
return
|
|
|
|
}
|
2022-01-20 17:11:25 +05:30
|
|
|
routing.UpdateFuncInfo(req.Context(), funcInfo)
|
2020-11-13 18:21:07 +05:30
|
|
|
|
2022-03-23 02:32:26 +05:30
|
|
|
rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/")
|
2023-03-22 01:32:49 +05:30
|
|
|
rPath = util.PathJoinRelX(rPath)
|
|
|
|
if rPath == "" || rPath == "." {
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Error(w, "file not found", http.StatusNotFound)
|
2021-03-23 22:50:24 +05:30
|
|
|
return
|
|
|
|
}
|
2020-11-18 04:14:52 +05:30
|
|
|
|
|
|
|
fi, err := objStore.Stat(rPath)
|
|
|
|
if err == nil && httpcache.HandleTimeCache(req, w, fi) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// If we have matched and access to release or issue
|
2020-11-13 18:21:07 +05:30
|
|
|
fr, err := objStore.Open(rPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) {
|
|
|
|
log.Warn("Unable to find %s %s", prefix, rPath)
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Error(w, "file not found", http.StatusNotFound)
|
2020-11-13 18:21:07 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err)
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Error(w, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath), http.StatusInternalServerError)
|
2020-11-13 18:21:07 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fr.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(w, fr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error whilst rendering %s %s. Error: %v", prefix, rPath, err)
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Error(w, fmt.Sprintf("Error whilst rendering %s %s", prefix, rPath), http.StatusInternalServerError)
|
2020-11-13 18:21:07 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 05:03:54 +05:30
|
|
|
type dataStore map[string]interface{}
|
2020-11-13 18:21:07 +05:30
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
func (d *dataStore) GetData() map[string]interface{} {
|
2021-06-09 05:03:54 +05:30
|
|
|
return *d
|
2020-11-13 18:21:07 +05:30
|
|
|
}
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
// Recovery returns a middleware that recovers from any panics and writes a 500 and a log if so.
|
|
|
|
// This error will be created with the gitea 500 page.
|
2022-08-28 15:13:25 +05:30
|
|
|
func Recovery(ctx goctx.Context) func(next http.Handler) http.Handler {
|
|
|
|
_, rnd := templates.HTMLRenderer(ctx)
|
2021-01-26 21:06:53 +05:30
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2022-01-20 17:11:25 +05:30
|
|
|
routing.UpdatePanicError(req.Context(), err)
|
|
|
|
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, log.Stack(2))
|
|
|
|
log.Error("%s", combinedErr)
|
2021-01-26 21:06:53 +05:30
|
|
|
|
|
|
|
sessionStore := session.GetSession(req)
|
2020-11-16 13:03:41 +05:30
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
lc := middleware.Locale(w, req)
|
|
|
|
store := dataStore{
|
2021-06-09 05:03:54 +05:30
|
|
|
"Language": lc.Language(),
|
|
|
|
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
|
2022-06-28 02:28:46 +05:30
|
|
|
"locale": lc,
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
2020-11-13 18:21:07 +05:30
|
|
|
|
2023-04-18 00:35:19 +05:30
|
|
|
// TODO: this recovery handler is usually called without Gitea's web context, so we shouldn't touch that context too much
|
|
|
|
// Otherwise, the 500 page may cause new panics, eg: cache.GetContextWithData, it makes the developer&users couldn't find the original panic
|
|
|
|
user := context.GetContextUser(req) // almost always nil
|
2021-05-15 21:02:09 +05:30
|
|
|
if user == nil {
|
|
|
|
// Get user from session if logged in - do not attempt to sign-in
|
2021-06-09 23:23:16 +05:30
|
|
|
user = auth.SessionUser(sessionStore)
|
2021-05-15 21:02:09 +05:30
|
|
|
}
|
2020-11-13 18:21:07 +05:30
|
|
|
|
2023-03-09 02:10:04 +05:30
|
|
|
httpcache.SetCacheControlInHeader(w.Header(), 0, "no-transform")
|
2021-08-07 02:17:10 +05:30
|
|
|
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
|
2020-11-18 02:20:06 +05:30
|
|
|
|
2023-04-18 00:35:19 +05:30
|
|
|
if !setting.IsProd || (user != nil && user.IsAdmin) {
|
2021-06-09 05:03:54 +05:30
|
|
|
store["ErrorMsg"] = combinedErr
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
2023-04-18 00:35:19 +05:30
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
log.Error("HTML render in Recovery handler panics again: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2022-03-23 10:24:07 +05:30
|
|
|
err = rnd.HTML(w, http.StatusInternalServerError, "status/500", templates.BaseVars().Merge(store))
|
2021-01-26 21:06:53 +05:30
|
|
|
if err != nil {
|
2023-04-18 00:35:19 +05:30
|
|
|
log.Error("HTML render in Recovery handler fails again: %v", err)
|
2021-01-26 21:06:53 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-11-18 02:20:06 +05:30
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
})
|
2020-11-18 02:20:06 +05:30
|
|
|
}
|
2020-11-13 18:21:07 +05:30
|
|
|
}
|