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.

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