volume_server_handlers_write.go 4.9 KB

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