2023-08-31 08:37:03 +00:00
|
|
|
package handler
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-16 15:38:30 +00:00
|
|
|
"context"
|
2021-01-25 19:36:46 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2022-11-08 06:55:13 +00:00
|
|
|
"net/http"
|
2021-01-25 19:36:46 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2023-08-27 15:09:02 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/response"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/tokens"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/utils"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
"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-01-25 19:36:46 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-04-28 08:39:12 +00:00
|
|
|
const (
|
|
|
|
jsonHeader = "application/json; charset=UTF-8"
|
|
|
|
drainBufSize = 4096
|
|
|
|
)
|
2021-04-05 14:48:01 +00:00
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
type putResponse struct {
|
|
|
|
ObjectID string `json:"object_id"`
|
|
|
|
ContainerID string `json:"container_id"`
|
2022-09-09 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
func newPutResponse(addr oid.Address) *putResponse {
|
|
|
|
return &putResponse{
|
|
|
|
ObjectID: addr.Object().EncodeToString(),
|
|
|
|
ContainerID: addr.Container().EncodeToString(),
|
|
|
|
}
|
2022-09-09 06:33:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
func (pr *putResponse) encode(w io.Writer) error {
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", "\t")
|
|
|
|
return enc.Encode(pr)
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Upload handles multipart upload request.
|
2023-08-31 08:37:03 +00:00
|
|
|
func (h *Handler) Upload(req *fasthttp.RequestCtx) {
|
2021-01-25 19:36:46 +00:00
|
|
|
var (
|
2021-04-28 08:39:12 +00:00
|
|
|
file MultipartFile
|
2022-08-22 15:00:13 +00:00
|
|
|
idObj oid.ID
|
2022-07-25 09:47:48 +00:00
|
|
|
addr oid.Address
|
2023-05-24 09:19:15 +00:00
|
|
|
scid, _ = req.UserValue("cid").(string)
|
2023-08-31 08:37:03 +00:00
|
|
|
log = h.log.With(zap.String("cid", scid))
|
2023-05-24 09:19:15 +00:00
|
|
|
bodyStream = req.RequestBodyStream()
|
2021-04-28 08:39:12 +00:00
|
|
|
drainBuf = make([]byte, drainBufSize)
|
2021-01-25 19:36:46 +00:00
|
|
|
)
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2023-05-30 14:01:20 +00:00
|
|
|
ctx := utils.GetContextFromRequest(req)
|
2023-05-24 09:19:15 +00:00
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
idCnr, err := h.getContainerID(ctx, scid)
|
2022-04-20 09:17:20 +00:00
|
|
|
if err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
log.Error(logs.WrongContainerID, zap.Error(err))
|
2023-05-24 09:19:15 +00:00
|
|
|
response.Error(req, "wrong container id", fasthttp.StatusBadRequest)
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2021-01-25 19:36:46 +00:00
|
|
|
defer func() {
|
2021-04-07 12:54:30 +00:00
|
|
|
// If the temporary reader can be closed - let's close it.
|
2021-02-13 16:17:01 +00:00
|
|
|
if file == nil {
|
2021-01-26 08:35:05 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
err := file.Close()
|
|
|
|
log.Debug(
|
2023-08-27 15:09:02 +00:00
|
|
|
logs.CloseTemporaryMultipartFormFile,
|
2021-02-13 16:17:01 +00:00
|
|
|
zap.Stringer("address", addr),
|
|
|
|
zap.String("filename", file.FileName()),
|
2021-03-30 22:46:33 +00:00
|
|
|
zap.Error(err),
|
|
|
|
)
|
2021-01-25 19:36:46 +00:00
|
|
|
}()
|
2023-05-24 09:19:15 +00:00
|
|
|
boundary := string(req.Request.Header.MultipartFormBoundary())
|
2023-08-31 08:37:03 +00:00
|
|
|
if file, err = fetchMultipartFile(h.log, bodyStream, boundary); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
log.Error(logs.CouldNotReceiveMultipartForm, zap.Error(err))
|
2023-05-24 09:19:15 +00:00
|
|
|
response.Error(req, "could not receive multipart/form: "+err.Error(), fasthttp.StatusBadRequest)
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
2023-08-31 08:37:03 +00:00
|
|
|
filtered, err := filterHeaders(h.log, &req.Request.Header)
|
2022-11-17 15:01:38 +00:00
|
|
|
if err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
log.Error(logs.CouldNotProcessHeaders, zap.Error(err))
|
2023-05-24 09:19:15 +00:00
|
|
|
response.Error(req, err.Error(), fasthttp.StatusBadRequest)
|
2022-11-17 15:01:38 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-08 06:55:13 +00:00
|
|
|
|
2023-03-16 08:40:08 +00:00
|
|
|
now := time.Now()
|
2023-05-24 09:19:15 +00:00
|
|
|
if rawHeader := req.Request.Header.Peek(fasthttp.HeaderDate); rawHeader != nil {
|
2023-03-16 08:40:08 +00:00
|
|
|
if parsed, err := time.Parse(http.TimeFormat, string(rawHeader)); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
log.Warn(logs.CouldNotParseClientTime, zap.String("Date header", string(rawHeader)), zap.Error(err))
|
2023-03-16 08:40:08 +00:00
|
|
|
} else {
|
|
|
|
now = parsed
|
2022-11-08 06:55:13 +00:00
|
|
|
}
|
2023-03-16 08:40:08 +00:00
|
|
|
}
|
2022-11-08 06:55:13 +00:00
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
if err = utils.PrepareExpirationHeader(req, h.pool, filtered, now); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
log.Error(logs.CouldNotPrepareExpirationHeader, zap.Error(err))
|
2023-05-24 09:19:15 +00:00
|
|
|
response.Error(req, "could not prepare expiration header: "+err.Error(), fasthttp.StatusBadRequest)
|
2023-03-16 08:40:08 +00:00
|
|
|
return
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 08:10:56 +00:00
|
|
|
attributes := make([]object.Attribute, 0, len(filtered))
|
2021-01-26 15:36:53 +00:00
|
|
|
// prepares attributes from filtered headers
|
2021-01-25 19:36:46 +00:00
|
|
|
for key, val := range filtered {
|
|
|
|
attribute := object.NewAttribute()
|
|
|
|
attribute.SetKey(key)
|
|
|
|
attribute.SetValue(val)
|
2022-03-16 08:10:56 +00:00
|
|
|
attributes = append(attributes, *attribute)
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
2021-01-26 15:36:53 +00:00
|
|
|
// sets FileName attribute if it wasn't set from header
|
2021-01-28 13:48:45 +00:00
|
|
|
if _, ok := filtered[object.AttributeFileName]; !ok {
|
2021-01-25 19:36:46 +00:00
|
|
|
filename := object.NewAttribute()
|
|
|
|
filename.SetKey(object.AttributeFileName)
|
2021-02-13 16:17:01 +00:00
|
|
|
filename.SetValue(file.FileName())
|
2022-03-16 08:10:56 +00:00
|
|
|
attributes = append(attributes, *filename)
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
2021-01-26 15:36:53 +00:00
|
|
|
// sets Timestamp attribute if it wasn't set from header and enabled by settings
|
2023-09-05 15:17:22 +00:00
|
|
|
if _, ok := filtered[object.AttributeTimestamp]; !ok && h.config.DefaultTimestamp() {
|
2021-01-25 19:36:46 +00:00
|
|
|
timestamp := object.NewAttribute()
|
|
|
|
timestamp.SetKey(object.AttributeTimestamp)
|
|
|
|
timestamp.SetValue(strconv.FormatInt(time.Now().Unix(), 10))
|
2022-03-16 08:10:56 +00:00
|
|
|
attributes = append(attributes, *timestamp)
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
2021-05-28 08:57:28 +00:00
|
|
|
|
2022-03-02 10:46:11 +00:00
|
|
|
obj := object.New()
|
2022-04-19 15:46:51 +00:00
|
|
|
obj.SetContainerID(*idCnr)
|
2023-08-31 08:37:03 +00:00
|
|
|
obj.SetOwnerID(h.ownerID)
|
2022-03-02 10:46:11 +00:00
|
|
|
obj.SetAttributes(attributes...)
|
2021-05-28 08:57:28 +00:00
|
|
|
|
2022-04-07 12:56:18 +00:00
|
|
|
var prm pool.PrmObjectPut
|
|
|
|
prm.SetHeader(*obj)
|
|
|
|
prm.SetPayload(file)
|
2023-08-21 13:50:23 +00:00
|
|
|
prm.SetClientCut(h.config.ClientCut())
|
2022-04-19 15:46:51 +00:00
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
bt := h.fetchBearerToken(ctx)
|
2022-04-19 15:46:51 +00:00
|
|
|
if bt != nil {
|
|
|
|
prm.UseBearer(*bt)
|
|
|
|
}
|
2022-04-07 12:56:18 +00:00
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
if idObj, err = h.pool.PutObject(ctx, prm); err != nil {
|
|
|
|
h.handlePutFrostFSErr(req, err)
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
2021-05-28 08:57:28 +00:00
|
|
|
|
2022-08-22 15:00:13 +00:00
|
|
|
addr.SetObject(idObj)
|
2022-07-25 09:47:48 +00:00
|
|
|
addr.SetContainer(*idCnr)
|
2021-05-28 08:57:28 +00:00
|
|
|
|
2021-03-31 18:24:41 +00:00
|
|
|
// Try to return the response, otherwise, if something went wrong, throw an error.
|
2023-05-24 09:19:15 +00:00
|
|
|
if err = newPutResponse(addr).encode(req); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
log.Error(logs.CouldNotEncodeResponse, zap.Error(err))
|
2023-05-24 09:19:15 +00:00
|
|
|
response.Error(req, "could not encode response", fasthttp.StatusBadRequest)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-04-28 08:39:12 +00:00
|
|
|
// Multipart is multipart and thus can contain more than one part which
|
|
|
|
// we ignore at the moment. Also, when dealing with chunked encoding
|
|
|
|
// the last zero-length chunk might be left unread (because multipart
|
|
|
|
// reader only cares about its boundary and doesn't look further) and
|
|
|
|
// it will be (erroneously) interpreted as the start of the next
|
|
|
|
// pipelined header. Thus we need to drain the body buffer.
|
|
|
|
for {
|
|
|
|
_, err = bodyStream.Read(drainBuf)
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 18:24:41 +00:00
|
|
|
// Report status code and content type.
|
2023-05-24 09:19:15 +00:00
|
|
|
req.Response.SetStatusCode(fasthttp.StatusOK)
|
|
|
|
req.Response.Header.SetContentType(jsonHeader)
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
2021-03-31 16:58:42 +00:00
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
func (h *Handler) handlePutFrostFSErr(r *fasthttp.RequestCtx, err error) {
|
2023-03-13 11:55:19 +00:00
|
|
|
statusCode, msg, additionalFields := response.FormErrorResponse("could not store file in frostfs", err)
|
|
|
|
logFields := append([]zap.Field{zap.Error(err)}, additionalFields...)
|
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
h.log.Error(logs.CouldNotStoreFileInFrostfs, logFields...)
|
2023-03-13 11:55:19 +00:00
|
|
|
response.Error(r, msg, statusCode)
|
|
|
|
}
|
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
func (h *Handler) fetchBearerToken(ctx context.Context) *bearer.Token {
|
2021-05-28 08:57:28 +00:00
|
|
|
if tkn, err := tokens.LoadBearerToken(ctx); err == nil && tkn != nil {
|
2023-08-07 09:08:34 +00:00
|
|
|
return tkn
|
2021-03-31 16:58:42 +00:00
|
|
|
}
|
2023-08-07 09:08:34 +00:00
|
|
|
return nil
|
2021-03-31 16:58:42 +00:00
|
|
|
}
|