volume_server_handlers_write.go 4.8 KB

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