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"
|
2024-03-19 02:08:16 +08:00
|
|
|
"errors"
|
2021-08-10 06:16:45 +08:00
|
|
|
"fmt"
|
2016-07-21 12:20:22 +08:00
|
|
|
"io"
|
2022-03-07 16:24:59 +08:00
|
|
|
"math"
|
2019-01-03 06:23:25 +08:00
|
|
|
"mime"
|
2016-06-03 11:05:34 +08:00
|
|
|
"net/http"
|
2020-03-09 06:42:44 +08:00
|
|
|
"path/filepath"
|
2019-01-03 06:23:25 +08:00
|
|
|
"strconv"
|
2016-06-03 11:05:34 +08:00
|
|
|
"strings"
|
2020-04-08 23:12:00 +08:00
|
|
|
"time"
|
2016-06-03 11:05:34 +08:00
|
|
|
|
2023-05-24 22:45:06 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/mem"
|
|
|
|
|
2022-07-29 15:17:28 +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
|
|
|
)
|
|
|
|
|
2022-02-08 10:13:19 +08:00
|
|
|
// Validates the preconditions. Returns true if GET/HEAD operation should not proceed.
|
|
|
|
// Preconditions supported are:
|
2022-08-24 15:15:44 +08:00
|
|
|
//
|
|
|
|
// If-Modified-Since
|
|
|
|
// If-Unmodified-Since
|
|
|
|
// If-Match
|
|
|
|
// If-None-Match
|
2022-02-08 10:13:19 +08:00
|
|
|
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) {
|
2024-01-25 03:05:55 +08:00
|
|
|
setEtag(w, etag)
|
2022-02-08 10:13:19 +08:00
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else if ifModifiedSinceHeader != "" {
|
|
|
|
if t, parseError := time.Parse(http.TimeFormat, ifModifiedSinceHeader); parseError == nil {
|
2022-08-20 13:21:51 +08:00
|
|
|
if !t.Before(entry.Attr.Mtime) {
|
2024-01-25 03:05:55 +08:00
|
|
|
setEtag(w, etag)
|
2022-02-08 10:13:19 +08:00
|
|
|
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) {
|
2019-06-13 17:01:51 +08:00
|
|
|
|
2018-05-14 14:56:16 +08:00
|
|
|
path := r.URL.Path
|
2019-07-11 03:11:18 +08:00
|
|
|
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))
|
2018-05-26 18:49:46 +08:00
|
|
|
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)
|
2024-01-11 02:05:27 +08:00
|
|
|
stats.FilerHandlerCounter.WithLabelValues(stats.ErrorReadNotFound).Inc()
|
2019-10-24 16:51:26 +08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
} else {
|
2021-06-24 11:59:54 +08:00
|
|
|
glog.Errorf("Internal %s: %v", path, err)
|
2024-01-11 02:05:27 +08:00
|
|
|
stats.FilerHandlerCounter.WithLabelValues(stats.ErrorReadInternal).Inc()
|
2019-10-24 16:51:26 +08:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2018-05-14 14:56:16 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-12 22:14:49 +08:00
|
|
|
query := r.URL.Query()
|
|
|
|
|
2018-05-14 14:56:16 +08:00
|
|
|
if entry.IsDirectory() {
|
2018-07-07 17:18:47 +08:00
|
|
|
if fs.option.DisableDirListing {
|
2022-10-12 22:14:49 +08:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2024-03-19 02:08:16 +08:00
|
|
|
if query.Get("metadata") == "true" {
|
|
|
|
// Don't return directory meta if config value is set to true
|
|
|
|
if fs.option.ExposeDirectoryData == false {
|
|
|
|
writeJsonError(w, r, http.StatusForbidden, errors.New("directory listing is disabled"))
|
|
|
|
return
|
|
|
|
}
|
2016-07-21 12:20:22 +08:00
|
|
|
}
|
2023-04-11 22:36:22 +08:00
|
|
|
if entry.Attr.Mime == "" || (entry.Attr.Mime == s3_constants.FolderMimeType && r.Header.Get(s3_constants.AmzIdentityId) == "") {
|
|
|
|
// return index of directory for non s3 gateway
|
2022-08-24 15:15:44 +08:00
|
|
|
fs.listDirectoryHandler(w, r)
|
|
|
|
return
|
2022-08-23 16:16:46 +08:00
|
|
|
}
|
2022-08-24 15:15:44 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2023-04-11 22:36:22 +08:00
|
|
|
if isForDirectory && entry.Attr.Mime != s3_constants.FolderMimeType {
|
2019-07-11 03:11:18 +08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-19 02:08:16 +08:00
|
|
|
if query.Get("metadata") == "true" {
|
2022-03-06 18:12:12 +08:00
|
|
|
if query.Get("resolveManifest") == "true" {
|
|
|
|
if entry.Chunks, _, err = filer.ResolveChunkManifest(
|
|
|
|
fs.filer.MasterClient.GetLookupFileIdFunction(),
|
2022-11-15 22:33:36 +08:00
|
|
|
entry.GetChunks(), 0, math.MaxInt64); err != nil {
|
2022-03-06 18:12:12 +08:00
|
|
|
err = fmt.Errorf("failed to resolve chunk manifest, err: %s", err.Error())
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2023-05-24 22:45:06 +08:00
|
|
|
return
|
2022-03-06 18:12:12 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-18 00:42:52 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, entry)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-08 10:13:19 +08:00
|
|
|
if checkPreconditions(w, r, entry) {
|
2021-05-24 19:59:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-25 03:05:55 +08:00
|
|
|
etag := filer.ETagEntry(entry)
|
2018-05-28 20:39:12 +08:00
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
|
|
|
|
2020-03-09 06:42:44 +08:00
|
|
|
// mime type
|
2019-03-28 02:41:11 +08:00
|
|
|
mimeType := entry.Attr.Mime
|
2018-05-31 11:24:57 +08:00
|
|
|
if mimeType == "" {
|
2020-03-09 06:42:44 +08:00
|
|
|
if ext := filepath.Ext(entry.Name()); ext != "" {
|
2018-05-31 11:24:57 +08:00
|
|
|
mimeType = mime.TypeByExtension(ext)
|
|
|
|
}
|
2018-05-28 20:39:12 +08:00
|
|
|
}
|
|
|
|
if mimeType != "" {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
2023-02-01 18:38:05 +08:00
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
2018-05-28 20:39:12 +08:00
|
|
|
}
|
2020-03-09 06:42:44 +08:00
|
|
|
|
2020-11-03 16:15:51 +08:00
|
|
|
// print out the header from extended properties
|
|
|
|
for k, v := range entry.Extended {
|
2021-09-13 19:00:57 +08:00
|
|
|
if !strings.HasPrefix(k, "xattr-") {
|
|
|
|
// "xattr-" prefix is set in filesys.XATTR_PREFIX
|
|
|
|
w.Header().Set(k, string(v))
|
|
|
|
}
|
2020-11-03 16:15:51 +08:00
|
|
|
}
|
|
|
|
|
2021-01-05 13:01:29 +08:00
|
|
|
//Seaweed custom header are not visible to Vue or javascript
|
2020-12-31 00:03:22 +08:00
|
|
|
seaweedHeaders := []string{}
|
2021-07-29 13:43:12 +08:00
|
|
|
for header := range w.Header() {
|
2021-01-05 12:58:46 +08:00
|
|
|
if strings.HasPrefix(header, "Seaweed-") {
|
|
|
|
seaweedHeaders = append(seaweedHeaders, header)
|
2020-12-31 00:03:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
seaweedHeaders = append(seaweedHeaders, "Content-Disposition")
|
|
|
|
w.Header().Set("Access-Control-Expose-Headers", strings.Join(seaweedHeaders, ","))
|
|
|
|
|
2020-10-27 16:49:31 +08:00
|
|
|
//set tag count
|
2021-10-20 19:01:06 +08:00
|
|
|
tagCount := 0
|
|
|
|
for k := range entry.Extended {
|
2022-05-31 13:57:41 +08:00
|
|
|
if strings.HasPrefix(k, s3_constants.AmzObjectTagging+"-") {
|
2021-10-20 19:01:06 +08:00
|
|
|
tagCount++
|
2020-10-27 16:49:31 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-20 19:01:06 +08:00
|
|
|
if tagCount > 0 {
|
2022-05-31 13:57:41 +08:00
|
|
|
w.Header().Set(s3_constants.AmzTagCount, strconv.Itoa(tagCount))
|
2021-10-20 19:01:06 +08:00
|
|
|
}
|
2020-10-27 16:49:31 +08:00
|
|
|
|
2020-04-08 23:12:00 +08:00
|
|
|
setEtag(w, etag)
|
2018-05-28 20:39:12 +08:00
|
|
|
|
2020-07-26 11:06:40 +08:00
|
|
|
filename := entry.Name()
|
2021-12-16 05:18:53 +08:00
|
|
|
adjustPassthroughHeaders(w, r, filename)
|
2020-07-26 11:06:40 +08:00
|
|
|
|
2020-12-10 16:15:22 +08:00
|
|
|
totalSize := int64(entry.Size())
|
|
|
|
|
2020-03-09 06:42:44 +08:00
|
|
|
if r.Method == "HEAD" {
|
2020-12-10 16:15:22 +08:00
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
2020-03-09 06:42:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-21 11:31:11 +08:00
|
|
|
if rangeReq := r.Header.Get("Range"); rangeReq == "" {
|
|
|
|
ext := filepath.Ext(filename)
|
2021-08-19 05:15:35 +08:00
|
|
|
if len(ext) > 0 {
|
|
|
|
ext = strings.ToLower(ext)
|
|
|
|
}
|
2020-03-21 11:31:11 +08:00
|
|
|
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)
|
2022-11-15 22:33:36 +08:00
|
|
|
err := filer.ReadAll(data, fs.filer.MasterClient, entry.GetChunks())
|
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)
|
2020-03-21 11:31:11 +08:00
|
|
|
io.Copy(w, rs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 02:35:52 +08:00
|
|
|
processRangeRequest(r, w, totalSize, mimeType, func(offset int64, size int64) (filer.DoStreamContent, error) {
|
2020-11-30 20:34:04 +08:00
|
|
|
if offset+size <= int64(len(entry.Content)) {
|
2024-01-30 02:35:52 +08:00
|
|
|
return func(writer io.Writer) error {
|
|
|
|
_, err := writer.Write(entry.Content[offset : offset+size])
|
|
|
|
if err != nil {
|
|
|
|
stats.FilerHandlerCounter.WithLabelValues(stats.ErrorWriteEntry).Inc()
|
|
|
|
glog.Errorf("failed to write entry content: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}, nil
|
2020-11-30 20:34:04 +08:00
|
|
|
}
|
2022-11-15 22:33:36 +08:00
|
|
|
chunks := entry.GetChunks()
|
2021-08-01 14:52:09 +08:00
|
|
|
if entry.IsInRemoteOnly() {
|
2021-08-10 06:16:45 +08:00
|
|
|
dir, name := entry.FullPath.DirAndName()
|
2021-10-31 10:27:25 +08:00
|
|
|
if resp, err := fs.CacheRemoteObjectToLocalCluster(context.Background(), &filer_pb.CacheRemoteObjectToLocalClusterRequest{
|
2021-08-10 06:16:45 +08:00
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
}); err != nil {
|
2024-01-11 02:05:27 +08:00
|
|
|
stats.FilerHandlerCounter.WithLabelValues(stats.ErrorReadCache).Inc()
|
2021-10-31 10:27:25 +08:00
|
|
|
glog.Errorf("CacheRemoteObjectToLocalCluster %s: %v", entry.FullPath, err)
|
2024-01-30 02:35:52 +08:00
|
|
|
return nil, fmt.Errorf("cache %s: %v", entry.FullPath, err)
|
2021-08-10 06:16:45 +08:00
|
|
|
} else {
|
2022-11-15 22:33:36 +08:00
|
|
|
chunks = resp.Entry.GetChunks()
|
2021-07-29 13:43:12 +08:00
|
|
|
}
|
2021-05-21 18:59:12 +08:00
|
|
|
}
|
2021-08-10 06:16:45 +08:00
|
|
|
|
2024-01-30 02:35:52 +08:00
|
|
|
streamFn, err := filer.PrepareStreamContentWithThrottler(fs.filer.MasterClient, chunks, offset, size, fs.option.DownloadMaxBytesPs)
|
2021-08-10 06:16:45 +08:00
|
|
|
if err != nil {
|
2024-01-11 02:05:27 +08:00
|
|
|
stats.FilerHandlerCounter.WithLabelValues(stats.ErrorReadStream).Inc()
|
2024-01-30 02:35:52 +08:00
|
|
|
glog.Errorf("failed to prepare stream content %s: %v", r.URL, err)
|
|
|
|
return nil, err
|
2021-08-10 06:16:45 +08:00
|
|
|
}
|
2024-01-30 02:35:52 +08:00
|
|
|
return func(writer io.Writer) error {
|
|
|
|
err := streamFn(writer)
|
|
|
|
if err != nil {
|
|
|
|
stats.FilerHandlerCounter.WithLabelValues(stats.ErrorReadStream).Inc()
|
|
|
|
glog.Errorf("failed to stream content %s: %v", r.URL, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}, nil
|
2020-03-08 10:06:48 +08:00
|
|
|
})
|
|
|
|
}
|