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.

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