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-02-08 16:24:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object/address"
|
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-02-08 16:24:23 +00:00
|
|
|
func (r request) headObject(clnt pool.Object, objectAddress *address.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
|
|
|
|
}
|
|
|
|
|
|
|
|
bearerOpt := bearerOpts(r.RequestCtx)
|
2022-02-08 16:24:23 +00:00
|
|
|
obj, err := clnt.HeadObject(r.RequestCtx, *objectAddress, bearerOpt)
|
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) {
|
|
|
|
return clnt.ObjectRange(r.RequestCtx, *objectAddress, 0, sz, bearerOpt)
|
|
|
|
})
|
|
|
|
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) {
|
|
|
|
resp.Header.Set(hdrObjectID, obj.ID().String())
|
|
|
|
resp.Header.Set(hdrOwnerID, obj.OwnerID().String())
|
|
|
|
resp.Header.Set(hdrContainerID, obj.ContainerID().String())
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|