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.

167 lines
4.5 KiB

9 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
9 years ago
9 years ago
9 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/operation"
  12. "github.com/chrislusf/seaweedfs/weed/stats"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/topology"
  15. )
  16. func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  17. stats.VolumeServerRequestCounter.WithLabelValues("post").Inc()
  18. start := time.Now()
  19. defer func() {
  20. stats.VolumeServerRequestHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds())
  21. }()
  22. if e := r.ParseForm(); e != nil {
  23. glog.V(0).Infoln("form parse error:", e)
  24. writeJsonError(w, r, http.StatusBadRequest, e)
  25. return
  26. }
  27. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  28. volumeId, ve := needle.NewVolumeId(vid)
  29. if ve != nil {
  30. glog.V(0).Infoln("NewVolumeId error:", ve)
  31. writeJsonError(w, r, http.StatusBadRequest, ve)
  32. return
  33. }
  34. if !vs.maybeCheckJwtAuthorization(r, vid, fid, true) {
  35. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  36. return
  37. }
  38. needle, originalSize, ne := needle.CreateNeedleFromRequest(r, vs.FixJpgOrientation, vs.fileSizeLimitBytes)
  39. if ne != nil {
  40. writeJsonError(w, r, http.StatusBadRequest, ne)
  41. return
  42. }
  43. ret := operation.UploadResult{}
  44. _, isUnchanged, writeError := topology.ReplicatedWrite(vs.GetMaster(), vs.store, volumeId, needle, r)
  45. // http 304 status code does not allow body
  46. if writeError == nil && isUnchanged {
  47. w.WriteHeader(http.StatusNotModified)
  48. return
  49. }
  50. httpStatus := http.StatusCreated
  51. if writeError != nil {
  52. httpStatus = http.StatusInternalServerError
  53. ret.Error = writeError.Error()
  54. }
  55. if needle.HasName() {
  56. ret.Name = string(needle.Name)
  57. }
  58. ret.Size = uint32(originalSize)
  59. ret.ETag = needle.Etag()
  60. setEtag(w, ret.ETag)
  61. writeJsonQuiet(w, r, httpStatus, ret)
  62. }
  63. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  64. stats.VolumeServerRequestCounter.WithLabelValues("delete").Inc()
  65. start := time.Now()
  66. defer func() {
  67. stats.VolumeServerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds())
  68. }()
  69. n := new(needle.Needle)
  70. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  71. volumeId, _ := needle.NewVolumeId(vid)
  72. n.ParsePath(fid)
  73. if !vs.maybeCheckJwtAuthorization(r, vid, fid, true) {
  74. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  75. return
  76. }
  77. // glog.V(2).Infof("volume %s deleting %s", vid, n)
  78. cookie := n.Cookie
  79. ecVolume, hasEcVolume := vs.store.FindEcVolume(volumeId)
  80. if hasEcVolume {
  81. count, err := vs.store.DeleteEcShardNeedle(context.Background(), ecVolume, n, cookie)
  82. writeDeleteResult(err, count, w, r)
  83. return
  84. }
  85. _, ok := vs.store.ReadVolumeNeedle(volumeId, n)
  86. if ok != nil {
  87. m := make(map[string]uint32)
  88. m["size"] = 0
  89. writeJsonQuiet(w, r, http.StatusNotFound, m)
  90. return
  91. }
  92. if n.Cookie != cookie {
  93. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  94. writeJsonError(w, r, http.StatusBadRequest, errors.New("File Random Cookie does not match."))
  95. return
  96. }
  97. count := int64(n.Size)
  98. if n.IsChunkedManifest() {
  99. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  100. if e != nil {
  101. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Load chunks manifest error: %v", e))
  102. return
  103. }
  104. // make sure all chunks had deleted before delete manifest
  105. if e := chunkManifest.DeleteChunks(vs.GetMaster(), vs.grpcDialOption); e != nil {
  106. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Delete chunks error: %v", e))
  107. return
  108. }
  109. count = chunkManifest.Size
  110. }
  111. n.LastModified = uint64(time.Now().Unix())
  112. if len(r.FormValue("ts")) > 0 {
  113. modifiedTime, err := strconv.ParseInt(r.FormValue("ts"), 10, 64)
  114. if err == nil {
  115. n.LastModified = uint64(modifiedTime)
  116. }
  117. }
  118. _, err := topology.ReplicatedDelete(vs.GetMaster(), vs.store, volumeId, n, r)
  119. writeDeleteResult(err, count, w, r)
  120. }
  121. func writeDeleteResult(err error, count int64, w http.ResponseWriter, r *http.Request) {
  122. if err == nil {
  123. m := make(map[string]int64)
  124. m["size"] = count
  125. writeJsonQuiet(w, r, http.StatusAccepted, m)
  126. } else {
  127. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Deletion Failed: %v", err))
  128. }
  129. }
  130. func setEtag(w http.ResponseWriter, etag string) {
  131. if etag != "" {
  132. if strings.HasPrefix(etag, "\"") {
  133. w.Header().Set("ETag", etag)
  134. } else {
  135. w.Header().Set("ETag", "\""+etag+"\"")
  136. }
  137. }
  138. }