[#214] Support the Date header on upload

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-11-08 09:55:13 +03:00 committed by Alex Vanin
parent 9dad47502e
commit be47263c42
3 changed files with 14 additions and 5 deletions

View file

@ -70,7 +70,7 @@ func filterHeaders(l *zap.Logger, header *fasthttp.RequestHeader) map[string]str
return result return result
} }
func prepareExpirationHeader(headers map[string]string, epochDurations *epochDurations) error { func prepareExpirationHeader(headers map[string]string, epochDurations *epochDurations, now time.Time) error {
expirationInEpoch := headers[object.SysAttributeExpEpoch] expirationInEpoch := headers[object.SysAttributeExpEpoch]
if timeRFC3339, ok := headers[utils.ExpirationRFC3339Attr]; ok { if timeRFC3339, ok := headers[utils.ExpirationRFC3339Attr]; ok {
@ -79,7 +79,6 @@ func prepareExpirationHeader(headers map[string]string, epochDurations *epochDur
return fmt.Errorf("couldn't parse value %s of header %s", timeRFC3339, utils.ExpirationRFC3339Attr) return fmt.Errorf("couldn't parse value %s of header %s", timeRFC3339, utils.ExpirationRFC3339Attr)
} }
now := time.Now().UTC()
if expTime.Before(now) { if expTime.Before(now) {
return fmt.Errorf("value %s of header %s must be in the future", timeRFC3339, utils.ExpirationRFC3339Attr) return fmt.Errorf("value %s of header %s must be in the future", timeRFC3339, utils.ExpirationRFC3339Attr)
} }
@ -94,7 +93,6 @@ func prepareExpirationHeader(headers map[string]string, epochDurations *epochDur
} }
expTime := time.Unix(value, 0) expTime := time.Unix(value, 0)
now := time.Now()
if expTime.Before(now) { if expTime.Before(now) {
return fmt.Errorf("value %s of header %s must be in the future", timestamp, utils.ExpirationTimestampAttr) return fmt.Errorf("value %s of header %s must be in the future", timestamp, utils.ExpirationTimestampAttr)
} }

View file

@ -178,7 +178,7 @@ func TestPrepareExpirationHeader(t *testing.T) {
}, },
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
err := prepareExpirationHeader(tc.headers, tc.durations) err := prepareExpirationHeader(tc.headers, tc.durations, time.Now())
if tc.err { if tc.err {
require.Error(t, err) require.Error(t, err)
} else { } else {

View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/http"
"strconv" "strconv"
"time" "time"
@ -121,7 +122,17 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) {
response.Error(c, "could not get epoch durations from network info: "+err.Error(), fasthttp.StatusBadRequest) response.Error(c, "could not get epoch durations from network info: "+err.Error(), fasthttp.StatusBadRequest)
return return
} }
if err = prepareExpirationHeader(filtered, epochDuration); err != nil {
now := time.Now()
if rawHeader := c.Request.Header.Peek(fasthttp.HeaderDate); rawHeader != nil {
if parsed, err := time.Parse(http.TimeFormat, string(rawHeader)); err != nil {
log.Warn("could not parse client time", zap.String("Date header", string(rawHeader)), zap.Error(err))
} else {
now = parsed
}
}
if err = prepareExpirationHeader(filtered, epochDuration, now); err != nil {
log.Error("could not parse expiration header", zap.Error(err)) log.Error("could not parse expiration header", zap.Error(err))
response.Error(c, "could not parse expiration header: "+err.Error(), fasthttp.StatusBadRequest) response.Error(c, "could not parse expiration header: "+err.Error(), fasthttp.StatusBadRequest)
return return