summaryrefslogtreecommitdiffabout
path: root/lib
Unidiff
Diffstat (limited to 'lib') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/sitecing_interface_cgi.cc4
1 files changed, 3 insertions, 1 deletions
diff --git a/lib/sitecing_interface_cgi.cc b/lib/sitecing_interface_cgi.cc
index 59ae25a..1acb23c 100644
--- a/lib/sitecing_interface_cgi.cc
+++ b/lib/sitecing_interface_cgi.cc
@@ -1,48 +1,50 @@
1#include <cassert> 1#include <cassert>
2#include "sitecing/sitecing_interface_cgi.h" 2#include "sitecing/sitecing_interface_cgi.h"
3 3
4namespace sitecing { 4namespace sitecing {
5 5
6 sitecing_interface_cgi::sitecing_interface_cgi(sitespace *s) 6 sitecing_interface_cgi::sitecing_interface_cgi(sitespace *s)
7 : outs(&prebuffer), sitecing_interface(&outs), ss(s), cgigw(NULL), headers_flushed(false) { 7 : outs(&prebuffer), sitecing_interface(&outs), ss(s), cgigw(NULL), headers_flushed(false) {
8 } 8 }
9 9
10 void sitecing_interface_cgi::prepare(kingate::cgi_gateway *cg) { 10 void sitecing_interface_cgi::prepare(kingate::cgi_gateway *cg) {
11 cgigw = cg; 11 cgigw = cg;
12 headers.clear(); 12 headers.clear();
13 headers["Content-Type"] = "text/html"; 13 headers["Content-Type"] = "text/html";
14 prebuffer.str(""); 14 prebuffer.str("");
15 headers_flushed = false; 15 headers_flushed = false;
16 set_buffering(true); 16 set_buffering(true);
17 } 17 }
18 18
19 void sitecing_interface_cgi::flush(bool keep_buffering) { 19 void sitecing_interface_cgi::flush(bool keep_buffering) {
20 assert(cgigw); 20 assert(cgigw);
21 flush_headers(); 21 flush_headers();
22 if(is_buffering()) { 22 if(is_buffering()) {
23 streampos count = prebuffer.pubseekoff(0,ios_base::cur,ios_base::out); 23 streampos count = prebuffer.pubseekoff(0,ios_base::cur,ios_base::out);
24 cgigw->out().write(prebuffer.str().c_str(),count); 24 if(count>0) {
25 cgigw->out().write(prebuffer.str().c_str(),count);
26 }
25 prebuffer.str(""); 27 prebuffer.str("");
26 } 28 }
27 cgigw->out().flush(); 29 cgigw->out().flush();
28 set_buffering(keep_buffering); 30 set_buffering(keep_buffering);
29 } 31 }
30 32
31 void sitecing_interface_cgi::set_buffering(bool do_buffer) { 33 void sitecing_interface_cgi::set_buffering(bool do_buffer) {
32 if(!do_buffer) 34 if(!do_buffer)
33 flush_headers(); 35 flush_headers();
34 outs.rdbuf(do_buffer?&prebuffer:cgigw->out().rdbuf()); 36 outs.rdbuf(do_buffer?&prebuffer:cgigw->out().rdbuf());
35 } 37 }
36 38
37 bool sitecing_interface_cgi::is_buffering() { 39 bool sitecing_interface_cgi::is_buffering() {
38 return outs.rdbuf()==&prebuffer; 40 return outs.rdbuf()==&prebuffer;
39 } 41 }
40 42
41 void sitecing_interface_cgi::flush_headers() { 43 void sitecing_interface_cgi::flush_headers() {
42 assert(cgigw); 44 assert(cgigw);
43 if(headers_flushed) 45 if(headers_flushed)
44 return; 46 return;
45 for(kingate::headers::const_iterator i=headers.begin();i!=headers.end();i++) 47 for(kingate::headers::const_iterator i=headers.begin();i!=headers.end();i++)
46 cgigw->out() << i->first << ": " << i->second << "\n"; 48 cgigw->out() << i->first << ": " << i->second << "\n";
47 cgigw->out() << "\n"; 49 cgigw->out() << "\n";
48 headers_flushed = true; 50 headers_flushed = true;