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.
 
 
 
 
 
 

245 lines
4.3 KiB

  1. #define CPPCMS_SOURCE
  2. #include "application.h"
  3. #include "http_context.h"
  4. #include "service.h"
  5. #include "cppcms_error.h"
  6. #include "url_dispatcher.h"
  7. #include "intrusive_ptr.h"
  8. #include "applications_pool.h"
  9. #include "http_response.h"
  10. #include "views_pool.h"
  11. #include <set>
  12. #include <vector>
  13. #include "config.h"
  14. #ifdef CPPCMS_USE_EXTERNAL_BOOST
  15. # include <boost/bind.hpp>
  16. #else // Internal Boost
  17. # include <cppcms_boost/bind.hpp>
  18. namespace boost = cppcms_boost;
  19. #endif
  20. namespace cppcms {
  21. struct application::data {
  22. data(cppcms::service *s):
  23. service(s),
  24. conn(0),
  25. pool_id(-1)
  26. {
  27. }
  28. cppcms::service *service;
  29. intrusive_ptr<http::context> conn;
  30. int pool_id;
  31. url_dispatcher url;
  32. std::vector<application *> managed_children;
  33. };
  34. application::application(cppcms::service &srv) :
  35. d(new data(&srv)),
  36. refs_(0)
  37. {
  38. parent_=root_=this;
  39. }
  40. application::~application()
  41. {
  42. for(unsigned i=0;i<d->managed_children.size();i++) {
  43. delete d->managed_children[i];
  44. d->managed_children[i]=0;
  45. }
  46. }
  47. cppcms::service &application::service()
  48. {
  49. return *d->service;
  50. }
  51. json::value const &application::settings()
  52. {
  53. return service().settings();
  54. }
  55. http::request &application::request()
  56. {
  57. return context().request();
  58. }
  59. http::response &application::response()
  60. {
  61. return context().response();
  62. }
  63. url_dispatcher &application::dispatcher()
  64. {
  65. return d->url;
  66. }
  67. intrusive_ptr<http::context> application::get_context()
  68. {
  69. return root()->d->conn;
  70. }
  71. http::context &application::context()
  72. {
  73. if(!root()->d->conn)
  74. throw cppcms_error("Access to unassigned context");
  75. return *root()->d->conn;
  76. }
  77. intrusive_ptr<http::context> application::release_context()
  78. {
  79. intrusive_ptr<http::context> ptr=root()->d->conn;
  80. assign_context(0);
  81. return ptr;
  82. }
  83. bool application::is_asynchronous()
  84. {
  85. return pool_id() < 0;
  86. }
  87. void application::assign_context(intrusive_ptr<http::context> conn)
  88. {
  89. root()->d->conn=conn;
  90. }
  91. void application::pool_id(int id)
  92. {
  93. d->pool_id=id;
  94. }
  95. int application::pool_id()
  96. {
  97. return d->pool_id;
  98. }
  99. application *application::parent()
  100. {
  101. return parent_;
  102. }
  103. application *application::root()
  104. {
  105. return root_;
  106. }
  107. void application::parent(application *app)
  108. {
  109. parent_=app;
  110. root_=app->root();
  111. }
  112. void application::add(application &app)
  113. {
  114. if(app.parent()!=this)
  115. app.parent(this);
  116. }
  117. void application::add(application &app,std::string regex,int part)
  118. {
  119. add(app);
  120. url_dispatcher().mount(regex,app,part);
  121. }
  122. void application::attach(application *app)
  123. {
  124. d->managed_children.push_back(app);
  125. add(*app);
  126. }
  127. void application::main(std::string url)
  128. {
  129. if(!dispatcher().dispatch(url)) {
  130. response().make_error_response(http::response::not_found);
  131. }
  132. }
  133. void application::attach(application *app,std::string regex,int part)
  134. {
  135. d->managed_children.push_back(app);
  136. add(*app,regex,part);
  137. }
  138. void application::render(std::string template_name,base_content &content)
  139. {
  140. service().views_pool().render(context().skin(),template_name,response().out(),content);
  141. }
  142. void application::render(std::string skin,std::string template_name,base_content &content)
  143. {
  144. service().views_pool().render(skin,template_name,response().out(),content);
  145. }
  146. void application::render(std::string template_name,std::ostream &out,base_content &content)
  147. {
  148. service().views_pool().render(context().skin(),template_name,out,content);
  149. }
  150. void application::render(std::string skin,std::string template_name,std::ostream &out,base_content &content)
  151. {
  152. service().views_pool().render(skin,template_name,out,content);
  153. }
  154. cache_interface &application::cache()
  155. {
  156. return context().cache();
  157. }
  158. session_interface &application::session()
  159. {
  160. return context().session();
  161. }
  162. void application::recycle()
  163. {
  164. if(root()->d->conn) {
  165. response().finalize();
  166. context().async_complete_response();
  167. }
  168. assign_context(0);
  169. }
  170. void intrusive_ptr_add_ref(application *app)
  171. {
  172. ++(app->root()->refs_);
  173. }
  174. // REMEMBER THIS IS CALLED FROM DESTRUCTOR!!!
  175. void intrusive_ptr_release(application *app)
  176. {
  177. // it is called in destructors... So be very careful
  178. try {
  179. app = app->root();
  180. long refs=--(app->refs_);
  181. if(refs > 0)
  182. return;
  183. cppcms::service &service=app->service();
  184. try {
  185. app->recycle();
  186. }
  187. catch(...) {
  188. if(app->pool_id() < 0) {
  189. service.applications_pool().put(app);
  190. }
  191. else
  192. delete app;
  193. throw;
  194. }
  195. service.applications_pool().put(app);
  196. // return the application to pool... or delete it if "pooled"
  197. }
  198. catch(...)
  199. {
  200. // FIXME LOG IT?
  201. }
  202. }
  203. } // cppcms