Procházet zdrojové kódy

web: correctly serving go-get pages for subdirs (#6318)

* web: correctly serving go-get page for subdirs

* Update CHANGELOG

* Fix golint error
ᴜɴᴋɴᴡᴏɴ před 4 roky
rodič
revize
594a2dc41f
4 změnil soubory, kde provedl 66 přidání a 50 odebrání
  1. 2 0
      CHANGELOG.md
  2. 2 2
      internal/cmd/web.go
  3. 0 48
      internal/context/context.go
  4. 62 0
      internal/context/go_get.go

+ 2 - 0
CHANGELOG.md

@@ -16,6 +16,8 @@ All notable changes to Gogs are documented in this file.
 
 ### Fixed
 
+- _Regression:_ Pages are correctly rendered when requesting `?go-get=1` for subdirectories. [#6314](https://github.com/gogs/gogs/issues/6314)
+
 ### Removed
 
 - ⚠️ Migrations before 0.12 are removed, installations not on 0.12 should upgrade to it to run the migrations and then upgrade to 0.13.

+ 2 - 2
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)
 	})
 
 	// ***************************

+ 0 - 48
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")

+ 62 - 0
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,
+			},
+		)))
+	}
+}