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.

163 lines
4.4 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
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)
  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. httpStatus := http.StatusCreated
  46. if isUnchanged {
  47. httpStatus = http.StatusNotModified
  48. }
  49. if writeError != nil {
  50. httpStatus = http.StatusInternalServerError
  51. ret.Error = writeError.Error()
  52. }
  53. if needle.HasName() {
  54. ret.Name = string(needle.Name)
  55. }
  56. ret.Size = uint32(originalSize)
  57. ret.ETag = needle.Etag()
  58. setEtag(w, ret.ETag)
  59. writeJsonQuiet(w, r, httpStatus, ret)
  60. }
  61. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  62. stats.VolumeServerRequestCounter.WithLabelValues("delete").Inc()
  63. start := time.Now()
  64. defer func() {
  65. stats.VolumeServerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds())
  66. }()
  67. n := new(needle.Needle)
  68. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  69. volumeId, _ := needle.NewVolumeId(vid)
  70. n.ParsePath(fid)
  71. if !vs.maybeCheckJwtAuthorization(r, vid, fid, true) {
  72. writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
  73. return
  74. }
  75. // glog.V(2).Infof("volume %s deleting %s", vid, n)
  76. cookie := n.Cookie
  77. ecVolume, hasEcVolume := vs.store.FindEcVolume(volumeId)
  78. if hasEcVolume {
  79. count, err := vs.store.DeleteEcShardNeedle(context.Background(), ecVolume, n, cookie)
  80. writeDeleteResult(err, count, w, r)
  81. return
  82. }
  83. _, ok := vs.store.ReadVolumeNeedle(volumeId, n)
  84. if ok != nil {
  85. m := make(map[string]uint32)
  86. m["size"] = 0
  87. writeJsonQuiet(w, r, http.StatusNotFound, m)
  88. return
  89. }
  90. if n.Cookie != cookie {
  91. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  92. writeJsonError(w, r, http.StatusBadRequest, errors.New("File Random Cookie does not match."))
  93. return
  94. }
  95. count := int64(n.Size)
  96. if n.IsChunkedManifest() {
  97. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  98. if e != nil {
  99. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Load chunks manifest error: %v", e))
  100. return
  101. }
  102. // make sure all chunks had deleted before delete manifest
  103. if e := chunkManifest.DeleteChunks(vs.GetMaster(), vs.grpcDialOption); e != nil {
  104. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Delete chunks error: %v", e))
  105. return
  106. }
  107. count = chunkManifest.Size
  108. }
  109. n.LastModified = uint64(time.Now().Unix())
  110. if len(r.FormValue("ts")) > 0 {
  111. modifiedTime, err := strconv.ParseInt(r.FormValue("ts"), 10, 64)
  112. if err == nil {
  113. n.LastModified = uint64(modifiedTime)
  114. }
  115. }
  116. _, err := topology.ReplicatedDelete(vs.GetMaster(), vs.store, volumeId, n, r)
  117. writeDeleteResult(err, count, w, r)
  118. }
  119. func writeDeleteResult(err error, count int64, w http.ResponseWriter, r *http.Request) {
  120. if err == nil {
  121. m := make(map[string]int64)
  122. m["size"] = count
  123. writeJsonQuiet(w, r, http.StatusAccepted, m)
  124. } else {
  125. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Deletion Failed: %v", err))
  126. }
  127. }
  128. func setEtag(w http.ResponseWriter, etag string) {
  129. if etag != "" {
  130. if strings.HasPrefix(etag, "\"") {
  131. w.Header().Set("ETag", etag)
  132. } else {
  133. w.Header().Set("ETag", "\""+etag+"\"")
  134. }
  135. }
  136. }