From 1a621ace79d861ee2ae84f3053969489ce7727e2 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Mon, 28 Jun 2021 09:49:44 -0400 Subject: [PATCH] Optimization: only validate UTF-8 if TTL exceeds any of the max values --- server/server.go | 17 ++++++++++------- server/server_test.go | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/server/server.go b/server/server.go index e0b155f..42fab96 100644 --- a/server/server.go +++ b/server/server.go @@ -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 } diff --git a/server/server_test.go b/server/server_test.go index c07128c..561d574 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -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) {