Skip to content

Commit 1177a19

Browse files
jonasfranztechknowlogick
authored andcommitted
Use ResolveReference instead of path.Join (#4073)
Signed-off-by: Jonas Franz <[email protected]>
1 parent d1954ae commit 1177a19

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

modules/markup/html.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,25 @@ func cutoutVerbosePrefix(prefix string) string {
114114

115115
// URLJoin joins url components, like path.Join, but preserving contents
116116
func URLJoin(base string, elems ...string) string {
117-
u, err := url.Parse(base)
117+
if !strings.HasSuffix(base, "/") {
118+
base += "/"
119+
}
120+
baseURL, err := url.Parse(base)
118121
if err != nil {
119122
log.Error(4, "URLJoin: Invalid base URL %s", base)
120123
return ""
121124
}
122-
joinArgs := make([]string, 0, len(elems)+1)
123-
joinArgs = append(joinArgs, u.Path)
124-
joinArgs = append(joinArgs, elems...)
125-
u.Path = path.Join(joinArgs...)
126-
return u.String()
125+
joinedPath := path.Join(elems...)
126+
argURL, err := url.Parse(joinedPath)
127+
if err != nil {
128+
log.Error(4, "URLJoin: Invalid arg %s", joinedPath)
129+
return ""
130+
}
131+
joinedURL := baseURL.ResolveReference(argURL).String()
132+
if !baseURL.IsAbs() {
133+
return joinedURL[1:] // Removing leading '/'
134+
}
135+
return joinedURL
127136
}
128137

129138
// RenderIssueIndexPatternOptions options for RenderIssueIndexPattern function

modules/markup/html_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func TestURLJoin(t *testing.T) {
8383
"a", "b/c/"),
8484
newTest("a/b/d",
8585
"a/", "b/c/", "/../d/"),
86+
newTest("https://try.gitea.io/a/b/c#d",
87+
"https://try.gitea.io", "a/b", "c#d"),
8688
} {
8789
assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
8890
}

0 commit comments

Comments
 (0)