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"
|
2021-08-10 06:16:45 +08:00
|
|
|
"fmt"
|
2016-07-21 12:20:22 +08:00
|
|
|
"io"
|
2019-01-03 06:23:25 +08:00
|
|
|
"mime"
|
2016-06-03 11:05:34 +08:00
|
|
|
"net/http"
|
2020-12-28 11:28:33 +08:00
|
|
|
"net/url"
|
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
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2016-06-03 11:05:34 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2020-03-21 11:31:11 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/images"
|
2020-03-08 09:01:39 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-10-29 16:05:40 +08:00
|
|
|
xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
|
2019-06-16 03:21:44 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats"
|
2020-03-23 15:01:34 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2016-06-03 11:05:34 +08:00
|
|
|
)
|
|
|
|
|
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 {
|
2019-10-24 16:51:26 +08:00
|
|
|
glog.V(1).Infof("Not found %s: %v", path, err)
|
|
|
|
stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
} else {
|
2021-06-24 11:59:54 +08:00
|
|
|
glog.Errorf("Internal %s: %v", path, err)
|
2019-10-24 16:51:26 +08:00
|
|
|
stats.FilerRequestCounter.WithLabelValues("read.internalerror").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
|
|
|
|
}
|
|
|
|
fs.listDirectoryHandler(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-11 03:11:18 +08:00
|
|
|
if isForDirectory {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-24 19:59:44 +08:00
|
|
|
// set etag
|
|
|
|
etag := filer.ETagEntry(entry)
|
|
|
|
if ifm := r.Header.Get("If-Match"); ifm != "" && ifm != "\""+etag+"\"" {
|
|
|
|
w.WriteHeader(http.StatusPreconditionFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2020-03-09 06:42:44 +08:00
|
|
|
|
2020-04-08 23:12:00 +08:00
|
|
|
// if modified since
|
|
|
|
if !entry.Attr.Mtime.IsZero() {
|
|
|
|
w.Header().Set("Last-Modified", entry.Attr.Mtime.UTC().Format(http.TimeFormat))
|
|
|
|
if r.Header.Get("If-Modified-Since") != "" {
|
|
|
|
if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
|
2021-04-13 03:04:53 +08:00
|
|
|
if !t.Before(entry.Attr.Mtime) {
|
2020-04-08 23:12:00 +08:00
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 16:15:51 +08:00
|
|
|
// print out the header from extended properties
|
|
|
|
for k, v := range entry.Extended {
|
|
|
|
w.Header().Set(k, string(v))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if r.Method == "GET" {
|
|
|
|
tagCount := 0
|
2020-11-03 16:15:51 +08:00
|
|
|
for k := range entry.Extended {
|
2020-10-29 16:05:40 +08:00
|
|
|
if strings.HasPrefix(k, xhttp.AmzObjectTagging+"-") {
|
2020-10-27 16:49:31 +08:00
|
|
|
tagCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tagCount > 0 {
|
2020-10-29 16:05:40 +08:00
|
|
|
w.Header().Set(xhttp.AmzTagCount, strconv.Itoa(tagCount))
|
2020-10-27 16:49:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 23:12:00 +08:00
|
|
|
if inm := r.Header.Get("If-None-Match"); inm == "\""+etag+"\"" {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setEtag(w, etag)
|
2018-05-28 20:39:12 +08:00
|
|
|
|
2020-07-26 11:06:40 +08:00
|
|
|
filename := entry.Name()
|
2020-12-28 11:28:33 +08:00
|
|
|
filename = url.QueryEscape(filename)
|
2020-07-26 11:09:19 +08:00
|
|
|
adjustHeaderContentDisposition(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 {
|
2020-09-01 15:21:19 +08:00
|
|
|
data, err := filer.ReadAll(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)
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rs, _, _ := images.Resized(ext, bytes.NewReader(data), width, height, mode)
|
2020-03-21 11:31:11 +08:00
|
|
|
io.Copy(w, rs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:45:21 +08:00
|
|
|
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)) {
|
2020-12-10 16:15:22 +08:00
|
|
|
_, err := writer.Write(entry.Content[offset : offset+size])
|
2021-03-11 09:13:01 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("failed to write entry content: %v", err)
|
|
|
|
}
|
2020-11-30 20:34:04 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-08-10 06:16:45 +08:00
|
|
|
chunks := entry.Chunks
|
2021-08-01 14:52:09 +08:00
|
|
|
if entry.IsInRemoteOnly() {
|
2021-08-10 06:16:45 +08:00
|
|
|
dir, name := entry.FullPath.DirAndName()
|
|
|
|
if resp, err := fs.DownloadToLocal(context.Background(), &filer_pb.DownloadToLocalRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
}); err != nil {
|
2021-08-10 07:03:03 +08:00
|
|
|
return fmt.Errorf("cache %s: %v", entry.FullPath, err)
|
2021-08-10 06:16:45 +08:00
|
|
|
} else {
|
|
|
|
chunks = resp.Entry.Chunks
|
2021-07-29 13:43:12 +08:00
|
|
|
}
|
2021-05-21 18:59:12 +08:00
|
|
|
}
|
2021-08-10 06:16:45 +08:00
|
|
|
|
|
|
|
err = filer.StreamContent(fs.filer.MasterClient, writer, chunks, offset, size)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("failed to stream content %s: %v", r.URL, err)
|
|
|
|
}
|
2021-05-21 18:59:12 +08:00
|
|
|
return err
|
2020-03-08 10:06:48 +08:00
|
|
|
})
|
|
|
|
}
|