Skip to content

Commit 1a621ac

Browse files
author
Philipp Heckel
committed
Optimization: only validate UTF-8 if TTL exceeds any of the max values
1 parent d3e7d08 commit 1a621ac

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

server/server.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,17 @@ func (s *Server) getTTL(r *http.Request, peakedBody *util.PeakedReadCloser) (tim
599599
// If the given TTL is larger than the max allowed value, set it to the max value.
600600
// Special handling for text: if the body is a short text (as per our peaking), the text max value applies.
601601
// It may be a little inefficient to always check for UTF-8, but I think it's fine.
602-
maxTTL := s.config.FileExpireAfterNonTextMax
603-
isShortText := !peakedBody.LimitReached && utf8.Valid(peakedBody.PeakedBytes)
604-
if isShortText {
605-
maxTTL = s.config.FileExpireAfterTextMax
606-
}
607-
if maxTTL > 0 && ttl > maxTTL {
608-
ttl = maxTTL
602+
if ttl > s.config.FileExpireAfterNonTextMax || ttl > s.config.FileExpireAfterTextMax {
603+
maxTTL := s.config.FileExpireAfterNonTextMax
604+
isShortText := !peakedBody.LimitReached && utf8.Valid(peakedBody.PeakedBytes)
605+
if isShortText {
606+
maxTTL = s.config.FileExpireAfterTextMax
607+
}
608+
if maxTTL > 0 && ttl > maxTTL {
609+
ttl = maxTTL
610+
}
609611
}
612+
610613
return ttl, nil
611614
}
612615

server/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func TestServer_HandleClipboardPutTextWithTooLargeTTL(t *testing.T) {
379379
test.Status(t, rr, http.StatusOK)
380380

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

385385
func TestServer_HandleClipboardPutLongTextWithTooLargeTTL(t *testing.T) {

0 commit comments

Comments
 (0)