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.

97 lines
2.3 KiB

9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
6 years ago
9 years ago
6 years ago
5 years ago
5 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "io"
  5. "mime"
  6. "net/http"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  13. "github.com/chrislusf/seaweedfs/weed/stats"
  14. )
  15. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  16. path := r.URL.Path
  17. isForDirectory := strings.HasSuffix(path, "/")
  18. if isForDirectory && len(path) > 1 {
  19. path = path[:len(path)-1]
  20. }
  21. entry, err := fs.filer.FindEntry(context.Background(), filer2.FullPath(path))
  22. if err != nil {
  23. if path == "/" {
  24. fs.listDirectoryHandler(w, r)
  25. return
  26. }
  27. if err == filer_pb.ErrNotFound {
  28. glog.V(1).Infof("Not found %s: %v", path, err)
  29. stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
  30. w.WriteHeader(http.StatusNotFound)
  31. } else {
  32. glog.V(0).Infof("Internal %s: %v", path, err)
  33. stats.FilerRequestCounter.WithLabelValues("read.internalerror").Inc()
  34. w.WriteHeader(http.StatusInternalServerError)
  35. }
  36. return
  37. }
  38. if entry.IsDirectory() {
  39. if fs.option.DisableDirListing {
  40. w.WriteHeader(http.StatusMethodNotAllowed)
  41. return
  42. }
  43. fs.listDirectoryHandler(w, r)
  44. return
  45. }
  46. if isForDirectory {
  47. w.WriteHeader(http.StatusNotFound)
  48. return
  49. }
  50. if len(entry.Chunks) == 0 {
  51. glog.V(1).Infof("no file chunks for %s, attr=%+v", path, entry.Attr)
  52. stats.FilerRequestCounter.WithLabelValues("read.nocontent").Inc()
  53. w.WriteHeader(http.StatusNoContent)
  54. return
  55. }
  56. w.Header().Set("Accept-Ranges", "bytes")
  57. w.Header().Set("Last-Modified", entry.Attr.Mtime.Format(http.TimeFormat))
  58. // mime type
  59. mimeType := entry.Attr.Mime
  60. if mimeType == "" {
  61. if ext := filepath.Ext(entry.Name()); ext != "" {
  62. mimeType = mime.TypeByExtension(ext)
  63. }
  64. }
  65. if mimeType != "" {
  66. w.Header().Set("Content-Type", mimeType)
  67. }
  68. // set etag
  69. setEtag(w, filer2.ETag(entry.Chunks))
  70. if r.Method == "HEAD" {
  71. w.Header().Set("Content-Length", strconv.FormatInt(int64(filer2.TotalSize(entry.Chunks)), 10))
  72. return
  73. }
  74. filename := entry.Name()
  75. adjustHeadersAfterHEAD(w, r, filename)
  76. totalSize := int64(filer2.TotalSize(entry.Chunks))
  77. processRangeRequst(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  78. return filer2.StreamContent(fs.filer.MasterClient, w, entry.Chunks, offset, int(size))
  79. })
  80. }