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.

116 lines
3.9 KiB

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