seaweedfs/weed/server/filer_server_handlers_read.go

98 lines
2.3 KiB
Go
Raw Normal View History

2016-06-03 11:05:34 +08:00
package weed_server
import (
2019-03-16 06:55:34 +08:00
"context"
2016-07-21 12:20:22 +08:00
"io"
"mime"
2016-06-03 11:05:34 +08:00
"net/http"
"path/filepath"
"strconv"
2016-06-03 11:05:34 +08:00
"strings"
2018-05-28 02:52:26 +08:00
"github.com/chrislusf/seaweedfs/weed/filer2"
2016-06-03 11:05:34 +08:00
"github.com/chrislusf/seaweedfs/weed/glog"
2020-03-08 09:01:39 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2019-06-16 03:21:44 +08:00
"github.com/chrislusf/seaweedfs/weed/stats"
2016-06-03 11:05:34 +08:00
)
2016-07-21 12:20:22 +08:00
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
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]
}
2019-03-16 06:55:34 +08:00
entry, err := fs.filer.FindEntry(context.Background(), filer2.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 {
glog.V(1).Infof("Not found %s: %v", path, err)
stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc()
w.WriteHeader(http.StatusNotFound)
} else {
glog.V(0).Infof("Internal %s: %v", path, err)
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
}
if isForDirectory {
w.WriteHeader(http.StatusNotFound)
return
}
2018-05-14 14:56:16 +08:00
if len(entry.Chunks) == 0 {
glog.V(1).Infof("no file chunks for %s, attr=%+v", path, entry.Attr)
2019-06-16 03:21:44 +08:00
stats.FilerRequestCounter.WithLabelValues("read.nocontent").Inc()
2018-05-14 14:56:16 +08:00
w.WriteHeader(http.StatusNoContent)
2016-07-21 12:20:22 +08:00
return
}
w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Last-Modified", entry.Attr.Mtime.Format(http.TimeFormat))
// 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)
}
// set etag
2018-09-10 07:25:43 +08:00
setEtag(w, filer2.ETag(entry.Chunks))
if r.Method == "HEAD" {
w.Header().Set("Content-Length", strconv.FormatInt(int64(filer2.TotalSize(entry.Chunks)), 10))
return
}
filename := entry.Name()
adjustHeadersAfterHEAD(w, r, filename)
totalSize := int64(filer2.TotalSize(entry.Chunks))
2020-03-08 10:06:48 +08:00
processRangeRequst(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
2020-03-08 10:09:30 +08:00
return filer2.StreamContent(fs.filer.MasterClient, w, entry.Chunks, offset, int(size))
2020-03-08 10:06:48 +08:00
})
}