seaweedfs/weed/server/filer_server_handlers_read.go

254 lines
7.4 KiB
Go
Raw Normal View History

2016-06-03 11:05:34 +08:00
package weed_server
import (
2020-04-28 15:05:47 +08:00
"bytes"
2019-03-16 06:55:34 +08:00
"context"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/util/mem"
2016-07-21 12:20:22 +08:00
"io"
2022-03-07 16:24:59 +08:00
"math"
"mime"
2016-06-03 11:05:34 +08:00
"net/http"
"path/filepath"
"strconv"
2016-06-03 11:05:34 +08:00
"strings"
"time"
2016-06-03 11:05:34 +08:00
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/images"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/stats"
"github.com/seaweedfs/seaweedfs/weed/util"
2016-06-03 11:05:34 +08:00
)
// Validates the preconditions. Returns true if GET/HEAD operation should not proceed.
// Preconditions supported are:
//
// If-Modified-Since
// If-Unmodified-Since
// If-Match
// If-None-Match
func checkPreconditions(w http.ResponseWriter, r *http.Request, entry *filer.Entry) bool {
etag := filer.ETagEntry(entry)
/// When more than one conditional request header field is present in a
/// request, the order in which the fields are evaluated becomes
/// important. In practice, the fields defined in this document are
/// consistently implemented in a single, logical order, since "lost
/// update" preconditions have more strict requirements than cache
/// validation, a validated cache is more efficient than a partial
/// response, and entity tags are presumed to be more accurate than date
/// validators. https://tools.ietf.org/html/rfc7232#section-5
if entry.Attr.Mtime.IsZero() {
return false
}
w.Header().Set("Last-Modified", entry.Attr.Mtime.UTC().Format(http.TimeFormat))
ifMatchETagHeader := r.Header.Get("If-Match")
ifUnmodifiedSinceHeader := r.Header.Get("If-Unmodified-Since")
if ifMatchETagHeader != "" {
if util.CanonicalizeETag(etag) != util.CanonicalizeETag(ifMatchETagHeader) {
w.WriteHeader(http.StatusPreconditionFailed)
return true
}
} else if ifUnmodifiedSinceHeader != "" {
if t, parseError := time.Parse(http.TimeFormat, ifUnmodifiedSinceHeader); parseError == nil {
if t.Before(entry.Attr.Mtime) {
w.WriteHeader(http.StatusPreconditionFailed)
return true
}
}
}
ifNoneMatchETagHeader := r.Header.Get("If-None-Match")
ifModifiedSinceHeader := r.Header.Get("If-Modified-Since")
if ifNoneMatchETagHeader != "" {
if util.CanonicalizeETag(etag) == util.CanonicalizeETag(ifNoneMatchETagHeader) {
w.WriteHeader(http.StatusNotModified)
return true
}
} else if ifModifiedSinceHeader != "" {
if t, parseError := time.Parse(http.TimeFormat, ifModifiedSinceHeader); parseError == nil {
if !t.Before(entry.Attr.Mtime) {
w.WriteHeader(http.StatusNotModified)
return true
}
}
}
return false
}
2021-07-19 17:59:12 +08:00
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
2018-05-14 14:56:16 +08:00
path := r.URL.Path
isForDirectory := strings.HasSuffix(path, "/")
if isForDirectory && len(path) > 1 {
2018-05-14 14:56:16 +08:00
path = path[:len(path)-1]
}
2020-03-23 15:01:34 +08:00
entry, err := fs.filer.FindEntry(context.Background(), util.FullPath(path))
if err != nil {
2018-10-12 15:45:28 +08:00
if path == "/" {
fs.listDirectoryHandler(w, r)
return
}
2020-03-08 09:01:39 +08:00
if err == filer_pb.ErrNotFound {
2022-09-05 09:17:01 +08:00
glog.V(2).Infof("Not found %s: %v", path, err)
stats.FilerRequestCounter.WithLabelValues(stats.ErrorReadNotFound).Inc()
w.WriteHeader(http.StatusNotFound)
} else {
2021-06-24 11:59:54 +08:00
glog.Errorf("Internal %s: %v", path, err)
stats.FilerRequestCounter.WithLabelValues(stats.ErrorReadInternal).Inc()
w.WriteHeader(http.StatusInternalServerError)
}
2018-05-14 14:56:16 +08:00
return
}
if entry.IsDirectory() {
2018-07-07 17:18:47 +08:00
if fs.option.DisableDirListing {
2016-07-21 12:20:22 +08:00
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if entry.Attr.Mime == "" {
fs.listDirectoryHandler(w, r)
return
}
// inform S3 API this is a user created directory key object
w.Header().Set(s3_constants.X_SeaweedFS_Header_Directory_Key, "true")
2016-07-21 12:20:22 +08:00
}
if isForDirectory {
w.WriteHeader(http.StatusNotFound)
return
}
query := r.URL.Query()
if query.Get("metadata") == "true" {
if query.Get("resolveManifest") == "true" {
if entry.Chunks, _, err = filer.ResolveChunkManifest(
fs.filer.MasterClient.GetLookupFileIdFunction(),
2022-03-07 16:24:59 +08:00
entry.Chunks, 0, math.MaxInt64); err != nil {
err = fmt.Errorf("failed to resolve chunk manifest, err: %s", err.Error())
writeJsonError(w, r, http.StatusInternalServerError, err)
}
}
2022-02-18 00:42:52 +08:00
writeJsonQuiet(w, r, http.StatusOK, entry)
return
}
2021-05-24 19:59:44 +08:00
etag := filer.ETagEntry(entry)
if checkPreconditions(w, r, entry) {
2021-05-24 19:59:44 +08:00
return
}
w.Header().Set("Accept-Ranges", "bytes")
// mime type
mimeType := entry.Attr.Mime
if mimeType == "" {
if ext := filepath.Ext(entry.Name()); ext != "" {
mimeType = mime.TypeByExtension(ext)
}
}
if mimeType != "" {
w.Header().Set("Content-Type", mimeType)
}
// print out the header from extended properties
for k, v := range entry.Extended {
if !strings.HasPrefix(k, "xattr-") {
// "xattr-" prefix is set in filesys.XATTR_PREFIX
w.Header().Set(k, string(v))
}
}
//Seaweed custom header are not visible to Vue or javascript
seaweedHeaders := []string{}
for header := range w.Header() {
if strings.HasPrefix(header, "Seaweed-") {
seaweedHeaders = append(seaweedHeaders, header)
}
}
seaweedHeaders = append(seaweedHeaders, "Content-Disposition")
w.Header().Set("Access-Control-Expose-Headers", strings.Join(seaweedHeaders, ","))
//set tag count
tagCount := 0
for k := range entry.Extended {
if strings.HasPrefix(k, s3_constants.AmzObjectTagging+"-") {
tagCount++
}
}
if tagCount > 0 {
w.Header().Set(s3_constants.AmzTagCount, strconv.Itoa(tagCount))
}
setEtag(w, etag)
filename := entry.Name()
adjustPassthroughHeaders(w, r, filename)
totalSize := int64(entry.Size())
if r.Method == "HEAD" {
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
return
}
if rangeReq := r.Header.Get("Range"); rangeReq == "" {
ext := filepath.Ext(filename)
if len(ext) > 0 {
ext = strings.ToLower(ext)
}
width, height, mode, shouldResize := shouldResizeImages(ext, r)
if shouldResize {
2022-02-26 18:16:47 +08:00
data := mem.Allocate(int(totalSize))
defer mem.Free(data)
err := filer.ReadAll(data, fs.filer.MasterClient, entry.Chunks)
2020-04-28 15:05:47 +08:00
if err != nil {
glog.Errorf("failed to read %s: %v", path, err)
2022-04-24 11:07:27 +08:00
w.WriteHeader(http.StatusInternalServerError)
2020-04-28 15:05:47 +08:00
return
}
rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
io.Copy(w, rs)
return
}
}
processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
2020-11-30 20:34:04 +08:00
if offset+size <= int64(len(entry.Content)) {
_, err := writer.Write(entry.Content[offset : offset+size])
2021-03-11 09:13:01 +08:00
if err != nil {
stats.FilerRequestCounter.WithLabelValues(stats.ErrorWriteEntry).Inc()
2021-03-11 09:13:01 +08:00
glog.Errorf("failed to write entry content: %v", err)
}
2020-11-30 20:34:04 +08:00
return err
}
chunks := entry.Chunks
2021-08-01 14:52:09 +08:00
if entry.IsInRemoteOnly() {
dir, name := entry.FullPath.DirAndName()
2021-10-31 10:27:25 +08:00
if resp, err := fs.CacheRemoteObjectToLocalCluster(context.Background(), &filer_pb.CacheRemoteObjectToLocalClusterRequest{
Directory: dir,
Name: name,
}); err != nil {
stats.FilerRequestCounter.WithLabelValues(stats.ErrorReadCache).Inc()
2021-10-31 10:27:25 +08:00
glog.Errorf("CacheRemoteObjectToLocalCluster %s: %v", entry.FullPath, err)
2021-08-10 07:03:03 +08:00
return fmt.Errorf("cache %s: %v", entry.FullPath, err)
} else {
chunks = resp.Entry.Chunks
}
}
err = filer.StreamContentWithThrottler(fs.filer.MasterClient, writer, chunks, offset, size, fs.option.DownloadMaxBytesPs)
if err != nil {
stats.FilerRequestCounter.WithLabelValues(stats.ErrorReadStream).Inc()
glog.Errorf("failed to stream content %s: %v", r.URL, err)
}
return err
2020-03-08 10:06:48 +08:00
})
}