volume_server_handlers.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "sync/atomic"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/security"
  12. "github.com/seaweedfs/seaweedfs/weed/stats"
  13. )
  14. /*
  15. If volume server is started with a separated public port, the public port will
  16. be more "secure".
  17. Public port currently only supports reads.
  18. Later writes on public port can have one of the 3
  19. security settings:
  20. 1. not secured
  21. 2. secured by white list
  22. 3. secured by JWT(Json Web Token)
  23. */
  24. func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Request) {
  25. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  26. if r.Header.Get("Origin") != "" {
  27. w.Header().Set("Access-Control-Allow-Origin", "*")
  28. w.Header().Set("Access-Control-Allow-Credentials", "true")
  29. }
  30. stats.VolumeServerRequestCounter.WithLabelValues(r.Method).Inc()
  31. start := time.Now()
  32. defer func(start time.Time) {
  33. stats.VolumeServerRequestHistogram.WithLabelValues(r.Method).Observe(time.Since(start).Seconds())
  34. }(start)
  35. switch r.Method {
  36. case "GET", "HEAD":
  37. stats.ReadRequest()
  38. vs.inFlightDownloadDataLimitCond.L.Lock()
  39. inFlightDownloadSize := atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  40. for vs.concurrentDownloadLimit != 0 && inFlightDownloadSize > vs.concurrentDownloadLimit {
  41. select {
  42. case <-r.Context().Done():
  43. glog.V(4).Infof("request cancelled from %s: %v", r.RemoteAddr, r.Context().Err())
  44. w.WriteHeader(http.StatusInternalServerError)
  45. vs.inFlightDownloadDataLimitCond.L.Unlock()
  46. return
  47. default:
  48. glog.V(4).Infof("wait because inflight download data %d > %d", inFlightDownloadSize, vs.concurrentDownloadLimit)
  49. vs.inFlightDownloadDataLimitCond.Wait()
  50. }
  51. inFlightDownloadSize = atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  52. }
  53. vs.inFlightDownloadDataLimitCond.L.Unlock()
  54. vs.GetOrHeadHandler(w, r)
  55. case "DELETE":
  56. stats.DeleteRequest()
  57. vs.guard.WhiteList(vs.DeleteHandler)(w, r)
  58. case "PUT", "POST":
  59. contentLength := getContentLength(r)
  60. // exclude the replication from the concurrentUploadLimitMB
  61. if r.URL.Query().Get("type") != "replicate" && vs.concurrentUploadLimit != 0 {
  62. startTime := time.Now()
  63. vs.inFlightUploadDataLimitCond.L.Lock()
  64. inFlightUploadDataSize := atomic.LoadInt64(&vs.inFlightUploadDataSize)
  65. for inFlightUploadDataSize > vs.concurrentUploadLimit {
  66. //wait timeout check
  67. if startTime.Add(vs.inflightUploadDataTimeout).Before(time.Now()) {
  68. vs.inFlightUploadDataLimitCond.L.Unlock()
  69. err := fmt.Errorf("reject because inflight upload data %d > %d, and wait timeout", inFlightUploadDataSize, vs.concurrentUploadLimit)
  70. glog.V(1).Infof("too many requests: %v", err)
  71. writeJsonError(w, r, http.StatusTooManyRequests, err)
  72. return
  73. }
  74. glog.V(4).Infof("wait because inflight upload data %d > %d", inFlightUploadDataSize, vs.concurrentUploadLimit)
  75. vs.inFlightUploadDataLimitCond.Wait()
  76. inFlightUploadDataSize = atomic.LoadInt64(&vs.inFlightUploadDataSize)
  77. }
  78. vs.inFlightUploadDataLimitCond.L.Unlock()
  79. }
  80. atomic.AddInt64(&vs.inFlightUploadDataSize, contentLength)
  81. defer func() {
  82. atomic.AddInt64(&vs.inFlightUploadDataSize, -contentLength)
  83. if vs.concurrentUploadLimit != 0 {
  84. vs.inFlightUploadDataLimitCond.Signal()
  85. }
  86. }()
  87. // processs uploads
  88. stats.WriteRequest()
  89. vs.guard.WhiteList(vs.PostHandler)(w, r)
  90. case "OPTIONS":
  91. stats.ReadRequest()
  92. w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS")
  93. w.Header().Add("Access-Control-Allow-Headers", "*")
  94. }
  95. }
  96. func getContentLength(r *http.Request) int64 {
  97. contentLength := r.Header.Get("Content-Length")
  98. if contentLength != "" {
  99. length, err := strconv.ParseInt(contentLength, 10, 64)
  100. if err != nil {
  101. return 0
  102. }
  103. return length
  104. }
  105. return 0
  106. }
  107. func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
  108. w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION)
  109. if r.Header.Get("Origin") != "" {
  110. w.Header().Set("Access-Control-Allow-Origin", "*")
  111. w.Header().Set("Access-Control-Allow-Credentials", "true")
  112. }
  113. switch r.Method {
  114. case "GET", "HEAD":
  115. stats.ReadRequest()
  116. vs.inFlightDownloadDataLimitCond.L.Lock()
  117. inFlightDownloadSize := atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  118. for vs.concurrentDownloadLimit != 0 && inFlightDownloadSize > vs.concurrentDownloadLimit {
  119. glog.V(4).Infof("wait because inflight download data %d > %d", inFlightDownloadSize, vs.concurrentDownloadLimit)
  120. vs.inFlightDownloadDataLimitCond.Wait()
  121. inFlightDownloadSize = atomic.LoadInt64(&vs.inFlightDownloadDataSize)
  122. }
  123. vs.inFlightDownloadDataLimitCond.L.Unlock()
  124. vs.GetOrHeadHandler(w, r)
  125. case "OPTIONS":
  126. stats.ReadRequest()
  127. w.Header().Add("Access-Control-Allow-Methods", "GET, OPTIONS")
  128. w.Header().Add("Access-Control-Allow-Headers", "*")
  129. }
  130. }
  131. func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string, isWrite bool) bool {
  132. var signingKey security.SigningKey
  133. if isWrite {
  134. if len(vs.guard.SigningKey) == 0 {
  135. return true
  136. } else {
  137. signingKey = vs.guard.SigningKey
  138. }
  139. } else {
  140. if len(vs.guard.ReadSigningKey) == 0 {
  141. return true
  142. } else {
  143. signingKey = vs.guard.ReadSigningKey
  144. }
  145. }
  146. tokenStr := security.GetJwt(r)
  147. if tokenStr == "" {
  148. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  149. return false
  150. }
  151. token, err := security.DecodeJwt(signingKey, tokenStr, &security.SeaweedFileIdClaims{})
  152. if err != nil {
  153. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  154. return false
  155. }
  156. if !token.Valid {
  157. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  158. return false
  159. }
  160. if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
  161. if sepIndex := strings.LastIndex(fid, "_"); sepIndex > 0 {
  162. fid = fid[:sepIndex]
  163. }
  164. return sc.Fid == vid+","+fid
  165. }
  166. glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
  167. return false
  168. }