Menu

[r126]: / framework / branches / cache_support / worker_thread.cpp  Maximize  Restore  History

Download this file

158 lines (129 with data), 2.7 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "worker_thread.h"
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
using namespace cgicc;
Worker_Thread::Worker_Thread()
{
}
void Worker_Thread::main()
{
out.puts("<h1>Hello World</h2>\n");
}
bool Worker_Thread::check_gzip( FCGX_Request *fcgi)
{
char *ptr;
bool out_gzip=false;
if((ptr=FCGX_GetParam("HTTP_ACCEPT_ENCODING",fcgi->envp))!=NULL) {
if(strstr(ptr,"gzip")!=NULL) {
out_gzip=true;
}
}
if(global_config.lval("gzip.enable",0)==0) {
out_gzip=false;
}
return out_gzip;
}
void Worker_Thread::run(FCGX_Request *fcgi)
{
#ifdef FCGX_API_ACCEPT_ONLY_EXISTS
if(FCGX_Continue_r(fcgi)<0){
return;
}
#endif
io = auto_ptr<FCgiIO>(new FCgiIO(*fcgi));
cgi = auto_ptr<Cgicc>( new Cgicc(&*io) );
env=&(cgi->getEnvironment());
set_header(new HTTPHTMLHeader);
out_gzip=check_gzip(fcgi);
is_cached=false;
gzip_cache="";
string_cache="";
try {
/**********/
main();
/**********/
if(response_header.get() == NULL) {
throw HTTP_Error("Looks like a bug");
}
}
catch( HTTP_Error &error_message) {
int err_code;
out.reset();
string msg;
if(error_message.is_404()) {
err_code=404;
msg="Not Found: ";
msg+=error_message.get();
}
else {
err_code=500;
msg=error_message.get();
}
set_header(new HTTPStatusHeader(err_code,msg));
out.puts(msg.c_str());
}
flush_output(*io);
out.reset();
response_header.reset();
cgi.reset();
io.reset();
FCGX_Finish_r(fcgi);
}
void Worker_Thread::flush_output(FCgiIO &io)
{
if(out_gzip) {
using namespace boost::iostreams;
io<<"Content-Encoding: gzip\r\n";
io<<*response_header;
if(is_cached) {
cerr<<"gzip, cache\n";
io<<gzip_cache;
}
else {
gzip_params params;
long level,length;
if((level=global_config.lval("gzip.level",-1))!=-1){
params.level=level;
}
filtering_ostream zstream;
if((length=global_config.lval("gzip.buffer",-1))!=-1){
zstream.push(gzip_compressor(params,length));
}
else {
zstream.push(gzip_compressor(params));
}
zstream.push(io);
zstream<<out.get();
cerr<<"gzip, no cache\n";
}
}
else {
io<<*response_header;
if(is_cached) {
cerr<<"Not gzip, cache\n";
io<<string_cache;
}
else {
io<<out.get();
cerr<<"Not gzip, no cache\n";
}
}
}
bool Worker_Thread::cache_try(string const &key)
{
if(out_gzip) {
is_cached=global_cache->fetch_gzip(key,gzip_cache);
}
else {
is_cached=global_cache->fetch_string(key,string_cache);
}
return is_cached;
}
void Worker_Thread::cache_store(string const &key)
{
global_cache->insert(key,out.get());
}
void Worker_Thread::cache_drop(string const &key)
{
global_cache->drop_prefix(key);
}
MongoDB Logo MongoDB