Browse Source

pkg/context: use host address and apply insecure flag for go-get=1 when needed (#5305)

Unknwon 5 years ago
parent
commit
6690023555
2 changed files with 9 additions and 8 deletions
  1. 7 8
      pkg/context/context.go
  2. 2 0
      pkg/setting/setting.go

+ 7 - 8
pkg/context/context.go

@@ -263,13 +263,11 @@ func Contexter() macaron.Handler {
 				branchName = repo.DefaultBranch
 			}
 
-			// Non-80 port needs to match port number in import path as well
-			host := setting.Domain
-			if setting.HTTPPort != "80" {
-				host += ":" + setting.HTTPPort
-			}
-
 			prefix := setting.AppURL + path.Join(ownerName, repoName, "src", branchName)
+			insecureFlag := ""
+			if !strings.HasPrefix(setting.AppURL, "https://") {
+				insecureFlag = "--insecure "
+			}
 			c.PlainText(http.StatusOK, []byte(com.Expand(`<!doctype html>
 <html>
 	<head>
@@ -277,14 +275,15 @@ func Contexter() macaron.Handler {
 		<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
 	</head>
 	<body>
-		go get {GoGetImport}
+		go get {InsecureFlag}{GoGetImport}
 	</body>
 </html>
 `, map[string]string{
-				"GoGetImport":    path.Join(host, setting.AppSubURL, repo.FullName()),
+				"GoGetImport":    path.Join(setting.HostAddress, setting.AppSubURL, repo.FullName()),
 				"CloneLink":      models.ComposeHTTPSCloneURL(ownerName, repoName),
 				"GoDocDirectory": prefix + "{/dir}",
 				"GoDocFile":      prefix + "{/dir}/{file}#L{line}",
+				"InsecureFlag":   insecureFlag,
 			})))
 			return
 		}

+ 2 - 0
pkg/setting/setting.go

@@ -61,6 +61,7 @@ var (
 	AppSubURLDepth int // Number of slashes
 	AppPath        string
 	AppDataPath    string
+	HostAddress    string // AppURL without protocol and slashes
 
 	// Server settings
 	Protocol             Scheme
@@ -463,6 +464,7 @@ func NewContext() {
 	// This value is empty if site does not have sub-url.
 	AppSubURL = strings.TrimSuffix(url.Path, "/")
 	AppSubURLDepth = strings.Count(AppSubURL, "/")
+	HostAddress = url.Host
 
 	Protocol = SCHEME_HTTP
 	if sec.Key("PROTOCOL").String() == "https" {