Menu

[r1918]: / framework / trunk / src / http_context.cpp  Maximize  Restore  History

Download this file

280 lines (240 with data), 6.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include "cgi_api.h"
#include <cppcms/service.h>
#include <cppcms/http_context.h>
#include <cppcms/http_request.h>
#include <cppcms/http_response.h>
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/thread_pool.h>
#include <cppcms/views_pool.h>
#include <cppcms/cache_interface.h>
#include <cppcms/session_interface.h>
#include <cppcms/cppcms_error.h>
#include <booster/log.h>
#include <booster/backtrace.h>
#include "cached_settings.h"
#include <cppcms/config.h>
#ifdef CPPCMS_USE_EXTERNAL_BOOST
# include <boost/bind.hpp>
#else // Internal Boost
# include <cppcms_boost/bind.hpp>
namespace boost = cppcms_boost;
#endif
namespace cppcms {
namespace http {
struct context::_data {
std::locale locale;
std::string skin;
http::request request;
std::auto_ptr<http::response> response;
std::auto_ptr<cache_interface> cache;
std::auto_ptr<session_interface> session;
_data(context &cntx) :
locale(cntx.connection().service().locale()),
request(cntx.connection())
{
}
};
context::context(booster::shared_ptr<impl::cgi::connection> conn) :
conn_(conn)
{
d.reset(new _data(*this));
d->response.reset(new http::response(*this));
skin(service().views_pool().default_skin());
d->cache.reset(new cache_interface(*this));
d->session.reset(new session_interface(*this));
}
std::string context::skin()
{
return d->skin;
}
cache_interface &context::cache()
{
return *d->cache;
}
void context::skin(std::string const &skin)
{
d->skin=skin;
}
void context::run()
{
conn_->async_prepare_request(this,boost::bind(&context::on_request_ready,self(),_1));
}
void context::on_request_ready(bool error)
{
if(error) return;
char const *host = conn_->cgetenv("HTTP_HOST");
char const *path_info = conn_->cgetenv("PATH_INFO");
char const *script_name = conn_->cgetenv("SCRIPT_NAME");
std::string matched;
booster::intrusive_ptr<application> app = service().applications_pool().get(host,script_name,path_info,matched);
if(!app) {
response().io_mode(http::response::asynchronous);
response().make_error_response(http::response::not_found);
async_complete_response();
return;
}
app->assign_context(self());
if(app->is_asynchronous()) {
response().io_mode(http::response::asynchronous);
// Don't post, as context may be reassigned
dispatch(app,matched,false);
}
else {
app->service().thread_pool().post(boost::bind(&context::dispatch,app,matched,true));
}
}
void context::complete_response()
{
response().finalize();
conn_->complete_response();
if(conn_->is_reuseable()) {
booster::shared_ptr<context> cont(new context(conn_));
service().post(boost::bind(&context::run,cont));
}
conn_.reset();
}
// static
void context::dispatch(booster::intrusive_ptr<application> app,std::string url,bool syncronous)
{
try {
if(syncronous)
app->context().session().load();
app->main(url);
}
catch(request_forgery_error const &e) {
if(app->get_context() && !app->response().some_output_was_written()) {
app->response().make_error_response(http::response::forbidden);
}
}
catch(std::exception const &e){
BOOSTER_ERROR("cppcms") << "Caught exception ["<<e.what()<<"]\n" << booster::trace(e) ;
if(app->get_context()) {
if(!app->response().some_output_was_written()) {
if(app->service().cached_settings().security.display_error_message) {
std::ostringstream ss;
ss << e.what() << '\n';
ss << booster::trace(e);
app->response().make_error_response(http::response::internal_server_error,ss.str());
}
else
app->response().make_error_response(http::response::internal_server_error);
}
}
}
if(app->get_context()) {
if(syncronous) {
app->context().complete_response();
}
else {
app->context().async_complete_response();
}
}
}
namespace {
void wrapper(context::handler const &h,bool r)
{
h(r ? context::operation_aborted : context::operation_completed);
}
}
void context::async_flush_output(context::handler const &h)
{
if(response().io_mode() != http::response::asynchronous) {
throw cppcms_error("Can't use asynchronouse operations when I/O mode is synchronous");
}
conn_->async_write_response(
response(),
false,
boost::bind(wrapper,h,_1));
}
void context::async_complete_response()
{
response().finalize();
if(response().io_mode() == http::response::asynchronous || response().io_mode() == http::response::asynchronous_raw) {
conn_->async_write_response(
response(),
true,
boost::bind(&context::try_restart,self(),_1));
return;
}
conn_->async_complete_response(boost::bind(&context::try_restart,self(),_1));
}
void context::try_restart(bool e)
{
if(e) return;
if(conn_->is_reuseable()) {
booster::shared_ptr<context> cont(new context(conn_));
cont->run();
}
conn_.reset();
}
booster::shared_ptr<context> context::self()
{
return shared_from_this();
}
context::~context()
{
}
void context::async_on_peer_reset(booster::callback<void()> const &h)
{
conn_->aync_wait_for_close_by_peer(h);
}
impl::cgi::connection &context::connection()
{
return *conn_;
}
cppcms::service &context::service()
{
return conn_->service();
}
http::request &context::request()
{
return d->request;
}
http::response &context::response()
{
return *d->response;
}
json::value const &context::settings()
{
return conn_->service().settings();
}
std::locale context::locale()
{
return d->locale;
}
void context::locale(std::locale const &new_locale)
{
d->locale=new_locale;
if(response().some_output_was_written())
response().out().imbue(d->locale);
}
void context::locale(std::string const &name)
{
locale(service().locale(name));
}
session_interface &context::session()
{
return *d->session;
}
} // http
} // cppcms
MongoDB Logo MongoDB