filer_server_handlers.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package weed_server
  2. import (
  3. "errors"
  4. "net/http"
  5. "strings"
  6. "sync/atomic"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/security"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. "github.com/seaweedfs/seaweedfs/weed/stats"
  12. )
  13. func (fs *FilerServer) filerHandler(w http.ResponseWriter, r *http.Request) {
  14. start := time.Now()
  15. if r.Header.Get("Origin") != "" {
  16. w.Header().Set("Access-Control-Allow-Origin", "*")
  17. w.Header().Set("Access-Control-Expose-Headers", "*")
  18. w.Header().Set("Access-Control-Allow-Headers", "*")
  19. w.Header().Set("Access-Control-Allow-Credentials", "true")
  20. w.Header().Set("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  21. }
  22. if r.Method == "OPTIONS" {
  23. OptionsHandler(w, r, false)
  24. return
  25. }
  26. isReadHttpCall := r.Method == "GET" || r.Method == "HEAD"
  27. if !fs.maybeCheckJwtAuthorization(r, !isReadHttpCall) {
  28. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  29. return
  30. }
  31. // proxy to volume servers
  32. var fileId string
  33. if strings.HasPrefix(r.RequestURI, "/?proxyChunkId=") {
  34. fileId = r.RequestURI[len("/?proxyChunkId="):]
  35. }
  36. if fileId != "" {
  37. stats.FilerRequestCounter.WithLabelValues(stats.ChunkProxy).Inc()
  38. fs.proxyToVolumeServer(w, r, fileId)
  39. stats.FilerRequestHistogram.WithLabelValues(stats.ChunkProxy).Observe(time.Since(start).Seconds())
  40. return
  41. }
  42. stats.FilerRequestCounter.WithLabelValues(r.Method).Inc()
  43. defer func() {
  44. stats.FilerRequestHistogram.WithLabelValues(r.Method).Observe(time.Since(start).Seconds())
  45. }()
  46. w.Header().Set("Server", "SeaweedFS Filer "+util.VERSION)
  47. switch r.Method {
  48. case "GET":
  49. fs.GetOrHeadHandler(w, r)
  50. case "HEAD":
  51. fs.GetOrHeadHandler(w, r)
  52. case "DELETE":
  53. if _, ok := r.URL.Query()["tagging"]; ok {
  54. fs.DeleteTaggingHandler(w, r)
  55. } else {
  56. fs.DeleteHandler(w, r)
  57. }
  58. case "POST", "PUT":
  59. // wait until in flight data is less than the limit
  60. contentLength := getContentLength(r)
  61. fs.inFlightDataLimitCond.L.Lock()
  62. inFlightDataSize := atomic.LoadInt64(&fs.inFlightDataSize)
  63. for fs.option.ConcurrentUploadLimit != 0 && inFlightDataSize > fs.option.ConcurrentUploadLimit {
  64. glog.V(4).Infof("wait because inflight data %d > %d", inFlightDataSize, fs.option.ConcurrentUploadLimit)
  65. fs.inFlightDataLimitCond.Wait()
  66. inFlightDataSize = atomic.LoadInt64(&fs.inFlightDataSize)
  67. }
  68. fs.inFlightDataLimitCond.L.Unlock()
  69. atomic.AddInt64(&fs.inFlightDataSize, contentLength)
  70. defer func() {
  71. atomic.AddInt64(&fs.inFlightDataSize, -contentLength)
  72. fs.inFlightDataLimitCond.Signal()
  73. }()
  74. if r.Method == "PUT" {
  75. if _, ok := r.URL.Query()["tagging"]; ok {
  76. fs.PutTaggingHandler(w, r)
  77. } else {
  78. fs.PostHandler(w, r, contentLength)
  79. }
  80. } else { // method == "POST"
  81. fs.PostHandler(w, r, contentLength)
  82. }
  83. }
  84. }
  85. func (fs *FilerServer) readonlyFilerHandler(w http.ResponseWriter, r *http.Request) {
  86. start := time.Now()
  87. if r.Header.Get("Origin") != "" {
  88. w.Header().Set("Access-Control-Allow-Origin", "*")
  89. w.Header().Set("Access-Control-Allow-Headers", "*")
  90. w.Header().Set("Access-Control-Allow-Credentials", "true")
  91. }
  92. stats.FilerRequestCounter.WithLabelValues(r.Method).Inc()
  93. defer func() {
  94. stats.FilerRequestHistogram.WithLabelValues(r.Method).Observe(time.Since(start).Seconds())
  95. }()
  96. // We handle OPTIONS first because it never should be authenticated
  97. if r.Method == "OPTIONS" {
  98. OptionsHandler(w, r, true)
  99. return
  100. }
  101. if !fs.maybeCheckJwtAuthorization(r, false) {
  102. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  103. return
  104. }
  105. w.Header().Set("Server", "SeaweedFS Filer "+util.VERSION)
  106. switch r.Method {
  107. case "GET":
  108. fs.GetOrHeadHandler(w, r)
  109. case "HEAD":
  110. fs.GetOrHeadHandler(w, r)
  111. }
  112. }
  113. func OptionsHandler(w http.ResponseWriter, r *http.Request, isReadOnly bool) {
  114. if isReadOnly {
  115. w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
  116. } else {
  117. w.Header().Set("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  118. w.Header().Set("Access-Control-Expose-Headers", "*")
  119. }
  120. w.Header().Set("Access-Control-Allow-Origin", "*")
  121. w.Header().Set("Access-Control-Allow-Headers", "*")
  122. w.Header().Set("Access-Control-Allow-Credentials", "true")
  123. }
  124. // maybeCheckJwtAuthorization returns true if access should be granted, false if it should be denied
  125. func (fs *FilerServer) maybeCheckJwtAuthorization(r *http.Request, isWrite bool) bool {
  126. var signingKey security.SigningKey
  127. if isWrite {
  128. if len(fs.filerGuard.SigningKey) == 0 {
  129. return true
  130. } else {
  131. signingKey = fs.filerGuard.SigningKey
  132. }
  133. } else {
  134. if len(fs.filerGuard.ReadSigningKey) == 0 {
  135. return true
  136. } else {
  137. signingKey = fs.filerGuard.ReadSigningKey
  138. }
  139. }
  140. tokenStr := security.GetJwt(r)
  141. if tokenStr == "" {
  142. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  143. return false
  144. }
  145. token, err := security.DecodeJwt(signingKey, tokenStr, &security.SeaweedFilerClaims{})
  146. if err != nil {
  147. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  148. return false
  149. }
  150. if !token.Valid {
  151. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  152. return false
  153. } else {
  154. return true
  155. }
  156. }