2014-03-15 19:01:50 +08:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2016-03-11 11:56:52 -05:00
|
|
|
package context
|
2014-03-15 19:01:50 +08:00
|
|
|
|
|
|
|
|
import (
|
2014-03-15 09:17:16 -04:00
|
|
|
"fmt"
|
2014-04-16 00:27:29 +08:00
|
|
|
"io"
|
2014-03-15 19:01:50 +08:00
|
|
|
"net/http"
|
2014-03-22 16:40:09 -04:00
|
|
|
"strings"
|
2014-03-19 21:57:55 +08:00
|
|
|
"time"
|
2014-03-15 19:01:50 +08:00
|
|
|
|
2015-10-15 21:28:12 -04:00
|
|
|
"github.com/go-macaron/cache"
|
|
|
|
|
"github.com/go-macaron/csrf"
|
|
|
|
|
"github.com/go-macaron/i18n"
|
|
|
|
|
"github.com/go-macaron/session"
|
|
|
|
|
"gopkg.in/macaron.v1"
|
2020-02-20 02:25:02 +08:00
|
|
|
log "unknwon.dev/clog/v2"
|
2014-03-21 21:06:47 +08:00
|
|
|
|
2020-02-22 09:05:26 +08:00
|
|
|
"gogs.io/gogs/internal/conf"
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/db"
|
2020-03-16 01:22:27 +08:00
|
|
|
"gogs.io/gogs/internal/errutil"
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/form"
|
|
|
|
|
"gogs.io/gogs/internal/template"
|
2014-03-15 19:01:50 +08:00
|
|
|
)
|
|
|
|
|
|
2014-03-15 09:17:16 -04:00
|
|
|
// Context represents context of a request.
|
2014-03-15 19:01:50 +08:00
|
|
|
type Context struct {
|
2014-07-26 00:24:27 -04:00
|
|
|
*macaron.Context
|
2014-07-31 17:25:34 -04:00
|
|
|
Cache cache.Cache
|
|
|
|
|
csrf csrf.CSRF
|
2014-07-26 00:24:27 -04:00
|
|
|
Flash *session.Flash
|
|
|
|
|
Session session.Store
|
|
|
|
|
|
2017-06-03 06:54:06 -04:00
|
|
|
Link string // Current request URL
|
2019-10-24 01:51:46 -07:00
|
|
|
User *db.User
|
2017-04-06 23:48:49 -04:00
|
|
|
IsLogged bool
|
2014-11-18 11:07:16 -05:00
|
|
|
IsBasicAuth bool
|
2018-11-28 21:05:58 -05:00
|
|
|
IsTokenAuth bool
|
2014-03-16 00:03:23 +08:00
|
|
|
|
2016-03-11 11:56:52 -05:00
|
|
|
Repo *Repository
|
2016-03-13 17:37:44 -04:00
|
|
|
Org *Organization
|
2014-03-15 19:01:50 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 02:08:53 +08:00
|
|
|
// RawTitle sets the "Title" field in template data.
|
|
|
|
|
func (c *Context) RawTitle(title string) {
|
|
|
|
|
c.Data["Title"] = title
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Title localizes the "Title" field in template data.
|
2017-04-07 00:49:30 -04:00
|
|
|
func (c *Context) Title(locale string) {
|
2020-03-09 02:08:53 +08:00
|
|
|
c.RawTitle(c.Tr(locale))
|
2017-04-07 00:49:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PageIs sets "PageIsxxx" field in template data.
|
|
|
|
|
func (c *Context) PageIs(name string) {
|
|
|
|
|
c.Data["PageIs"+name] = true
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-07 21:44:55 -04:00
|
|
|
// Require sets "Requirexxx" field in template data.
|
|
|
|
|
func (c *Context) Require(name string) {
|
|
|
|
|
c.Data["Require"+name] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Context) RequireHighlightJS() {
|
|
|
|
|
c.Require("HighlightJS")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Context) RequireSimpleMDE() {
|
|
|
|
|
c.Require("SimpleMDE")
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-11 21:06:24 +08:00
|
|
|
func (c *Context) RequireAutosize() {
|
|
|
|
|
c.Require("Autosize")
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 23:15:20 +08:00
|
|
|
func (c *Context) RequireDropzone() {
|
|
|
|
|
c.Require("Dropzone")
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-07 00:49:30 -04:00
|
|
|
// FormErr sets "Err_xxx" field in template data.
|
|
|
|
|
func (c *Context) FormErr(names ...string) {
|
|
|
|
|
for i := range names {
|
|
|
|
|
c.Data["Err_"+names[i]] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 23:48:49 -04:00
|
|
|
// UserID returns ID of current logged in user.
|
|
|
|
|
// It returns 0 if visitor is anonymous.
|
|
|
|
|
func (c *Context) UserID() int64 {
|
|
|
|
|
if !c.IsLogged {
|
2017-03-17 19:17:40 -04:00
|
|
|
return 0
|
|
|
|
|
}
|
2017-04-06 23:48:49 -04:00
|
|
|
return c.User.ID
|
2017-03-17 19:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
2014-05-05 13:08:01 -04:00
|
|
|
// HasError returns true if error occurs in form validation.
|
2017-06-03 07:26:09 -04:00
|
|
|
func (c *Context) HasApiError() bool {
|
|
|
|
|
hasErr, ok := c.Data["HasError"]
|
2014-05-05 13:08:01 -04:00
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return hasErr.(bool)
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func (c *Context) GetErrMsg() string {
|
|
|
|
|
return c.Data["ErrorMsg"].(string)
|
2014-05-05 13:08:01 -04:00
|
|
|
}
|
|
|
|
|
|
2014-03-15 10:52:14 -04:00
|
|
|
// HasError returns true if error occurs in form validation.
|
2017-06-03 07:26:09 -04:00
|
|
|
func (c *Context) HasError() bool {
|
|
|
|
|
hasErr, ok := c.Data["HasError"]
|
2014-03-15 10:52:14 -04:00
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Flash.ErrorMsg = c.Data["ErrorMsg"].(string)
|
|
|
|
|
c.Data["Flash"] = c.Flash
|
2014-03-15 10:52:14 -04:00
|
|
|
return hasErr.(bool)
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 19:47:56 +08:00
|
|
|
// HasValue returns true if value of given name exists.
|
2017-06-03 07:26:09 -04:00
|
|
|
func (c *Context) HasValue(name string) bool {
|
|
|
|
|
_, ok := c.Data[name]
|
2015-07-08 19:47:56 +08:00
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 01:03:44 -04:00
|
|
|
// HTML responses template with given status.
|
2017-06-03 07:26:09 -04:00
|
|
|
func (c *Context) HTML(status int, name string) {
|
2017-02-09 19:29:59 -05:00
|
|
|
log.Trace("Template: %s", name)
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Context.HTML(status, name)
|
2014-03-20 07:50:26 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-04 19:25:05 -04:00
|
|
|
// Success responses template with status http.StatusOK.
|
2017-04-05 09:17:21 -04:00
|
|
|
func (c *Context) Success(name string) {
|
2017-04-04 19:25:05 -04:00
|
|
|
c.HTML(http.StatusOK, name)
|
2017-03-30 01:03:44 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-06 00:14:30 -04:00
|
|
|
// JSONSuccess responses JSON with status http.StatusOK.
|
|
|
|
|
func (c *Context) JSONSuccess(data interface{}) {
|
|
|
|
|
c.JSON(http.StatusOK, data)
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-30 13:55:40 -04:00
|
|
|
// RawRedirect simply calls underlying Redirect method with no escape.
|
|
|
|
|
func (c *Context) RawRedirect(location string, status ...int) {
|
|
|
|
|
c.Context.Redirect(location, status...)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
// Redirect responses redirection with given location and status.
|
2018-09-28 23:56:45 -04:00
|
|
|
// It escapes special characters in the location string.
|
|
|
|
|
func (c *Context) Redirect(location string, status ...int) {
|
|
|
|
|
c.Context.Redirect(template.EscapePound(location), status...)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
// RedirectSubpath responses redirection with given location and status.
|
2020-02-22 09:05:26 +08:00
|
|
|
// It prepends setting.Server.Subpath to the location string.
|
2020-03-16 01:22:27 +08:00
|
|
|
func (c *Context) RedirectSubpath(location string, status ...int) {
|
2020-02-22 09:05:26 +08:00
|
|
|
c.Redirect(conf.Server.Subpath+location, status...)
|
2017-04-07 00:49:30 -04:00
|
|
|
}
|
|
|
|
|
|
2014-03-15 10:52:14 -04:00
|
|
|
// RenderWithErr used for page has form validation but need to prompt error to users.
|
2017-06-03 07:26:09 -04:00
|
|
|
func (c *Context) RenderWithErr(msg, tpl string, f interface{}) {
|
2017-02-23 11:39:09 -05:00
|
|
|
if f != nil {
|
2017-06-03 07:26:09 -04:00
|
|
|
form.Assign(f, c.Data)
|
2014-04-03 15:50:55 -04:00
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Flash.ErrorMsg = msg
|
|
|
|
|
c.Data["Flash"] = c.Flash
|
|
|
|
|
c.HTML(http.StatusOK, tpl)
|
2014-03-15 10:52:14 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-04 19:25:05 -04:00
|
|
|
// NotFound renders the 404 page.
|
2017-06-03 07:26:09 -04:00
|
|
|
func (c *Context) NotFound() {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Title("status.page_not_found")
|
|
|
|
|
c.HTML(http.StatusNotFound, fmt.Sprintf("status/%d", http.StatusNotFound))
|
2017-04-04 19:25:05 -04:00
|
|
|
}
|
|
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
// Error renders the 500 page.
|
|
|
|
|
func (c *Context) Error(err error, msg string) {
|
2020-08-16 21:31:08 +08:00
|
|
|
log.ErrorDepth(4, "%s: %v", msg, err)
|
2020-03-16 01:22:27 +08:00
|
|
|
|
|
|
|
|
c.Title("status.internal_server_error")
|
|
|
|
|
|
|
|
|
|
// Only in non-production mode or admin can see the actual error message.
|
|
|
|
|
if !conf.IsProdMode() || (c.IsLogged && c.User.IsAdmin) {
|
|
|
|
|
c.Data["ErrorMsg"] = err
|
|
|
|
|
}
|
|
|
|
|
c.HTML(http.StatusInternalServerError, fmt.Sprintf("status/%d", http.StatusInternalServerError))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Errorf renders the 500 response with formatted message.
|
|
|
|
|
func (c *Context) Errorf(err error, format string, args ...interface{}) {
|
|
|
|
|
c.Error(err, fmt.Sprintf(format, args...))
|
2017-02-10 16:05:11 -05:00
|
|
|
}
|
|
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
// NotFoundOrError responses with 404 page for not found error and 500 page otherwise.
|
|
|
|
|
func (c *Context) NotFoundOrError(err error, msg string) {
|
|
|
|
|
if errutil.IsNotFound(err) {
|
2017-04-04 19:25:05 -04:00
|
|
|
c.NotFound()
|
2016-07-26 02:48:17 +08:00
|
|
|
return
|
|
|
|
|
}
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NotFoundOrErrorf is same as NotFoundOrError but with formatted message.
|
|
|
|
|
func (c *Context) NotFoundOrErrorf(err error, format string, args ...interface{}) {
|
|
|
|
|
c.NotFoundOrError(err, fmt.Sprintf(format, args...))
|
2016-07-26 02:48:17 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
func (c *Context) PlainText(status int, msg string) {
|
|
|
|
|
c.Render.PlainText(status, []byte(msg))
|
2015-03-28 22:30:05 +08:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func (c *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) {
|
2014-04-16 00:27:29 +08:00
|
|
|
modtime := time.Now()
|
|
|
|
|
for _, p := range params {
|
|
|
|
|
switch v := p.(type) {
|
|
|
|
|
case time.Time:
|
|
|
|
|
modtime = v
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Resp.Header().Set("Content-Description", "File Transfer")
|
|
|
|
|
c.Resp.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
|
c.Resp.Header().Set("Content-Disposition", "attachment; filename="+name)
|
|
|
|
|
c.Resp.Header().Set("Content-Transfer-Encoding", "binary")
|
|
|
|
|
c.Resp.Header().Set("Expires", "0")
|
|
|
|
|
c.Resp.Header().Set("Cache-Control", "must-revalidate")
|
|
|
|
|
c.Resp.Header().Set("Pragma", "public")
|
|
|
|
|
http.ServeContent(c.Resp, c.Req.Request, name, modtime, r)
|
2014-04-10 14:37:43 -04:00
|
|
|
}
|
|
|
|
|
|
2014-07-26 00:24:27 -04:00
|
|
|
// Contexter initializes a classic context for a request.
|
|
|
|
|
func Contexter() macaron.Handler {
|
2017-06-03 06:54:06 -04:00
|
|
|
return func(ctx *macaron.Context, l i18n.Locale, cache cache.Cache, sess session.Store, f *session.Flash, x csrf.CSRF) {
|
|
|
|
|
c := &Context{
|
|
|
|
|
Context: ctx,
|
2014-07-31 17:25:34 -04:00
|
|
|
Cache: cache,
|
|
|
|
|
csrf: x,
|
2014-07-26 00:24:27 -04:00
|
|
|
Flash: f,
|
|
|
|
|
Session: sess,
|
2020-02-22 09:05:26 +08:00
|
|
|
Link: conf.Server.Subpath + strings.TrimSuffix(ctx.Req.URL.Path, "/"),
|
2016-03-11 11:56:52 -05:00
|
|
|
Repo: &Repository{
|
|
|
|
|
PullRequest: &PullRequest{},
|
2016-03-06 23:57:46 -05:00
|
|
|
},
|
2016-03-13 17:37:44 -04:00
|
|
|
Org: &Organization{},
|
2014-03-15 19:01:50 +08:00
|
|
|
}
|
2018-09-28 23:56:45 -04:00
|
|
|
c.Data["Link"] = template.EscapePound(c.Link)
|
2017-06-03 06:54:06 -04:00
|
|
|
c.Data["PageStartTime"] = time.Now()
|
|
|
|
|
|
2020-02-22 09:05:26 +08:00
|
|
|
if len(conf.HTTP.AccessControlAllowOrigin) > 0 {
|
|
|
|
|
c.Header().Set("Access-Control-Allow-Origin", conf.HTTP.AccessControlAllowOrigin)
|
2020-10-10 23:09:42 +08:00
|
|
|
c.Header().Set("Access-Control-Allow-Credentials", "true")
|
2017-06-03 06:54:06 -04:00
|
|
|
c.Header().Set("Access-Control-Max-Age", "3600")
|
|
|
|
|
c.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With")
|
|
|
|
|
}
|
2014-03-22 20:49:53 +08:00
|
|
|
|
2018-11-28 21:05:58 -05:00
|
|
|
// Get user from session or header when possible
|
2020-09-20 11:19:02 +08:00
|
|
|
c.User, c.IsBasicAuth, c.IsTokenAuth = authenticatedUser(c.Context, c.Session)
|
2017-06-03 06:54:06 -04:00
|
|
|
|
|
|
|
|
if c.User != nil {
|
|
|
|
|
c.IsLogged = true
|
|
|
|
|
c.Data["IsLogged"] = c.IsLogged
|
|
|
|
|
c.Data["LoggedUser"] = c.User
|
|
|
|
|
c.Data["LoggedUserID"] = c.User.ID
|
|
|
|
|
c.Data["LoggedUserName"] = c.User.Name
|
|
|
|
|
c.Data["IsAdmin"] = c.User.IsAdmin
|
2014-11-06 22:06:41 -05:00
|
|
|
} else {
|
2017-06-03 06:54:06 -04:00
|
|
|
c.Data["LoggedUserID"] = 0
|
|
|
|
|
c.Data["LoggedUserName"] = ""
|
2014-03-15 20:50:17 +08:00
|
|
|
}
|
2014-03-15 19:01:50 +08:00
|
|
|
|
2014-07-24 15:19:59 +02:00
|
|
|
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
|
2017-06-03 06:54:06 -04:00
|
|
|
if c.Req.Method == "POST" && strings.Contains(c.Req.Header.Get("Content-Type"), "multipart/form-data") {
|
2020-02-29 16:29:17 +08:00
|
|
|
if err := c.Req.ParseMultipartForm(conf.Attachment.MaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, "parse multipart form")
|
2014-07-24 15:19:59 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 06:54:06 -04:00
|
|
|
c.Data["CSRFToken"] = x.GetToken()
|
2018-09-28 23:56:45 -04:00
|
|
|
c.Data["CSRFTokenHTML"] = template.Safe(`<input type="hidden" name="_csrf" value="` + x.GetToken() + `">`)
|
2017-02-09 19:29:59 -05:00
|
|
|
log.Trace("Session ID: %s", sess.ID())
|
2017-06-03 06:54:06 -04:00
|
|
|
log.Trace("CSRF Token: %v", c.Data["CSRFToken"])
|
2014-03-19 21:57:55 +08:00
|
|
|
|
2020-02-27 18:06:38 +08:00
|
|
|
c.Data["ShowRegistrationButton"] = !conf.Auth.DisableRegistration
|
2020-02-29 16:29:17 +08:00
|
|
|
c.Data["ShowFooterBranding"] = conf.Other.ShowFooterBranding
|
2015-02-06 21:16:23 -05:00
|
|
|
|
2019-07-28 14:59:51 -07:00
|
|
|
c.renderNoticeBanner()
|
2019-07-28 08:47:35 +02:00
|
|
|
|
2020-03-23 22:18:05 +08:00
|
|
|
// 🚨 SECURITY: Prevent MIME type sniffing in some browsers,
|
|
|
|
|
// see https://github.com/gogs/gogs/issues/5397 for details.
|
|
|
|
|
c.Header().Set("X-Content-Type-Options", "nosniff")
|
2022-05-03 17:54:14 +08:00
|
|
|
c.Header().Set("X-Frame-Options", "deny")
|
2020-03-23 22:18:05 +08:00
|
|
|
|
2017-06-03 06:54:06 -04:00
|
|
|
ctx.Map(c)
|
2014-03-15 19:01:50 +08:00
|
|
|
}
|
|
|
|
|
}
|