Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
bugfix: parse the lastmodified header to timestamp
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <starnop@163.com>
  • Loading branch information
starnop committed Jun 18, 2019
1 parent bc5cc1d commit 6e3d4eb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
3 changes: 2 additions & 1 deletion common/util/http_util.go
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions common/util/net_util.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions common/util/net_util_test.go
Expand Up @@ -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")
}
5 changes: 1 addition & 4 deletions supernode/daemon/mgr/cdn/cache_detector.go
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions supernode/daemon/mgr/cdn/manager.go
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/md5"
"path"
"strconv"

"github.com/dragonflyoss/Dragonfly/apis/types"
cutil "github.com/dragonflyoss/Dragonfly/common/util"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 6e3d4eb

Please sign in to comment.