Skip to content

Commit 3ee913e

Browse files
authored
Merge pull request dragonflyoss#617 from Starnop/last-modified-parse
bugfix: parse the lastmodified header to timestamp
2 parents 903e76c + b900393 commit 3ee913e

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

common/util/http_util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ func IsExpired(url string, headers map[string]string, lastModified int64, eTag s
274274
headers = make(map[string]string)
275275
}
276276
if lastModified > 0 {
277-
headers["If-Modified-Since"] = strconv.FormatInt(lastModified, 10)
277+
lastModifiedStr, _ := ConvertTimeIntToString(lastModified)
278+
headers["If-Modified-Since"] = lastModifiedStr
278279
}
279280
if !IsEmptyStr(eTag) {
280281
headers["If-None-Match"] = eTag

common/util/net_util.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ package util
1818

1919
import (
2020
"bufio"
21+
"fmt"
2122
"net"
23+
"net/http"
2224
"os"
2325
"os/exec"
2426
"regexp"
2527
"runtime"
2628
"strconv"
2729
"strings"
30+
"time"
2831

2932
log "github.com/sirupsen/logrus"
3033
)
3134

3235
const (
3336
separator = "&"
37+
layoutGMT = "GMT"
3438
)
3539

3640
var defaultRateLimit = "20M"
@@ -236,6 +240,24 @@ func GetAllIPs() (ipList []string, err error) {
236240
return
237241
}
238242

243+
// ConvertTimeStringToInt converts a string time to a int64 timestamp.
244+
func ConvertTimeStringToInt(timeStr string) (int64, error) {
245+
formatTime, err := time.ParseInLocation(http.TimeFormat, timeStr, time.UTC)
246+
if err != nil {
247+
return 0, err
248+
}
249+
250+
return formatTime.Unix() * int64(1000), nil
251+
}
252+
253+
// ConvertTimeIntToString converts a int64 timestamp to a string time.
254+
func ConvertTimeIntToString(timestamp int64) (string, error) {
255+
localTime := time.Unix(timestamp/int64(1000), 0)
256+
timeString := localTime.UTC().Format(http.TimeFormat)
257+
258+
return fmt.Sprintf("%s%s", timeString[:len(timeString)-3], layoutGMT), nil
259+
}
260+
239261
// slice2Map translate a slice to a map with
240262
// the value in slice as the key and true as the value.
241263
func slice2Map(value []string) map[string]bool {

common/util/net_util_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,17 @@ func (suite *UtilSuite) TestConvertHeaders(c *check.C) {
202202
c.Assert(headers, check.DeepEquals, v.e)
203203
}
204204
}
205+
206+
func (suite *UtilSuite) TestConvertTimeStringToInt(c *check.C) {
207+
timeStr := "Fri, 15 Jun 2018 14:40:41 GMT"
208+
result, err := ConvertTimeStringToInt(timeStr)
209+
c.Check(err, check.IsNil)
210+
c.Check(result, check.Equals, int64(1529073641000))
211+
}
212+
213+
func (suite *UtilSuite) TestConvertTimeIntToString(c *check.C) {
214+
timestamp := int64(1529073641000)
215+
result, err := ConvertTimeIntToString(timestamp)
216+
c.Check(err, check.IsNil)
217+
c.Check(result, check.Equals, "Fri, 15 Jun 2018 14:40:41 GMT")
218+
}

supernode/daemon/mgr/cdn/cache_detector.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,5 @@ func checkSameFile(task *types.TaskInfo, metaData *fileMetaData) bool {
124124
return metaData.Md5 == task.Md5
125125
}
126126

127-
if cutil.IsEmptyStr(metaData.Md5) {
128-
return metaData.Identifier == task.Identifier
129-
}
130-
return false
127+
return metaData.Identifier == task.Identifier
131128
}

supernode/daemon/mgr/cdn/manager.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"crypto/md5"
66
"path"
7-
"strconv"
87

98
"github.com/dragonflyoss/Dragonfly/apis/types"
109
cutil "github.com/dragonflyoss/Dragonfly/common/util"
@@ -166,7 +165,7 @@ func (cm *Manager) handleCDNResult(ctx context.Context, task *types.TaskInfo, re
166165
}
167166

168167
func (cm *Manager) updateLastModifiedAndETag(ctx context.Context, taskID, lastModified, eTag string) {
169-
lastModifiedInt, _ := strconv.ParseInt(lastModified, 10, 64)
168+
lastModifiedInt, _ := cutil.ConvertTimeStringToInt(lastModified)
170169
if err := cm.metaDataManager.updateLastModifiedAndETag(ctx, taskID, lastModifiedInt, eTag); err != nil {
171170
logrus.Errorf("failed to update LastModified(%s) and ETag(%s) for taskID %s: %v", lastModified, eTag, taskID, err)
172171
}

0 commit comments

Comments
 (0)