code.gitea.io/gitea@v1.21.7/routers/common/redirect.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package common 5 6 import ( 7 "net/http" 8 9 "code.gitea.io/gitea/modules/httplib" 10 ) 11 12 // FetchRedirectDelegate helps the "fetch" requests to redirect to the correct location 13 func FetchRedirectDelegate(resp http.ResponseWriter, req *http.Request) { 14 // When use "fetch" to post requests and the response is a redirect, browser's "location.href = uri" has limitations. 15 // 1. change "location" from old "/foo" to new "/foo#hash", the browser will not reload the page. 16 // 2. when use "window.reload()", the hash is not respected, the newly loaded page won't scroll to the hash target. 17 // The typical page is "issue comment" page. The backend responds "/owner/repo/issues/1#comment-2", 18 // then frontend needs this delegate to redirect to the new location with hash correctly. 19 redirect := req.PostFormValue("redirect") 20 if httplib.IsRiskyRedirectURL(redirect) { 21 resp.WriteHeader(http.StatusBadRequest) 22 return 23 } 24 resp.Header().Add("Location", redirect) 25 resp.WriteHeader(http.StatusSeeOther) 26 }