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

Using set_content_provider with length of 0 makes it infinitely loop #1545

Closed
gtnardy opened this issue Mar 31, 2023 · 2 comments
Closed

Using set_content_provider with length of 0 makes it infinitely loop #1545

gtnardy opened this issue Mar 31, 2023 · 2 comments
Labels

Comments

@gtnardy
Copy link

gtnardy commented Mar 31, 2023

Hey, I got an interesting case when trying to send an empty file through set_content_provider,

I have a thing simple as this just to clarify (reduced code):

struct NFile
{
	std::string Data;
	std::uint64_t Size;
};

NFile* File;

// Load file content into File.data string.
// ...

Res.set_content_provider(
	File->Size,
	"application/octet-stream",
	[File](std::size_t Offset, std::size_t Length, httplib::DataSink& Sink)
	{
		Sink.write(&File->Data[Offset], std::min(Length, 1024 * 1024)); // 1 MB Chunks
		return true;
	}
);

But if string is empty (file is empty), the content_provider keeps looping infinitely (triggering the callback), but the request never finishes in fact.

Not sure if this should be something I would need to treat (I'm going to), or if it can enlighten that there's a bug somewhere in the lib internally.

@PixlRainbow
Copy link
Contributor

PixlRainbow commented Apr 1, 2023

you can return false in your content provider to cancel the response as a temporary workaround, but zero length is likely also an internal bug with httplib as it checks for < instead of <=

@yhirose yhirose added the bug label Apr 8, 2023
@yhirose yhirose closed this as completed in d587548 Apr 8, 2023
@yhirose
Copy link
Owner

yhirose commented Apr 8, 2023

@gtnardy thanks for the report!

ExclusiveOrange pushed a commit to ExclusiveOrange/cpp-httplib-exor that referenced this issue May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants