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.

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