gitea源码

misc.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package misc
  4. import (
  5. "net/http"
  6. "path"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/httpcache"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/util"
  12. )
  13. func SSHInfo(rw http.ResponseWriter, req *http.Request) {
  14. if !git.DefaultFeatures().SupportProcReceive {
  15. rw.WriteHeader(http.StatusNotFound)
  16. return
  17. }
  18. rw.Header().Set("content-type", "text/json;charset=UTF-8")
  19. _, err := rw.Write([]byte(`{"type":"agit","version":1}`))
  20. if err != nil {
  21. log.Error("fail to write result: err: %v", err)
  22. rw.WriteHeader(http.StatusInternalServerError)
  23. return
  24. }
  25. rw.WriteHeader(http.StatusOK)
  26. }
  27. func DummyOK(w http.ResponseWriter, req *http.Request) {
  28. w.WriteHeader(http.StatusOK)
  29. }
  30. func RobotsTxt(w http.ResponseWriter, req *http.Request) {
  31. robotsTxt := util.FilePathJoinAbs(setting.CustomPath, "public/robots.txt")
  32. if ok, _ := util.IsExist(robotsTxt); !ok {
  33. robotsTxt = util.FilePathJoinAbs(setting.CustomPath, "robots.txt") // the legacy "robots.txt"
  34. }
  35. httpcache.SetCacheControlInHeader(w.Header(), httpcache.CacheControlForPublicStatic())
  36. http.ServeFile(w, req, robotsTxt)
  37. }
  38. func StaticRedirect(target string) func(w http.ResponseWriter, req *http.Request) {
  39. return func(w http.ResponseWriter, req *http.Request) {
  40. http.Redirect(w, req, path.Join(setting.StaticURLPrefix, target), http.StatusMovedPermanently)
  41. }
  42. }