@@ -15,6 +15,7 @@ import (
1515 "code.gitea.io/gitea/modules/git"
1616 "code.gitea.io/gitea/modules/setting"
1717 api "code.gitea.io/gitea/modules/structs"
18+ "code.gitea.io/gitea/modules/util"
1819)
1920
2021// ContentType repo content type
@@ -158,7 +159,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
158159 return nil , fmt .Errorf ("no commit found for the ref [ref: %s]" , ref )
159160 }
160161
161- selfURL , err := url .Parse (fmt . Sprintf ( "%s /contents/%s?ref=%s" , repo . APIURL (), treePath , origRef ))
162+ selfURL , err := url .Parse (repo . APIURL () + " /contents/" + util . PathEscapeSegments ( treePath ) + "?ref=" + url . QueryEscape ( origRef ))
162163 if err != nil {
163164 return nil , err
164165 }
@@ -217,23 +218,23 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
217218 }
218219 // Handle links
219220 if entry .IsRegular () || entry .IsLink () {
220- downloadURL , err := url .Parse (fmt . Sprintf ( "%s /raw/%s/%s/%s" , repo . HTMLURL (), refType , ref , treePath ))
221+ downloadURL , err := url .Parse (repo . HTMLURL () + " /raw/" + url . PathEscape ( string ( refType )) + "/" + util . PathEscapeSegments ( ref ) + "/" + util . PathEscapeSegments ( treePath ))
221222 if err != nil {
222223 return nil , err
223224 }
224225 downloadURLString := downloadURL .String ()
225226 contentsResponse .DownloadURL = & downloadURLString
226227 }
227228 if ! entry .IsSubModule () {
228- htmlURL , err := url .Parse (fmt . Sprintf ( "%s /src/%s/%s/%s" , repo . HTMLURL (), refType , ref , treePath ))
229+ htmlURL , err := url .Parse (repo . HTMLURL () + " /src/" + url . PathEscape ( string ( refType )) + "/" + util . PathEscapeSegments ( ref ) + "/" + util . PathEscapeSegments ( treePath ))
229230 if err != nil {
230231 return nil , err
231232 }
232233 htmlURLString := htmlURL .String ()
233234 contentsResponse .HTMLURL = & htmlURLString
234235 contentsResponse .Links .HTMLURL = & htmlURLString
235236
236- gitURL , err := url .Parse (fmt . Sprintf ( "%s /git/blobs/%s" , repo . APIURL (), entry .ID .String ()))
237+ gitURL , err := url .Parse (repo . APIURL () + " /git/blobs/" + url . PathEscape ( entry .ID .String ()))
237238 if err != nil {
238239 return nil , err
239240 }
0 commit comments