Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Two handy extensions to work with HTTPClientResponse body#736

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
makoni wants to merge13 commits intoswift-server:main
base:main
Choose a base branch
Loading
frommakoni:collectedData

Conversation

@makoni
Copy link

@makonimakoni commentedApr 8, 2024
edited
Loading

Two extensions forHTTPClientResponse to avoid boilerplate code.

Reading a response data currently:

letexpectedBytes= headers.first(name:"content-length").flatMap(Int.init)??1024*1024varbytes=tryawait body.collect(upTo: expectedBytes)iflet data= bytes.readData(length: bytes.readableBytes){    // handle data}

With the extension:

iflet data=tryawait response.data(upTo:1024*1024){    // handle data}

Additionally an extension forbytes. Currently:

letexpectedBytes= headers.first(name:"content-length").flatMap(Int.init)??1024*1024varbytes=tryawait body.collect(upTo: expectedBytes)

With the extension:

varbytes=tryawait response.bytes(upTo:1024*1024)

Copy link
Collaborator

@LukasaLukasa left a comment

Choose a reason for hiding this comment

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

Generally these look really good! Can you add a couple of tests for them as well?

@swift-serverswift-server deleted a comment fromMizhkaaApr 9, 2024
@makonimakoni requested a review fromLukasaApril 9, 2024 11:25
@makoni
Copy link
Author

It's been a while but I updated it :)

/// - Parameter maxBytes: The maximum number of bytes this method is allowed to accumulate.
/// - Returns: Bytes collected over time
publicfunc bytes(upTo maxBytes:Int)asyncthrows->ByteBuffer{
letexpectedBytes=self.headers.first(name:"content-length").flatMap(Int.init)?? maxBytes
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should always usemaxBytes and never the value fromcontent-length.

Copy link
Author

Choose a reason for hiding this comment

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

Done

@makonimakoni requested a review fromLukasaAugust 15, 2025 15:27
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabianfettfabianfettAwaiting requested review from fabianfett

@LukasaLukasaAwaiting requested review from Lukasa

Requested changes must be addressed to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@makoni@Lukasa@fabianfett

[8]ページ先頭

©2009-2025 Movatter.jp