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.

192 lines
4.9 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
4 years ago
3 years ago
3 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "mime"
  8. "net/http"
  9. "net/url"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/chrislusf/seaweedfs/weed/filer"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/images"
  17. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  18. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  19. "github.com/chrislusf/seaweedfs/weed/stats"
  20. "github.com/chrislusf/seaweedfs/weed/util"
  21. )
  22. func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
  23. path := r.URL.Path
  24. isForDirectory := strings.HasSuffix(path, "/")
  25. if isForDirectory && len(path) > 1 {
  26. path = path[:len(path)-1]
  27. }
  28. entry, err := fs.filer.FindEntry(context.Background(), util.FullPath(path))
  29. if err != nil {
  30. if path == "/" {
  31. fs.listDirectoryHandler(w, r)
  32. return
  33. }
  34. if err == filer_pb.ErrNotFound {
  35. glog.V(1).Infof("Not found %s: %v", path, err)
  36. stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
  37. w.WriteHeader(http.StatusNotFound)
  38. } else {
  39. glog.Errorf("Internal %s: %v", path, err)
  40. stats.FilerRequestCounter.WithLabelValues("read.internalerror").Inc()
  41. w.WriteHeader(http.StatusInternalServerError)
  42. }
  43. return
  44. }
  45. if entry.IsDirectory() {
  46. if fs.option.DisableDirListing {
  47. w.WriteHeader(http.StatusMethodNotAllowed)
  48. return
  49. }
  50. fs.listDirectoryHandler(w, r)
  51. return
  52. }
  53. if isForDirectory {
  54. w.WriteHeader(http.StatusNotFound)
  55. return
  56. }
  57. // set etag
  58. etag := filer.ETagEntry(entry)
  59. if ifm := r.Header.Get("If-Match"); ifm != "" && (ifm != "\""+etag+"\"" && ifm != etag){
  60. w.WriteHeader(http.StatusPreconditionFailed)
  61. return
  62. }
  63. w.Header().Set("Accept-Ranges", "bytes")
  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.Before(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. if !strings.HasPrefix(k, "xattr-") {
  89. // "xattr-" prefix is set in filesys.XATTR_PREFIX
  90. w.Header().Set(k, string(v))
  91. }
  92. }
  93. //Seaweed custom header are not visible to Vue or javascript
  94. seaweedHeaders := []string{}
  95. for header := range w.Header() {
  96. if strings.HasPrefix(header, "Seaweed-") {
  97. seaweedHeaders = append(seaweedHeaders, header)
  98. }
  99. }
  100. seaweedHeaders = append(seaweedHeaders, "Content-Disposition")
  101. w.Header().Set("Access-Control-Expose-Headers", strings.Join(seaweedHeaders, ","))
  102. //set tag count
  103. if r.Method == "GET" {
  104. tagCount := 0
  105. for k := range entry.Extended {
  106. if strings.HasPrefix(k, xhttp.AmzObjectTagging+"-") {
  107. tagCount++
  108. }
  109. }
  110. if tagCount > 0 {
  111. w.Header().Set(xhttp.AmzTagCount, strconv.Itoa(tagCount))
  112. }
  113. }
  114. if inm := r.Header.Get("If-None-Match"); inm == "\""+etag+"\"" {
  115. w.WriteHeader(http.StatusNotModified)
  116. return
  117. }
  118. setEtag(w, etag)
  119. filename := entry.Name()
  120. filename = url.QueryEscape(filename)
  121. adjustHeaderContentDisposition(w, r, filename)
  122. totalSize := int64(entry.Size())
  123. if r.Method == "HEAD" {
  124. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  125. return
  126. }
  127. if rangeReq := r.Header.Get("Range"); rangeReq == "" {
  128. ext := filepath.Ext(filename)
  129. if len(ext) > 0 {
  130. ext = strings.ToLower(ext)
  131. }
  132. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  133. if shouldResize {
  134. data, err := filer.ReadAll(fs.filer.MasterClient, entry.Chunks)
  135. if err != nil {
  136. glog.Errorf("failed to read %s: %v", path, err)
  137. w.WriteHeader(http.StatusNotModified)
  138. return
  139. }
  140. rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
  141. io.Copy(w, rs)
  142. return
  143. }
  144. }
  145. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  146. if offset+size <= int64(len(entry.Content)) {
  147. _, err := writer.Write(entry.Content[offset : offset+size])
  148. if err != nil {
  149. glog.Errorf("failed to write entry content: %v", err)
  150. }
  151. return err
  152. }
  153. chunks := entry.Chunks
  154. if entry.IsInRemoteOnly() {
  155. dir, name := entry.FullPath.DirAndName()
  156. if resp, err := fs.DownloadToLocal(context.Background(), &filer_pb.DownloadToLocalRequest{
  157. Directory: dir,
  158. Name: name,
  159. }); err != nil {
  160. return fmt.Errorf("cache %s: %v", entry.FullPath, err)
  161. } else {
  162. chunks = resp.Entry.Chunks
  163. }
  164. }
  165. err = filer.StreamContent(fs.filer.MasterClient, writer, chunks, offset, size)
  166. if err != nil {
  167. glog.Errorf("failed to stream content %s: %v", r.URL, err)
  168. }
  169. return err
  170. })
  171. }