File tree 2 files changed +17
-6
lines changed 2 files changed +17
-6
lines changed Original file line number Diff line number Diff line change @@ -114,16 +114,25 @@ func cutoutVerbosePrefix(prefix string) string {
114
114
115
115
// URLJoin joins url components, like path.Join, but preserving contents
116
116
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 )
118
121
if err != nil {
119
122
log .Error (4 , "URLJoin: Invalid base URL %s" , base )
120
123
return ""
121
124
}
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
127
136
}
128
137
129
138
// RenderIssueIndexPatternOptions options for RenderIssueIndexPattern function
Original file line number Diff line number Diff line change @@ -83,6 +83,8 @@ func TestURLJoin(t *testing.T) {
83
83
"a" , "b/c/" ),
84
84
newTest ("a/b/d" ,
85
85
"a/" , "b/c/" , "/../d/" ),
86
+ newTest ("https://try.gitea.io/a/b/c#d" ,
87
+ "https://try.gitea.io" , "a/b" , "c#d" ),
86
88
} {
87
89
assert .Equal (t , test .Expected , URLJoin (test .Base , test .Elements ... ))
88
90
}
You can’t perform that action at this time.
0 commit comments