filer_server_handlers_read.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package weed_server
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "mime"
  7. "net/http"
  8. "net/url"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/chrislusf/seaweedfs/weed/filer"
  14. "github.com/chrislusf/seaweedfs/weed/glog"
  15. "github.com/chrislusf/seaweedfs/weed/images"
  16. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  17. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  18. "github.com/chrislusf/seaweedfs/weed/stats"
  19. "github.com/chrislusf/seaweedfs/weed/util"
  20. )
  21. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  22. path := r.URL.Path
  23. isForDirectory := strings.HasSuffix(path, "/")
  24. if isForDirectory && len(path) > 1 {
  25. path = path[:len(path)-1]
  26. }
  27. entry, err := fs.filer.FindEntry(context.Background(), util.FullPath(path))
  28. if err != nil {
  29. if path == "/" {
  30. fs.listDirectoryHandler(w, r)
  31. return
  32. }
  33. if err == filer_pb.ErrNotFound {
  34. glog.V(1).Infof("Not found %s: %v", path, err)
  35. stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
  36. w.WriteHeader(http.StatusNotFound)
  37. } else {
  38. glog.V(0).Infof("Internal %s: %v", path, err)
  39. stats.FilerRequestCounter.WithLabelValues("read.internalerror").Inc()
  40. w.WriteHeader(http.StatusInternalServerError)
  41. }
  42. return
  43. }
  44. if entry.IsDirectory() {
  45. if fs.option.DisableDirListing {
  46. w.WriteHeader(http.StatusMethodNotAllowed)
  47. return
  48. }
  49. fs.listDirectoryHandler(w, r)
  50. return
  51. }
  52. if isForDirectory {
  53. w.WriteHeader(http.StatusNotFound)
  54. return
  55. }
  56. w.Header().Set("Accept-Ranges", "bytes")
  57. // mime type
  58. mimeType := entry.Attr.Mime
  59. if mimeType == "" {
  60. if ext := filepath.Ext(entry.Name()); ext != "" {
  61. mimeType = mime.TypeByExtension(ext)
  62. }
  63. }
  64. if mimeType != "" {
  65. w.Header().Set("Content-Type", mimeType)
  66. }
  67. // if modified since
  68. if !entry.Attr.Mtime.IsZero() {
  69. w.Header().Set("Last-Modified", entry.Attr.Mtime.UTC().Format(http.TimeFormat))
  70. if r.Header.Get("If-Modified-Since") != "" {
  71. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  72. if t.After(entry.Attr.Mtime) {
  73. w.WriteHeader(http.StatusNotModified)
  74. return
  75. }
  76. }
  77. }
  78. }
  79. // print out the header from extended properties
  80. for k, v := range entry.Extended {
  81. w.Header().Set(k, string(v))
  82. }
  83. //Seaweed custom header are not visible to Vue or javascript
  84. seaweedHeaders := []string{}
  85. for header, _ := range w.Header() {
  86. if strings.HasPrefix(header, "Seaweed-") {
  87. seaweedHeaders = append(seaweedHeaders, header)
  88. }
  89. }
  90. seaweedHeaders = append(seaweedHeaders, "Content-Disposition")
  91. w.Header().Set("Access-Control-Expose-Headers", strings.Join(seaweedHeaders, ","))
  92. //set tag count
  93. if r.Method == "GET" {
  94. tagCount := 0
  95. for k := range entry.Extended {
  96. if strings.HasPrefix(k, xhttp.AmzObjectTagging+"-") {
  97. tagCount++
  98. }
  99. }
  100. if tagCount > 0 {
  101. w.Header().Set(xhttp.AmzTagCount, strconv.Itoa(tagCount))
  102. }
  103. }
  104. // set etag
  105. etag := filer.ETagEntry(entry)
  106. if inm := r.Header.Get("If-None-Match"); inm == "\""+etag+"\"" {
  107. w.WriteHeader(http.StatusNotModified)
  108. return
  109. }
  110. setEtag(w, etag)
  111. filename := entry.Name()
  112. filename = url.QueryEscape(filename)
  113. adjustHeaderContentDisposition(w, r, filename)
  114. totalSize := int64(entry.Size())
  115. if r.Method == "HEAD" {
  116. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  117. return
  118. }
  119. if rangeReq := r.Header.Get("Range"); rangeReq == "" {
  120. ext := filepath.Ext(filename)
  121. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  122. if shouldResize {
  123. data, err := filer.ReadAll(fs.filer.MasterClient, entry.Chunks)
  124. if err != nil {
  125. glog.Errorf("failed to read %s: %v", path, err)
  126. w.WriteHeader(http.StatusNotModified)
  127. return
  128. }
  129. rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
  130. io.Copy(w, rs)
  131. return
  132. }
  133. }
  134. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64, httpStatusCode int) error {
  135. if httpStatusCode != 0 {
  136. w.WriteHeader(httpStatusCode)
  137. }
  138. if offset+size <= int64(len(entry.Content)) {
  139. _, err := writer.Write(entry.Content[offset : offset+size])
  140. if err != nil {
  141. glog.Errorf("failed to write entry content: %v", err)
  142. }
  143. return err
  144. }
  145. return filer.StreamContent(fs.filer.MasterClient, writer, entry.Chunks, offset, size)
  146. })
  147. }