2015-02-26 07:59:07 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2015-12-15 06:38:58 +00:00
|
|
|
"bytes"
|
2015-02-26 07:59:07 +00:00
|
|
|
"io"
|
|
|
|
"mime"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
2015-12-15 06:38:58 +00:00
|
|
|
"path"
|
2015-02-26 07:59:07 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-02-27 09:42:41 +00:00
|
|
|
"net/url"
|
|
|
|
|
2015-04-16 19:18:06 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/go/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/images"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/storage"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/util"
|
2015-02-26 07:59:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
|
|
|
|
|
|
|
|
func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
n := new(storage.Needle)
|
|
|
|
vid, fid, filename, ext, _ := parseURLPath(r.URL.Path)
|
|
|
|
volumeId, err := storage.NewVolumeId(vid)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(2).Infoln("parsing error:", err, r.URL.Path)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = n.ParsePath(fid)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(2).Infoln("parsing fid error:", err, r.URL.Path)
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(4).Infoln("volume", volumeId, "reading", n)
|
|
|
|
if !vs.store.HasVolume(volumeId) {
|
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
|
|
|
|
}
|
2015-02-26 07:59:07 +00:00
|
|
|
lookupResult, err := operation.Lookup(vs.GetMasterNode(), volumeId.String())
|
|
|
|
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))
|
|
|
|
u.Path = r.URL.Path
|
|
|
|
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
|
2015-05-26 07:58:41 +00:00
|
|
|
count, e := vs.store.ReadVolumeNeedle(volumeId, n)
|
2015-02-26 07:59:07 +00:00
|
|
|
glog.V(4).Infoln("read bytes", count, "error", e)
|
|
|
|
if e != nil || count <= 0 {
|
|
|
|
glog.V(0).Infoln("read error:", e, r.URL.Path)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2016-05-13 07:19:57 +00:00
|
|
|
defer n.ReleaseMemory()
|
2015-02-26 07:59:07 +00:00
|
|
|
if n.Cookie != cookie {
|
|
|
|
glog.V(0).Infoln("request", r.URL.Path, "with unmaching cookie seen:", cookie, "expected:", n.Cookie, "from", r.RemoteAddr, "agent", r.UserAgent())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
etag := n.Etag()
|
|
|
|
if inm := r.Header.Get("If-None-Match"); inm == etag {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Etag", etag)
|
2015-12-01 12:23:50 +00:00
|
|
|
|
|
|
|
if vs.tryHandleChunkedFile(n, filename, w, r) {
|
|
|
|
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 == "" {
|
|
|
|
ext = path.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
|
|
|
|
2015-02-26 07:59:07 +00:00
|
|
|
if ext != ".gz" {
|
|
|
|
if n.IsGzipped() {
|
|
|
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
} else {
|
2015-12-02 13:27:29 +00:00
|
|
|
if n.Data, err = operation.UnGzipData(n.Data); err != nil {
|
2015-12-15 03:26:16 +00:00
|
|
|
glog.V(0).Infoln("ungzip error:", err, r.URL.Path)
|
2015-02-26 07:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ext == ".png" || ext == ".jpg" || ext == ".gif" {
|
|
|
|
width, height := 0, 0
|
|
|
|
if r.FormValue("width") != "" {
|
|
|
|
width, _ = strconv.Atoi(r.FormValue("width"))
|
|
|
|
}
|
|
|
|
if r.FormValue("height") != "" {
|
|
|
|
height, _ = strconv.Atoi(r.FormValue("height"))
|
|
|
|
}
|
|
|
|
n.Data, _, _ = images.Resized(ext, n.Data, width, height)
|
|
|
|
}
|
|
|
|
|
2015-12-15 03:26:16 +00:00
|
|
|
if e := writeResponseContent(filename, mtype, bytes.NewReader(n.Data), w, r); e != nil {
|
|
|
|
glog.V(2).Infoln("response write error:", e)
|
2015-02-26 07:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 12:23:50 +00:00
|
|
|
|
2016-04-14 08:46:16 +00:00
|
|
|
func (vs *VolumeServer) FaviconHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, err := images.Asset("favicon/favicon.ico")
|
|
|
|
if err != nil {
|
2016-04-15 18:04:39 +00:00
|
|
|
glog.V(2).Infoln("favicon read error:", err)
|
|
|
|
return
|
2016-04-14 08:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if e := writeResponseContent("favicon.ico", "image/x-icon", bytes.NewReader(data), w, r); e != nil {
|
|
|
|
glog.V(2).Infoln("response write error:", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 12:23:50 +00:00
|
|
|
func (vs *VolumeServer) tryHandleChunkedFile(n *storage.Needle, fileName string, w http.ResponseWriter, r *http.Request) (processed bool) {
|
2015-12-01 12:53:44 +00:00
|
|
|
if !n.IsChunkedManifest() {
|
2015-12-01 12:23:50 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-12-02 08:35:16 +00:00
|
|
|
|
2015-12-02 13:27:29 +00:00
|
|
|
chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
|
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
|
|
|
|
}
|
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")
|
|
|
|
|
2015-12-02 07:00:46 +00:00
|
|
|
chunkedFileReader := &operation.ChunkedFileReader{
|
2015-12-01 12:23:50 +00:00
|
|
|
Manifest: chunkManifest,
|
|
|
|
Master: vs.GetMasterNode(),
|
|
|
|
}
|
|
|
|
defer chunkedFileReader.Close()
|
2015-12-15 03:26:16 +00:00
|
|
|
if e := writeResponseContent(fileName, mType, chunkedFileReader, w, r); e != nil {
|
|
|
|
glog.V(2).Infoln("response write error:", e)
|
|
|
|
}
|
2015-12-15 06:38:58 +00:00
|
|
|
return true
|
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 == "" {
|
|
|
|
if ext := path.Ext(filename); ext != "" {
|
|
|
|
mimeType = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mimeType != "" {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
}
|
|
|
|
if filename != "" {
|
2016-05-12 10:11:27 +00:00
|
|
|
contentDisposition := "inline"
|
|
|
|
if r.FormValue("dl") != "" {
|
|
|
|
if dl, _ := strconv.ParseBool(r.FormValue("dl")); dl {
|
|
|
|
contentDisposition = "attachment"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Disposition", contentDisposition+`; filename="`+fileNameEscaper.Replace(filename)+`"`)
|
2015-12-15 03:26:16 +00:00
|
|
|
}
|
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-01 12:23:50 +00:00
|
|
|
rangeReq := r.Header.Get("Range")
|
|
|
|
if rangeReq == "" {
|
2015-12-15 03:26:16 +00:00
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
|
|
|
if _, e = rs.Seek(0, 0); e != nil {
|
|
|
|
return e
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
2015-12-15 03:26:16 +00:00
|
|
|
_, e = io.Copy(w, rs)
|
|
|
|
return e
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//the rest is dealing with partial content request
|
|
|
|
//mostly copy from src/pkg/net/http/fs.go
|
2015-12-15 03:26:16 +00:00
|
|
|
ranges, err := parseRange(rangeReq, totalSize)
|
2015-12-01 12:23:50 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable)
|
2015-12-15 03:26:16 +00:00
|
|
|
return nil
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
2015-12-15 03:26:16 +00:00
|
|
|
if sumRangesSize(ranges) > totalSize {
|
2015-12-01 12:23:50 +00:00
|
|
|
// The total number of bytes in all the ranges
|
|
|
|
// is larger than the size of the file by
|
|
|
|
// itself, so this is probably an attack, or a
|
|
|
|
// dumb client. Ignore the range request.
|
2015-12-15 03:26:16 +00:00
|
|
|
return nil
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
|
|
|
if len(ranges) == 0 {
|
2015-12-15 03:26:16 +00:00
|
|
|
return nil
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
|
|
|
if len(ranges) == 1 {
|
|
|
|
// RFC 2616, Section 14.16:
|
|
|
|
// "When an HTTP message includes the content of a single
|
|
|
|
// range (for example, a response to a request for a
|
|
|
|
// single range, or to a request for a set of ranges
|
|
|
|
// that overlap without any holes), this content is
|
|
|
|
// transmitted with a Content-Range header, and a
|
|
|
|
// Content-Length header showing the number of bytes
|
|
|
|
// actually transferred.
|
|
|
|
// ...
|
|
|
|
// A response to a request for a single range MUST NOT
|
|
|
|
// be sent using the multipart/byteranges media type."
|
|
|
|
ra := ranges[0]
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(ra.length, 10))
|
2015-12-15 03:26:16 +00:00
|
|
|
w.Header().Set("Content-Range", ra.contentRange(totalSize))
|
2015-12-01 12:23:50 +00:00
|
|
|
w.WriteHeader(http.StatusPartialContent)
|
2015-12-15 03:26:16 +00:00
|
|
|
if _, e = rs.Seek(ra.start, 0); e != nil {
|
|
|
|
return e
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
2015-12-15 03:26:16 +00:00
|
|
|
|
|
|
|
_, e = io.CopyN(w, rs, ra.length)
|
|
|
|
return e
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
|
|
|
// process multiple ranges
|
|
|
|
for _, ra := range ranges {
|
2015-12-15 03:26:16 +00:00
|
|
|
if ra.start > totalSize {
|
2015-12-01 12:23:50 +00:00
|
|
|
http.Error(w, "Out of Range", http.StatusRequestedRangeNotSatisfiable)
|
2015-12-15 03:26:16 +00:00
|
|
|
return nil
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 03:26:16 +00:00
|
|
|
sendSize := rangesMIMESize(ranges, mimeType, totalSize)
|
2015-12-01 12:23:50 +00:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
mw := multipart.NewWriter(pw)
|
|
|
|
w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
|
|
|
|
sendContent := pr
|
|
|
|
defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
|
|
|
|
go func() {
|
|
|
|
for _, ra := range ranges {
|
2015-12-15 03:26:16 +00:00
|
|
|
part, e := mw.CreatePart(ra.mimeHeader(mimeType, totalSize))
|
|
|
|
if e != nil {
|
|
|
|
pw.CloseWithError(e)
|
2015-12-01 12:23:50 +00:00
|
|
|
return
|
|
|
|
}
|
2015-12-15 03:26:16 +00:00
|
|
|
if _, e = rs.Seek(ra.start, 0); e != nil {
|
|
|
|
pw.CloseWithError(e)
|
|
|
|
return
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
2015-12-15 03:26:16 +00:00
|
|
|
if _, e = io.CopyN(part, rs, ra.length); e != nil {
|
|
|
|
pw.CloseWithError(e)
|
2015-12-01 12:23:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mw.Close()
|
|
|
|
pw.Close()
|
|
|
|
}()
|
|
|
|
if w.Header().Get("Content-Encoding") == "" {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusPartialContent)
|
2015-12-15 03:26:16 +00:00
|
|
|
_, e = io.CopyN(w, sendContent, sendSize)
|
|
|
|
return e
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|