2020-08-19 23:36:46 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-06-24 11:10:00 +00:00
|
|
|
"fmt"
|
2020-08-19 23:36:46 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2021-06-24 11:10:00 +00:00
|
|
|
"strings"
|
2021-06-30 15:10:11 +00:00
|
|
|
"time"
|
2020-08-19 23:36:46 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2020-08-19 23:36:46 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-06-30 15:10:11 +00:00
|
|
|
type getObjectArgs struct {
|
|
|
|
IfModifiedSince time.Time
|
|
|
|
IfUnmodifiedSince time.Time
|
|
|
|
}
|
|
|
|
|
2021-06-24 11:10:00 +00:00
|
|
|
func fetchRangeHeader(headers http.Header, fullSize uint64) (*layer.RangeParams, error) {
|
|
|
|
const prefix = "bytes="
|
|
|
|
rangeHeader := headers.Get("Range")
|
|
|
|
if len(rangeHeader) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(rangeHeader, prefix) {
|
|
|
|
return nil, fmt.Errorf("unknown unit in range header")
|
|
|
|
}
|
|
|
|
arr := strings.Split(strings.TrimPrefix(rangeHeader, prefix), "-")
|
|
|
|
if len(arr) != 2 || (len(arr[0]) == 0 && len(arr[1]) == 0) {
|
|
|
|
return nil, fmt.Errorf("unknown byte-range-set")
|
|
|
|
}
|
|
|
|
|
|
|
|
var end, start uint64
|
|
|
|
var err0, err1 error
|
|
|
|
base, bitSize := 10, 64
|
|
|
|
|
|
|
|
if len(arr[0]) == 0 {
|
|
|
|
end, err1 = strconv.ParseUint(arr[1], base, bitSize)
|
|
|
|
start = fullSize - end
|
|
|
|
end = fullSize - 1
|
|
|
|
} else if len(arr[1]) == 0 {
|
|
|
|
start, err0 = strconv.ParseUint(arr[0], base, bitSize)
|
|
|
|
end = fullSize - 1
|
|
|
|
} else {
|
|
|
|
start, err0 = strconv.ParseUint(arr[0], base, bitSize)
|
|
|
|
end, err1 = strconv.ParseUint(arr[1], base, bitSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err0 != nil || err1 != nil || start > end {
|
|
|
|
return nil, fmt.Errorf("invalid Range header")
|
|
|
|
}
|
|
|
|
return &layer.RangeParams{Start: start, End: end}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:39:48 +00:00
|
|
|
func writeHeaders(h http.Header, info *layer.ObjectInfo) {
|
2021-06-30 12:29:01 +00:00
|
|
|
if len(info.ContentType) > 0 {
|
|
|
|
h.Set(api.ContentType, info.ContentType)
|
|
|
|
}
|
2021-06-29 13:40:26 +00:00
|
|
|
h.Set(api.LastModified, info.Created.Format(http.TimeFormat))
|
|
|
|
h.Set(api.ContentLength, strconv.FormatInt(info.Size, 10))
|
|
|
|
h.Set(api.ETag, info.HashSum)
|
2021-01-14 17:39:48 +00:00
|
|
|
|
|
|
|
for key, val := range info.Headers {
|
|
|
|
h.Set("X-"+key, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:46 +00:00
|
|
|
func (h *handler) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2021-06-24 11:10:00 +00:00
|
|
|
err error
|
|
|
|
inf *layer.ObjectInfo
|
|
|
|
params *layer.RangeParams
|
2020-08-19 23:36:46 +00:00
|
|
|
|
|
|
|
req = mux.Vars(r)
|
|
|
|
bkt = req["bucket"]
|
|
|
|
obj = req["object"]
|
|
|
|
rid = api.GetRequestID(r.Context())
|
|
|
|
)
|
|
|
|
|
2021-06-30 15:10:11 +00:00
|
|
|
args, err := parseGetObjectArgs(r.Header)
|
|
|
|
if err != nil {
|
2021-07-01 07:45:55 +00:00
|
|
|
writeError(w, r, h.log, "could not parse request params", rid, bkt, obj, err)
|
2021-06-30 15:10:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:36:46 +00:00
|
|
|
if inf, err = h.obj.GetObjectInfo(r.Context(), bkt, obj); err != nil {
|
2021-06-24 11:10:00 +00:00
|
|
|
writeError(w, r, h.log, "could not find object", rid, bkt, obj, err)
|
2020-08-19 23:36:46 +00:00
|
|
|
return
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
2021-06-30 15:10:11 +00:00
|
|
|
|
|
|
|
if !args.IfModifiedSince.IsZero() && inf.Created.Before(args.IfModifiedSince) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !args.IfUnmodifiedSince.IsZero() && inf.Created.After(args.IfUnmodifiedSince) {
|
|
|
|
w.WriteHeader(http.StatusPreconditionFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-24 11:10:00 +00:00
|
|
|
if params, err = fetchRangeHeader(r.Header, uint64(inf.Size)); err != nil {
|
|
|
|
writeError(w, r, h.log, "could not parse range header", rid, bkt, obj, err)
|
|
|
|
return
|
|
|
|
}
|
2021-01-14 17:39:48 +00:00
|
|
|
writeHeaders(w.Header(), inf)
|
2021-06-24 11:10:00 +00:00
|
|
|
if params != nil {
|
|
|
|
writeRangeHeaders(w, params, inf.Size)
|
|
|
|
}
|
2021-06-30 12:29:01 +00:00
|
|
|
|
|
|
|
getParams := &layer.GetObjectParams{
|
|
|
|
Bucket: inf.Bucket,
|
|
|
|
Object: inf.Name,
|
|
|
|
Writer: w,
|
|
|
|
Range: params,
|
|
|
|
}
|
|
|
|
if err = h.obj.GetObject(r.Context(), getParams); err != nil {
|
2021-06-30 09:29:43 +00:00
|
|
|
writeError(w, r, h.log, "could not get object", rid, bkt, obj, err)
|
|
|
|
}
|
2021-06-24 11:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 15:10:11 +00:00
|
|
|
func parseGetObjectArgs(headers http.Header) (*getObjectArgs, error) {
|
|
|
|
var err error
|
|
|
|
args := &getObjectArgs{}
|
|
|
|
|
2021-07-01 07:45:55 +00:00
|
|
|
if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.IfModifiedSince)); err != nil {
|
|
|
|
return nil, err
|
2021-06-30 15:10:11 +00:00
|
|
|
}
|
2021-07-01 07:45:55 +00:00
|
|
|
if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err != nil {
|
|
|
|
return nil, err
|
2021-06-30 15:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
2021-07-01 07:45:55 +00:00
|
|
|
func parseHTTPTime(data string) (time.Time, error) {
|
|
|
|
var result time.Time
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, err = time.Parse(http.TimeFormat, data); err != nil {
|
|
|
|
return result, fmt.Errorf("couldn't parse http time %s: %w", data, err)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-06-24 11:10:00 +00:00
|
|
|
func writeRangeHeaders(w http.ResponseWriter, params *layer.RangeParams, size int64) {
|
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
|
|
|
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", params.Start, params.End, size))
|
|
|
|
w.WriteHeader(http.StatusPartialContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeError(w http.ResponseWriter, r *http.Request, log *zap.Logger, msg, rid, bkt, obj string, err error) {
|
|
|
|
log.Error(msg,
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.String("bucket_name", bkt),
|
|
|
|
zap.String("object_name", obj),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
api.WriteErrorResponse(r.Context(), w, api.Error{
|
|
|
|
Code: api.GetAPIError(api.ErrInternalError).Code,
|
|
|
|
Description: err.Error(),
|
|
|
|
HTTPStatusCode: http.StatusInternalServerError,
|
|
|
|
}, r.URL)
|
2020-08-19 23:36:46 +00:00
|
|
|
}
|