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.

189 lines
4.8 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+"\"" {
  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. 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. if inm := r.Header.Get("If-None-Match"); inm == "\""+etag+"\"" {
  112. w.WriteHeader(http.StatusNotModified)
  113. return
  114. }
  115. setEtag(w, etag)
  116. filename := entry.Name()
  117. filename = url.QueryEscape(filename)
  118. adjustHeaderContentDisposition(w, r, filename)
  119. totalSize := int64(entry.Size())
  120. if r.Method == "HEAD" {
  121. w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
  122. return
  123. }
  124. if rangeReq := r.Header.Get("Range"); rangeReq == "" {
  125. ext := filepath.Ext(filename)
  126. if len(ext) > 0 {
  127. ext = strings.ToLower(ext)
  128. }
  129. width, height, mode, shouldResize := shouldResizeImages(ext, r)
  130. if shouldResize {
  131. data, err := filer.ReadAll(fs.filer.MasterClient, entry.Chunks)
  132. if err != nil {
  133. glog.Errorf("failed to read %s: %v", path, err)
  134. w.WriteHeader(http.StatusNotModified)
  135. return
  136. }
  137. rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
  138. io.Copy(w, rs)
  139. return
  140. }
  141. }
  142. processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
  143. if offset+size <= int64(len(entry.Content)) {
  144. _, err := writer.Write(entry.Content[offset : offset+size])
  145. if err != nil {
  146. glog.Errorf("failed to write entry content: %v", err)
  147. }
  148. return err
  149. }
  150. chunks := entry.Chunks
  151. if entry.IsInRemoteOnly() {
  152. dir, name := entry.FullPath.DirAndName()
  153. if resp, err := fs.DownloadToLocal(context.Background(), &filer_pb.DownloadToLocalRequest{
  154. Directory: dir,
  155. Name: name,
  156. }); err != nil {
  157. return fmt.Errorf("cache %s: %v", entry.FullPath, err)
  158. } else {
  159. chunks = resp.Entry.Chunks
  160. }
  161. }
  162. err = filer.StreamContent(fs.filer.MasterClient, writer, chunks, offset, size)
  163. if err != nil {
  164. glog.Errorf("failed to stream content %s: %v", r.URL, err)
  165. }
  166. return err
  167. })
  168. }