You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

redirect.go 1.1KB

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