filer_server_handlers.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package weed_server
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/util"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/stats"
  8. )
  9. func (fs *FilerServer) filerHandler(w http.ResponseWriter, r *http.Request) {
  10. start := time.Now()
  11. // proxy to volume servers
  12. var fileId string
  13. if strings.HasPrefix(r.RequestURI, "/?proxyChunkId=") {
  14. fileId = r.RequestURI[len("/?proxyChunkId="):]
  15. }
  16. if fileId != "" {
  17. stats.FilerRequestCounter.WithLabelValues("proxy").Inc()
  18. fs.proxyToVolumeServer(w, r, fileId)
  19. stats.FilerRequestHistogram.WithLabelValues("proxy").Observe(time.Since(start).Seconds())
  20. return
  21. }
  22. w.Header().Set("Server", "SeaweedFS Filer "+util.VERSION)
  23. if r.Header.Get("Origin") != "" {
  24. w.Header().Set("Access-Control-Allow-Origin", "*")
  25. w.Header().Set("Access-Control-Allow-Credentials", "true")
  26. }
  27. switch r.Method {
  28. case "GET":
  29. stats.FilerRequestCounter.WithLabelValues("get").Inc()
  30. fs.GetOrHeadHandler(w, r, true)
  31. stats.FilerRequestHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds())
  32. case "HEAD":
  33. stats.FilerRequestCounter.WithLabelValues("head").Inc()
  34. fs.GetOrHeadHandler(w, r, false)
  35. stats.FilerRequestHistogram.WithLabelValues("head").Observe(time.Since(start).Seconds())
  36. case "DELETE":
  37. stats.FilerRequestCounter.WithLabelValues("delete").Inc()
  38. if _, ok := r.URL.Query()["tagging"]; ok {
  39. fs.DeleteTaggingHandler(w, r)
  40. } else {
  41. fs.DeleteHandler(w, r)
  42. }
  43. stats.FilerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds())
  44. case "PUT":
  45. stats.FilerRequestCounter.WithLabelValues("put").Inc()
  46. if _, ok := r.URL.Query()["tagging"]; ok {
  47. fs.PutTaggingHandler(w, r)
  48. } else {
  49. fs.PostHandler(w, r)
  50. }
  51. stats.FilerRequestHistogram.WithLabelValues("put").Observe(time.Since(start).Seconds())
  52. case "POST":
  53. stats.FilerRequestCounter.WithLabelValues("post").Inc()
  54. fs.PostHandler(w, r)
  55. stats.FilerRequestHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds())
  56. case "OPTIONS":
  57. stats.FilerRequestCounter.WithLabelValues("options").Inc()
  58. OptionsHandler(w, r, false)
  59. stats.FilerRequestHistogram.WithLabelValues("head").Observe(time.Since(start).Seconds())
  60. }
  61. }
  62. func (fs *FilerServer) readonlyFilerHandler(w http.ResponseWriter, r *http.Request) {
  63. w.Header().Set("Server", "SeaweedFS Filer "+util.VERSION)
  64. if r.Header.Get("Origin") != "" {
  65. w.Header().Set("Access-Control-Allow-Origin", "*")
  66. w.Header().Set("Access-Control-Allow-Credentials", "true")
  67. }
  68. start := time.Now()
  69. switch r.Method {
  70. case "GET":
  71. stats.FilerRequestCounter.WithLabelValues("get").Inc()
  72. fs.GetOrHeadHandler(w, r, true)
  73. stats.FilerRequestHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds())
  74. case "HEAD":
  75. stats.FilerRequestCounter.WithLabelValues("head").Inc()
  76. fs.GetOrHeadHandler(w, r, false)
  77. stats.FilerRequestHistogram.WithLabelValues("head").Observe(time.Since(start).Seconds())
  78. case "OPTIONS":
  79. stats.FilerRequestCounter.WithLabelValues("options").Inc()
  80. OptionsHandler(w, r, true)
  81. stats.FilerRequestHistogram.WithLabelValues("head").Observe(time.Since(start).Seconds())
  82. }
  83. }
  84. func OptionsHandler(w http.ResponseWriter, r *http.Request, isReadOnly bool) {
  85. if isReadOnly {
  86. w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
  87. } else {
  88. w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  89. }
  90. w.Header().Add("Access-Control-Allow-Headers", "*")
  91. }