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

DataSink outputstream fails handling single chars #1831

Open
stevem2t opened this issue May 6, 2024 · 0 comments
Open

DataSink outputstream fails handling single chars #1831

stevem2t opened this issue May 6, 2024 · 0 comments

Comments

@stevem2t
Copy link

stevem2t commented May 6, 2024

Hello,
please consider the following code:

#include <httplib.h>
using namespace httplib;

int main()
{
	auto provider = []( size_t /*off*/, DataSink& sink )->bool{
				auto& os = sink.os;
				os << "line 1\n";
				os << 'X';
				os << "DONE OK\n";
				sink.done();
				return true;
	};
	
	httplib::Server srv;
	srv.Get("/", [&](const httplib::Request &, httplib::Response &rsp) {
		rsp.set_content_provider( "text/html", provider );
	});
	srv.listen("0.0.0.0", 8080);
}

compile, run, and do a curl localhost:8080/
Expected:

line 1
X
DONE OK

But we get:

line 1

The explanation is easy:

  • The datasink streambuf only implements xsputn
  • it has no buffer set.
  • a single char write will call default streambuf::overflow(), which justs returns eof...
  • and the the stream eof/error is set and nothing more is emitted.

The minimum is probably just to implement overflow(), which, without buffer support, just needs to be like:

		int_type overflow( int_type ch ) override {
			if( ch != traits_type::eof() )
			{
				char cc = ch;
				if( 1 == xsputn( &cc, 1 ) )
					return ch; // or whatever not eof...
			}
			return traits_type::eof();
		  }

However for real usability, user buffer customization (via pubsetbuf) would be useful.
Please tell me if you want to go directly with a proper buffer support of if the interim simple version might be enough.
Best,

--
Steve

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

No branches or pull requests

1 participant