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.

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