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.
 
 
 
 
 
 

265 lines
7.4 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 "applications_pool.h"
  21. #include "application.h"
  22. #include "service.h"
  23. #include "cppcms_error.h"
  24. #include "json.h"
  25. #include <set>
  26. #include <vector>
  27. #include "config.h"
  28. #ifdef CPPCMS_USE_EXTERNAL_BOOST
  29. # include <boost/regex.hpp>
  30. # include <boost/shared_ptr.hpp>
  31. #else // Internal Boost
  32. # include <cppcms_boost/regex.hpp>
  33. # include <cppcms_boost/shared_ptr.hpp>
  34. namespace boost = cppcms_boost;
  35. #endif
  36. #include <booster/thread.h>
  37. namespace cppcms {
  38. struct applications_pool::basic_app_data : public booster::noncopyable {
  39. basic_app_data(std::string script) :
  40. script_name(script),
  41. match(0),
  42. use_regex(0)
  43. {
  44. }
  45. basic_app_data(std::string script,std::string pat,int select) :
  46. script_name(script),
  47. expr(pat),
  48. match(select),
  49. use_regex(1)
  50. {
  51. }
  52. std::string script_name;
  53. boost::regex expr;
  54. int match;
  55. bool use_regex;
  56. private:
  57. void check() const
  58. {
  59. if( !script_name.empty()
  60. && script_name[0]!='/'
  61. && script_name[0]!='.'
  62. && script_name!="*")
  63. {
  64. throw cppcms_error("Scipt name should be either '*', start with '.' or '/' or be empty");
  65. }
  66. }
  67. };
  68. struct applications_pool::app_data : public applications_pool::basic_app_data {
  69. app_data(std::string script,std::auto_ptr<applications_pool::factory> f) :
  70. basic_app_data(script),
  71. factory(f),
  72. size(0)
  73. {
  74. }
  75. app_data(std::string script,std::string pat,int select,std::auto_ptr<applications_pool::factory> f) :
  76. basic_app_data(script,pat,select),
  77. factory(f),
  78. size(0)
  79. {
  80. }
  81. std::auto_ptr<applications_pool::factory> factory;
  82. int size;
  83. std::set<application *> pool;
  84. ~app_data()
  85. {
  86. std::set<application *>::iterator p;
  87. for(p=pool.begin();p!=pool.end();++p) {
  88. delete *p;
  89. }
  90. }
  91. };
  92. struct applications_pool::long_running_app_data : public applications_pool::basic_app_data
  93. {
  94. long_running_app_data(std::string script) :
  95. basic_app_data(script)
  96. {
  97. }
  98. long_running_app_data(std::string script,std::string pat,int select) :
  99. basic_app_data(script,pat,select)
  100. {
  101. }
  102. };
  103. struct applications_pool::data {
  104. std::vector<boost::shared_ptr<app_data> > apps;
  105. typedef std::map<application *,boost::shared_ptr<long_running_app_data> > long_running_aps_type;
  106. long_running_aps_type long_running_aps;
  107. int limit;
  108. booster::recursive_mutex mutex;
  109. };
  110. typedef booster::unique_lock<booster::recursive_mutex> lock_it;
  111. applications_pool::applications_pool(service &srv,int limit) :
  112. srv_(&srv),
  113. d(new applications_pool::data())
  114. {
  115. d->limit=limit;
  116. }
  117. applications_pool::~applications_pool()
  118. {
  119. }
  120. std::string applications_pool::script_name()
  121. {
  122. return srv_->settings().get("service.default_script_name","*");
  123. }
  124. void applications_pool::mount(std::auto_ptr<factory> aps)
  125. {
  126. lock_it lock(d->mutex);
  127. d->apps.push_back(boost::shared_ptr<app_data>(new app_data(script_name(),aps)));
  128. }
  129. void applications_pool::mount(std::auto_ptr<factory> aps,std::string path_info,int select)
  130. {
  131. lock_it lock(d->mutex);
  132. d->apps.push_back(boost::shared_ptr<app_data>(new app_data(script_name(),path_info,select,aps)));
  133. }
  134. void applications_pool::mount(std::auto_ptr<factory> aps,std::string script_name)
  135. {
  136. lock_it lock(d->mutex);
  137. d->apps.push_back(boost::shared_ptr<app_data>(new app_data(script_name,aps)));
  138. }
  139. void applications_pool::mount(std::auto_ptr<factory> aps,std::string script_name,std::string path_info,int select)
  140. {
  141. lock_it lock(d->mutex);
  142. d->apps.push_back(boost::shared_ptr<app_data>(new app_data(script_name,path_info,select,aps)));
  143. }
  144. void applications_pool::mount(intrusive_ptr<application> app)
  145. {
  146. lock_it lock(d->mutex);
  147. d->long_running_aps[app.get()]=
  148. boost::shared_ptr<long_running_app_data>(new long_running_app_data(script_name()));
  149. }
  150. void applications_pool::mount(intrusive_ptr<application> app,std::string path_info,int select)
  151. {
  152. lock_it lock(d->mutex);
  153. d->long_running_aps[app.get()]=
  154. boost::shared_ptr<long_running_app_data>(new long_running_app_data(script_name(),path_info,select));
  155. }
  156. void applications_pool::mount(intrusive_ptr<application> app,std::string script_name)
  157. {
  158. lock_it lock(d->mutex);
  159. d->long_running_aps[app.get()]=
  160. boost::shared_ptr<long_running_app_data>(new long_running_app_data(script_name));
  161. }
  162. void applications_pool::mount(intrusive_ptr<application> app,std::string script_name,std::string path_info,int select)
  163. {
  164. lock_it lock(d->mutex);
  165. d->long_running_aps[app.get()]=
  166. boost::shared_ptr<long_running_app_data>(new long_running_app_data(script_name,path_info,select));
  167. }
  168. bool applications_pool::matched(basic_app_data &data,std::string script_name,std::string path_info,std::string &matched)
  169. {
  170. std::string const expected_name=data.script_name;
  171. if(expected_name!="*" && !expected_name.empty()) {
  172. if(expected_name[0]=='/') {
  173. if(script_name!=expected_name)
  174. return false;
  175. }
  176. else { // if(sn[0]=='.')
  177. if( script_name.size() <= expected_name.size()
  178. || script_name.substr(script_name.size() - expected_name.size())!=expected_name)
  179. {
  180. return false;
  181. }
  182. }
  183. }
  184. else if(expected_name=="*" && script_name.empty())
  185. return false;
  186. else if(expected_name.empty() && !script_name.empty())
  187. return false;
  188. boost::cmatch match;
  189. if(!data.use_regex) {
  190. matched=path_info;
  191. return true;
  192. }
  193. else if(boost::regex_match(path_info.c_str(),match,data.expr)) {
  194. matched=match[data.match];
  195. return true;
  196. }
  197. else {
  198. return false;
  199. }
  200. }
  201. intrusive_ptr<application> applications_pool::get(std::string script_name,std::string path_info,std::string &m)
  202. {
  203. lock_it lock(d->mutex);
  204. for(unsigned i=0;i<d->apps.size();i++) {
  205. if(!matched(*d->apps[i],script_name,path_info,m))
  206. continue;
  207. if(d->apps[i]->pool.empty()) {
  208. intrusive_ptr<application> app=(*d->apps[i]->factory)(*srv_).release();
  209. app->pool_id(i);
  210. return app;
  211. }
  212. d->apps[i]->size--;
  213. intrusive_ptr<application> app(*(d->apps[i]->pool.begin()));
  214. d->apps[i]->pool.erase(app.get());
  215. return app;
  216. }
  217. for(data::long_running_aps_type::iterator p=d->long_running_aps.begin();p!=d->long_running_aps.end();++p){
  218. if(!matched(*p->second,script_name,path_info,m))
  219. continue;
  220. intrusive_ptr<application> app=p->first;
  221. return app;
  222. }
  223. return 0;
  224. }
  225. void applications_pool::put(application *app)
  226. {
  227. lock_it lock(d->mutex);
  228. if(!app) return;
  229. int id=app->pool_id();
  230. if(id < 0) {
  231. d->long_running_aps.erase(app);
  232. delete app;
  233. return;
  234. }
  235. if(unsigned(id) >= d->apps.size() || d->apps[id]->size >= d->limit) {
  236. delete app;
  237. return;
  238. }
  239. d->apps[id]->pool.insert(app);
  240. d->apps[id]->size++;
  241. }
  242. } //cppcms