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.

143 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
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. args := struct {
  39. Path string
  40. Entries interface{}
  41. Limit int
  42. LastFileName string
  43. ShouldDisplayLoadMore bool
  44. }{
  45. path,
  46. entries,
  47. limit,
  48. lastFileName,
  49. shouldDisplayLoadMore,
  50. }
  51. if r.Header.Get("Accept") == "application/json" {
  52. writeJsonQuiet(w, r, http.StatusOK, args)
  53. } else {
  54. ui.StatusTpl.Execute(w, args)
  55. }
  56. }
  57. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  58. path := r.URL.Path
  59. if strings.HasSuffix(path, "/") && len(path) > 1 {
  60. path = path[:len(path)-1]
  61. }
  62. found, entry, err := fs.filer.FindEntry(filer2.FullPath(path))
  63. if !found || err != nil {
  64. glog.V(3).Infof("Not found %s: %v", path, err)
  65. w.WriteHeader(http.StatusNotFound)
  66. return
  67. }
  68. if entry.IsDirectory() {
  69. if fs.disableDirListing {
  70. w.WriteHeader(http.StatusMethodNotAllowed)
  71. return
  72. }
  73. fs.listDirectoryHandler(w, r)
  74. return
  75. }
  76. if len(entry.Chunks) == 0 {
  77. glog.V(3).Infof("Empty %s: %v", path)
  78. w.WriteHeader(http.StatusNoContent)
  79. return
  80. }
  81. // FIXME pick the right fid
  82. fileId := string(entry.Chunks[0].Fid)
  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. }