common.H 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. * */
  13. /*
  14. * common.H
  15. *
  16. * Created on: Apr 6, 2013
  17. * Author: xaxaxa
  18. */
  19. #ifndef CPPSP_COMMON_H_
  20. #define CPPSP_COMMON_H_
  21. #include <cpoll/cpoll.H>
  22. #include <vector>
  23. #include <unordered_map>
  24. #include "page.H"
  25. using namespace std;
  26. typedef CP::String String;
  27. namespace cppsp
  28. {
  29. extern const char* gxx;
  30. class ParserException: public std::exception
  31. {
  32. public:
  33. string message;
  34. int32_t number;
  35. ParserException();
  36. ParserException(int32_t number);
  37. ParserException(string message, int32_t number = 0);
  38. ~ParserException() throw ();
  39. const char* what() const throw ();
  40. };
  41. class CompileException: public std::exception
  42. {
  43. public:
  44. string message;
  45. string compilerOutput;
  46. CompileException();
  47. CompileException(string message);
  48. ~CompileException() throw ();
  49. const char* what() const throw ();
  50. };
  51. /**
  52. Internal API.
  53. */
  54. class loadedPage
  55. {
  56. public:
  57. Server* srv;
  58. loadedPage& operator=(const loadedPage& other) = delete;
  59. timespec lastLoad { 0, 0 }; //CLOCK_REALTIME
  60. timespec lastCheck { 0, 0 }; //CLOCK_MONOTONIC
  61. void* dlHandle;
  62. const uint8_t* stringTable;
  63. int stringTableLen;
  64. typedef int (*getObjectSize_t)();
  65. typedef Page* (*createObject_t)(void* mem);
  66. typedef Page* (*createObject1_t)(RGC::Allocator* alloc);
  67. typedef void (*initModule_t)(Server* s);
  68. typedef void (*deinitModule_t)();
  69. int (*getObjectSize)();
  70. createObject_t createObject;
  71. createObject1_t createObject1;
  72. RGC::Ref<CP::File> compile_fd;
  73. CP::MemoryStream ms;
  74. //Delegate<void(loadedPage&)> compileCB;
  75. struct _loadCB
  76. {
  77. RGC::Allocator* alloc;
  78. //void* parameter is either the constructed page (if doCreate is true), or
  79. //the dlopen()ed handle
  80. Delegate<void(void*, exception* ex)> cb;
  81. bool doCreate;
  82. void operator()(loadedPage* This, exception* ex);
  83. };
  84. vector<_loadCB> loadCB;
  85. string path;
  86. string cPath;
  87. string txtPath;
  88. string dllPath;
  89. int stringTableFD;
  90. pid_t compilerPID;
  91. bool loaded;
  92. bool compiling;
  93. void readCB(int r);
  94. void deleteTmpfiles();
  95. void afterCompile(bool success);
  96. void beginRead();
  97. void doCompile(CP::Poll& p, string wd, const vector<string>& cxxopts);
  98. void doLoad();
  99. void doUnload();
  100. Page* doCreate(RGC::Allocator* a);
  101. loadedPage();
  102. ~loadedPage();
  103. //returns: 0: no-op; 1: should reload; 2: should recompile
  104. int shouldCompile();
  105. };
  106. /**
  107. Internal API.
  108. */
  109. struct staticPage
  110. {
  111. String data;
  112. timespec lastLoad { 0, 0 }; //CLOCK_REALTIME
  113. timespec lastCheck { 0, 0 }; //CLOCK_MONOTONIC
  114. timespec lastUse; //CLOCK_MONOTONIC
  115. string path;
  116. bool loaded;
  117. void doLoad();
  118. void doUnload();
  119. bool shouldReload();
  120. staticPage();
  121. ~staticPage();
  122. };
  123. /**
  124. Internal API.
  125. */
  126. class cppspManager
  127. {
  128. public:
  129. CP::StringPool sp;
  130. unordered_map<String, loadedPage*> cache;
  131. unordered_map<String, staticPage*> staticCache;
  132. vector<string> cxxopts;
  133. timespec curTime { 0, 0 };
  134. timespec curClockTime { 0, 0 }; //CLOCK_REALTIME
  135. String curRFCTime;
  136. int threadID;
  137. cppspManager();
  138. ~cppspManager();
  139. void loadPage(CP::Poll& p, String wd, String path, RGC::Allocator* a,
  140. Delegate<void(Page*, exception* ex)> cb);
  141. void loadModule(CP::Poll& p, Server* srv, String wd, String path,
  142. Delegate<void(void*, exception* ex)> cb);
  143. String loadStaticPage(String path);
  144. bool shouldCheck(loadedPage& p);
  145. bool shouldCheck(staticPage& p);
  146. };
  147. /**
  148. Internal function. Parses a .cppsp page.
  149. @unmaintained_api
  150. */
  151. void doParse(const char* name, const char* in, int inLen, CP::Stream& out, CP::Stream& st_out,
  152. vector<string>& c_opts);
  153. /**
  154. Internal function. Compiles a .cppsp page.
  155. @return file descriptor connected to the standard output of the compiler.
  156. @unmaintained_api
  157. */
  158. CP::File* compilePage(string wd, string path, const vector<string>& cxxopts, pid_t& pid);
  159. /**
  160. Combine two paths. Result is stored in buf. Does not write a null byte.
  161. The memory block pointed to by buf must be at least l1 + l2 bytes.
  162. @return the length of the string written to buf
  163. */
  164. int combinePath(const char* p1, int l1, const char* p2, int l2, char* buf);
  165. /**
  166. Combine two paths. Result is stored in buf. Does not write a null byte.
  167. The memory block pointed to by buf must be at least strlen(p1) + strlen(p2) bytes.
  168. @return the length of the string written to buf
  169. */
  170. int combinePath(const char* p1, const char* p2, char* buf);
  171. /**
  172. Combine two paths securely. The resulting path is guaranteed to be under p1. (The user can not use ".." to leave the root directory)
  173. The memory block pointed to by buf must be at least l1 + l2 bytes.
  174. @return the length of the string written to buf
  175. */
  176. int combinePathChroot(const char* p1, int l1, const char* p2, int l2, char* buf);
  177. /**
  178. Combine two paths securely. The resulting path is guaranteed to be under p1. (The user can not use ".." to leave the root directory)
  179. The memory block pointed to by buf must be at least strlen(p1) + strlen(p2) bytes.
  180. @return the length of the string written to buf
  181. */
  182. int combinePathChroot(const char* p1, const char* p2, char* buf);
  183. /**
  184. Combine two paths. Result is allocated from sp.
  185. @return the resulting path
  186. */
  187. String combinePath(String p1, String p2, CP::StringPool& sp);
  188. /**
  189. Combine two paths securely. The resulting path is guaranteed to be under p1. (The user can not use ".." to leave the root directory)
  190. @return the resulting path
  191. */
  192. String combinePathChroot(String p1, String p2, CP::StringPool& sp);
  193. /**
  194. Internal function.
  195. @unmaintained_api
  196. */
  197. cppspManager* cppspManager_new();
  198. /**
  199. Internal function.
  200. @unmaintained_api
  201. */
  202. void cppspManager_delete(cppspManager* mgr);
  203. /**
  204. Internal function.
  205. @unmaintained_api
  206. */
  207. void loadPage(cppspManager* mgr, CP::Poll& p, String wd, String path, RGC::Allocator* a,
  208. Delegate<void(Page*, exception* ex)> cb);
  209. /**
  210. Internal function.
  211. @unmaintained_api
  212. */
  213. void loadModule(cppspManager* mgr, CP::Poll& p, Server* srv, String wd, String path,
  214. Delegate<void(void*, exception* ex)> cb);
  215. /**
  216. Internal function.
  217. @unmaintained_api
  218. */
  219. String loadStaticPage(cppspManager* mgr, String path);
  220. /**
  221. Internal function.
  222. @unmaintained_api
  223. */
  224. vector<string>& CXXOpts(cppspManager* mgr);
  225. /**
  226. Internal function.
  227. @unmaintained_api
  228. */
  229. void updateTime(cppspManager* mgr);
  230. /**
  231. Internal function.
  232. @unmaintained_api
  233. */
  234. void handleError(exception* ex, cppsp::Response& resp, String path);
  235. }
  236. #endif /* COMMON_H_ */