2015-02-26 07:59:07 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2015-12-15 06:38:58 +00:00
|
|
|
"bytes"
|
2020-02-26 05:50:12 +00:00
|
|
|
"encoding/json"
|
2019-06-06 07:29:02 +00:00
|
|
|
"errors"
|
2019-10-24 05:11:42 +00:00
|
|
|
"fmt"
|
2015-02-26 07:59:07 +00:00
|
|
|
"io"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
2016-06-13 22:07:17 +00:00
|
|
|
"net/url"
|
2020-03-08 22:42:44 +00:00
|
|
|
"path/filepath"
|
2015-02-26 07:59:07 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/images"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2019-06-15 19:21:44 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats"
|
2019-04-19 04:43:36 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2015-02-26 07:59:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
|
|
|
|
|
|
|
|
func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
|
2019-06-14 07:54:56 +00:00
|
|
|
|
2020-08-01 18:17:19 +00:00
|
|
|
// println(r.Method + " " + r.URL.Path)
|
|
|
|
|
2019-06-15 19:21:44 +00:00
|
|
|
stats.VolumeServerRequestCounter.WithLabelValues("get").Inc()
|
2019-06-14 07:54:56 +00:00
|
|
|
start := time.Now()
|
2019-06-15 19:21:44 +00:00
|
|
|
defer func() { stats.VolumeServerRequestHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds()) }()
|
2019-06-14 07:54:56 +00:00
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
n := new(needle.Needle)
|
2015-02-26 07:59:07 +00:00
|
|
|
vid, fid, filename, ext, _ := parseURLPath(r.URL.Path)
|
2019-06-06 07:29:02 +00:00
|
|
|
|
|
|
|
if !vs.maybeCheckJwtAuthorization(r, vid, fid, false) {
|
|
|
|
writeJsonError(w, r, http.StatusUnauthorized, errors.New("wrong jwt"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
volumeId, err := needle.NewVolumeId(vid)
|
2015-02-26 07:59:07 +00:00
|
|
|
if err != nil {
|
2020-04-21 23:13:13 +00:00
|
|
|
glog.V(2).Infof("parsing vid %s: %v", r.URL.Path, err)
|
2015-02-26 07:59:07 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = n.ParsePath(fid)
|
|
|
|
if err != nil {
|
2020-04-21 23:13:13 +00:00
|
|
|
glog.V(2).Infof("parsing fid %s: %v", r.URL.Path, err)
|
2015-02-26 07:59:07 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-25 01:26:03 +00:00
|
|
|
// glog.V(4).Infoln("volume", volumeId, "reading", n)
|
2019-05-27 08:29:46 +00:00
|
|
|
hasVolume := vs.store.HasVolume(volumeId)
|
2019-05-28 04:40:51 +00:00
|
|
|
_, hasEcVolume := vs.store.FindEcVolume(volumeId)
|
|
|
|
if !hasVolume && !hasEcVolume {
|
2015-08-03 21:43:15 +00:00
|
|
|
if !vs.ReadRedirect {
|
|
|
|
glog.V(2).Infoln("volume is not local:", err, r.URL.Path)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2018-06-01 07:39:39 +00:00
|
|
|
lookupResult, err := operation.Lookup(vs.GetMaster(), volumeId.String())
|
2015-02-26 07:59:07 +00:00
|
|
|
glog.V(2).Infoln("volume", volumeId, "found on", lookupResult, "error", err)
|
|
|
|
if err == nil && len(lookupResult.Locations) > 0 {
|
2016-02-27 09:42:41 +00:00
|
|
|
u, _ := url.Parse(util.NormalizeUrl(lookupResult.Locations[0].PublicUrl))
|
2019-10-24 05:11:42 +00:00
|
|
|
u.Path = fmt.Sprintf("%s/%s,%s", u.Path, vid, fid)
|
2016-02-27 09:42:41 +00:00
|
|
|
arg := url.Values{}
|
|
|
|
if c := r.FormValue("collection"); c != "" {
|
|
|
|
arg.Set("collection", c)
|
|
|
|
}
|
|
|
|
u.RawQuery = arg.Encode()
|
|
|
|
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
|
|
|
|
|
2015-02-26 07:59:07 +00:00
|
|
|
} else {
|
|
|
|
glog.V(2).Infoln("lookup error:", err, r.URL.Path)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cookie := n.Cookie
|
2019-05-27 08:29:46 +00:00
|
|
|
var count int
|
|
|
|
if hasVolume {
|
|
|
|
count, err = vs.store.ReadVolumeNeedle(volumeId, n)
|
2019-05-28 04:40:51 +00:00
|
|
|
} else if hasEcVolume {
|
2020-02-26 05:50:12 +00:00
|
|
|
count, err = vs.store.ReadEcShardNeedle(volumeId, n)
|
2019-05-27 08:29:46 +00:00
|
|
|
}
|
2020-01-25 01:26:03 +00:00
|
|
|
// glog.V(4).Infoln("read bytes", count, "error", err)
|
2019-05-27 08:29:46 +00:00
|
|
|
if err != nil || count < 0 {
|
2019-06-06 06:20:26 +00:00
|
|
|
glog.V(0).Infof("read %s isNormalVolume %v error: %v", r.URL.Path, hasVolume, err)
|
2015-02-26 07:59:07 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n.Cookie != cookie {
|
2018-11-22 08:42:56 +00:00
|
|
|
glog.V(0).Infof("request %s with cookie:%x expected:%x from %s agent %s", r.URL.Path, cookie, n.Cookie, r.RemoteAddr, r.UserAgent())
|
2015-02-26 07:59:07 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n.LastModified != 0 {
|
|
|
|
w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).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 {
|
|
|
|
if t.Unix() >= int64(n.LastModified) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-09 23:25:43 +00:00
|
|
|
if inm := r.Header.Get("If-None-Match"); inm == "\""+n.Etag()+"\"" {
|
2015-02-26 07:59:07 +00:00
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
2020-03-08 22:42:44 +00:00
|
|
|
setEtag(w, n.Etag())
|
2015-12-01 12:23:50 +00:00
|
|
|
|
2017-01-08 01:16:29 +00:00
|
|
|
if n.HasPairs() {
|
|
|
|
pairMap := make(map[string]string)
|
|
|
|
err = json.Unmarshal(n.Pairs, &pairMap)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln("Unmarshal pairs error:", err)
|
|
|
|
}
|
|
|
|
for k, v := range pairMap {
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 04:23:15 +00:00
|
|
|
if vs.tryHandleChunkedFile(n, filename, ext, w, r) {
|
2015-12-01 12:23:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-26 07:59:07 +00:00
|
|
|
if n.NameSize > 0 && filename == "" {
|
|
|
|
filename = string(n.Name)
|
2015-12-15 03:26:16 +00:00
|
|
|
if ext == "" {
|
2020-03-08 22:42:44 +00:00
|
|
|
ext = filepath.Ext(filename)
|
2015-02-26 07:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mtype := ""
|
|
|
|
if n.MimeSize > 0 {
|
|
|
|
mt := string(n.Mime)
|
2015-05-16 10:55:58 +00:00
|
|
|
if !strings.HasPrefix(mt, "application/octet-stream") {
|
2015-02-26 07:59:07 +00:00
|
|
|
mtype = mt
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 03:26:16 +00:00
|
|
|
|
2020-08-01 18:17:19 +00:00
|
|
|
if n.IsCompressed() {
|
|
|
|
if _, _, _, shouldResize := shouldResizeImages(ext, r); shouldResize {
|
|
|
|
if n.Data, err = util.DecompressData(n.Data); err != nil {
|
|
|
|
glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
|
|
|
|
}
|
|
|
|
} else if strings.Contains(r.Header.Get("Accept-Encoding"), "zstd") && util.IsZstdContent(n.Data) {
|
|
|
|
w.Header().Set("Content-Encoding", "zstd")
|
|
|
|
} else if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && util.IsGzippedContent(n.Data) {
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
} else {
|
|
|
|
if n.Data, err = util.DecompressData(n.Data); err != nil {
|
|
|
|
glog.V(0).Infoln("uncompress error:", err, r.URL.Path)
|
2015-02-26 07:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 07:58:52 +00:00
|
|
|
|
2018-07-24 08:09:32 +00:00
|
|
|
rs := conditionallyResizeImages(bytes.NewReader(n.Data), ext, r)
|
2015-02-26 07:59:07 +00:00
|
|
|
|
2018-07-05 02:34:17 +00:00
|
|
|
if e := writeResponseContent(filename, mtype, rs, w, r); e != nil {
|
2015-12-15 03:26:16 +00:00
|
|
|
glog.V(2).Infoln("response write error:", e)
|
2015-02-26 07:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 12:23:50 +00:00
|
|
|
|
2020-04-03 04:23:15 +00:00
|
|
|
func (vs *VolumeServer) tryHandleChunkedFile(n *needle.Needle, fileName string, ext string, w http.ResponseWriter, r *http.Request) (processed bool) {
|
2017-03-13 01:40:24 +00:00
|
|
|
if !n.IsChunkedManifest() || r.URL.Query().Get("cm") == "false" {
|
2015-12-01 12:23:50 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-12-02 08:35:16 +00:00
|
|
|
|
2020-06-20 05:45:27 +00:00
|
|
|
chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsCompressed())
|
2015-12-01 12:23:50 +00:00
|
|
|
if e != nil {
|
2015-12-15 06:38:58 +00:00
|
|
|
glog.V(0).Infof("load chunked manifest (%s) error: %v", r.URL.Path, e)
|
2015-12-01 12:23:50 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if fileName == "" && chunkManifest.Name != "" {
|
|
|
|
fileName = chunkManifest.Name
|
|
|
|
}
|
2018-07-05 02:34:17 +00:00
|
|
|
|
2020-04-03 04:23:15 +00:00
|
|
|
if ext == "" {
|
|
|
|
ext = filepath.Ext(fileName)
|
|
|
|
}
|
2018-07-05 02:34:17 +00:00
|
|
|
|
2015-12-15 03:26:16 +00:00
|
|
|
mType := ""
|
2015-12-01 12:23:50 +00:00
|
|
|
if chunkManifest.Mime != "" {
|
|
|
|
mt := chunkManifest.Mime
|
|
|
|
if !strings.HasPrefix(mt, "application/octet-stream") {
|
2015-12-15 03:26:16 +00:00
|
|
|
mType = mt
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 03:26:16 +00:00
|
|
|
|
2015-12-01 12:23:50 +00:00
|
|
|
w.Header().Set("X-File-Store", "chunked")
|
|
|
|
|
2020-03-20 22:46:16 +00:00
|
|
|
chunkedFileReader := operation.NewChunkedFileReader(chunkManifest.Chunks, vs.GetMaster())
|
2015-12-01 12:23:50 +00:00
|
|
|
defer chunkedFileReader.Close()
|
2018-07-24 07:58:52 +00:00
|
|
|
|
|
|
|
rs := conditionallyResizeImages(chunkedFileReader, ext, r)
|
|
|
|
|
|
|
|
if e := writeResponseContent(fileName, mType, rs, w, r); e != nil {
|
|
|
|
glog.V(2).Infoln("response write error:", e)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func conditionallyResizeImages(originalDataReaderSeeker io.ReadSeeker, ext string, r *http.Request) io.ReadSeeker {
|
|
|
|
rs := originalDataReaderSeeker
|
2020-07-17 11:57: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 {
|
|
|
|
rs, _, _ = images.Resized(ext, originalDataReaderSeeker, width, height, mode)
|
|
|
|
}
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldResizeImages(ext string, r *http.Request) (width, height int, mode string, shouldResize bool) {
|
2018-07-05 02:34:17 +00:00
|
|
|
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" {
|
|
|
|
if r.FormValue("width") != "" {
|
|
|
|
width, _ = strconv.Atoi(r.FormValue("width"))
|
|
|
|
}
|
|
|
|
if r.FormValue("height") != "" {
|
|
|
|
height, _ = strconv.Atoi(r.FormValue("height"))
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 03:31:11 +00:00
|
|
|
mode = r.FormValue("mode")
|
|
|
|
shouldResize = width > 0 || height > 0
|
|
|
|
return
|
2015-12-15 03:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
totalSize, e := rs.Seek(0, 2)
|
|
|
|
if mimeType == "" {
|
2020-03-08 22:42:44 +00:00
|
|
|
if ext := filepath.Ext(filename); ext != "" {
|
2015-12-15 03:26:16 +00:00
|
|
|
mimeType = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mimeType != "" {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
}
|
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
2020-03-08 22:42:44 +00:00
|
|
|
|
2020-07-26 03:09:19 +00:00
|
|
|
adjustHeaderContentDisposition(w, r, filename)
|
2020-07-26 03:06:40 +00:00
|
|
|
|
2015-12-15 03:26:16 +00:00
|
|
|
if r.Method == "HEAD" {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-01 12:23:50 +00:00
|
|
|
|
2020-03-21 03:31:11 +00:00
|
|
|
processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
|
2020-03-08 02:06:48 +00:00
|
|
|
if _, e = rs.Seek(offset, 0); e != nil {
|
2015-12-15 03:26:16 +00:00
|
|
|
return e
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
2020-03-08 02:06:48 +00:00
|
|
|
_, e = io.CopyN(writer, rs, size)
|
2015-12-15 03:26:16 +00:00
|
|
|
return e
|
2020-03-08 02:06:48 +00:00
|
|
|
})
|
|
|
|
return nil
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|