Skip to content
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

KTOR-7683 Fix for empty response with respondSource #4494

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bjhham
Copy link
Contributor

@bjhham bjhham commented Nov 19, 2024

Subsystem
Server, I/O

Motivation
KTOR-7683 call.respondSource returns empty response but passes in tests

Solution
Turns out, the problem was using the Source.remaining extension value as the content length, which only checks the size of the buffer, which starts as 0 when wrapping an InputStream with the asSource() extension.

The fix here is adding an optional contentLength parameter on respondSource, so the responsibility is delegated to the user. I also generalized these functions to use RawSource to reflect the kotlinx-io API, and allow for non-buffered sources.

@bjhham bjhham force-pushed the bjhham/respond-source-empty-fix branch from 44a7e14 to ac54ab9 Compare November 19, 2024 11:10
) {
respond(ChannelWriterContent({ writeBuffer(source) }, contentType, status, source.remaining))
respond(ChannelWriterContent({ writeBuffer(source) }, contentType, status, contentLength))
Copy link
Member

Choose a reason for hiding this comment

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

Can it break something that we pass contentLength instead source.remaining now, when contentLength is null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It'll just omit the Content-Length header when it's not specified, which could make some clients angry, but it's still a valid response.

Copy link
Member

@marychatte marychatte left a comment

Choose a reason for hiding this comment

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

lgtm

@bjhham bjhham force-pushed the bjhham/respond-source-empty-fix branch 3 times, most recently from 0c59f48 to af37898 Compare November 26, 2024 08:52
@bjhham bjhham force-pushed the bjhham/respond-source-empty-fix branch from af37898 to a92e5ac Compare November 26, 2024 08:52
@bjhham bjhham force-pushed the bjhham/respond-source-empty-fix branch from b492e95 to 7f5c8e3 Compare November 26, 2024 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants