gitea源码

123456789101112131415161718192021222324252627
  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.IsCurrentGiteaSiteURL(req.Context(), redirect) {
  17. resp.WriteHeader(http.StatusBadRequest)
  18. return
  19. }
  20. resp.Header().Add("Location", redirect)
  21. resp.WriteHeader(http.StatusSeeOther)
  22. }