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.

195 lines
5.6 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. "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/util"
  6. "encoding/json"
  7. "errors"
  8. "github.com/syndtr/goleveldb/leveldb"
  9. "io"
  10. "io/ioutil"
  11. "math/rand"
  12. "net/http"
  13. "net/url"
  14. "strconv"
  15. "strings"
  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. io.Copy(w, resp.Body)
  102. }
  103. func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  104. query := r.URL.Query()
  105. assignResult, ae := operation.Assign(fs.master, 1, query.Get("replication"), fs.collection)
  106. if ae != nil {
  107. glog.V(0).Infoln("failing to assign a file id", ae.Error())
  108. writeJsonError(w, r, ae)
  109. return
  110. }
  111. u, _ := url.Parse("http://" + assignResult.PublicUrl + "/" + assignResult.Fid)
  112. glog.V(4).Infoln("post to", u)
  113. request := &http.Request{
  114. Method: r.Method,
  115. URL: u,
  116. Proto: r.Proto,
  117. ProtoMajor: r.ProtoMajor,
  118. ProtoMinor: r.ProtoMinor,
  119. Header: r.Header,
  120. Body: r.Body,
  121. Host: r.Host,
  122. ContentLength: r.ContentLength,
  123. }
  124. resp, do_err := util.Do(request)
  125. if do_err != nil {
  126. glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
  127. writeJsonError(w, r, do_err)
  128. return
  129. }
  130. defer resp.Body.Close()
  131. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  132. if ra_err != nil {
  133. glog.V(0).Infoln("failing to upload to volume server", ra_err.Error())
  134. writeJsonError(w, r, ra_err)
  135. return
  136. }
  137. glog.V(4).Infoln("post result", string(resp_body))
  138. var ret operation.UploadResult
  139. unmarshal_err := json.Unmarshal(resp_body, &ret)
  140. if unmarshal_err != nil {
  141. glog.V(0).Infoln("failing to read upload resonse", string(resp_body))
  142. writeJsonError(w, r, unmarshal_err)
  143. return
  144. }
  145. if ret.Error != "" {
  146. glog.V(0).Infoln("failing to post to volume server", ra_err.Error())
  147. writeJsonError(w, r, errors.New(ret.Error))
  148. return
  149. }
  150. path := r.URL.Path
  151. if strings.HasSuffix(path, "/") {
  152. if ret.Name != "" {
  153. path += ret.Name
  154. } else {
  155. operation.DeleteFile(fs.master, assignResult.Fid) //clean up
  156. glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
  157. writeJsonError(w, r, errors.New("Can not to write to folder "+path+" without a file name"))
  158. return
  159. }
  160. }
  161. glog.V(4).Infoln("saving", path, "=>", assignResult.Fid)
  162. if db_err := fs.filer.CreateFile(path, assignResult.Fid); db_err != nil {
  163. operation.DeleteFile(fs.master, assignResult.Fid) //clean up
  164. glog.V(0).Infoln("failing to write to filer server", db_err.Error())
  165. writeJsonError(w, r, db_err)
  166. return
  167. }
  168. }
  169. // curl -X DELETE http://localhost:8888/path/to
  170. // curl -X DELETE http://localhost:8888/path/to?recursive=true
  171. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  172. var err error
  173. var fid string
  174. if strings.HasSuffix(r.URL.Path, "/") {
  175. isRecursive := r.FormValue("recursive") == "true"
  176. err = fs.filer.DeleteDirectory(r.URL.Path, isRecursive)
  177. } else {
  178. fid, err = fs.filer.DeleteFile(r.URL.Path)
  179. if err == nil {
  180. err = operation.DeleteFile(fs.master, fid)
  181. }
  182. }
  183. if err == nil {
  184. w.WriteHeader(http.StatusAccepted)
  185. writeJsonQuiet(w, r, map[string]string{"error": ""})
  186. } else {
  187. glog.V(4).Infoln("deleting", r.URL.Path, ":", err.Error())
  188. writeJsonError(w, r, err)
  189. }
  190. }