2016-06-03 03:05:34 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2020-04-28 07:05:47 +00:00
|
|
|
"bytes"
|
2019-03-15 22:55:34 +00:00
|
|
|
"context"
|
2021-08-09 22:16:45 +00:00
|
|
|
"fmt"
|
2022-02-26 10:16:47 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util/mem"
|
2016-07-21 04:20:22 +00:00
|
|
|
"io"
|
2022-03-07 08:24:59 +00:00
|
|
|
"math"
|
2019-01-02 22:23:25 +00:00
|
|
|
"mime"
|
2016-06-03 03:05:34 +00:00
|
|
|
"net/http"
|
2020-03-08 22:42:44 +00:00
|
|
|
"path/filepath"
|
2019-01-02 22:23:25 +00:00
|
|
|
"strconv"
|
2016-06-03 03:05:34 +00:00
|
|
|
"strings"
|
2020-04-08 15:12:00 +00:00
|
|
|
"time"
|
2016-06-03 03:05:34 +00:00
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2016-06-03 03:05:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2020-03-21 03:31:11 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/images"
|
2020-03-08 01:01:39 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-10-29 08:05:40 +00:00
|
|
|
xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
|
2019-06-15 19:21:44 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats"
|
2020-03-23 07:01:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2016-06-03 03:05:34 +00:00
|
|
|
)
|
|
|
|
|
2022-02-08 02:13:19 +00: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.After(entry.Attr.Mtime) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:59:12 +00:00
|
|
|
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
|
2019-06-13 09:01:51 +00:00
|
|
|
|
2018-05-14 06:56:16 +00:00
|
|
|
path := r.URL.Path
|
2019-07-10 19:11:18 +00:00
|
|
|
isForDirectory := strings.HasSuffix(path, "/")
|
|
|
|
if isForDirectory && len(path) > 1 {
|
2018-05-14 06:56:16 +00:00
|
|
|
path = path[:len(path)-1]
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
entry, err := fs.filer.FindEntry(context.Background(), util.FullPath(path))
|
2018-05-26 10:49:46 +00:00
|
|
|
if err != nil {
|
2018-10-12 07:45:28 +00:00
|
|
|
if path == "/" {
|
|
|
|
fs.listDirectoryHandler(w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-03-08 01:01:39 +00:00
|
|
|
if err == filer_pb.ErrNotFound {
|
2019-10-24 08:51:26 +00:00
|
|
|
glog.V(1).Infof("Not found %s: %v", path, err)
|
2022-02-05 06:57:51 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ErrorReadNotFound).Inc()
|
2019-10-24 08:51:26 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
} else {
|
2021-06-24 03:59:54 +00:00
|
|
|
glog.Errorf("Internal %s: %v", path, err)
|
2022-02-05 06:57:51 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ErrorReadInternal).Inc()
|
2019-10-24 08:51:26 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2018-05-14 06:56:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.IsDirectory() {
|
2018-07-07 09:18:47 +00:00
|
|
|
if fs.option.DisableDirListing {
|
2016-07-21 04:20:22 +00:00
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fs.listDirectoryHandler(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:11:18 +00:00
|
|
|
if isForDirectory {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-06 10:12:12 +00:00
|
|
|
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 08:24:59 +00:00
|
|
|
entry.Chunks, 0, math.MaxInt64); err != nil {
|
2022-03-06 10:12:12 +00:00
|
|
|
err = fmt.Errorf("failed to resolve chunk manifest, err: %s", err.Error())
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
}
|
2022-02-17 16:42:52 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, entry)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:59:44 +00:00
|
|
|
etag := filer.ETagEntry(entry)
|
2022-02-08 02:13:19 +00:00
|
|
|
if checkPreconditions(w, r, entry) {
|
2021-05-24 11:59:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 12:39:12 +00:00
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
|
|
|
|
2020-03-08 22:42:44 +00:00
|
|
|
// mime type
|
2019-03-27 18:41:11 +00:00
|
|
|
mimeType := entry.Attr.Mime
|
2018-05-31 03:24:57 +00:00
|
|
|
if mimeType == "" {
|
2020-03-08 22:42:44 +00:00
|
|
|
if ext := filepath.Ext(entry.Name()); ext != "" {
|
2018-05-31 03:24:57 +00:00
|
|
|
mimeType = mime.TypeByExtension(ext)
|
|
|
|
}
|
2018-05-28 12:39:12 +00:00
|
|
|
}
|
|
|
|
if mimeType != "" {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
}
|
2020-03-08 22:42:44 +00:00
|
|
|
|
2020-11-03 08:15:51 +00:00
|
|
|
// print out the header from extended properties
|
|
|
|
for k, v := range entry.Extended {
|
2021-09-13 11:00:57 +00:00
|
|
|
if !strings.HasPrefix(k, "xattr-") {
|
|
|
|
// "xattr-" prefix is set in filesys.XATTR_PREFIX
|
|
|
|
w.Header().Set(k, string(v))
|
|
|
|
}
|
2020-11-03 08:15:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 05:01:29 +00:00
|
|
|
//Seaweed custom header are not visible to Vue or javascript
|
2020-12-30 16:03:22 +00:00
|
|
|
seaweedHeaders := []string{}
|
2021-07-29 05:43:12 +00:00
|
|
|
for header := range w.Header() {
|
2021-01-05 04:58:46 +00:00
|
|
|
if strings.HasPrefix(header, "Seaweed-") {
|
|
|
|
seaweedHeaders = append(seaweedHeaders, header)
|
2020-12-30 16:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
seaweedHeaders = append(seaweedHeaders, "Content-Disposition")
|
|
|
|
w.Header().Set("Access-Control-Expose-Headers", strings.Join(seaweedHeaders, ","))
|
|
|
|
|
2020-10-27 08:49:31 +00:00
|
|
|
//set tag count
|
2021-10-20 11:01:06 +00:00
|
|
|
tagCount := 0
|
|
|
|
for k := range entry.Extended {
|
|
|
|
if strings.HasPrefix(k, xhttp.AmzObjectTagging+"-") {
|
|
|
|
tagCount++
|
2020-10-27 08:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-20 11:01:06 +00:00
|
|
|
if tagCount > 0 {
|
|
|
|
w.Header().Set(xhttp.AmzTagCount, strconv.Itoa(tagCount))
|
|
|
|
}
|
2020-10-27 08:49:31 +00:00
|
|
|
|
2020-04-08 15:12:00 +00:00
|
|
|
setEtag(w, etag)
|
2018-05-28 12:39:12 +00:00
|
|
|
|
2020-07-26 03:06:40 +00:00
|
|
|
filename := entry.Name()
|
2021-12-15 21:18:53 +00:00
|
|
|
adjustPassthroughHeaders(w, r, filename)
|
2020-07-26 03:06:40 +00:00
|
|
|
|
2020-12-10 08:15:22 +00:00
|
|
|
totalSize := int64(entry.Size())
|
|
|
|
|
2020-03-08 22:42:44 +00:00
|
|
|
if r.Method == "HEAD" {
|
2020-12-10 08:15:22 +00:00
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
2020-03-08 22:42:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-21 03:31:11 +00:00
|
|
|
if rangeReq := r.Header.Get("Range"); rangeReq == "" {
|
|
|
|
ext := filepath.Ext(filename)
|
2021-08-18 21:15:35 +00:00
|
|
|
if len(ext) > 0 {
|
|
|
|
ext = strings.ToLower(ext)
|
|
|
|
}
|
2020-03-21 03:31:11 +00:00
|
|
|
width, height, mode, shouldResize := shouldResizeImages(ext, r)
|
|
|
|
if shouldResize {
|
2022-02-26 10:16:47 +00:00
|
|
|
data := mem.Allocate(int(totalSize))
|
|
|
|
defer mem.Free(data)
|
|
|
|
err := filer.ReadAll(data, fs.filer.MasterClient, entry.Chunks)
|
2020-04-28 07:05:47 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("failed to read %s: %v", path, err)
|
2022-04-24 03:07:27 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2020-04-28 07:05:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
|
2020-03-21 03:31:11 +00:00
|
|
|
io.Copy(w, rs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 06:45:21 +00:00
|
|
|
processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
|
2020-11-30 12:34:04 +00:00
|
|
|
if offset+size <= int64(len(entry.Content)) {
|
2020-12-10 08:15:22 +00:00
|
|
|
_, err := writer.Write(entry.Content[offset : offset+size])
|
2021-03-11 01:13:01 +00:00
|
|
|
if err != nil {
|
2022-02-05 06:57:51 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ErrorWriteEntry).Inc()
|
2021-03-11 01:13:01 +00:00
|
|
|
glog.Errorf("failed to write entry content: %v", err)
|
|
|
|
}
|
2020-11-30 12:34:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-09 22:16:45 +00:00
|
|
|
chunks := entry.Chunks
|
2021-08-01 06:52:09 +00:00
|
|
|
if entry.IsInRemoteOnly() {
|
2021-08-09 22:16:45 +00:00
|
|
|
dir, name := entry.FullPath.DirAndName()
|
2021-10-31 02:27:25 +00:00
|
|
|
if resp, err := fs.CacheRemoteObjectToLocalCluster(context.Background(), &filer_pb.CacheRemoteObjectToLocalClusterRequest{
|
2021-08-09 22:16:45 +00:00
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
}); err != nil {
|
2022-02-05 06:57:51 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ErrorReadCache).Inc()
|
2021-10-31 02:27:25 +00:00
|
|
|
glog.Errorf("CacheRemoteObjectToLocalCluster %s: %v", entry.FullPath, err)
|
2021-08-09 23:03:03 +00:00
|
|
|
return fmt.Errorf("cache %s: %v", entry.FullPath, err)
|
2021-08-09 22:16:45 +00:00
|
|
|
} else {
|
|
|
|
chunks = resp.Entry.Chunks
|
2021-07-29 05:43:12 +00:00
|
|
|
}
|
2021-05-21 10:59:12 +00:00
|
|
|
}
|
2021-08-09 22:16:45 +00:00
|
|
|
|
|
|
|
err = filer.StreamContent(fs.filer.MasterClient, writer, chunks, offset, size)
|
|
|
|
if err != nil {
|
2022-02-05 06:57:51 +00:00
|
|
|
stats.FilerRequestCounter.WithLabelValues(stats.ErrorReadStream).Inc()
|
2021-08-09 22:16:45 +00:00
|
|
|
glog.Errorf("failed to stream content %s: %v", r.URL, err)
|
|
|
|
}
|
2021-05-21 10:59:12 +00:00
|
|
|
return err
|
2020-03-08 02:06:48 +00:00
|
|
|
})
|
|
|
|
}
|