Skip to content

Commit

Permalink
Add basic ETag support to cinodefs web interface
Browse files Browse the repository at this point in the history
  • Loading branch information
byo committed Dec 10, 2023
1 parent 683fa92 commit 3397ab6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
19 changes: 19 additions & 0 deletions pkg/cinodefs/httphandler/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package httphandler

import (
"crypto/sha256"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -80,6 +81,11 @@ func (h *Handler) serveGet(w http.ResponseWriter, r *http.Request, log *slog.Log
return
}

if h.handleEtag(w, r, fileEP, log) {
// Client ETag matches, can optimize out the data
return
}

rc, err := h.FS.OpenEntrypointData(r.Context(), fileEP)
if h.handleHttpError(err, w, log, "Error opening file") {
return
Expand All @@ -102,3 +108,16 @@ func (h *Handler) handleHttpError(err error, w http.ResponseWriter, log *slog.Lo
}
return false
}

func (h *Handler) handleEtag(w http.ResponseWriter, r *http.Request, ep *cinodefs.Entrypoint, log *slog.Logger) bool {
currentEtag := fmt.Sprintf("\"%X\"", sha256.Sum256(ep.Bytes()))

if strings.Contains(r.Header.Get("If-None-Match"), currentEtag) {
log.Debug("Valid ETag found, sending 304 Not Modified")
w.WriteHeader(http.StatusNotModified)
return true
}

w.Header().Set("ETag", currentEtag)
return false
}
40 changes: 37 additions & 3 deletions pkg/cinodefs/httphandler/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,27 @@ func (s *HandlerTestSuite) setEntry(t *testing.T, data string, path ...string) {
require.NoError(t, err)
}

func (s *HandlerTestSuite) getEntry(t *testing.T, path string) (string, string, int) {
resp, err := http.Get(s.server.URL + path)
func (s *HandlerTestSuite) getEntryETag(t *testing.T, path, etag string) (string, string, string, int) {
req, err := http.NewRequest(http.MethodGet, s.server.URL+path, nil)
require.NoError(t, err)

if etag != "" {
req.Header.Set("If-None-Match", etag)
}

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()

data, err := io.ReadAll(resp.Body)
require.NoError(t, err)

return string(data), resp.Header.Get("content-type"), resp.StatusCode
return string(data), resp.Header.Get("content-type"), resp.Header.Get("ETag"), resp.StatusCode
}

func (s *HandlerTestSuite) getEntry(t *testing.T, path string) (string, string, int) {
data, contentType, _, code := s.getEntryETag(t, path, "")
return data, contentType, code
}

func (s *HandlerTestSuite) getData(t *testing.T, path string) string {
Expand All @@ -119,6 +131,28 @@ func (s *HandlerTestSuite) TestSuccessfulFileDownload() {
require.Equal(s.T(), "hello", readBack)
}

func (s *HandlerTestSuite) TestEtag() {
s.setEntry(s.T(), "hello", "file.txt")

readBack, _, etag, code := s.getEntryETag(s.T(), "/file.txt", "")
require.NotEmpty(s.T(), etag)
require.Greater(s.T(), len(etag), 10)
require.Equal(s.T(), http.StatusOK, code)
require.Equal(s.T(), "hello", readBack)

readBack, _, _, code = s.getEntryETag(s.T(), "/file.txt", etag)
require.Equal(s.T(), http.StatusNotModified, code)
require.Empty(s.T(), readBack)

s.setEntry(s.T(), "updated", "file.txt")

readBack, _, etag2, code := s.getEntryETag(s.T(), "/file.txt", etag)
require.Equal(s.T(), http.StatusOK, code)
require.Greater(s.T(), len(etag2), 10)
require.NotEqual(s.T(), etag, etag2)
require.Equal(s.T(), "updated", readBack)
}

func (s *HandlerTestSuite) TestNonGetRequest() {
t := s.T()
resp, err := http.Post(s.server.URL, "text/plain", strings.NewReader("Hello world!"))
Expand Down

0 comments on commit 3397ab6

Please sign in to comment.