New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default max body size for streaming request body collection #2312
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
18910f0
Added .defaultMaxBodySize to RouteBuilder protocol
calebkleveter a86288f
Added .group(maxSize:) and .grouped(maxSize:configure:) methods to Ro…
calebkleveter f297a9b
Verify body size in Request.Body.collect(max:) method on .collected case
calebkleveter baa184c
Changed default .collect maxSize to 1 megabyte
calebkleveter eb89bf7
Use group/router default request body sizes in RoutesBuilder.on respo…
calebkleveter aa6bdec
Created RouteTests.testConfigurableMaxBodySize test case
calebkleveter 8b32960
Added check for app.routes.defaultMaxBodySize setter in RouteTests.te…
calebkleveter 713bb1b
Fix maxSize and body.readableBytes comperison operators
calebkleveter d130d05
Address comments
jdmcd e949d4d
update
tanner0101 21d1dc3
updates
tanner0101 b55cf05
updates
tanner0101 0024074
updates
tanner0101 deb0284
updates
tanner0101 b09b40a
updates
tanner0101 86e817d
Fuzzy
jdmcd File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Foundation | ||
|
||
/// Represents a number of bytes: | ||
/// | ||
/// let bytes: ByteCount = "1mb" | ||
/// print(bytes.value) // 1048576 | ||
/// | ||
/// let bytes: ByteCount = 1_000_000 | ||
/// print(bytes.value) // 1000000 | ||
|
||
/// let bytes: ByteCount = "2kb" | ||
/// print(bytes.value) // 2048 | ||
public struct ByteCount: Equatable { | ||
/// The value in Bytes | ||
public let value: Int | ||
|
||
public init(value: Int) { | ||
self.value = value | ||
} | ||
} | ||
|
||
extension ByteCount: ExpressibleByIntegerLiteral { | ||
/// Initializes the `ByteCount` with the raw byte count | ||
/// - Parameter value: The number of bytes | ||
public init(integerLiteral value: Int) { | ||
self.value = value | ||
} | ||
} | ||
|
||
extension ByteCount: ExpressibleByStringLiteral { | ||
/// Initializes the `ByteCount` via a descriptive string. Available suffixes are: | ||
/// `kb`, `mb`, `gb`, `tb` | ||
/// - Parameter value: The string value (`1mb`) | ||
public init(stringLiteral value: String) { | ||
// Short path if it's an int wrapped in a string | ||
if let intValue = Int(value) { | ||
self.value = intValue | ||
return | ||
} | ||
|
||
let validSuffixes = [ | ||
"kb": 10, | ||
"mb": 20, | ||
"gb": 30, | ||
"tb": 40 | ||
] | ||
|
||
let cleanValue = value.lowercased().trimmingCharacters(in: .whitespaces).replacingOccurrences(of: " ", with: "") | ||
for suffix in validSuffixes { | ||
guard cleanValue.hasSuffix(suffix.key) else { continue } | ||
guard let stringIntValue = cleanValue.components(separatedBy: suffix.key).first else { | ||
fatalError("Invalid string format") | ||
} | ||
|
||
guard let intValue = Int(stringIntValue) else { | ||
fatalError("Invalid int value: \(stringIntValue)") | ||
} | ||
|
||
self.value = intValue << suffix.value | ||
return | ||
} | ||
|
||
// Assert failure here because all cases are handled in the above loop | ||
fatalError("Could not parse byte count string: \(value)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing
nil
no longer means "unlimited". It now means "use default". I think this is a better API but this brings up a couple of questions:I don't think so since I view the current behavior as a bug.
I don't think so as this opens your server up to an easy attack vector. If you really want to get around this you can use
Int.max
as the body size. This is the maximum theoretical size anyway since we need to be able to represent the body size as an integer.