Menu

[r2375]: / framework / trunk / cppcms / http_context.h  Maximize  Restore  History

Download this file

280 lines (245 with data), 8.1 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
278
279
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCMS_HTTP_CONTEXT_H
#define CPPCMS_HTTP_CONTEXT_H
#include <cppcms/defs.h>
#include <booster/hold_ptr.h>
#include <booster/intrusive_ptr.h>
#include <booster/shared_ptr.h>
#include <booster/enable_shared_from_this.h>
#include <booster/callback.h>
#include <booster/noncopyable.h>
#include <locale>
namespace cppcms {
class service;
class application;
class application_specific_pool;
class cache_interface;
class session_interface;
namespace json { class value; }
namespace impl { namespace cgi { class connection; } }
///
/// \brief This namespace represent classes that are directly connected to handing HTTP requests and responses
///
namespace http {
class request;
class response;
class file;
///
/// \brief context is a central class that holds all specific connection related information.
/// It encapsulates CGI request and response, cache, session and locale information
///
/// Instance of this class is created upon client requests, it provides access to all
/// connection related interfaces. This class is unique per each applications hierarchy
/// and destroyed when HTTP request/response is completed
///
class CPPCMS_API context :
public booster::noncopyable,
public booster::enable_shared_from_this<context>
{
public:
/// \cond INTERNAL
context(booster::shared_ptr<impl::cgi::connection> conn);
~context();
impl::cgi::connection &connection();
void run();
/// \endcond
///
/// Get an interface to HTTP request
///
http::request &request();
///
/// Get an interface to HTTP response
///
http::response &response();
///
/// Get global settings. Same as cppcms::service::settings
///
json::value const &settings();
///
/// Get an interface to CppCMS Cache
///
cache_interface &cache();
///
/// Get an interface to current session
///
/// Note, when using asynchronous CppCMS applications, session data is not fetched
/// and is not updated, because session access may be not cheap, So when using
/// session_interface in asynchronous application make sure you call session_inerface::load
/// member function
///
session_interface &session();
///
/// Get current context locale
///
std::locale locale();
///
/// Set locale explicitly. Note, it changes the locale of the response().out() stream as
/// well
///
void locale(std::locale const &new_locale);
///
/// Set locale by name. Similar to locale(service().generator(name)).
///
/// Note: it changes the locale of the response().out() stream as well
///
void locale(std::string const &name);
///
/// Get the central service instance
///
cppcms::service &service();
///
/// Get current views skin name
///
std::string skin();
///
/// Set current views skin name
///
void skin(std::string const &name);
typedef enum {
operation_completed, ///< Asynchronous operation completed successfully
operation_aborted ///< Asynchronous operation was canceled
} completion_type;
typedef booster::callback<void(completion_type)> handler;
///
/// Send all pending output data to the client and
/// finalize the connection. Note, you can't use this
/// object for communication any more.
///
void complete_response();
///
/// Send all pending output data to the client and
/// finalize the connection. Note, you can't use this
/// object for communication any more.
///
void async_complete_response();
///
/// Send all pending data to user, when operation is complete
/// call handler \a h with status.
///
/// Note: if the status is operation_aborted, you can't use
/// this connection any more, the peer gone.
///
void async_flush_output(handler const &h);
///
/// Set handler for peer reset events. It is useful to cleanup
/// connections that had timeout or just disconnected by user
///
/// Notes:
///
/// -# if async_complete_response was called, handler would not
/// be called any more.
/// -# If async_flush_output fails, this does not mean that
/// this handler would be called as well, so you need to check both
///
void async_on_peer_reset(booster::callback<void()> const &h);
///
/// Submit the context to alternative pool - allows to transfer context from application to application, \a matched_url
/// would be given to application::main for processing
///
/// Note:
///
/// - no output should be itransfered on this context
/// - the context MUST be detached for existing application
///
/// This function can be called from any thread
///
/// \ver{v1_2}
void submit_to_pool(booster::shared_ptr<application_specific_pool> pool,std::string const &matched_url);
///
/// Submit the context to alternative application - allows to transfer context from application to application, \a matched_url
/// would be given to application::main for processing
///
/// Note:
///
/// - the app should be asynchronous
/// - no output should be itransfered on this context
/// - the context MUST be detached for existing application
///
/// This function can be called from any thread
///
/// \ver{v1_2}
void submit_to_asynchronous_application(booster::intrusive_ptr<application> app,std::string const &matched_url);
private:
struct holder { virtual ~holder() {} };
template<typename T>
struct specific_holder : public holder {
specific_holder(T *ptr) : p(ptr) {}
virtual ~specific_holder() {}
booster::hold_ptr<T> p;
};
public:
///
/// Get context specific value of type T binded to context. If none is stored or
/// type mismatched NULL is returned
///
/// \ver{v1_2}
template<typename T>
T *get_specific()
{
specific_holder<T> *sh=dynamic_cast<specific_holder<T> *>(get_holder());
if(!sh)
return 0;
return sh->p.get();
}
///
/// Reset context specific value of type T binded to context. Old value is deleted
///
/// \ver{v1_2}
template<typename T>
void reset_specific(T *ptr = 0)
{
if(ptr == 0) {
set_holder(0);
return;
}
specific_holder<T> *sh=dynamic_cast<specific_holder<T> *>(get_holder());
if(sh) {
sh->p.reset(ptr);
}
else {
specific_holder<T> *sh = new specific_holder<T>(ptr);
set_holder(sh);
}
}
///
/// Release context specific value binded to context.
///
/// \ver{v1_2}
template<typename T>
T *release_specific()
{
T *r = 0;
specific_holder<T> *sh=dynamic_cast<specific_holder<T> *>(get_holder());
if(sh) {
r = sh->p.release();
}
set_holder(0);
return r;
}
private:
void set_holder(holder *p);
holder *get_holder();
friend class impl::cgi::connection;
int on_content_progress(size_t n);
int on_headers_ready();
int translate_exception();
void make_error_message(std::exception const &e);
void on_request_ready(bool error);
void submit_to_pool_internal(booster::shared_ptr<application_specific_pool> pool,std::string const &matched,bool now);
static void dispatch(booster::shared_ptr<application_specific_pool> const &pool,booster::shared_ptr<context> const &self,std::string const &url);
static void dispatch(booster::intrusive_ptr<application> const &app,std::string const &url,bool syncronous);
void try_restart(bool e);
booster::shared_ptr<context> self();
struct _data;
booster::hold_ptr<_data> d;
booster::shared_ptr<impl::cgi::connection> conn_;
};
}
};
#endif
MongoDB Logo MongoDB