aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-09-09 20:14:48 +0800
committerGitHub <noreply@github.com>2020-09-09 20:14:48 +0800
commit594a2dc41f6e70f0cc535b125316d05e12666fda (patch)
tree3c057919672661de65c908f92186c744f6417c86 /internal
parent06193ed825094581da4fb5550804d7445fc5ae13 (diff)
web: correctly serving go-get pages for subdirs (#6318)
* web: correctly serving go-get page for subdirs * Update CHANGELOG * Fix golint error
Diffstat (limited to 'internal')
-rw-r--r--internal/cmd/web.go4
-rw-r--r--internal/context/context.go48
-rw-r--r--internal/context/go_get.go62
3 files changed, 64 insertions, 50 deletions
diff --git a/internal/cmd/web.go b/internal/cmd/web.go
index 43b9dfc5..09c173f4 100644
--- a/internal/cmd/web.go
+++ b/internal/cmd/web.go
@@ -611,7 +611,7 @@ func runWeb(c *cli.Context) error {
m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.MustBeNotBare, context.RepoRef(), repo.CompareDiff)
}, ignSignIn, context.RepoAssignment())
m.Group("/:username/:reponame", func() {
- m.Get("", repo.Home)
+ m.Get("", context.ServeGoGet(), repo.Home)
m.Get("/stars", repo.Stars)
m.Get("/watchers", repo.Watchers)
}, ignSignIn, context.RepoAssignment(), context.RepoRef())
@@ -659,7 +659,7 @@ func runWeb(c *cli.Context) error {
lfs.RegisterRoutes(m.Router)
})
- m.Route("/*", "GET,POST,OPTIONS", repo.HTTPContexter(), repo.HTTP)
+ m.Route("/*", "GET,POST,OPTIONS", context.ServeGoGet(), repo.HTTPContexter(), repo.HTTP)
})
// ***************************
diff --git a/internal/context/context.go b/internal/context/context.go
index d05767ba..2971700b 100644
--- a/internal/context/context.go
+++ b/internal/context/context.go
@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
- "path"
"strings"
"time"
@@ -16,7 +15,6 @@ import (
"github.com/go-macaron/csrf"
"github.com/go-macaron/i18n"
"github.com/go-macaron/session"
- "github.com/unknwon/com"
"gopkg.in/macaron.v1"
log "unknwon.dev/clog/v2"
@@ -249,52 +247,6 @@ func Contexter() macaron.Handler {
c.Data["Link"] = template.EscapePound(c.Link)
c.Data["PageStartTime"] = time.Now()
- // Quick responses appropriate go-get meta with status 200
- // regardless of if user have access to the repository,
- // or the repository does not exist at all.
- // This is particular a workaround for "go get" command which does not respect
- // .netrc file.
- if c.Query("go-get") == "1" {
- ownerName := c.Params(":username")
- repoName := c.Params(":reponame")
- branchName := "master"
-
- owner, err := db.GetUserByName(ownerName)
- if err != nil {
- c.NotFoundOrError(err, "get user by name")
- return
- }
-
- repo, err := db.GetRepositoryByName(owner.ID, repoName)
- if err == nil && len(repo.DefaultBranch) > 0 {
- branchName = repo.DefaultBranch
- }
-
- prefix := conf.Server.ExternalURL + path.Join(ownerName, repoName, "src", branchName)
- insecureFlag := ""
- if !strings.HasPrefix(conf.Server.ExternalURL, "https://") {
- insecureFlag = "--insecure "
- }
- c.PlainText(http.StatusOK, com.Expand(`<!doctype html>
-<html>
- <head>
- <meta name="go-import" content="{GoGetImport} git {CloneLink}">
- <meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
- </head>
- <body>
- go get {InsecureFlag}{GoGetImport}
- </body>
-</html>
-`, map[string]string{
- "GoGetImport": path.Join(conf.Server.URL.Host, conf.Server.Subpath, ownerName, repoName),
- "CloneLink": db.ComposeHTTPSCloneURL(ownerName, repoName),
- "GoDocDirectory": prefix + "{/dir}",
- "GoDocFile": prefix + "{/dir}/{file}#L{line}",
- "InsecureFlag": insecureFlag,
- }))
- return
- }
-
if len(conf.HTTP.AccessControlAllowOrigin) > 0 {
c.Header().Set("Access-Control-Allow-Origin", conf.HTTP.AccessControlAllowOrigin)
c.Header().Set("'Access-Control-Allow-Credentials' ", "true")
diff --git a/internal/context/go_get.go b/internal/context/go_get.go
new file mode 100644
index 00000000..2851b5db
--- /dev/null
+++ b/internal/context/go_get.go
@@ -0,0 +1,62 @@
+package context
+
+import (
+ "net/http"
+ "path"
+ "strings"
+
+ "github.com/unknwon/com"
+ "gopkg.in/macaron.v1"
+
+ "gogs.io/gogs/internal/conf"
+ "gogs.io/gogs/internal/db"
+)
+
+// ServeGoGet does quick responses for appropriate go-get meta with status OK
+// regardless of whether the user has access to the repository, or the repository
+// does exist at all. This is particular a workaround for "go get" command which
+// does not respect .netrc file.
+func ServeGoGet() macaron.Handler {
+ return func(c *macaron.Context) {
+ if c.Query("go-get") != "1" {
+ return
+ }
+
+ ownerName := c.Params(":username")
+ repoName := c.Params(":reponame")
+ branchName := "master"
+
+ owner, err := db.Users.GetByUsername(ownerName)
+ if err == nil {
+ repo, err := db.Repos.GetByName(owner.ID, repoName)
+ if err == nil && repo.DefaultBranch != "" {
+ branchName = repo.DefaultBranch
+ }
+ }
+
+ prefix := conf.Server.ExternalURL + path.Join(ownerName, repoName, "src", branchName)
+ insecureFlag := ""
+ if !strings.HasPrefix(conf.Server.ExternalURL, "https://") {
+ insecureFlag = "--insecure "
+ }
+ c.PlainText(http.StatusOK, []byte(com.Expand(`<!doctype html>
+<html>
+ <head>
+ <meta name="go-import" content="{GoGetImport} git {CloneLink}">
+ <meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
+ </head>
+ <body>
+ go get {InsecureFlag}{GoGetImport}
+ </body>
+</html>
+`,
+ map[string]string{
+ "GoGetImport": path.Join(conf.Server.URL.Host, conf.Server.Subpath, ownerName, repoName),
+ "CloneLink": db.ComposeHTTPSCloneURL(ownerName, repoName),
+ "GoDocDirectory": prefix + "{/dir}",
+ "GoDocFile": prefix + "{/dir}/{file}#L{line}",
+ "InsecureFlag": insecureFlag,
+ },
+ )))
+ }
+}