mirror of https://github.com/gogs/gogs.git
repo: always response go-get meta when requested (#1878)
parent
f4d61ac6d2
commit
e33c714073
|
@ -9,9 +9,11 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Unknwon/com"
|
||||||
"github.com/go-macaron/cache"
|
"github.com/go-macaron/cache"
|
||||||
"github.com/go-macaron/csrf"
|
"github.com/go-macaron/csrf"
|
||||||
"github.com/go-macaron/i18n"
|
"github.com/go-macaron/i18n"
|
||||||
|
@ -20,6 +22,7 @@ import (
|
||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
|
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
|
"github.com/gogits/gogs/models/errors"
|
||||||
"github.com/gogits/gogs/pkg/auth"
|
"github.com/gogits/gogs/pkg/auth"
|
||||||
"github.com/gogits/gogs/pkg/form"
|
"github.com/gogits/gogs/pkg/form"
|
||||||
"github.com/gogits/gogs/pkg/setting"
|
"github.com/gogits/gogs/pkg/setting"
|
||||||
|
@ -33,6 +36,7 @@ type Context struct {
|
||||||
Flash *session.Flash
|
Flash *session.Flash
|
||||||
Session session.Store
|
Session session.Store
|
||||||
|
|
||||||
|
Link string // Current request URL
|
||||||
User *models.User
|
User *models.User
|
||||||
IsLogged bool
|
IsLogged bool
|
||||||
IsBasicAuth bool
|
IsBasicAuth bool
|
||||||
|
@ -202,63 +206,102 @@ func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interfa
|
||||||
|
|
||||||
// Contexter initializes a classic context for a request.
|
// Contexter initializes a classic context for a request.
|
||||||
func Contexter() macaron.Handler {
|
func Contexter() macaron.Handler {
|
||||||
return func(c *macaron.Context, l i18n.Locale, cache cache.Cache, sess session.Store, f *session.Flash, x csrf.CSRF) {
|
return func(ctx *macaron.Context, l i18n.Locale, cache cache.Cache, sess session.Store, f *session.Flash, x csrf.CSRF) {
|
||||||
ctx := &Context{
|
c := &Context{
|
||||||
Context: c,
|
Context: ctx,
|
||||||
Cache: cache,
|
Cache: cache,
|
||||||
csrf: x,
|
csrf: x,
|
||||||
Flash: f,
|
Flash: f,
|
||||||
Session: sess,
|
Session: sess,
|
||||||
|
Link: setting.AppSubURL + strings.TrimSuffix(ctx.Req.URL.Path, "/"),
|
||||||
Repo: &Repository{
|
Repo: &Repository{
|
||||||
PullRequest: &PullRequest{},
|
PullRequest: &PullRequest{},
|
||||||
},
|
},
|
||||||
Org: &Organization{},
|
Org: &Organization{},
|
||||||
}
|
}
|
||||||
|
c.Data["Link"] = c.Link
|
||||||
|
c.Data["PageStartTime"] = time.Now()
|
||||||
|
|
||||||
if len(setting.HTTP.AccessControlAllowOrigin) > 0 {
|
// Quick responses appropriate go-get meta with status 200
|
||||||
ctx.Header().Set("Access-Control-Allow-Origin", setting.HTTP.AccessControlAllowOrigin)
|
// regardless of if user have access to the repository,
|
||||||
ctx.Header().Set("'Access-Control-Allow-Credentials' ", "true")
|
// or the repository does not exist at all.
|
||||||
ctx.Header().Set("Access-Control-Max-Age", "3600")
|
// This is particular a workaround for "go get" command which does not respect
|
||||||
ctx.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With")
|
// .netrc file.
|
||||||
|
if c.Query("go-get") == "1" {
|
||||||
|
ownerName := ctx.Params(":username")
|
||||||
|
repoName := ctx.Params(":reponame")
|
||||||
|
branchName := "master"
|
||||||
|
|
||||||
|
owner, err := models.GetUserByName(ownerName)
|
||||||
|
if err != nil {
|
||||||
|
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, err := models.GetRepositoryByName(owner.ID, repoName)
|
||||||
|
if err == nil && len(repo.DefaultBranch) > 0 {
|
||||||
|
branchName = repo.DefaultBranch
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix := setting.AppURL + path.Join(ownerName, repoName, "src", branchName)
|
||||||
|
ctx.PlainText(http.StatusOK, []byte(com.Expand(`
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
|
||||||
|
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
go get {GoGetImport}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`, map[string]string{
|
||||||
|
"GoGetImport": path.Join(setting.Domain, setting.AppSubURL, c.Link),
|
||||||
|
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
|
||||||
|
"GoDocDirectory": prefix + "{/dir}",
|
||||||
|
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
|
||||||
|
})))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute current URL for real-time change language.
|
if len(setting.HTTP.AccessControlAllowOrigin) > 0 {
|
||||||
ctx.Data["Link"] = setting.AppSubURL + strings.TrimSuffix(ctx.Req.URL.Path, "/")
|
c.Header().Set("Access-Control-Allow-Origin", setting.HTTP.AccessControlAllowOrigin)
|
||||||
|
c.Header().Set("'Access-Control-Allow-Credentials' ", "true")
|
||||||
ctx.Data["PageStartTime"] = time.Now()
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
// Get user from session if logined.
|
// Get user from session if logined.
|
||||||
ctx.User, ctx.IsBasicAuth = auth.SignedInUser(ctx.Context, ctx.Session)
|
c.User, c.IsBasicAuth = auth.SignedInUser(c.Context, c.Session)
|
||||||
|
|
||||||
if ctx.User != nil {
|
if c.User != nil {
|
||||||
ctx.IsLogged = true
|
c.IsLogged = true
|
||||||
ctx.Data["IsLogged"] = ctx.IsLogged
|
c.Data["IsLogged"] = c.IsLogged
|
||||||
ctx.Data["LoggedUser"] = ctx.User
|
c.Data["LoggedUser"] = c.User
|
||||||
ctx.Data["LoggedUserID"] = ctx.User.ID
|
c.Data["LoggedUserID"] = c.User.ID
|
||||||
ctx.Data["LoggedUserName"] = ctx.User.Name
|
c.Data["LoggedUserName"] = c.User.Name
|
||||||
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
|
c.Data["IsAdmin"] = c.User.IsAdmin
|
||||||
} else {
|
} else {
|
||||||
ctx.Data["LoggedUserID"] = 0
|
c.Data["LoggedUserID"] = 0
|
||||||
ctx.Data["LoggedUserName"] = ""
|
c.Data["LoggedUserName"] = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
|
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
|
||||||
if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
|
if c.Req.Method == "POST" && strings.Contains(c.Req.Header.Get("Content-Type"), "multipart/form-data") {
|
||||||
if err := ctx.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
|
if err := c.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
|
||||||
ctx.Handle(500, "ParseMultipartForm", err)
|
c.Handle(500, "ParseMultipartForm", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Data["CSRFToken"] = x.GetToken()
|
c.Data["CSRFToken"] = x.GetToken()
|
||||||
ctx.Data["CSRFTokenHTML"] = template.HTML(`<input type="hidden" name="_csrf" value="` + x.GetToken() + `">`)
|
c.Data["CSRFTokenHTML"] = template.HTML(`<input type="hidden" name="_csrf" value="` + x.GetToken() + `">`)
|
||||||
log.Trace("Session ID: %s", sess.ID())
|
log.Trace("Session ID: %s", sess.ID())
|
||||||
log.Trace("CSRF Token: %v", ctx.Data["CSRFToken"])
|
log.Trace("CSRF Token: %v", c.Data["CSRFToken"])
|
||||||
|
|
||||||
ctx.Data["ShowRegistrationButton"] = setting.Service.ShowRegistrationButton
|
c.Data["ShowRegistrationButton"] = setting.Service.ShowRegistrationButton
|
||||||
ctx.Data["ShowFooterBranding"] = setting.ShowFooterBranding
|
c.Data["ShowFooterBranding"] = setting.ShowFooterBranding
|
||||||
ctx.Data["ShowFooterVersion"] = setting.ShowFooterVersion
|
c.Data["ShowFooterVersion"] = setting.ShowFooterVersion
|
||||||
|
|
||||||
c.Map(ctx)
|
ctx.Map(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue