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.

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