volume_server_handlers_write.go 4.8 KB

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