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.
 
 
 
 
 
 

242 lines
5.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #define CPPCMS_SOURCE
  20. #include "cgi_api.h"
  21. #include "service.h"
  22. #include "http_context.h"
  23. #include "http_request.h"
  24. #include "http_response.h"
  25. #include "application.h"
  26. #include "applications_pool.h"
  27. #include "thread_pool.h"
  28. #include "views_pool.h"
  29. #include "cache_interface.h"
  30. #include "session_interface.h"
  31. #include "cppcms_error.h"
  32. #include "config.h"
  33. #ifdef CPPCMS_USE_EXTERNAL_BOOST
  34. # include <boost/bind.hpp>
  35. #else // Internal Boost
  36. # include <cppcms_boost/bind.hpp>
  37. namespace boost = cppcms_boost;
  38. #endif
  39. namespace cppcms {
  40. namespace http {
  41. struct context::data {
  42. std::locale locale;
  43. std::string skin;
  44. http::request request;
  45. std::auto_ptr<http::response> response;
  46. std::auto_ptr<cache_interface> cache;
  47. std::auto_ptr<session_interface> session;
  48. data(context &cntx) :
  49. locale(cntx.connection().service().locale()),
  50. request(cntx.connection())
  51. {
  52. }
  53. };
  54. context::context(intrusive_ptr<impl::cgi::connection> conn) :
  55. conn_(conn)
  56. {
  57. d.reset(new data(*this));
  58. d->response.reset(new http::response(*this));
  59. skin(service().views_pool().default_skin());
  60. d->cache.reset(new cache_interface(*this));
  61. d->session.reset(new session_interface(*this));
  62. }
  63. std::string context::skin()
  64. {
  65. return d->skin;
  66. }
  67. cache_interface &context::cache()
  68. {
  69. return *d->cache;
  70. }
  71. void context::skin(std::string const &skin)
  72. {
  73. d->skin=skin;
  74. }
  75. void context::run()
  76. {
  77. conn_->async_prepare_request(d->request,boost::bind(&context::on_request_ready,self(),_1));
  78. }
  79. void context::on_request_ready(bool error)
  80. {
  81. if(error) return;
  82. std::string path_info = conn_->getenv("PATH_INFO");
  83. std::string script_name = conn_->getenv("SCRIPT_NAME");
  84. std::string matched;
  85. intrusive_ptr<application> app = service().applications_pool().get(script_name,path_info,matched);
  86. if(!app) {
  87. response().io_mode(http::response::asynchronous);
  88. response().make_error_response(http::response::not_found);
  89. async_complete_response();
  90. return;
  91. }
  92. app->assign_context(self());
  93. if(app->is_asynchronous()) {
  94. response().io_mode(http::response::asynchronous);
  95. app->service().post(boost::bind(&context::dispatch,app,matched,false));
  96. }
  97. else {
  98. app->service().thread_pool().post(boost::bind(&context::dispatch,app,matched,true));
  99. }
  100. }
  101. // static
  102. void context::dispatch(intrusive_ptr<application> app,std::string url,bool syncronous)
  103. {
  104. try {
  105. if(syncronous)
  106. app->context().session().load();
  107. app->main(url);
  108. }
  109. catch(std::exception const &e){
  110. if(app->get_context() && !app->response().some_output_was_written()) {
  111. app->response().make_error_response(http::response::internal_server_error,e.what());
  112. }
  113. else {
  114. // TODO log it
  115. std::cerr<<"Catched excepion ["<<e.what()<<"]"<<std::endl;
  116. }
  117. }
  118. }
  119. namespace {
  120. void wrapper(context::handler const &h,bool r)
  121. {
  122. h(r ? context::operation_aborted : context::operation_completed);
  123. }
  124. }
  125. void context::async_flush_output(context::handler const &h)
  126. {
  127. if(response().io_mode() != http::response::asynchronous) {
  128. throw cppcms_error("Can't use asynchronouse operations when I/O mode is synchronous");
  129. }
  130. conn_->async_write_response(
  131. response(),
  132. false,
  133. boost::bind(wrapper,h,_1));
  134. }
  135. void context::async_complete_response()
  136. {
  137. if(response().io_mode() == http::response::asynchronous || response().io_mode() == http::response::asynchronous_raw) {
  138. conn_->async_write_response(
  139. response(),
  140. true,
  141. boost::bind(&context::try_restart,self(),_1));
  142. return;
  143. }
  144. conn_->async_complete_response(boost::bind(&context::try_restart,self(),_1));
  145. }
  146. void context::try_restart(bool e)
  147. {
  148. if(e) return;
  149. if(conn_->is_reuseable()) {
  150. intrusive_ptr<context> cont=new context(conn_);
  151. cont->run();
  152. }
  153. conn_=0;
  154. }
  155. intrusive_ptr<context> context::self()
  156. {
  157. intrusive_ptr<context> ptr(this);
  158. return ptr;
  159. }
  160. context::~context()
  161. {
  162. }
  163. void context::async_on_peer_reset(booster::function<void()> const &h)
  164. {
  165. // For some wired can't go without bind on SunCC
  166. conn_->aync_wait_for_close_by_peer(boost::bind(h));
  167. }
  168. impl::cgi::connection &context::connection()
  169. {
  170. return *conn_;
  171. }
  172. cppcms::service &context::service()
  173. {
  174. return conn_->service();
  175. }
  176. http::request &context::request()
  177. {
  178. return d->request;
  179. }
  180. http::response &context::response()
  181. {
  182. return *d->response;
  183. }
  184. json::value const &context::settings()
  185. {
  186. return conn_->service().settings();
  187. }
  188. std::locale context::locale()
  189. {
  190. return d->locale;
  191. }
  192. void context::locale(std::locale const &new_locale)
  193. {
  194. d->locale=new_locale;
  195. if(response().some_output_was_written())
  196. response().out().imbue(d->locale);
  197. }
  198. void context::locale(std::string const &name)
  199. {
  200. locale(service().locale(name));
  201. }
  202. session_interface &context::session()
  203. {
  204. return *d->session;
  205. }
  206. } // http
  207. } // cppcms