From be47263c422e3dc15d54d29ec39191419af29985 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Tue, 8 Nov 2022 09:55:13 +0300 Subject: [PATCH] [#214] Support the Date header on upload Signed-off-by: Denis Kirillov --- uploader/filter.go | 4 +--- uploader/filter_test.go | 2 +- uploader/upload.go | 13 ++++++++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/uploader/filter.go b/uploader/filter.go index e9e9395..e453cfe 100644 --- a/uploader/filter.go +++ b/uploader/filter.go @@ -70,7 +70,7 @@ func filterHeaders(l *zap.Logger, header *fasthttp.RequestHeader) map[string]str 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] 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) } - now := time.Now().UTC() if expTime.Before(now) { 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) - now := time.Now() if expTime.Before(now) { return fmt.Errorf("value %s of header %s must be in the future", timestamp, utils.ExpirationTimestampAttr) } diff --git a/uploader/filter_test.go b/uploader/filter_test.go index f1e300d..585fac8 100644 --- a/uploader/filter_test.go +++ b/uploader/filter_test.go @@ -178,7 +178,7 @@ func TestPrepareExpirationHeader(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 { require.Error(t, err) } else { diff --git a/uploader/upload.go b/uploader/upload.go index 2db7c34..8e65456 100644 --- a/uploader/upload.go +++ b/uploader/upload.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "net/http" "strconv" "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) 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)) response.Error(c, "could not parse expiration header: "+err.Error(), fasthttp.StatusBadRequest) return