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.

140 lines
3.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
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. ui "github.com/chrislusf/seaweedfs/weed/server/filer_ui"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/syndtr/goleveldb/leveldb"
  13. )
  14. // listDirectoryHandler lists directories and folers under a directory
  15. // files are sorted by name and paginated via "lastFileName" and "limit".
  16. // sub directories are listed on the first page, when "lastFileName"
  17. // is empty.
  18. func (fs *FilerServer) listDirectoryHandler(w http.ResponseWriter, r *http.Request) {
  19. if !strings.HasSuffix(r.URL.Path, "/") {
  20. return
  21. }
  22. limit, limit_err := strconv.Atoi(r.FormValue("limit"))
  23. if limit_err != nil {
  24. limit = 100
  25. }
  26. lastFileName := r.FormValue("lastFileName")
  27. files, err := fs.filer.ListFiles(r.URL.Path, lastFileName, limit)
  28. if err == leveldb.ErrNotFound {
  29. glog.V(0).Infof("Error %s", err)
  30. w.WriteHeader(http.StatusNotFound)
  31. return
  32. }
  33. directories, err2 := fs.filer.ListDirectories(r.URL.Path)
  34. if err2 == leveldb.ErrNotFound {
  35. glog.V(0).Infof("Error %s", err)
  36. w.WriteHeader(http.StatusNotFound)
  37. return
  38. }
  39. shouldDisplayLoadMore := len(files) > 0
  40. lastFileName = ""
  41. if len(files) > 0 {
  42. lastFileName = files[len(files)-1].Name
  43. files2, err3 := fs.filer.ListFiles(r.URL.Path, lastFileName, limit)
  44. if err3 == leveldb.ErrNotFound {
  45. glog.V(0).Infof("Error %s", err)
  46. w.WriteHeader(http.StatusNotFound)
  47. return
  48. }
  49. shouldDisplayLoadMore = len(files2) > 0
  50. }
  51. args := struct {
  52. Path string
  53. Files interface{}
  54. Directories interface{}
  55. Limit int
  56. LastFileName string
  57. ShouldDisplayLoadMore bool
  58. }{
  59. r.URL.Path,
  60. files,
  61. directories,
  62. limit,
  63. lastFileName,
  64. shouldDisplayLoadMore,
  65. }
  66. ui.StatusTpl.Execute(w, args)
  67. }
  68. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  69. if strings.HasSuffix(r.URL.Path, "/") {
  70. if fs.disableDirListing {
  71. w.WriteHeader(http.StatusMethodNotAllowed)
  72. return
  73. }
  74. fs.listDirectoryHandler(w, r)
  75. return
  76. }
  77. fileId, err := fs.filer.FindFile(r.URL.Path)
  78. if err == leveldb.ErrNotFound {
  79. glog.V(3).Infoln("Not found in db", r.URL.Path)
  80. w.WriteHeader(http.StatusNotFound)
  81. return
  82. }
  83. urlLocation, err := operation.LookupFileId(fs.getMasterNode(), fileId)
  84. if err != nil {
  85. glog.V(1).Infoln("operation LookupFileId %s failed, err is %s", fileId, err.Error())
  86. w.WriteHeader(http.StatusNotFound)
  87. return
  88. }
  89. urlString := urlLocation
  90. if fs.redirectOnRead {
  91. http.Redirect(w, r, urlString, http.StatusFound)
  92. return
  93. }
  94. u, _ := url.Parse(urlString)
  95. q := u.Query()
  96. for key, values := range r.URL.Query() {
  97. for _, value := range values {
  98. q.Add(key, value)
  99. }
  100. }
  101. u.RawQuery = q.Encode()
  102. request := &http.Request{
  103. Method: r.Method,
  104. URL: u,
  105. Proto: r.Proto,
  106. ProtoMajor: r.ProtoMajor,
  107. ProtoMinor: r.ProtoMinor,
  108. Header: r.Header,
  109. Body: r.Body,
  110. Host: r.Host,
  111. ContentLength: r.ContentLength,
  112. }
  113. glog.V(3).Infoln("retrieving from", u)
  114. resp, do_err := util.Do(request)
  115. if do_err != nil {
  116. glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
  117. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  118. return
  119. }
  120. defer resp.Body.Close()
  121. for k, v := range resp.Header {
  122. w.Header()[k] = v
  123. }
  124. w.WriteHeader(resp.StatusCode)
  125. io.Copy(w, resp.Body)
  126. }