-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 @@ -15,48 +15,69 @@ namespace sitecing { using namespace std; /** * The interface to site-C-ing core for the CGI component. */ class sitecing_interface_cgi : public sitecing_interface { public: /** * Pointer to the CGI gateway interface. */ kingate::cgi_gateway* cgigw; /** * Type for the map of headers to spit out. */ typedef map<string,string> headers_t; /** * The list of headers to spit out. */ headers_t headers; /** * Here is where we prebuffer output. */ - ostringstream prebuffer; + stringbuf prebuffer; + /** + * Output stream, initially going to prebuffer. + */ + ostream outs; + /** + * Have headers been sent yet? + */ + bool headers_flushed; /** * Pointer to the sitespace object. */ sitespace *ss; // XXX: or does it belong to the generic interface? or should this 'generic' interface exist at all? /** * @param s Pointer to the sitespace object. */ sitecing_interface_cgi(sitespace *s); /** - * @todo TODO: wish I could remember -- document me. + * Set up interface for the (possibly, new) cgi gateway object, + * reset headers, empty buffer, etc. */ void prepare(kingate::cgi_gateway *cg); /** - * @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); }; } #endif /* __SITECING_SITECING_INTERFACE_CGI_H */ 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 @@ -1,25 +1,51 @@ #include <cassert> #include "sitecing/sitecing_interface_cgi.h" 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) { } void sitecing_interface_cgi::prepare(kingate::cgi_gateway *cg) { cgigw = cg; headers.clear(); headers["Content-Type"] = "text/html"; 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; } } |