diff options
author | Runxi Yu <me@runxiyu.org> | 2025-08-17 03:09:52 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-08-17 03:09:52 +0800 |
commit | 009a6e397651a9540b6c6bb74ef2230eeda9b577 (patch) | |
tree | 0a808670d95aba5804a73a558cc335a49d230aa6 /forged/internal/bare/limit.go | |
parent | Remove HTML templates from main server (diff) | |
download | forge-009a6e397651a9540b6c6bb74ef2230eeda9b577.tar.gz forge-009a6e397651a9540b6c6bb74ef2230eeda9b577.tar.zst forge-009a6e397651a9540b6c6bb74ef2230eeda9b577.zip |
Some mass renaming
Diffstat (limited to 'forged/internal/bare/limit.go')
-rw-r--r-- | forged/internal/bare/limit.go | 58 |
1 files changed, 0 insertions, 58 deletions
diff --git a/forged/internal/bare/limit.go b/forged/internal/bare/limit.go deleted file mode 100644 index 212bc05..0000000 --- a/forged/internal/bare/limit.go +++ /dev/null @@ -1,58 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: Copyright (c) 2025 Drew Devault <https://drewdevault.com> - -package bare - -import ( - "errors" - "io" -) - -var ( - maxUnmarshalBytes uint64 = 1024 * 1024 * 32 /* 32 MiB */ - maxArrayLength uint64 = 1024 * 4 /* 4096 elements */ - maxMapSize uint64 = 1024 -) - -// MaxUnmarshalBytes sets the maximum size of a message decoded by unmarshal. -// By default, this is set to 32 MiB. -func MaxUnmarshalBytes(bytes uint64) { - maxUnmarshalBytes = bytes -} - -// MaxArrayLength sets maximum number of elements in array. Defaults to 4096 elements -func MaxArrayLength(length uint64) { - maxArrayLength = length -} - -// MaxMapSize sets maximum size of map. Defaults to 1024 key/value pairs -func MaxMapSize(size uint64) { - maxMapSize = size -} - -// Use MaxUnmarshalBytes to prevent this error from occuring on messages which -// are large by design. -var ErrLimitExceeded = errors.New("Maximum message size exceeded") - -// Identical to io.LimitedReader, except it returns our custom error instead of -// EOF if the limit is reached. -type limitedReader struct { - R io.Reader - N uint64 -} - -func (l *limitedReader) Read(p []byte) (n int, err error) { - if l.N <= 0 { - return 0, ErrLimitExceeded - } - if uint64(len(p)) > l.N { - p = p[0:l.N] - } - n, err = l.R.Read(p) - l.N -= uint64(n) - return -} - -func newLimitedReader(r io.Reader) *limitedReader { - return &limitedReader{r, maxUnmarshalBytes} -} |