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.

134 lines
3.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
6 years ago
5 years ago
9 years ago
5 years ago
6 years ago
4 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "mime"
  7. "net/http"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/images"
  15. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  16. "github.com/chrislusf/seaweedfs/weed/stats"
  17. "github.com/chrislusf/seaweedfs/weed/util"
  18. )
  19. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  20. path := r.URL.Path
  21. isForDirectory := strings.HasSuffix(path, "/")
  22. if isForDirectory && len(path) > 1 {
  23. path = path[:len(path)-1]
  24. }
  25. entry, err := fs.filer.FindEntry(context.Background(), util.FullPath(path))
  26. if err != nil {
  27. if path == "/" {
  28. fs.listDirectoryHandler(w, r)
  29. return
  30. }
  31. if err == filer_pb.ErrNotFound {
  32. glog.V(1).Infof("Not found %s: %v", path, err)
  33. stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
  34. w.WriteHeader(http.StatusNotFound)
  35. } else {
  36. glog.V(0).Infof("Internal %s: %v", path, err)
  37. stats.FilerRequestCounter.WithLabelValues("read.internalerror").Inc()
  38. w.WriteHeader(http.StatusInternalServerError)
  39. }
  40. return
  41. }
  42. if entry.IsDirectory() {
  43. if fs.option.DisableDirListing {
  44. w.WriteHeader(http.StatusMethodNotAllowed)
  45. return
  46. }
  47. fs.listDirectoryHandler(w, r)
  48. return
  49. }
  50. if isForDirectory {
  51. w.WriteHeader(http.StatusNotFound)
  52. return
  53. }
  54. if len(entry.Chunks) == 0 {
  55. glog.V(1).Infof("no file chunks for %s, attr=%+v", path, entry.Attr)
  56. stats.FilerRequestCounter.WithLabelValues("read.nocontent").Inc()
  57. w.WriteHeader(http.StatusNoContent)
  58. return
  59. }
  60. w.Header().Set("Accept-Ranges", "bytes")
  61. w.Header().Set("Last-Modified", entry.Attr.Mtime.Format(http.TimeFormat))
  62. // mime type
  63. mimeType := entry.Attr.Mime
  64. if mimeType == "" {
  65. if ext := filepath.Ext(entry.Name()); ext != "" {
  66. mimeType = mime.TypeByExtension(ext)
  67. }
  68. }
  69. if mimeType != "" {
  70. w.Header().Set("Content-Type", mimeType)
  71. }
  72. // if modified since
  73. if !entry.Attr.Mtime.IsZero() {
  74. w.Header().Set("Last-Modified", entry.Attr.Mtime.UTC().Format(http.TimeFormat))
  75. if r.Header.Get("If-Modified-Since") != "" {
  76. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  77. if t.After(entry.Attr.Mtime) {
  78. w.WriteHeader(http.StatusNotModified)
  79. return
  80. }
  81. }
  82. }
  83. }
  84. // set etag
  85. etag := filer2.ETagEntry(entry)
  86. if inm := r.Header.Get("If-None-Match"); inm == "\""+etag+"\"" {
  87. w.WriteHeader(http.StatusNotModified)
  88. return
  89. }
  90. setEtag(w, etag)
  91. filename := entry.Name()
  92. adjustHeaderContentDisposition(w, r, filename)
  93. if r.Method == "HEAD" {
  94. w.Header().Set("Content-Length", strconv.FormatInt(int64(filer2.TotalSize(entry.Chunks)), 10))
  95. return
  96. }
  97. totalSize := int64(filer2.TotalSize(entry.Chunks))
  98. if rangeReq := r.Header.Get("Range"); rangeReq == "" {
  99. ext := filepath.Ext(filename)
  100. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  101. if shouldResize {
  102. data, err := filer2.ReadAll(fs.filer.MasterClient, entry.Chunks)
  103. if err != nil {
  104. glog.Errorf("failed to read %s: %v", path, err)
  105. w.WriteHeader(http.StatusNotModified)
  106. return
  107. }
  108. rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
  109. io.Copy(w, rs)
  110. return
  111. }
  112. }
  113. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  114. return filer2.StreamContent(fs.filer.MasterClient, writer, entry.Chunks, offset, size)
  115. })
  116. }