Skip to content

Commit

Permalink
x/pkgsite: Trim HTTP or HTTPS Scheme from Source Code links
Browse files Browse the repository at this point in the history
Trimming the URL by creating a new template function which trims the
scheme of the URL

Fixes golang/go#40943
  • Loading branch information
Rahul Wadhwani committed Aug 28, 2020
1 parent 7427310 commit 6177bf8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions content/static/html/pages/overview.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
<h2>Source Code</h2>
<p class="Overview-sourceCodeLink">
{{if .RepositoryURL}}
Repository: <a href="{{.RepositoryURL}}" target="_blank" rel="noopener">{{.RepositoryURL}}</a><br/>
Repository: <a href="{{.RepositoryURL}}" target="_blank" rel="noopener">{{removeURLScheme .RepositoryURL}}</a><br/>
{{else}}
Source code link not available.
{{end}}
{{if .PackageSourceURL}}
Package: <a href="{{.PackageSourceURL}}" target="_blank" rel="noopener">{{.PackageSourceURL}}</a>
Package: <a href="{{.PackageSourceURL}}" target="_blank" rel="noopener">{{removeURLScheme .PackageSourceURL}}</a>
{{end}}
</p>
</div>
Expand Down
16 changes: 16 additions & 0 deletions internal/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -437,6 +438,20 @@ func executeTemplate(ctx context.Context, templateName string, tmpl *template.Te
return buf.Bytes(), nil
}

// removeURLScheme removes schemes from the URL
func removeURLScheme(urlString string) (string, error) {
baseURL, err := url.Parse(urlString)
if err != nil {
return baseURL.String(), fmt.Errorf("Error parsing URL: %q", baseURL.String())
}
scheme := baseURL.Scheme
if len(scheme) > 0 {
urlWithoutScheme := strings.Replace(baseURL.String(), scheme, "", 1)
return strings.Replace(urlWithoutScheme, "://", "", 1), nil
}
return baseURL.String(), nil
}

// parsePageTemplates parses html templates contained in the given base
// directory in order to generate a map of Name->*template.Template.
//
Expand Down Expand Up @@ -477,6 +492,7 @@ func parsePageTemplates(base template.TrustedSource) (map[string]*template.Templ
"commaseparate": func(s []string) string {
return strings.Join(s, ", ")
},
"removeURLScheme": removeURLScheme,
}).ParseFilesFromTrustedSources(join(base, tsc("base.tmpl")))
if err != nil {
return nil, fmt.Errorf("ParseFiles: %v", err)
Expand Down
16 changes: 8 additions & 8 deletions internal/frontend/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ func serverTestCases() []serverTestCase {
pagecheck.OverviewDetails(&pagecheck.Overview{
ModuleLink: "/mod/" + sample.ModulePath + "@v1.0.0",
ModuleLinkText: pkgV100.ModulePath,
RepoURL: "https://" + sample.ModulePath,
PackageURL: "https://" + sample.ModulePath + "/tree/v1.0.0/foo",
RepoURL: sample.ModulePath,
PackageURL: sample.ModulePath + "/tree/v1.0.0/foo",
ReadmeContent: "readme",
ReadmeSource: sample.ModulePath + "@v1.0.0/README.md",
})),
Expand Down Expand Up @@ -605,7 +605,7 @@ func serverTestCases() []serverTestCase {
pagecheck.OverviewDetails(&pagecheck.Overview{
ModuleLink: "/mod/" + sample.ModulePath,
ModuleLinkText: dir.ModulePath,
RepoURL: "https://" + sample.ModulePath,
RepoURL: sample.ModulePath,
ReadmeContent: "readme",
ReadmeSource: sample.ModulePath + "@v1.0.0/README.md",
})),
Expand Down Expand Up @@ -644,7 +644,7 @@ func serverTestCases() []serverTestCase {
ModuleLink: "/std@go1.13",
ModuleLinkText: "Standard Library",
ReadmeContent: "readme",
RepoURL: "https://" + sample.ModulePath, // wrong, but hard to change
RepoURL: sample.ModulePath, // wrong, but hard to change
ReadmeSource: "go.googlesource.com/go/+/refs/tags/go1.13/README.md",
})),
},
Expand All @@ -668,7 +668,7 @@ func serverTestCases() []serverTestCase {
ModuleLink: "/mod/" + sample.ModulePath,
ModuleLinkText: sample.ModulePath,
ReadmeContent: "readme",
RepoURL: "https://" + sample.ModulePath,
RepoURL: sample.ModulePath,
ReadmeSource: sample.ModulePath + "@v1.0.0/README.md",
})),
},
Expand All @@ -684,7 +684,7 @@ func serverTestCases() []serverTestCase {
ModuleLink: "/mod/" + sample.ModulePath,
ModuleLinkText: sample.ModulePath,
ReadmeContent: "readme",
RepoURL: "https://" + sample.ModulePath,
RepoURL: sample.ModulePath,
ReadmeSource: sample.ModulePath + "@v1.0.0/README.md",
})),
},
Expand Down Expand Up @@ -721,7 +721,7 @@ func serverTestCases() []serverTestCase {
ModuleLink: fmt.Sprintf("/mod/%s@%s", sample.ModulePath, sample.VersionString),
ModuleLinkText: sample.ModulePath,
ReadmeContent: "readme",
RepoURL: "https://" + sample.ModulePath,
RepoURL: sample.ModulePath,
ReadmeSource: sample.ModulePath + "@v1.0.0/README.md",
})),
},
Expand All @@ -735,7 +735,7 @@ func serverTestCases() []serverTestCase {
ModuleLink: fmt.Sprintf("/mod/%s@%s", sample.ModulePath, pseudoVersion),
ModuleLinkText: sample.ModulePath,
ReadmeContent: "readme",
RepoURL: "https://" + sample.ModulePath,
RepoURL: sample.ModulePath,
ReadmeSource: sample.ModulePath + "@" + pseudoVersion + "/README.md",
})),
},
Expand Down
2 changes: 1 addition & 1 deletion internal/testing/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// These sample values can be used to construct test cases.
var (
ModulePath = "github.com/valid/module_name"
RepositoryURL = "https://github.com/valid/module_name"
RepositoryURL = "github.com/valid/module_name"
VersionString = "v1.0.0"
CommitTime = NowTruncated()
LicenseMetadata = []*licenses.Metadata{
Expand Down

0 comments on commit 6177bf8

Please sign in to comment.