Skip to content

Handle ResponseAccumulator not being able to buffer large response in memory #637

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

Merged
merged 8 commits into from
Oct 10, 2022

Conversation

dnadoba
Copy link
Collaborator

@dnadoba dnadoba commented Oct 7, 2022

Motivation

If the response being buffered by the ResponseAccumulator is too large (larger than UInt32.max), a crash will occur.

Modifications

  • Introduce an new public var maxBodySize: Int on ResponseAccumulator
  • throw a new ResponseTooBigError if the response bodies size is larger than maxBodySize

Result

Will now fail graciously if too big a response is buffered in memory via the ResponseAccumulator.
In addition, a user can configure the max size themself.

@dnadoba dnadoba force-pushed the dn-max-buffer-size branch from e675f60 to a9122e8 Compare October 7, 2022 18:06
public var maxBodySize: Int = maxByteBufferSize {
didSet {
precondition(
self.maxBodySize <= Self.maxByteBufferSize,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relatively minor, but wouldn't it also make sense to verify that the new value isn't negative?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in f0947e9

public init(request: HTTPClient.Request) {
self.request = request
}

public func didReceiveHead(task: HTTPClient.Task<Response>, _ head: HTTPResponseHead) -> EventLoopFuture<Void> {
switch self.state {
case .idle:
if let contentLength = head.headers.first(name: "Content-Length"),
let announcedBodySize = Int(contentLength),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be tolerating the possibility of a non-integer Content-Length field here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dnadoba dnadoba force-pushed the dn-max-buffer-size branch from 075241b to 986bb05 Compare October 10, 2022 09:26
@Lukasa Lukasa merged commit 9937d87 into swift-server:main Oct 10, 2022
@dnadoba dnadoba deleted the dn-max-buffer-size branch October 10, 2022 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants