Skip to content

Commit 69d81b6

Browse files
authored
Handle insecure and ports in go get (#7041)
* Handle insecure and ports in go get * Fix IsExternalURL for non-standard ports
1 parent 9ca7fcd commit 69d81b6

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

modules/context/context.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ func Contexter() macaron.Handler {
257257
branchName = repo.DefaultBranch
258258
}
259259
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
260+
261+
appURL, _ := url.Parse(setting.AppURL)
262+
263+
insecure := ""
264+
if appURL.Scheme == string(setting.HTTP) {
265+
insecure = "--insecure "
266+
}
260267
c.Header().Set("Content-Type", "text/html")
261268
c.WriteHeader(http.StatusOK)
262269
c.Write([]byte(com.Expand(`<!doctype html>
@@ -266,14 +273,15 @@ func Contexter() macaron.Handler {
266273
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
267274
</head>
268275
<body>
269-
go get {GoGetImport}
276+
go get {Insecure}{GoGetImport}
270277
</body>
271278
</html>
272279
`, map[string]string{
273280
"GoGetImport": ComposeGoGetImport(ownerName, strings.TrimSuffix(repoName, ".git")),
274281
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
275282
"GoDocDirectory": prefix + "{/dir}",
276283
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
284+
"Insecure": insecure,
277285
})))
278286
return
279287
}

modules/context/repo.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
188188

189189
// ComposeGoGetImport returns go-get-import meta content.
190190
func ComposeGoGetImport(owner, repo string) string {
191-
return path.Join(setting.Domain, setting.AppSubURL, url.PathEscape(owner), url.PathEscape(repo))
191+
/// setting.AppUrl is guaranteed to be parse as url
192+
appURL, _ := url.Parse(setting.AppURL)
193+
194+
return path.Join(appURL.Host, setting.AppSubURL, url.PathEscape(owner), url.PathEscape(repo))
192195
}
193196

194197
// EarlyResponseForGoGetMeta responses appropriate go-get meta with status 200

modules/util/url.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ func IsExternalURL(rawURL string) bool {
5252
if err != nil {
5353
return true
5454
}
55-
if len(parsed.Host) != 0 && strings.Replace(parsed.Host, "www.", "", 1) != strings.Replace(setting.Domain, "www.", "", 1) {
55+
appURL, _ := url.Parse(setting.AppURL)
56+
if len(parsed.Host) != 0 && strings.Replace(parsed.Host, "www.", "", 1) != strings.Replace(appURL.Host, "www.", "", 1) {
5657
return true
5758
}
5859
return false

modules/util/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestURLJoin(t *testing.T) {
4646
}
4747

4848
func TestIsExternalURL(t *testing.T) {
49-
setting.Domain = "try.gitea.io"
49+
setting.AppURL = "https://try.gitea.io"
5050
type test struct {
5151
Expected bool
5252
RawURL string

0 commit comments

Comments
 (0)