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.7 KiB

9 years ago
9 years ago
9 years ago
9 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
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/filer"
  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 tag count
  85. if r.Method == "GET" {
  86. tagCount := 0
  87. for k, _ := range entry.Extended {
  88. if strings.HasPrefix(k, "x-amz-tagging-") {
  89. tagCount++
  90. }
  91. }
  92. if tagCount > 0 {
  93. w.Header().Set("x-amz-tag-count", strconv.Itoa(tagCount))
  94. }
  95. }
  96. // set etag
  97. etag := filer.ETagEntry(entry)
  98. if inm := r.Header.Get("If-None-Match"); inm == "\""+etag+"\"" {
  99. w.WriteHeader(http.StatusNotModified)
  100. return
  101. }
  102. setEtag(w, etag)
  103. filename := entry.Name()
  104. adjustHeaderContentDisposition(w, r, filename)
  105. if r.Method == "HEAD" {
  106. w.Header().Set("Content-Length", strconv.FormatInt(int64(entry.Size()), 10))
  107. return
  108. }
  109. totalSize := int64(entry.Size())
  110. if rangeReq := r.Header.Get("Range"); rangeReq == "" {
  111. ext := filepath.Ext(filename)
  112. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  113. if shouldResize {
  114. data, err := filer.ReadAll(fs.filer.MasterClient, entry.Chunks)
  115. if err != nil {
  116. glog.Errorf("failed to read %s: %v", path, err)
  117. w.WriteHeader(http.StatusNotModified)
  118. return
  119. }
  120. rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
  121. io.Copy(w, rs)
  122. return
  123. }
  124. }
  125. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  126. return filer.StreamContent(fs.filer.MasterClient, writer, entry.Chunks, offset, size)
  127. })
  128. }