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.

149 lines
3.6 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/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/chrislusf/seaweedfs/weed/filer2"
  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. path := r.URL.Path
  20. if strings.HasSuffix(path, "/") && len(path) > 1 {
  21. path = path[:len(path)-1]
  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. entries, err := fs.filer.ListDirectoryEntries(filer2.FullPath(path), lastFileName, false, limit)
  29. if err != nil {
  30. glog.V(0).Infof("listDirectory %s %s $d: %s", path, lastFileName, limit, err)
  31. w.WriteHeader(http.StatusNotFound)
  32. return
  33. }
  34. shouldDisplayLoadMore := len(entries) == limit
  35. if path == "/" {
  36. path = ""
  37. }
  38. if len(entries) > 0 {
  39. lastFileName = entries[len(entries)-1].Name()
  40. }
  41. glog.V(4).Infof("listDirectory %s, last file %s, limit %d: %d items", path, lastFileName, limit, len(entries))
  42. args := struct {
  43. Path string
  44. Entries interface{}
  45. Limit int
  46. LastFileName string
  47. ShouldDisplayLoadMore bool
  48. }{
  49. path,
  50. entries,
  51. limit,
  52. lastFileName,
  53. shouldDisplayLoadMore,
  54. }
  55. if r.Header.Get("Accept") == "application/json" {
  56. writeJsonQuiet(w, r, http.StatusOK, args)
  57. } else {
  58. ui.StatusTpl.Execute(w, args)
  59. }
  60. }
  61. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  62. path := r.URL.Path
  63. if strings.HasSuffix(path, "/") && len(path) > 1 {
  64. path = path[:len(path)-1]
  65. }
  66. found, entry, err := fs.filer.FindEntry(filer2.FullPath(path))
  67. if !found || err != nil {
  68. glog.V(3).Infof("Not found %s: %v", path, err)
  69. w.WriteHeader(http.StatusNotFound)
  70. return
  71. }
  72. if entry.IsDirectory() {
  73. if fs.disableDirListing {
  74. w.WriteHeader(http.StatusMethodNotAllowed)
  75. return
  76. }
  77. fs.listDirectoryHandler(w, r)
  78. return
  79. }
  80. if len(entry.Chunks) == 0 {
  81. glog.V(3).Infof("Empty %s: %v", path)
  82. w.WriteHeader(http.StatusNoContent)
  83. return
  84. }
  85. // FIXME pick the right fid
  86. fileId := entry.Chunks[0].FileId
  87. urlLocation, err := operation.LookupFileId(fs.getMasterNode(), fileId)
  88. if err != nil {
  89. glog.V(1).Infoln("operation LookupFileId %s failed, err is %s", fileId, err.Error())
  90. w.WriteHeader(http.StatusNotFound)
  91. return
  92. }
  93. urlString := urlLocation
  94. if fs.redirectOnRead {
  95. http.Redirect(w, r, urlString, http.StatusFound)
  96. return
  97. }
  98. u, _ := url.Parse(urlString)
  99. q := u.Query()
  100. for key, values := range r.URL.Query() {
  101. for _, value := range values {
  102. q.Add(key, value)
  103. }
  104. }
  105. u.RawQuery = q.Encode()
  106. request := &http.Request{
  107. Method: r.Method,
  108. URL: u,
  109. Proto: r.Proto,
  110. ProtoMajor: r.ProtoMajor,
  111. ProtoMinor: r.ProtoMinor,
  112. Header: r.Header,
  113. Body: r.Body,
  114. Host: r.Host,
  115. ContentLength: r.ContentLength,
  116. }
  117. glog.V(3).Infoln("retrieving from", u)
  118. resp, do_err := util.Do(request)
  119. if do_err != nil {
  120. glog.V(0).Infoln("failing to connect to volume server", do_err.Error())
  121. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  122. return
  123. }
  124. defer resp.Body.Close()
  125. for k, v := range resp.Header {
  126. w.Header()[k] = v
  127. }
  128. w.WriteHeader(resp.StatusCode)
  129. io.Copy(w, resp.Body)
  130. }