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.

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