-rw-r--r-- | include/sitecing/sitecing_interface_cgi.h | 29 | ||||
-rw-r--r-- | lib/sitecing_interface_cgi.cc | 34 |
2 files changed, 55 insertions, 8 deletions
diff --git a/include/sitecing/sitecing_interface_cgi.h b/include/sitecing/sitecing_interface_cgi.h index cab947c..da538d6 100644 --- a/include/sitecing/sitecing_interface_cgi.h +++ b/include/sitecing/sitecing_interface_cgi.h @@ -38,3 +38,11 @@ namespace sitecing { */ - ostringstream prebuffer; + stringbuf prebuffer; + /** + * Output stream, initially going to prebuffer. + */ + ostream outs; + /** + * Have headers been sent yet? + */ + bool headers_flushed; /** @@ -50,3 +58,4 @@ namespace sitecing { /** - * @todo TODO: wish I could remember -- document me. + * Set up interface for the (possibly, new) cgi gateway object, + * reset headers, empty buffer, etc. */ @@ -54,5 +63,17 @@ namespace sitecing { /** - * @todo TODO: wish I could remember -- document me. + * Flush output stream. + */ + void flush(bool keep_buffering=false); + /** + * Send headers to the output stream, if we haven't yet. + */ + void flush_headers(); + /** + * Are we buffering now? + */ + bool is_buffering(); + /** + * Control output buffering. */ - void flush(); + void set_buffering(bool do_buffer); diff --git a/lib/sitecing_interface_cgi.cc b/lib/sitecing_interface_cgi.cc index 5c3d295..f2bd093 100644 --- a/lib/sitecing_interface_cgi.cc +++ b/lib/sitecing_interface_cgi.cc @@ -6,3 +6,3 @@ namespace sitecing { sitecing_interface_cgi::sitecing_interface_cgi(sitespace *s) - : sitecing_interface(&prebuffer), ss(s), cgigw(NULL) { + : outs(&prebuffer), sitecing_interface(&outs), ss(s), cgigw(NULL), headers_flushed(false) { } @@ -14,10 +14,36 @@ namespace sitecing { prebuffer.str(""); + headers_flushed = false; + set_buffering(true); } - void sitecing_interface_cgi::flush() { + void sitecing_interface_cgi::flush(bool keep_buffering) { assert(cgigw); + flush_headers(); + if(is_buffering()) { + streampos count = prebuffer.pubseekoff(0,ios_base::cur,ios_base::out); + cgigw->out().write(prebuffer.str().c_str(),count); + prebuffer.str(""); + } + cgigw->out().flush(); + set_buffering(keep_buffering); + } + + void sitecing_interface_cgi::set_buffering(bool do_buffer) { + if(!do_buffer) + flush_headers(); + outs.rdbuf(do_buffer?&prebuffer:cgigw->out().rdbuf()); + } + + bool sitecing_interface_cgi::is_buffering() { + return outs.rdbuf()==&prebuffer; + } + + void sitecing_interface_cgi::flush_headers() { + assert(cgigw); + if(headers_flushed) + return; for(headers_t::const_iterator i=headers.begin();i!=headers.end();i++) cgigw->out() << i->first << ": " << i->second << "\n"; - (cgigw->out() << "\n").write(prebuffer.str().c_str(),prebuffer.tellp()); - cgigw->out().flush(); + cgigw->out() << "\n"; + headers_flushed = true; } |