0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-14 22:11:32 +01:00

Handle insecure and ports in go get (#7041)

* Handle insecure and ports in go get

* Fix IsExternalURL for non-standard ports
This commit is contained in:
zeripath 2019-05-27 22:08:38 +01:00 committed by GitHub
parent 9ca7fcddbb
commit 69d81b6569
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 4 deletions

View file

@ -257,6 +257,13 @@ func Contexter() macaron.Handler {
branchName = repo.DefaultBranch branchName = repo.DefaultBranch
} }
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName)) prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
appURL, _ := url.Parse(setting.AppURL)
insecure := ""
if appURL.Scheme == string(setting.HTTP) {
insecure = "--insecure "
}
c.Header().Set("Content-Type", "text/html") c.Header().Set("Content-Type", "text/html")
c.WriteHeader(http.StatusOK) c.WriteHeader(http.StatusOK)
c.Write([]byte(com.Expand(`<!doctype html> c.Write([]byte(com.Expand(`<!doctype html>
@ -266,7 +273,7 @@ func Contexter() macaron.Handler {
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}"> <meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
</head> </head>
<body> <body>
go get {GoGetImport} go get {Insecure}{GoGetImport}
</body> </body>
</html> </html>
`, map[string]string{ `, map[string]string{
@ -274,6 +281,7 @@ func Contexter() macaron.Handler {
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName), "CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
"GoDocDirectory": prefix + "{/dir}", "GoDocDirectory": prefix + "{/dir}",
"GoDocFile": prefix + "{/dir}/{file}#L{line}", "GoDocFile": prefix + "{/dir}/{file}#L{line}",
"Insecure": insecure,
}))) })))
return return
} }

View file

@ -188,7 +188,10 @@ func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
// ComposeGoGetImport returns go-get-import meta content. // ComposeGoGetImport returns go-get-import meta content.
func ComposeGoGetImport(owner, repo string) string { func ComposeGoGetImport(owner, repo string) string {
return path.Join(setting.Domain, setting.AppSubURL, url.PathEscape(owner), url.PathEscape(repo)) /// setting.AppUrl is guaranteed to be parse as url
appURL, _ := url.Parse(setting.AppURL)
return path.Join(appURL.Host, setting.AppSubURL, url.PathEscape(owner), url.PathEscape(repo))
} }
// EarlyResponseForGoGetMeta responses appropriate go-get meta with status 200 // EarlyResponseForGoGetMeta responses appropriate go-get meta with status 200

View file

@ -52,7 +52,8 @@ func IsExternalURL(rawURL string) bool {
if err != nil { if err != nil {
return true return true
} }
if len(parsed.Host) != 0 && strings.Replace(parsed.Host, "www.", "", 1) != strings.Replace(setting.Domain, "www.", "", 1) { appURL, _ := url.Parse(setting.AppURL)
if len(parsed.Host) != 0 && strings.Replace(parsed.Host, "www.", "", 1) != strings.Replace(appURL.Host, "www.", "", 1) {
return true return true
} }
return false return false

View file

@ -46,7 +46,7 @@ func TestURLJoin(t *testing.T) {
} }
func TestIsExternalURL(t *testing.T) { func TestIsExternalURL(t *testing.T) {
setting.Domain = "try.gitea.io" setting.AppURL = "https://try.gitea.io"
type test struct { type test struct {
Expected bool Expected bool
RawURL string RawURL string