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.

234 lines
6.5 KiB

11 years ago
11 years ago
11 years ago
11 years ago
  1. package weed_server
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/operation"
  5. "code.google.com/p/weed-fs/go/stats"
  6. "code.google.com/p/weed-fs/go/storage"
  7. "code.google.com/p/weed-fs/go/topology"
  8. "mime"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
  15. func (vs *VolumeServer) storeHandler(w http.ResponseWriter, r *http.Request) {
  16. switch r.Method {
  17. case "GET":
  18. stats.ReadRequest()
  19. vs.GetOrHeadHandler(w, r, true)
  20. case "HEAD":
  21. stats.ReadRequest()
  22. vs.GetOrHeadHandler(w, r, false)
  23. case "DELETE":
  24. stats.DeleteRequest()
  25. secure(vs.whiteList, vs.DeleteHandler)(w, r)
  26. case "PUT":
  27. stats.WriteRequest()
  28. secure(vs.whiteList, vs.PostHandler)(w, r)
  29. case "POST":
  30. stats.WriteRequest()
  31. secure(vs.whiteList, vs.PostHandler)(w, r)
  32. }
  33. }
  34. func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  35. n := new(storage.Needle)
  36. vid, fid, filename, ext, _ := parseURLPath(r.URL.Path)
  37. volumeId, err := storage.NewVolumeId(vid)
  38. if err != nil {
  39. glog.V(2).Infoln("parsing error:", err, r.URL.Path)
  40. w.WriteHeader(http.StatusBadRequest)
  41. return
  42. }
  43. err = n.ParsePath(fid)
  44. if err != nil {
  45. glog.V(2).Infoln("parsing fid error:", err, r.URL.Path)
  46. w.WriteHeader(http.StatusBadRequest)
  47. return
  48. }
  49. glog.V(4).Infoln("volume", volumeId, "reading", n)
  50. if !vs.store.HasVolume(volumeId) {
  51. lookupResult, err := operation.Lookup(vs.masterNode, volumeId.String())
  52. glog.V(2).Infoln("volume", volumeId, "found on", lookupResult, "error", err)
  53. if err == nil && len(lookupResult.Locations) > 0 {
  54. http.Redirect(w, r, "http://"+lookupResult.Locations[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
  55. } else {
  56. glog.V(2).Infoln("lookup error:", err, r.URL.Path)
  57. w.WriteHeader(http.StatusNotFound)
  58. }
  59. return
  60. }
  61. cookie := n.Cookie
  62. count, e := vs.store.Read(volumeId, n)
  63. glog.V(4).Infoln("read bytes", count, "error", e)
  64. if e != nil || count <= 0 {
  65. glog.V(0).Infoln("read error:", e, r.URL.Path)
  66. w.WriteHeader(http.StatusNotFound)
  67. return
  68. }
  69. if n.Cookie != cookie {
  70. glog.V(0).Infoln("request", r.URL.Path, "with unmaching cookie seen:", cookie, "expected:", n.Cookie, "from", r.RemoteAddr, "agent", r.UserAgent())
  71. w.WriteHeader(http.StatusNotFound)
  72. return
  73. }
  74. if n.LastModified != 0 {
  75. w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
  76. if r.Header.Get("If-Modified-Since") != "" {
  77. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  78. if t.Unix() >= int64(n.LastModified) {
  79. w.WriteHeader(http.StatusNotModified)
  80. return
  81. }
  82. }
  83. }
  84. }
  85. if n.NameSize > 0 && filename == "" {
  86. filename = string(n.Name)
  87. dotIndex := strings.LastIndex(filename, ".")
  88. if dotIndex > 0 {
  89. ext = filename[dotIndex:]
  90. }
  91. }
  92. mtype := ""
  93. if ext != "" {
  94. mtype = mime.TypeByExtension(ext)
  95. }
  96. if n.MimeSize > 0 {
  97. mtype = string(n.Mime)
  98. }
  99. if mtype != "" {
  100. w.Header().Set("Content-Type", mtype)
  101. }
  102. if filename != "" {
  103. w.Header().Set("Content-Disposition", "filename="+fileNameEscaper.Replace(filename))
  104. }
  105. if ext != ".gz" {
  106. if n.IsGzipped() {
  107. if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  108. w.Header().Set("Content-Encoding", "gzip")
  109. } else {
  110. if n.Data, err = storage.UnGzipData(n.Data); err != nil {
  111. glog.V(0).Infoln("lookup error:", err, r.URL.Path)
  112. }
  113. }
  114. }
  115. }
  116. w.Header().Set("Content-Length", strconv.Itoa(len(n.Data)))
  117. if isGetMethod {
  118. if _, e = w.Write(n.Data); e != nil {
  119. glog.V(0).Infoln("response write error:", e)
  120. }
  121. }
  122. }
  123. func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  124. if e := r.ParseForm(); e != nil {
  125. glog.V(0).Infoln("form parse error:", e)
  126. writeJsonError(w, r, e)
  127. return
  128. }
  129. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  130. volumeId, ve := storage.NewVolumeId(vid)
  131. if ve != nil {
  132. glog.V(0).Infoln("NewVolumeId error:", ve)
  133. writeJsonError(w, r, ve)
  134. return
  135. }
  136. needle, ne := storage.NewNeedle(r)
  137. if ne != nil {
  138. writeJsonError(w, r, ne)
  139. return
  140. }
  141. ret := operation.UploadResult{}
  142. size, errorStatus := topology.ReplicatedWrite(vs.masterNode, vs.store, volumeId, needle, r)
  143. if errorStatus == "" {
  144. w.WriteHeader(http.StatusCreated)
  145. } else {
  146. w.WriteHeader(http.StatusInternalServerError)
  147. ret.Error = errorStatus
  148. }
  149. if needle.HasName() {
  150. ret.Name = string(needle.Name)
  151. }
  152. ret.Size = size
  153. writeJsonQuiet(w, r, ret)
  154. }
  155. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  156. n := new(storage.Needle)
  157. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  158. volumeId, _ := storage.NewVolumeId(vid)
  159. n.ParsePath(fid)
  160. glog.V(2).Infoln("deleting", n)
  161. cookie := n.Cookie
  162. count, ok := vs.store.Read(volumeId, n)
  163. if ok != nil {
  164. m := make(map[string]uint32)
  165. m["size"] = 0
  166. writeJsonQuiet(w, r, m)
  167. return
  168. }
  169. if n.Cookie != cookie {
  170. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  171. return
  172. }
  173. n.Size = 0
  174. ret := topology.ReplicatedDelete(vs.masterNode, vs.store, volumeId, n, r)
  175. if ret != 0 {
  176. w.WriteHeader(http.StatusAccepted)
  177. } else {
  178. w.WriteHeader(http.StatusInternalServerError)
  179. }
  180. m := make(map[string]uint32)
  181. m["size"] = uint32(count)
  182. writeJsonQuiet(w, r, m)
  183. }
  184. //Experts only: takes multiple fid parameters. This function does not propagate deletes to replicas.
  185. func (vs *VolumeServer) batchDeleteHandler(w http.ResponseWriter, r *http.Request) {
  186. r.ParseForm()
  187. var ret []operation.DeleteResult
  188. for _, fid := range r.Form["fid"] {
  189. vid, id_cookie, err := operation.ParseFileId(fid)
  190. if err != nil {
  191. ret = append(ret, operation.DeleteResult{Fid: fid, Error: err.Error()})
  192. continue
  193. }
  194. n := new(storage.Needle)
  195. volumeId, _ := storage.NewVolumeId(vid)
  196. n.ParsePath(id_cookie)
  197. glog.V(4).Infoln("batch deleting", n)
  198. cookie := n.Cookie
  199. if _, err := vs.store.Read(volumeId, n); err != nil {
  200. ret = append(ret, operation.DeleteResult{Fid: fid, Error: err.Error()})
  201. continue
  202. }
  203. if n.Cookie != cookie {
  204. ret = append(ret, operation.DeleteResult{Fid: fid, Error: "File Random Cookie does not match."})
  205. glog.V(0).Infoln("deleting", fid, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  206. return
  207. }
  208. if size, err := vs.store.Delete(volumeId, n); err != nil {
  209. ret = append(ret, operation.DeleteResult{Fid: fid, Error: err.Error()})
  210. } else {
  211. ret = append(ret, operation.DeleteResult{Fid: fid, Size: int(size)})
  212. }
  213. }
  214. w.WriteHeader(http.StatusAccepted)
  215. writeJsonQuiet(w, r, ret)
  216. }