2013-12-02 17:37:36 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2022-03-07 17:56:47 +08:00
|
|
|
"bufio"
|
2021-06-07 04:42:36 +08:00
|
|
|
"bytes"
|
2013-12-02 17:37:36 +08:00
|
|
|
"encoding/json"
|
2015-01-08 16:19:32 +08:00
|
|
|
"errors"
|
2013-12-02 17:37:36 +08:00
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
2020-03-09 06:42:44 +08:00
|
|
|
"io"
|
2021-05-10 15:13:55 +08:00
|
|
|
"io/fs"
|
2020-03-09 06:42:44 +08:00
|
|
|
"mime/multipart"
|
2013-12-02 17:37:36 +08:00
|
|
|
"net/http"
|
2021-10-12 14:33:45 +08:00
|
|
|
"net/url"
|
2013-12-02 17:37:36 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-04-24 22:00:48 +08:00
|
|
|
"sync"
|
2015-03-23 03:50:04 +08:00
|
|
|
"time"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2019-12-06 22:57:32 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-10-08 01:54:05 +08:00
|
|
|
|
2018-10-12 15:05:15 +08:00
|
|
|
"github.com/gorilla/mux"
|
2013-12-02 17:37:36 +08:00
|
|
|
)
|
|
|
|
|
2014-03-26 04:46:59 +08:00
|
|
|
var serverStats *stats.ServerStats
|
2015-03-23 03:50:04 +08:00
|
|
|
var startTime = time.Now()
|
2023-04-24 22:00:48 +08:00
|
|
|
var writePool = sync.Pool{New: func() interface{} {
|
|
|
|
return bufio.NewWriterSize(nil, 128*1024)
|
|
|
|
},
|
|
|
|
}
|
2014-03-26 04:46:59 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
serverStats = stats.NewServerStats()
|
|
|
|
go serverStats.Start()
|
|
|
|
}
|
|
|
|
|
2021-08-18 15:56:35 +08:00
|
|
|
// bodyAllowedForStatus is a copy of http.bodyAllowedForStatus non-exported function.
|
|
|
|
func bodyAllowedForStatus(status int) bool {
|
|
|
|
switch {
|
|
|
|
case status >= 100 && status <= 199:
|
|
|
|
return false
|
|
|
|
case status == http.StatusNoContent:
|
|
|
|
return false
|
|
|
|
case status == http.StatusNotModified:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-01-08 16:19:32 +08:00
|
|
|
func writeJson(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) (err error) {
|
2021-08-18 15:56:35 +08:00
|
|
|
if !bodyAllowedForStatus(httpStatus) {
|
2021-08-16 02:27:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-02 17:37:36 +08:00
|
|
|
var bytes []byte
|
2020-11-09 17:00:07 +08:00
|
|
|
if obj != nil {
|
|
|
|
if r.FormValue("pretty") != "" {
|
|
|
|
bytes, err = json.MarshalIndent(obj, "", " ")
|
|
|
|
} else {
|
|
|
|
bytes, err = json.Marshal(obj)
|
|
|
|
}
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-28 08:56:55 +08:00
|
|
|
|
|
|
|
if httpStatus >= 400 {
|
|
|
|
glog.V(0).Infof("response method:%s URL:%s with httpStatus:%d and JSON:%s",
|
|
|
|
r.Method, r.URL.String(), httpStatus, string(bytes))
|
|
|
|
}
|
|
|
|
|
2013-12-02 17:37:36 +08:00
|
|
|
callback := r.FormValue("callback")
|
|
|
|
if callback == "" {
|
2014-06-14 09:16:03 +08:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2015-01-08 16:19:32 +08:00
|
|
|
w.WriteHeader(httpStatus)
|
2019-08-10 05:38:54 +08:00
|
|
|
if httpStatus == http.StatusNotModified {
|
|
|
|
return
|
|
|
|
}
|
2013-12-02 17:37:36 +08:00
|
|
|
_, err = w.Write(bytes)
|
|
|
|
} else {
|
2014-06-14 09:16:03 +08:00
|
|
|
w.Header().Set("Content-Type", "application/javascript")
|
2015-01-08 16:19:32 +08:00
|
|
|
w.WriteHeader(httpStatus)
|
2019-08-10 05:38:54 +08:00
|
|
|
if httpStatus == http.StatusNotModified {
|
|
|
|
return
|
|
|
|
}
|
2013-12-02 17:37:36 +08:00
|
|
|
if _, err = w.Write([]uint8(callback)); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write([]uint8("(")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, string(bytes))
|
|
|
|
if _, err = w.Write([]uint8(")")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 16:19:32 +08:00
|
|
|
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapper for writeJson - just logs errors
|
2015-01-08 16:19:32 +08:00
|
|
|
func writeJsonQuiet(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) {
|
|
|
|
if err := writeJson(w, r, httpStatus, obj); err != nil {
|
2022-12-12 05:15:38 +08:00
|
|
|
glog.V(0).Infof("error writing JSON status %s %d: %v", r.URL, httpStatus, err)
|
2020-01-22 14:45:50 +08:00
|
|
|
glog.V(1).Infof("JSON content: %+v", obj)
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-08 16:19:32 +08:00
|
|
|
func writeJsonError(w http.ResponseWriter, r *http.Request, httpStatus int, err error) {
|
2013-12-02 17:37:36 +08:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["error"] = err.Error()
|
2022-09-02 00:04:15 +08:00
|
|
|
glog.V(1).Infof("error JSON response status %d: %s", httpStatus, m["error"])
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, httpStatus, m)
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func debug(params ...interface{}) {
|
2018-06-12 14:51:36 +08:00
|
|
|
glog.V(4).Infoln(params...)
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
|
2021-02-18 12:55:55 +08:00
|
|
|
func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterFn operation.GetMasterFn, grpcDialOption grpc.DialOption) {
|
2013-12-02 17:37:36 +08:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
if r.Method != "POST" {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusMethodNotAllowed, errors.New("Only submit via POST!"))
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("parsing upload file...")
|
2021-06-07 04:42:36 +08:00
|
|
|
bytesBuffer := bufPool.Get().(*bytes.Buffer)
|
|
|
|
defer bufPool.Put(bytesBuffer)
|
|
|
|
pu, pe := needle.ParseUpload(r, 256*1024*1024, bytesBuffer)
|
2013-12-02 17:37:36 +08:00
|
|
|
if pe != nil {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusBadRequest, pe)
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-07 22:06:58 +08:00
|
|
|
debug("assigning file id for", pu.FileName)
|
2015-01-06 19:31:26 +08:00
|
|
|
r.ParseForm()
|
2018-11-23 05:45:29 +08:00
|
|
|
count := uint64(1)
|
|
|
|
if r.FormValue("count") != "" {
|
|
|
|
count, pe = strconv.ParseUint(r.FormValue("count"), 10, 32)
|
|
|
|
if pe != nil {
|
|
|
|
writeJsonError(w, r, http.StatusBadRequest, pe)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-06-26 10:50:18 +08:00
|
|
|
ar := &operation.VolumeAssignRequest{
|
2018-11-23 05:45:29 +08:00
|
|
|
Count: count,
|
2019-12-06 07:51:24 +08:00
|
|
|
DataCenter: r.FormValue("dataCenter"),
|
2020-11-16 08:58:48 +08:00
|
|
|
Rack: r.FormValue("rack"),
|
2016-06-26 10:50:18 +08:00
|
|
|
Replication: r.FormValue("replication"),
|
|
|
|
Collection: r.FormValue("collection"),
|
|
|
|
Ttl: r.FormValue("ttl"),
|
2020-12-17 01:14:05 +08:00
|
|
|
DiskType: r.FormValue("disk"),
|
2016-06-26 10:50:18 +08:00
|
|
|
}
|
2021-02-18 12:55:55 +08:00
|
|
|
assignResult, ae := operation.Assign(masterFn, grpcDialOption, ar)
|
2013-12-02 17:37:36 +08:00
|
|
|
if ae != nil {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, ae)
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-03 02:16:50 +08:00
|
|
|
url := "http://" + assignResult.Url + "/" + assignResult.Fid
|
2020-03-07 22:06:58 +08:00
|
|
|
if pu.ModifiedTime != 0 {
|
|
|
|
url = url + "?ts=" + strconv.FormatUint(pu.ModifiedTime, 10)
|
2013-12-02 17:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
debug("upload file to store", url)
|
2021-09-07 07:20:49 +08:00
|
|
|
uploadOption := &operation.UploadOption{
|
|
|
|
UploadUrl: url,
|
|
|
|
Filename: pu.FileName,
|
|
|
|
Cipher: false,
|
|
|
|
IsInputCompressed: pu.IsGzipped,
|
|
|
|
MimeType: pu.MimeType,
|
|
|
|
PairMap: pu.PairMap,
|
|
|
|
Jwt: assignResult.Auth,
|
|
|
|
}
|
|
|
|
uploadResult, err := operation.UploadData(pu.Data, uploadOption)
|
2013-12-02 17:37:36 +08:00
|
|
|
if err != nil {
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-07 22:06:58 +08:00
|
|
|
m["fileName"] = pu.FileName
|
2013-12-02 17:37:36 +08:00
|
|
|
m["fid"] = assignResult.Fid
|
|
|
|
m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
|
2020-03-07 22:06:58 +08:00
|
|
|
m["size"] = pu.OriginalDataSize
|
2018-09-23 13:12:21 +08:00
|
|
|
m["eTag"] = uploadResult.ETag
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusCreated, m)
|
2013-12-02 17:37:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseURLPath(path string) (vid, fid, filename, ext string, isVolumeIdOnly bool) {
|
|
|
|
switch strings.Count(path, "/") {
|
|
|
|
case 3:
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
vid, fid, filename = parts[1], parts[2], parts[3]
|
|
|
|
ext = filepath.Ext(filename)
|
|
|
|
case 2:
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
vid, fid = parts[1], parts[2]
|
|
|
|
dotIndex := strings.LastIndex(fid, ".")
|
|
|
|
if dotIndex > 0 {
|
|
|
|
ext = fid[dotIndex:]
|
|
|
|
fid = fid[0:dotIndex]
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
sepIndex := strings.LastIndex(path, "/")
|
|
|
|
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
|
|
|
if commaIndex <= 0 {
|
|
|
|
vid, isVolumeIdOnly = path[sepIndex+1:], true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dotIndex := strings.LastIndex(path[sepIndex:], ".")
|
|
|
|
vid = path[sepIndex+1 : commaIndex]
|
|
|
|
fid = path[commaIndex+1:]
|
|
|
|
ext = ""
|
|
|
|
if dotIndex > 0 {
|
|
|
|
fid = path[commaIndex+1 : dotIndex]
|
|
|
|
ext = path[dotIndex:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-04-14 14:41:34 +08:00
|
|
|
|
2018-05-07 15:37:33 +08:00
|
|
|
func statsHealthHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
2020-06-02 15:10:35 +08:00
|
|
|
m["Version"] = util.Version()
|
2018-05-07 15:37:33 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
|
|
|
}
|
2014-03-26 04:46:59 +08:00
|
|
|
func statsCounterHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
2020-06-02 15:10:35 +08:00
|
|
|
m["Version"] = util.Version()
|
2014-03-27 04:22:27 +08:00
|
|
|
m["Counters"] = serverStats
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
2014-03-26 04:46:59 +08:00
|
|
|
}
|
2014-03-27 04:22:27 +08:00
|
|
|
|
2014-03-26 04:46:59 +08:00
|
|
|
func statsMemoryHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
2020-06-02 15:10:35 +08:00
|
|
|
m["Version"] = util.Version()
|
2014-03-27 04:22:27 +08:00
|
|
|
m["Memory"] = stats.MemStat()
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
2014-03-26 04:46:59 +08:00
|
|
|
}
|
2018-05-28 17:35:58 +08:00
|
|
|
|
2021-05-10 15:13:55 +08:00
|
|
|
var StaticFS fs.FS
|
|
|
|
|
2018-10-08 01:54:05 +08:00
|
|
|
func handleStaticResources(defaultMux *http.ServeMux) {
|
2021-05-10 15:13:55 +08:00
|
|
|
defaultMux.Handle("/favicon.ico", http.FileServer(http.FS(StaticFS)))
|
|
|
|
defaultMux.Handle("/seaweedfsstatic/", http.StripPrefix("/seaweedfsstatic", http.FileServer(http.FS(StaticFS))))
|
2018-05-28 17:35:58 +08:00
|
|
|
}
|
2018-10-12 15:05:15 +08:00
|
|
|
|
|
|
|
func handleStaticResources2(r *mux.Router) {
|
2021-05-10 15:13:55 +08:00
|
|
|
r.Handle("/favicon.ico", http.FileServer(http.FS(StaticFS)))
|
|
|
|
r.PathPrefix("/seaweedfsstatic/").Handler(http.StripPrefix("/seaweedfsstatic", http.FileServer(http.FS(StaticFS))))
|
2018-10-12 15:05:15 +08:00
|
|
|
}
|
2020-03-09 06:42:44 +08:00
|
|
|
|
2021-12-16 05:18:53 +08:00
|
|
|
func adjustPassthroughHeaders(w http.ResponseWriter, r *http.Request, filename string) {
|
|
|
|
for header, values := range r.Header {
|
2022-05-31 13:57:41 +08:00
|
|
|
if normalizedHeader, ok := s3_constants.PassThroughHeaders[strings.ToLower(header)]; ok {
|
2021-12-16 05:18:53 +08:00
|
|
|
w.Header()[normalizedHeader] = values
|
|
|
|
}
|
2021-10-12 14:29:52 +08:00
|
|
|
}
|
2021-12-16 05:18:53 +08:00
|
|
|
adjustHeaderContentDisposition(w, r, filename)
|
|
|
|
}
|
|
|
|
func adjustHeaderContentDisposition(w http.ResponseWriter, r *http.Request, filename string) {
|
|
|
|
if contentDisposition := w.Header().Get("Content-Disposition"); contentDisposition != "" {
|
2021-10-12 15:04:28 +08:00
|
|
|
return
|
|
|
|
}
|
2020-03-09 06:42:44 +08:00
|
|
|
if filename != "" {
|
2021-10-12 14:33:45 +08:00
|
|
|
filename = url.QueryEscape(filename)
|
2020-03-09 06:42:44 +08: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)+`"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:14:09 +08:00
|
|
|
func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64, mimeType string, writeFn func(writer io.Writer, offset int64, size int64) error) error {
|
2020-03-09 06:42:44 +08:00
|
|
|
rangeReq := r.Header.Get("Range")
|
2023-04-24 22:00:48 +08:00
|
|
|
bufferedWriter := writePool.Get().(*bufio.Writer)
|
|
|
|
bufferedWriter.Reset(w)
|
|
|
|
defer func() {
|
|
|
|
bufferedWriter.Flush()
|
|
|
|
writePool.Put(bufferedWriter)
|
|
|
|
}()
|
2020-03-09 06:42:44 +08:00
|
|
|
|
|
|
|
if rangeReq == "" {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
|
2022-03-07 17:56:47 +08:00
|
|
|
if err := writeFn(bufferedWriter, 0, totalSize); err != nil {
|
2022-09-08 11:14:09 +08:00
|
|
|
glog.Errorf("processRangeRequest: %v", err)
|
2020-03-09 06:42:44 +08:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2022-09-08 11:14:09 +08:00
|
|
|
return fmt.Errorf("processRangeRequest: %v", err)
|
2020-03-09 06:42:44 +08:00
|
|
|
}
|
2022-09-08 11:14:09 +08:00
|
|
|
return nil
|
2020-03-09 06:42:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//the rest is dealing with partial content request
|
|
|
|
//mostly copy from src/pkg/net/http/fs.go
|
|
|
|
ranges, err := parseRange(rangeReq, totalSize)
|
|
|
|
if err != nil {
|
2022-06-15 18:36:43 +08:00
|
|
|
glog.Errorf("processRangeRequest headers: %+v err: %v", w.Header(), err)
|
2020-03-09 06:42:44 +08:00
|
|
|
http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable)
|
2022-09-08 11:14:09 +08:00
|
|
|
return fmt.Errorf("processRangeRequest header: %v", err)
|
2020-03-09 06:42:44 +08:00
|
|
|
}
|
|
|
|
if sumRangesSize(ranges) > totalSize {
|
|
|
|
// 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.
|
2022-09-08 11:14:09 +08:00
|
|
|
return nil
|
2020-03-09 06:42:44 +08:00
|
|
|
}
|
|
|
|
if len(ranges) == 0 {
|
2022-09-08 11:14:09 +08:00
|
|
|
return nil
|
2020-03-09 06:42:44 +08: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))
|
|
|
|
w.Header().Set("Content-Range", ra.contentRange(totalSize))
|
|
|
|
|
2021-06-29 17:13:29 +08:00
|
|
|
w.WriteHeader(http.StatusPartialContent)
|
2022-03-07 17:56:47 +08:00
|
|
|
err = writeFn(bufferedWriter, ra.start, ra.length)
|
2020-03-09 06:42:44 +08:00
|
|
|
if err != nil {
|
2022-09-08 11:14:09 +08:00
|
|
|
glog.Errorf("processRangeRequest range[0]: %+v err: %v", w.Header(), err)
|
2020-03-09 06:42:44 +08:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2022-09-08 11:14:09 +08:00
|
|
|
return fmt.Errorf("processRangeRequest range[0]: %v", err)
|
2020-03-09 06:42:44 +08:00
|
|
|
}
|
2022-09-08 11:14:09 +08:00
|
|
|
return nil
|
2020-03-09 06:42:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// process multiple ranges
|
|
|
|
for _, ra := range ranges {
|
|
|
|
if ra.start > totalSize {
|
|
|
|
http.Error(w, "Out of Range", http.StatusRequestedRangeNotSatisfiable)
|
2022-09-08 11:14:09 +08:00
|
|
|
return fmt.Errorf("out of range: %v", err)
|
2020-03-09 06:42:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sendSize := rangesMIMESize(ranges, mimeType, totalSize)
|
|
|
|
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 {
|
|
|
|
part, e := mw.CreatePart(ra.mimeHeader(mimeType, totalSize))
|
|
|
|
if e != nil {
|
|
|
|
pw.CloseWithError(e)
|
|
|
|
return
|
|
|
|
}
|
2021-05-20 14:45:21 +08:00
|
|
|
if e = writeFn(part, ra.start, ra.length); e != nil {
|
2020-03-09 06:42:44 +08:00
|
|
|
pw.CloseWithError(e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mw.Close()
|
|
|
|
pw.Close()
|
|
|
|
}()
|
|
|
|
if w.Header().Get("Content-Encoding") == "" {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
|
|
|
|
}
|
2020-10-13 03:26:25 +08:00
|
|
|
w.WriteHeader(http.StatusPartialContent)
|
2022-03-07 17:56:47 +08:00
|
|
|
if _, err := io.CopyN(bufferedWriter, sendContent, sendSize); err != nil {
|
2022-06-15 18:36:43 +08:00
|
|
|
glog.Errorf("processRangeRequest err: %v", err)
|
2020-03-09 06:42:44 +08:00
|
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
2022-09-08 11:14:09 +08:00
|
|
|
return fmt.Errorf("processRangeRequest err: %v", err)
|
2020-03-09 06:42:44 +08:00
|
|
|
}
|
2022-09-08 11:14:09 +08:00
|
|
|
return nil
|
2020-03-09 06:42:44 +08:00
|
|
|
}
|