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.

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