2021-07-15 07:57:22 +00:00
|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
2022-02-08 16:24:23 +00:00
|
|
|
"io"
|
2021-07-15 07:57:22 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-11-12 11:37:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gw/response"
|
2021-07-15 07:57:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gw/tokens"
|
2021-11-30 07:22:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gw/utils"
|
2021-11-15 11:12:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-07-25 09:47:48 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-15 11:12:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
2021-07-15 07:57:22 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-03-04 06:53:49 +00:00
|
|
|
// max bytes needed to detect content type according to http.DetectContentType docs.
|
2021-07-15 07:57:22 +00:00
|
|
|
const sizeToDetectType = 512
|
|
|
|
|
2022-02-15 08:27:51 +00:00
|
|
|
const (
|
|
|
|
hdrObjectID = "X-Object-Id"
|
|
|
|
hdrOwnerID = "X-Owner-Id"
|
|
|
|
hdrContainerID = "X-Container-Id"
|
|
|
|
)
|
|
|
|
|
2022-07-25 09:47:48 +00:00
|
|
|
func (r request) headObject(clnt *pool.Pool, objectAddress oid.Address) {
|
2021-07-15 07:57:22 +00:00
|
|
|
var start = time.Now()
|
|
|
|
if err := tokens.StoreBearerToken(r.RequestCtx); err != nil {
|
|
|
|
r.log.Error("could not fetch and store bearer token", zap.Error(err))
|
2021-11-12 11:37:05 +00:00
|
|
|
response.Error(r.RequestCtx, "could not fetch and store bearer token", fasthttp.StatusBadRequest)
|
2021-07-15 07:57:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:56:18 +00:00
|
|
|
btoken := bearerToken(r.RequestCtx)
|
|
|
|
|
|
|
|
var prm pool.PrmObjectHead
|
2022-07-25 09:47:48 +00:00
|
|
|
prm.SetAddress(objectAddress)
|
2022-04-19 15:46:51 +00:00
|
|
|
if btoken != nil {
|
|
|
|
prm.UseBearer(*btoken)
|
|
|
|
}
|
2022-04-07 12:56:18 +00:00
|
|
|
|
2022-04-21 15:04:41 +00:00
|
|
|
obj, err := clnt.HeadObject(r.appCtx, prm)
|
2021-07-15 07:57:22 +00:00
|
|
|
if err != nil {
|
|
|
|
r.handleNeoFSErr(err, start)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-15 08:27:51 +00:00
|
|
|
r.Response.Header.Set(fasthttp.HeaderContentLength, strconv.FormatUint(obj.PayloadSize(), 10))
|
2021-07-15 07:57:22 +00:00
|
|
|
var contentType string
|
|
|
|
for _, attr := range obj.Attributes() {
|
|
|
|
key := attr.Key()
|
|
|
|
val := attr.Value()
|
|
|
|
if !isValidToken(key) || !isValidValue(val) {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-30 07:22:05 +00:00
|
|
|
r.Response.Header.Set(utils.UserAttributeHeaderPrefix+key, val)
|
2021-07-15 07:57:22 +00:00
|
|
|
switch key {
|
|
|
|
case object.AttributeTimestamp:
|
|
|
|
value, err := strconv.ParseInt(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
r.log.Info("couldn't parse creation date",
|
|
|
|
zap.String("key", key),
|
|
|
|
zap.String("val", val),
|
|
|
|
zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2022-02-15 08:27:51 +00:00
|
|
|
r.Response.Header.Set(fasthttp.HeaderLastModified, time.Unix(value, 0).UTC().Format(http.TimeFormat))
|
2021-07-15 07:57:22 +00:00
|
|
|
case object.AttributeContentType:
|
|
|
|
contentType = val
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 09:13:43 +00:00
|
|
|
|
|
|
|
idsToResponse(&r.Response, obj)
|
2021-07-15 07:57:22 +00:00
|
|
|
|
|
|
|
if len(contentType) == 0 {
|
2022-03-04 06:53:49 +00:00
|
|
|
contentType, _, err = readContentType(obj.PayloadSize(), func(sz uint64) (io.Reader, error) {
|
2022-04-07 12:56:18 +00:00
|
|
|
var prmRange pool.PrmObjectRange
|
2022-07-25 09:47:48 +00:00
|
|
|
prmRange.SetAddress(objectAddress)
|
2022-04-07 12:56:18 +00:00
|
|
|
prmRange.SetLength(sz)
|
2022-04-19 15:46:51 +00:00
|
|
|
if btoken != nil {
|
|
|
|
prmRange.UseBearer(*btoken)
|
|
|
|
}
|
2022-04-07 12:56:18 +00:00
|
|
|
|
2022-04-21 15:04:41 +00:00
|
|
|
return clnt.ObjectRange(r.appCtx, prmRange)
|
2022-03-04 06:53:49 +00:00
|
|
|
})
|
|
|
|
if err != nil && err != io.EOF {
|
2021-07-15 07:57:22 +00:00
|
|
|
r.handleNeoFSErr(err, start)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.SetContentType(contentType)
|
|
|
|
}
|
|
|
|
|
2022-02-15 09:13:43 +00:00
|
|
|
func idsToResponse(resp *fasthttp.Response, obj *object.Object) {
|
2022-04-19 15:46:51 +00:00
|
|
|
objID, _ := obj.ID()
|
|
|
|
cnrID, _ := obj.ContainerID()
|
|
|
|
resp.Header.Set(hdrObjectID, objID.String())
|
2022-02-15 09:13:43 +00:00
|
|
|
resp.Header.Set(hdrOwnerID, obj.OwnerID().String())
|
2022-04-19 15:46:51 +00:00
|
|
|
resp.Header.Set(hdrContainerID, cnrID.String())
|
2022-02-15 09:13:43 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 07:57:22 +00:00
|
|
|
// HeadByAddress handles head requests using simple cid/oid format.
|
|
|
|
func (d *Downloader) HeadByAddress(c *fasthttp.RequestCtx) {
|
|
|
|
d.byAddress(c, request.headObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadByAttribute handles attribute-based head requests.
|
|
|
|
func (d *Downloader) HeadByAttribute(c *fasthttp.RequestCtx) {
|
|
|
|
d.byAttribute(c, request.headObject)
|
|
|
|
}
|