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.

104 lines
2.8 KiB

9 years ago
9 years ago
9 years ago
  1. package weed_server
  2. import (
  3. "io"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "github.com/syndtr/goleveldb/leveldb"
  12. )
  13. // listDirectoryHandler lists directories and folers under a directory
  14. // files are sorted by name and paginated via "lastFileName" and "limit".
  15. // sub directories are listed on the first page, when "lastFileName"
  16. // is empty.
  17. func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
  18. if !strings.HasSuffix(r.URL.Path, "/") {
  19. return
  20. }
  21. dirlist, err := fs.filer.ListDirectories(r.URL.Path)
  22. if err == leveldb.ErrNotFound {
  23. glog.V(3).Infoln("Directory Not Found in db", r.URL.Path)
  24. w.WriteHeader(http.StatusNotFound)
  25. return
  26. }
  27. m := make(map[string]interface{})
  28. m["Directory"] = r.URL.Path
  29. lastFileName := r.FormValue("lastFileName")
  30. if lastFileName == "" {
  31. m["Subdirectories"] = dirlist
  32. }
  33. limit, limit_err := strconv.Atoi(r.FormValue("limit"))
  34. if limit_err != nil {
  35. limit = 100
  36. }
  37. m["Files"], _ = fs.filer.ListFiles(r.URL.Path, lastFileName, limit)
  38. writeJsonQuiet(w, r, http.StatusOK, m)
  39. }
  40. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  41. if strings.HasSuffix(r.URL.Path, "/") {
  42. if fs.disableDirListing {
  43. w.WriteHeader(http.StatusMethodNotAllowed)
  44. return
  45. }
  46. fs.listDirectoryHandler(w, r)
  47. return
  48. }
  49. fileId, err := fs.filer.FindFile(r.URL.Path)
  50. if err == leveldb.ErrNotFound {
  51. glog.V(3).Infoln("Not found in db", r.URL.Path)
  52. w.WriteHeader(http.StatusNotFound)
  53. return
  54. }
  55. urlLocation, err := operation.LookupFileId(fs.getMasterNode(), fileId)
  56. if err != nil {
  57. glog.V(1).Infoln("operation LookupFileId %s failed, err is %s", fileId, err.Error())
  58. w.WriteHeader(http.StatusNotFound)
  59. return
  60. }
  61. urlString := urlLocation
  62. if fs.redirectOnRead {
  63. http.Redirect(w, r, urlString, http.StatusFound)
  64. return
  65. }
  66. u, _ := url.Parse(urlString)
  67. q := u.Query()
  68. for key, values := range r.URL.Query() {
  69. for _, value := range values {
  70. q.Add(key, value)
  71. }
  72. }
  73. u.RawQuery = q.Encode()
  74. request := &http.Request{
  75. Method: r.Method,
  76. URL: u,
  77. Proto: r.Proto,
  78. ProtoMajor: r.ProtoMajor,
  79. ProtoMinor: r.ProtoMinor,
  80. Header: r.Header,
  81. Body: r.Body,
  82. Host: r.Host,
  83. ContentLength: r.ContentLength,
  84. }
  85. glog.V(3).Infoln("retrieving from", u)
  86. resp, do_err := util.Do(request)
  87. if do_err != nil {
  88. glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
  89. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  90. return
  91. }
  92. defer resp.Body.Close()
  93. for k, v := range resp.Header {
  94. w.Header()[k] = v
  95. }
  96. w.WriteHeader(resp.StatusCode)
  97. io.Copy(w, resp.Body)
  98. }