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.

201 lines
5.8 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "io/ioutil"
  7. "math/rand"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "github.com/chrislusf/weed-fs/go/glog"
  13. "github.com/chrislusf/weed-fs/go/operation"
  14. "github.com/chrislusf/weed-fs/go/util"
  15. "github.com/syndtr/goleveldb/leveldb"
  16. )
  17. func (fs *FilerServer) filerHandler(w http.ResponseWriter, r *http.Request) {
  18. switch r.Method {
  19. case "GET":
  20. fs.GetOrHeadHandler(w, r, true)
  21. case "HEAD":
  22. fs.GetOrHeadHandler(w, r, false)
  23. case "DELETE":
  24. fs.DeleteHandler(w, r)
  25. case "PUT":
  26. fs.PostHandler(w, r)
  27. case "POST":
  28. fs.PostHandler(w, r)
  29. }
  30. }
  31. func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
  32. if !strings.HasSuffix(r.URL.Path, "/") {
  33. return
  34. }
  35. dirlist, err := fs.filer.ListDirectories(r.URL.Path)
  36. if err == leveldb.ErrNotFound {
  37. glog.V(3).Infoln("Directory Not Found in db", r.URL.Path)
  38. w.WriteHeader(http.StatusNotFound)
  39. return
  40. }
  41. m := make(map[string]interface{})
  42. m["Directory"] = r.URL.Path
  43. m["Subdirectories"] = dirlist
  44. lastFileName := r.FormValue("lastFileName")
  45. limit, limit_err := strconv.Atoi(r.FormValue("limit"))
  46. if limit_err != nil {
  47. limit = 100
  48. }
  49. m["Files"], _ = fs.filer.ListFiles(r.URL.Path, lastFileName, limit)
  50. writeJsonQuiet(w, r, m)
  51. }
  52. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  53. if strings.HasSuffix(r.URL.Path, "/") {
  54. fs.listDirectoryHandler(w, r)
  55. return
  56. }
  57. fileId, err := fs.filer.FindFile(r.URL.Path)
  58. if err == leveldb.ErrNotFound {
  59. glog.V(3).Infoln("Not found in db", r.URL.Path)
  60. w.WriteHeader(http.StatusNotFound)
  61. return
  62. }
  63. parts := strings.Split(fileId, ",")
  64. if len(parts) != 2 {
  65. glog.V(1).Infoln("Invalid fileId", fileId)
  66. w.WriteHeader(http.StatusNotFound)
  67. return
  68. }
  69. lookup, lookupError := operation.Lookup(fs.master, parts[0])
  70. if lookupError != nil {
  71. glog.V(1).Infoln("Invalid lookup", lookupError.Error())
  72. w.WriteHeader(http.StatusNotFound)
  73. return
  74. }
  75. if len(lookup.Locations) == 0 {
  76. glog.V(1).Infoln("Can not find location for volume", parts[0])
  77. w.WriteHeader(http.StatusNotFound)
  78. return
  79. }
  80. urlLocation := lookup.Locations[rand.Intn(len(lookup.Locations))].PublicUrl
  81. u, _ := url.Parse("http://" + urlLocation + "/" + fileId)
  82. request := &http.Request{
  83. Method: r.Method,
  84. URL: u,
  85. Proto: r.Proto,
  86. ProtoMajor: r.ProtoMajor,
  87. ProtoMinor: r.ProtoMinor,
  88. Header: r.Header,
  89. Body: r.Body,
  90. Host: r.Host,
  91. ContentLength: r.ContentLength,
  92. }
  93. glog.V(3).Infoln("retrieving from", u)
  94. resp, do_err := util.Do(request)
  95. if do_err != nil {
  96. glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
  97. writeJsonError(w, r, do_err)
  98. return
  99. }
  100. defer resp.Body.Close()
  101. for k, v := range resp.Header {
  102. w.Header()[k] = v
  103. }
  104. w.WriteHeader(resp.StatusCode)
  105. io.Copy(w, resp.Body)
  106. }
  107. func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  108. query := r.URL.Query()
  109. assignResult, ae := operation.Assign(fs.master, 1, query.Get("replication"), fs.collection, query.Get("ttl"))
  110. if ae != nil {
  111. glog.V(0).Infoln("failing to assign a file id", ae.Error())
  112. writeJsonError(w, r, ae)
  113. return
  114. }
  115. u, _ := url.Parse("http://" + assignResult.PublicUrl + "/" + assignResult.Fid)
  116. glog.V(4).Infoln("post to", u)
  117. request := &http.Request{
  118. Method: r.Method,
  119. URL: u,
  120. Proto: r.Proto,
  121. ProtoMajor: r.ProtoMajor,
  122. ProtoMinor: r.ProtoMinor,
  123. Header: r.Header,
  124. Body: r.Body,
  125. Host: r.Host,
  126. ContentLength: r.ContentLength,
  127. }
  128. resp, do_err := util.Do(request)
  129. if do_err != nil {
  130. glog.V(0).Infoln("failing to connect to volume server", r.RequestURI, do_err.Error())
  131. writeJsonError(w, r, do_err)
  132. return
  133. }
  134. defer resp.Body.Close()
  135. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  136. if ra_err != nil {
  137. glog.V(0).Infoln("failing to upload to volume server", r.RequestURI, ra_err.Error())
  138. writeJsonError(w, r, ra_err)
  139. return
  140. }
  141. glog.V(4).Infoln("post result", string(resp_body))
  142. var ret operation.UploadResult
  143. unmarshal_err := json.Unmarshal(resp_body, &ret)
  144. if unmarshal_err != nil {
  145. glog.V(0).Infoln("failing to read upload resonse", r.RequestURI, string(resp_body))
  146. writeJsonError(w, r, unmarshal_err)
  147. return
  148. }
  149. if ret.Error != "" {
  150. glog.V(0).Infoln("failing to post to volume server", r.RequestURI, ret.Error)
  151. writeJsonError(w, r, errors.New(ret.Error))
  152. return
  153. }
  154. path := r.URL.Path
  155. if strings.HasSuffix(path, "/") {
  156. if ret.Name != "" {
  157. path += ret.Name
  158. } else {
  159. operation.DeleteFile(fs.master, assignResult.Fid) //clean up
  160. glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
  161. writeJsonError(w, r, errors.New("Can not to write to folder "+path+" without a file name"))
  162. return
  163. }
  164. }
  165. glog.V(4).Infoln("saving", path, "=>", assignResult.Fid)
  166. if db_err := fs.filer.CreateFile(path, assignResult.Fid); db_err != nil {
  167. operation.DeleteFile(fs.master, assignResult.Fid) //clean up
  168. glog.V(0).Infoln("failing to write to filer server", r.RequestURI, db_err.Error())
  169. writeJsonError(w, r, db_err)
  170. return
  171. }
  172. w.WriteHeader(http.StatusCreated)
  173. }
  174. // curl -X DELETE http://localhost:8888/path/to
  175. // curl -X DELETE http://localhost:8888/path/to?recursive=true
  176. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  177. var err error
  178. var fid string
  179. if strings.HasSuffix(r.URL.Path, "/") {
  180. isRecursive := r.FormValue("recursive") == "true"
  181. err = fs.filer.DeleteDirectory(r.URL.Path, isRecursive)
  182. } else {
  183. fid, err = fs.filer.DeleteFile(r.URL.Path)
  184. if err == nil {
  185. err = operation.DeleteFile(fs.master, fid)
  186. }
  187. }
  188. if err == nil {
  189. w.WriteHeader(http.StatusAccepted)
  190. writeJsonQuiet(w, r, map[string]string{"error": ""})
  191. } else {
  192. glog.V(4).Infoln("deleting", r.URL.Path, ":", err.Error())
  193. writeJsonError(w, r, err)
  194. }
  195. }