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.

172 lines
4.5 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. "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. if len(entry.Chunks) == 0 && len(entry.Content) == 0 {
  57. glog.V(1).Infof("no file chunks for %s, attr=%+v", path, entry.Attr)
  58. stats.FilerRequestCounter.WithLabelValues("read.nocontent").Inc()
  59. w.WriteHeader(http.StatusNoContent)
  60. return
  61. }
  62. w.Header().Set("Accept-Ranges", "bytes")
  63. w.Header().Set("Last-Modified", entry.Attr.Mtime.Format(http.TimeFormat))
  64. // mime type
  65. mimeType := entry.Attr.Mime
  66. if mimeType == "" {
  67. if ext := filepath.Ext(entry.Name()); ext != "" {
  68. mimeType = mime.TypeByExtension(ext)
  69. }
  70. }
  71. if mimeType != "" {
  72. w.Header().Set("Content-Type", mimeType)
  73. }
  74. // if modified since
  75. if !entry.Attr.Mtime.IsZero() {
  76. w.Header().Set("Last-Modified", entry.Attr.Mtime.UTC().Format(http.TimeFormat))
  77. if r.Header.Get("If-Modified-Since") != "" {
  78. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  79. if t.After(entry.Attr.Mtime) {
  80. w.WriteHeader(http.StatusNotModified)
  81. return
  82. }
  83. }
  84. }
  85. }
  86. // print out the header from extended properties
  87. for k, v := range entry.Extended {
  88. w.Header().Set(k, string(v))
  89. }
  90. //Seaweed custom header are not visible to Vue or javascript
  91. seaweedHeaders := []string{}
  92. for header, _ := range w.Header() {
  93. if strings.HasPrefix(header, "Seaweed-") {
  94. seaweedHeaders = append(seaweedHeaders, header)
  95. }
  96. }
  97. seaweedHeaders = append(seaweedHeaders, "Content-Disposition")
  98. w.Header().Set("Access-Control-Expose-Headers", strings.Join(seaweedHeaders, ","))
  99. //set tag count
  100. if r.Method == "GET" {
  101. tagCount := 0
  102. for k := range entry.Extended {
  103. if strings.HasPrefix(k, xhttp.AmzObjectTagging+"-") {
  104. tagCount++
  105. }
  106. }
  107. if tagCount > 0 {
  108. w.Header().Set(xhttp.AmzTagCount, strconv.Itoa(tagCount))
  109. }
  110. }
  111. // set etag
  112. etag := filer.ETagEntry(entry)
  113. if inm := r.Header.Get("If-None-Match"); inm == "\""+etag+"\"" {
  114. w.WriteHeader(http.StatusNotModified)
  115. return
  116. }
  117. setEtag(w, etag)
  118. filename := entry.Name()
  119. filename = url.QueryEscape(filename)
  120. adjustHeaderContentDisposition(w, r, filename)
  121. totalSize := int64(entry.Size())
  122. if r.Method == "HEAD" {
  123. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  124. return
  125. }
  126. if rangeReq := r.Header.Get("Range"); rangeReq == "" {
  127. ext := filepath.Ext(filename)
  128. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  129. if shouldResize {
  130. data, err := filer.ReadAll(fs.filer.MasterClient, entry.Chunks)
  131. if err != nil {
  132. glog.Errorf("failed to read %s: %v", path, err)
  133. w.WriteHeader(http.StatusNotModified)
  134. return
  135. }
  136. rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
  137. io.Copy(w, rs)
  138. return
  139. }
  140. }
  141. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64, httpStatusCode int) error {
  142. if httpStatusCode != 0 {
  143. w.WriteHeader(httpStatusCode)
  144. }
  145. if offset+size <= int64(len(entry.Content)) {
  146. _, err := writer.Write(entry.Content[offset : offset+size])
  147. return err
  148. }
  149. return filer.StreamContent(fs.filer.MasterClient, writer, entry.Chunks, offset, size)
  150. })
  151. }