From 8b367e01f9433e383c39ba1121c9e97d1909667f Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Tue, 26 Mar 2024 11:42:08 +0300 Subject: [PATCH] Use mtime instead of ctime To be able to get same ETags on different servers. --- go.mod | 1 - go.sum | 2 -- godlighty.go | 2 +- handler.go | 4 ++-- ctime.go => mtime.go | 16 +++++++--------- 5 files changed, 10 insertions(+), 15 deletions(-) rename ctime.go => mtime.go (75%) diff --git a/go.mod b/go.mod index 2278137..5eefacf 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,4 @@ require ( github.com/dustin/go-humanize v1.0.1 github.com/klauspost/compress v1.17.7 golang.org/x/net v0.19.0 - golang.org/x/sys v0.15.0 ) diff --git a/go.sum b/go.sum index 4dcbfbc..8693e67 100644 --- a/go.sum +++ b/go.sum @@ -6,5 +6,3 @@ github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLA github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/godlighty.go b/godlighty.go index 8b141c6..ee2ff75 100644 --- a/godlighty.go +++ b/godlighty.go @@ -1,6 +1,6 @@ // Highly-customizable HTTP, HTTP/2, HTTPS server package godlighty -const Version = "godlighty/0.6.0" +const Version = "godlighty/0.7.0" var BindAddr string diff --git a/handler.go b/handler.go index 354b505..10b0d2f 100644 --- a/handler.go +++ b/handler.go @@ -194,7 +194,7 @@ IndexLookuped: http.Error(w, "internal error", http.StatusInternalServerError) return } - etag, err = ctimeETag(fd) + etag, err = mtimeETag(fd) fd.Close() if err != nil { printErr(http.StatusInternalServerError, err) @@ -240,7 +240,7 @@ IndexLookuped: http.Error(w, "internal error", http.StatusInternalServerError) return } - etag, err = ctimeETag(fd) + etag, err = mtimeETag(fd) if err != nil { printErr(http.StatusInternalServerError, err) http.Error(w, "internal error", http.StatusInternalServerError) diff --git a/ctime.go b/mtime.go similarity index 75% rename from ctime.go rename to mtime.go index 72047e6..35afa48 100644 --- a/ctime.go +++ b/mtime.go @@ -19,19 +19,17 @@ import ( "encoding/base64" "encoding/binary" "os" - - "golang.org/x/sys/unix" ) -func ctimeETag(fd *os.File) (string, error) { - var stat unix.Stat_t - err := unix.Fstat(int(fd.Fd()), &stat) +func mtimeETag(fd *os.File) (string, error) { + fi, err := fd.Stat() if err != nil { return "", err } - sec, nsec := stat.Ctim.Unix() - buf := make([]byte, 8*2) - binary.BigEndian.PutUint64(buf[:8], uint64(sec)) - binary.BigEndian.PutUint64(buf[8:], uint64(nsec)) + mtime := fi.ModTime() + buf := make([]byte, 8*3) + binary.BigEndian.PutUint64(buf[:8], uint64(fi.Size())) + binary.BigEndian.PutUint64(buf[8:16], uint64(mtime.Unix())) + binary.BigEndian.PutUint64(buf[16:], uint64(mtime.Nanosecond())) return `"` + base64.RawURLEncoding.EncodeToString(buf) + `"`, nil } -- 2.44.0