Menu

[r429]: / framework / branches / new-templates / worker_thread.cpp  Maximize  Restore  History

Download this file

137 lines (116 with data), 2.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "worker_thread.h"
#include "global_config.h"
#include "thread_cache.h"
#include "base_view.h"
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include "manager.h"
using namespace cgicc;
namespace cppcms {
worker_thread::worker_thread(manager const &s) :
url(this),
app(s),
cache(this),
cout(&(this->out_buf))
{
caching_module=app.cache->get();
} ;
worker_thread::~worker_thread()
{
app.cache->del(caching_module);
}
void worker_thread::main()
{
cout<<"<h1>Hello World</h2>\n";
}
void worker_thread::set_header(HTTPHeader *h)
{
response_header=auto_ptr<HTTPHeader>(h);
};
void worker_thread::add_header(string s) {
other_headers.push_back(s);
};
void worker_thread::set_lang()
{
gt=&app.gettext->get();
}
void worker_thread::set_lang(string const &s)
{
gt=&app.gettext->get(s);
}
void worker_thread::run(cgicc_connection &cgi_conn)
{
cgi=&cgi_conn.cgi();
env=&(cgi->getEnvironment());
ostream &cgi_out=cgi_conn.cout();
set_lang();
other_headers.clear();
cache.reset();
out_buf.str("");
set_header(new HTTPHTMLHeader);
gzip=gzip_done=false;
string encoding;
if((encoding=cgi_conn.env("HTTP_ACCEPT_ENCODING"))!="") {
if(strstr(encoding.c_str(),"gzip")!=NULL) {
gzip=app.config.lval("gzip.enable",0);
}
}
try {
/**********/
main();
/**********/
if(response_header.get() == NULL) {
throw cppcms_error("Looks like a bug");
}
}
catch(std::exception const &e) {
string msg=e.what();
set_header(new HTTPStatusHeader(500,msg));
cgi_out<<"<html><body><p>"+msg+"</p><body></html>";
gzip=gzip_done=false;
other_headers.clear();
out_buf.str("");
return;
}
if(app.config.lval("server.disable_xpowered_by",0)==0) {
add_header("X-Powered-By: CppCMS/0.0alpha1");
}
for(list<string>::iterator h=other_headers.begin();h!=other_headers.end();h++) {
cgi_out<<*h<<"\n";
}
string out=out_buf.str();
out_buf.str("");
if(gzip) {
if(out.size()>0) {
if(gzip_done){
cgi_out<<"Content-Length: "<<out.size()<<"\n";
}
cgi_out<<"Content-Encoding: gzip\n";
cgi_out<<*response_header;
if(gzip_done) {
cgi_out<<out;
}
else{
long level=app.config.lval("gzip.level",-1);
long length=app.config.lval("gzip.buffer",-1);
deflate(out,cgi_out,level,length);
}
}
else {
cgi_out<<*response_header;
}
}
else {
cgi_out<<"Content-Length: "<<out.size()<<"\n";
cgi_out<<*response_header;
cgi_out<<out;
}
}
void worker_thread::render(string name,base_content &content)
{
using cppcms::details::views_storage;
auto_ptr<base_view> p(views_storage::instance().fetch_view(current_template,name,this,&content));
if(!p.get()) throw cppcms_error("Template "+name+" not found");
p->render();
};
}
MongoDB Logo MongoDB