ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

224 lines
4.5 KiB

  1. #define CPPCMS_SOURCE
  2. #include "cgi_api.h"
  3. #include "service.h"
  4. #include "http_context.h"
  5. #include "http_request.h"
  6. #include "http_response.h"
  7. #include "application.h"
  8. #include "applications_pool.h"
  9. #include "thread_pool.h"
  10. #include "views_pool.h"
  11. #include "cache_interface.h"
  12. #include "session_interface.h"
  13. #include "cppcms_error.h"
  14. #include "config.h"
  15. #ifdef CPPCMS_USE_EXTERNAL_BOOST
  16. # include <boost/bind.hpp>
  17. #else // Internal Boost
  18. # include <cppcms_boost/bind.hpp>
  19. namespace boost = cppcms_boost;
  20. #endif
  21. namespace cppcms {
  22. namespace http {
  23. struct context::data {
  24. std::locale locale;
  25. std::string skin;
  26. http::request request;
  27. std::auto_ptr<http::response> response;
  28. std::auto_ptr<cache_interface> cache;
  29. std::auto_ptr<session_interface> session;
  30. data(context &cntx) :
  31. locale(cntx.connection().service().locale()),
  32. request(cntx.connection())
  33. {
  34. }
  35. };
  36. context::context(intrusive_ptr<impl::cgi::connection> conn) :
  37. conn_(conn)
  38. {
  39. d.reset(new data(*this));
  40. d->response.reset(new http::response(*this));
  41. skin(service().views_pool().default_skin());
  42. d->cache.reset(new cache_interface(*this));
  43. d->session.reset(new session_interface(*this));
  44. }
  45. std::string context::skin()
  46. {
  47. return d->skin;
  48. }
  49. cache_interface &context::cache()
  50. {
  51. return *d->cache;
  52. }
  53. void context::skin(std::string const &skin)
  54. {
  55. d->skin=skin;
  56. }
  57. void context::run()
  58. {
  59. conn_->async_prepare_request(d->request,boost::bind(&context::on_request_ready,self(),_1));
  60. }
  61. void context::on_request_ready(bool error)
  62. {
  63. if(error) return;
  64. std::string path_info = conn_->getenv("PATH_INFO");
  65. std::string script_name = conn_->getenv("SCRIPT_NAME");
  66. std::string matched;
  67. intrusive_ptr<application> app = service().applications_pool().get(script_name,path_info,matched);
  68. if(!app) {
  69. response().io_mode(http::response::asynchronous);
  70. response().make_error_response(http::response::not_found);
  71. async_complete_response();
  72. return;
  73. }
  74. app->assign_context(self());
  75. if(app->is_asynchronous()) {
  76. response().io_mode(http::response::asynchronous);
  77. app->service().post(boost::bind(&context::dispatch,app,matched,false));
  78. }
  79. else {
  80. app->service().thread_pool().post(boost::bind(&context::dispatch,app,matched,true));
  81. }
  82. }
  83. // static
  84. void context::dispatch(intrusive_ptr<application> app,std::string url,bool syncronous)
  85. {
  86. try {
  87. if(syncronous)
  88. app->context().session().load();
  89. app->main(url);
  90. }
  91. catch(std::exception const &e){
  92. if(app->get_context() && !app->response().some_output_was_written()) {
  93. app->response().make_error_response(http::response::internal_server_error,e.what());
  94. }
  95. else {
  96. // TODO log it
  97. std::cerr<<"Catched excepion ["<<e.what()<<"]"<<std::endl;
  98. }
  99. }
  100. }
  101. namespace {
  102. void wrapper(context::handler const &h,bool r)
  103. {
  104. h(r ? context::operation_aborted : context::operation_completed);
  105. }
  106. }
  107. void context::async_flush_output(context::handler const &h)
  108. {
  109. if(response().io_mode() != http::response::asynchronous) {
  110. throw cppcms_error("Can't use asynchronouse operations when I/O mode is synchronous");
  111. }
  112. conn_->async_write_response(
  113. response(),
  114. false,
  115. boost::bind(wrapper,h,_1));
  116. }
  117. void context::async_complete_response()
  118. {
  119. if(response().io_mode() == http::response::asynchronous) {
  120. conn_->async_write_response(
  121. response(),
  122. true,
  123. boost::bind(&context::try_restart,self(),_1));
  124. return;
  125. }
  126. conn_->async_complete_response(boost::bind(&context::try_restart,self(),_1));
  127. }
  128. void context::try_restart(bool e)
  129. {
  130. if(e) return;
  131. if(conn_->is_reuseable()) {
  132. intrusive_ptr<context> cont=new context(conn_);
  133. cont->run();
  134. }
  135. conn_=0;
  136. }
  137. intrusive_ptr<context> context::self()
  138. {
  139. intrusive_ptr<context> ptr(this);
  140. return ptr;
  141. }
  142. context::~context()
  143. {
  144. }
  145. void context::async_on_peer_reset(util::callback0 const &h)
  146. {
  147. // For some wired can't go without bind on SunCC
  148. conn_->aync_wait_for_close_by_peer(boost::bind(h));
  149. }
  150. impl::cgi::connection &context::connection()
  151. {
  152. return *conn_;
  153. }
  154. cppcms::service &context::service()
  155. {
  156. return conn_->service();
  157. }
  158. http::request &context::request()
  159. {
  160. return d->request;
  161. }
  162. http::response &context::response()
  163. {
  164. return *d->response;
  165. }
  166. json::value const &context::settings()
  167. {
  168. return conn_->service().settings();
  169. }
  170. std::locale context::locale()
  171. {
  172. return d->locale;
  173. }
  174. void context::locale(std::locale const &new_locale)
  175. {
  176. d->locale=new_locale;
  177. if(response().some_output_was_written())
  178. response().out().imbue(d->locale);
  179. }
  180. void context::locale(std::string const &name)
  181. {
  182. locale(service().locale(name));
  183. }
  184. session_interface &context::session()
  185. {
  186. return *d->session;
  187. }
  188. } // http
  189. } // cppcms