@@ -16,6 +16,7 @@ import (
1616 "code.gitea.io/gitea/modules/git"
1717 "code.gitea.io/gitea/modules/setting"
1818 api "code.gitea.io/gitea/modules/structs"
19+ "code.gitea.io/gitea/modules/util"
1920)
2021
2122// ContentType repo content type
@@ -159,7 +160,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
159160 return nil , fmt .Errorf ("no commit found for the ref [ref: %s]" , ref )
160161 }
161162
162- selfURL , err := url .Parse (fmt . Sprintf ( "%s /contents/%s?ref=%s" , repo . APIURL (), treePath , origRef ))
163+ selfURL , err := url .Parse (repo . APIURL () + " /contents/" + util . PathEscapeSegments ( treePath ) + "?ref=" + url . QueryEscape ( origRef ))
163164 if err != nil {
164165 return nil , err
165166 }
@@ -218,23 +219,23 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
218219 }
219220 // Handle links
220221 if entry .IsRegular () || entry .IsLink () {
221- downloadURL , err := url .Parse (fmt . Sprintf ( "%s /raw/%s/%s/%s" , repo . HTMLURL (), refType , ref , treePath ))
222+ downloadURL , err := url .Parse (repo . HTMLURL () + " /raw/" + url . PathEscape ( string ( refType )) + "/" + util . PathEscapeSegments ( ref ) + "/" + util . PathEscapeSegments ( treePath ))
222223 if err != nil {
223224 return nil , err
224225 }
225226 downloadURLString := downloadURL .String ()
226227 contentsResponse .DownloadURL = & downloadURLString
227228 }
228229 if ! entry .IsSubModule () {
229- htmlURL , err := url .Parse (fmt . Sprintf ( "%s /src/%s/%s/%s" , repo . HTMLURL (), refType , ref , treePath ))
230+ htmlURL , err := url .Parse (repo . HTMLURL () + " /src/" + url . PathEscape ( string ( refType )) + "/" + util . PathEscapeSegments ( ref ) + "/" + util . PathEscapeSegments ( treePath ))
230231 if err != nil {
231232 return nil , err
232233 }
233234 htmlURLString := htmlURL .String ()
234235 contentsResponse .HTMLURL = & htmlURLString
235236 contentsResponse .Links .HTMLURL = & htmlURLString
236237
237- gitURL , err := url .Parse (fmt . Sprintf ( "%s /git/blobs/%s" , repo . APIURL (), entry .ID .String ()))
238+ gitURL , err := url .Parse (repo . APIURL () + " /git/blobs/" + url . PathEscape ( entry .ID .String ()))
238239 if err != nil {
239240 return nil , err
240241 }
0 commit comments