2021-07-15 07:57:22 +00:00
|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
2023-03-15 08:07:44 +00:00
|
|
|
"context"
|
2022-02-08 16:24:23 +00:00
|
|
|
"io"
|
2021-07-15 07:57:22 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/utils"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-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"
|
|
|
|
)
|
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
func headObject(ctx context.Context, req request, clnt *pool.Pool, objectAddress oid.Address) {
|
2021-07-15 07:57:22 +00:00
|
|
|
var start = time.Now()
|
|
|
|
|
2023-05-30 14:01:20 +00:00
|
|
|
btoken := bearerToken(ctx)
|
2022-04-07 12:56:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
obj, err := clnt.HeadObject(ctx, prm)
|
2021-07-15 07:57:22 +00:00
|
|
|
if err != nil {
|
2023-03-15 08:07:44 +00:00
|
|
|
req.handleFrostFSErr(err, start)
|
2021-07-15 07:57:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
req.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
|
|
|
|
}
|
2023-03-16 08:40:08 +00:00
|
|
|
|
|
|
|
key = utils.BackwardTransformIfSystem(key)
|
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
req.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 {
|
2023-03-15 08:07:44 +00:00
|
|
|
req.log.Info("couldn't parse creation date",
|
2021-07-15 07:57:22 +00:00
|
|
|
zap.String("key", key),
|
|
|
|
zap.String("val", val),
|
|
|
|
zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2023-03-15 08:07:44 +00:00
|
|
|
req.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
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
idsToResponse(&req.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
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
resObj, err := clnt.ObjectRange(ctx, prmRange)
|
2022-08-22 15:00:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &resObj, nil
|
2022-03-04 06:53:49 +00:00
|
|
|
})
|
|
|
|
if err != nil && err != io.EOF {
|
2023-03-15 08:07:44 +00:00
|
|
|
req.handleFrostFSErr(err, start)
|
2021-07-15 07:57:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 08:07:44 +00:00
|
|
|
req.SetContentType(contentType)
|
2021-07-15 07:57:22 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-05 08:19:35 +00:00
|
|
|
// HeadByAddressOrBucketName handles head requests using simple cid/oid or bucketname/key format.
|
|
|
|
func (d *Downloader) HeadByAddressOrBucketName(c *fasthttp.RequestCtx) {
|
|
|
|
test, _ := c.UserValue("oid").(string)
|
|
|
|
var id oid.ID
|
|
|
|
|
|
|
|
err := id.DecodeString(test)
|
|
|
|
if err != nil {
|
|
|
|
d.byBucketname(c, headObject)
|
|
|
|
} else {
|
|
|
|
d.byAddress(c, headObject)
|
|
|
|
}
|
2021-07-15 07:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadByAttribute handles attribute-based head requests.
|
|
|
|
func (d *Downloader) HeadByAttribute(c *fasthttp.RequestCtx) {
|
2023-03-15 08:07:44 +00:00
|
|
|
d.byAttribute(c, headObject)
|
2021-07-15 07:57:22 +00:00
|
|
|
}
|