Skip to content

Commit

Permalink
Optimization: only validate UTF-8 if TTL exceeds any of the max values
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Heckel committed Jun 28, 2021
1 parent d3e7d08 commit 1a621ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,17 @@ func (s *Server) getTTL(r *http.Request, peakedBody *util.PeakedReadCloser) (tim
// If the given TTL is larger than the max allowed value, set it to the max value.
// Special handling for text: if the body is a short text (as per our peaking), the text max value applies.
// It may be a little inefficient to always check for UTF-8, but I think it's fine.
maxTTL := s.config.FileExpireAfterNonTextMax
isShortText := !peakedBody.LimitReached && utf8.Valid(peakedBody.PeakedBytes)
if isShortText {
maxTTL = s.config.FileExpireAfterTextMax
}
if maxTTL > 0 && ttl > maxTTL {
ttl = maxTTL
if ttl > s.config.FileExpireAfterNonTextMax || ttl > s.config.FileExpireAfterTextMax {
maxTTL := s.config.FileExpireAfterNonTextMax
isShortText := !peakedBody.LimitReached && utf8.Valid(peakedBody.PeakedBytes)
if isShortText {
maxTTL = s.config.FileExpireAfterTextMax
}
if maxTTL > 0 && ttl > maxTTL {
ttl = maxTTL
}
}

return ttl, nil
}

Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func TestServer_HandleClipboardPutTextWithTooLargeTTL(t *testing.T) {
test.Status(t, rr, http.StatusOK)

ttl, _ := strconv.Atoi(rr.Header().Get("X-TTL"))
test.DurationEquals(t, 2 * time.Hour, time.Second*time.Duration(ttl))
test.DurationEquals(t, 2*time.Hour, time.Second*time.Duration(ttl))
}

func TestServer_HandleClipboardPutLongTextWithTooLargeTTL(t *testing.T) {
Expand Down

0 comments on commit 1a621ac

Please sign in to comment.