Skip to content

Commit

Permalink
support weak etag
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Nov 23, 2016
1 parent 44871ef commit 847ce5b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
14 changes: 10 additions & 4 deletions etag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// Version is this package's version.
const Version = "0.1.0"
const Version = "0.2.0"

type hashWriter struct {
rw http.ResponseWriter
Expand Down Expand Up @@ -51,7 +51,7 @@ func writeRaw(res http.ResponseWriter, hw hashWriter) {
}

// Handler wraps the http.Handler h with ETag support.
func Handler(h http.Handler) http.Handler {
func Handler(h http.Handler, weak bool) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
hw := hashWriter{rw: res, hash: sha1.New(), buf: bytes.NewBuffer(nil)}
h.ServeHTTP(&hw, req)
Expand All @@ -66,8 +66,14 @@ func Handler(h http.Handler) http.Handler {
return
}

resHeader.Set(headers.ETag, fmt.Sprintf("%v-%v", strconv.Itoa(hw.len),
hex.EncodeToString(hw.hash.Sum(nil))))
etag := fmt.Sprintf("%v-%v", strconv.Itoa(hw.len),
hex.EncodeToString(hw.hash.Sum(nil)))

if weak {
etag = "w/" + etag
}

resHeader.Set(headers.ETag, etag)

if fresh.IsFresh(req.Header, resHeader) {
res.WriteHeader(http.StatusNotModified)
Expand Down
24 changes: 21 additions & 3 deletions etag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type EmptyEtagSuite struct {

func (s *EmptyEtagSuite) SetupTest() {
mux := http.NewServeMux()
mux.Handle("/", Handler(http.HandlerFunc(emptyHandlerFunc)))
mux.Handle("/", Handler(http.HandlerFunc(emptyHandlerFunc), false))

s.server = httptest.NewServer(mux)
}
Expand All @@ -44,14 +44,20 @@ func TestEmptyEtag(t *testing.T) {
type EtagSuite struct {
suite.Suite

server *httptest.Server
server *httptest.Server
weakServer *httptest.Server
}

func (s *EtagSuite) SetupTest() {
mux := http.NewServeMux()
mux.Handle("/", Handler(http.HandlerFunc(handlerFunc)))
mux.Handle("/", Handler(http.HandlerFunc(handlerFunc), false))

s.server = httptest.NewServer(mux)

wmux := http.NewServeMux()
wmux.Handle("/", Handler(http.HandlerFunc(handlerFunc), true))

s.weakServer = httptest.NewServer(wmux)
}

func (s EtagSuite) TestEtagExists() {
Expand All @@ -66,6 +72,18 @@ func (s EtagSuite) TestEtagExists() {
s.Equal(fmt.Sprintf("%v-%v", len(testStrBytes), hex.EncodeToString(h.Sum(nil))), res.Header.Get(headers.ETag))
}

func (s EtagSuite) TestWeakEtagExists() {
res, err := http.Get(s.weakServer.URL + "/")

s.Nil(err)
s.Equal(http.StatusOK, res.StatusCode)

h := sha1.New()
h.Write(testStrBytes)

s.Equal(fmt.Sprintf("w/%v-%v", len(testStrBytes), hex.EncodeToString(h.Sum(nil))), res.Header.Get(headers.ETag))
}

func (s EtagSuite) TestMatch() {
req, err := http.NewRequest(http.MethodGet, s.server.URL+"/", nil)
s.Nil(err)
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ func Example() {
res.Write([]byte("Hello World"))
})

http.ListenAndServe(":8080", etag.Handler(mux))
http.ListenAndServe(":8080", etag.Handler(mux, false))
}

0 comments on commit 847ce5b

Please sign in to comment.