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.

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