From 6e3d4ebc7a08c5b903d90f58c3b9e5ece5949097 Mon Sep 17 00:00:00 2001 From: Starnop Date: Tue, 18 Jun 2019 19:59:37 +0800 Subject: [PATCH] bugfix: parse the lastmodified header to timestamp Signed-off-by: Starnop --- common/util/http_util.go | 3 ++- common/util/net_util.go | 22 ++++++++++++++++++++++ common/util/net_util_test.go | 14 ++++++++++++++ supernode/daemon/mgr/cdn/cache_detector.go | 5 +---- supernode/daemon/mgr/cdn/manager.go | 3 +-- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/common/util/http_util.go b/common/util/http_util.go index 1025be867..7d27584dd 100644 --- a/common/util/http_util.go +++ b/common/util/http_util.go @@ -274,7 +274,8 @@ func IsExpired(url string, headers map[string]string, lastModified int64, eTag s headers = make(map[string]string) } if lastModified > 0 { - headers["If-Modified-Since"] = strconv.FormatInt(lastModified, 10) + lastModifiedStr, _ := ConvertTimeIntToString(lastModified) + headers["If-Modified-Since"] = lastModifiedStr } if !IsEmptyStr(eTag) { headers["If-None-Match"] = eTag diff --git a/common/util/net_util.go b/common/util/net_util.go index d23debf58..f6b01f808 100644 --- a/common/util/net_util.go +++ b/common/util/net_util.go @@ -18,19 +18,23 @@ package util import ( "bufio" + "fmt" "net" + "net/http" "os" "os/exec" "regexp" "runtime" "strconv" "strings" + "time" log "github.com/sirupsen/logrus" ) const ( separator = "&" + layoutGMT = "GMT" ) var defaultRateLimit = "20M" @@ -236,6 +240,24 @@ func GetAllIPs() (ipList []string, err error) { return } +// ConvertTimeStringToInt converts a string time to a int64 timestamp. +func ConvertTimeStringToInt(timeStr string) (int64, error) { + formatTime, err := time.ParseInLocation(http.TimeFormat, timeStr, time.UTC) + if err != nil { + return 0, err + } + + return formatTime.Unix() * int64(1000), nil +} + +// ConvertTimeIntToString converts a int64 timestamp to a string time. +func ConvertTimeIntToString(timestamp int64) (string, error) { + localTime := time.Unix(timestamp/int64(1000), 0) + timeString := localTime.UTC().Format(http.TimeFormat) + + return fmt.Sprintf("%s%s", timeString[:len(timeString)-3], layoutGMT), nil +} + // slice2Map translate a slice to a map with // the value in slice as the key and true as the value. func slice2Map(value []string) map[string]bool { diff --git a/common/util/net_util_test.go b/common/util/net_util_test.go index 3a5b5badd..a0c283674 100644 --- a/common/util/net_util_test.go +++ b/common/util/net_util_test.go @@ -202,3 +202,17 @@ func (suite *UtilSuite) TestConvertHeaders(c *check.C) { c.Assert(headers, check.DeepEquals, v.e) } } + +func (suite *UtilSuite) TestConvertTimeStringToInt(c *check.C) { + timeStr := "Fri, 15 Jun 2018 14:40:41 GMT" + result, err := ConvertTimeStringToInt(timeStr) + c.Check(err, check.IsNil) + c.Check(result, check.Equals, int64(1529073641000)) +} + +func (suite *UtilSuite) TestConvertTimeIntToString(c *check.C) { + timestamp := int64(1529073641000) + result, err := ConvertTimeIntToString(timestamp) + c.Check(err, check.IsNil) + c.Check(result, check.Equals, "Fri, 15 Jun 2018 14:40:41 GMT") +} diff --git a/supernode/daemon/mgr/cdn/cache_detector.go b/supernode/daemon/mgr/cdn/cache_detector.go index 4720390a3..20cf38fb4 100644 --- a/supernode/daemon/mgr/cdn/cache_detector.go +++ b/supernode/daemon/mgr/cdn/cache_detector.go @@ -124,8 +124,5 @@ func checkSameFile(task *types.TaskInfo, metaData *fileMetaData) bool { return metaData.Md5 == task.Md5 } - if cutil.IsEmptyStr(metaData.Md5) { - return metaData.Identifier == task.Identifier - } - return false + return metaData.Identifier == task.Identifier } diff --git a/supernode/daemon/mgr/cdn/manager.go b/supernode/daemon/mgr/cdn/manager.go index 6ce30d89f..528f26577 100644 --- a/supernode/daemon/mgr/cdn/manager.go +++ b/supernode/daemon/mgr/cdn/manager.go @@ -4,7 +4,6 @@ import ( "context" "crypto/md5" "path" - "strconv" "github.com/dragonflyoss/Dragonfly/apis/types" cutil "github.com/dragonflyoss/Dragonfly/common/util" @@ -166,7 +165,7 @@ func (cm *Manager) handleCDNResult(ctx context.Context, task *types.TaskInfo, re } func (cm *Manager) updateLastModifiedAndETag(ctx context.Context, taskID, lastModified, eTag string) { - lastModifiedInt, _ := strconv.ParseInt(lastModified, 10, 64) + lastModifiedInt, _ := cutil.ConvertTimeStringToInt(lastModified) if err := cm.metaDataManager.updateLastModifiedAndETag(ctx, taskID, lastModifiedInt, eTag); err != nil { logrus.Errorf("failed to update LastModified(%s) and ETag(%s) for taskID %s: %v", lastModified, eTag, taskID, err) }