PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/AObjectServer/AOS_Base/AOSConfiguration.hpp

http://aobjectserver.codeplex.com
C++ Header | 564 lines | 141 code | 87 blank | 336 comment | 0 complexity | bda527fe4a2e15abcd6502a6786cc318 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. Written by Alex Chachanashvili
  3. $Id: AOSConfiguration.hpp 336 2009-01-15 22:11:41Z achacha $
  4. */
  5. #ifndef INCLUDED__AOSConfiguration_HPP__
  6. #define INCLUDED__AOSConfiguration_HPP__
  7. #include "apiAOS_Base.hpp"
  8. #include "AOSAdminInterface.hpp"
  9. #include "AOSDirectoryConfig.hpp"
  10. #include "ABitArray.hpp"
  11. #include "AFileSystem.hpp"
  12. #include "AXmlDocument.hpp"
  13. #include "AXmlElement.hpp"
  14. class AUrl;
  15. class AFile_Socket;
  16. class AOSController;
  17. class ASynchronization;
  18. class ALog;
  19. class AOSServices;
  20. class AOSContext;
  21. /*!
  22. Global configuration
  23. */
  24. class AOS_BASE_API AOSConfiguration : public AOSAdminInterface
  25. {
  26. public:
  27. //! Number of times to retry reading first char in HTTP header
  28. static int FIRST_CHAR_RETRIES;
  29. //! Initial sleep time waiting on reading first char
  30. static int SLEEP_STARTTIME;
  31. //! Increment the sleep every retry if first char is not read
  32. static int SLEEP_INCREMENT;
  33. //! If HTTP pipelining is enabled
  34. static bool IS_HTTP_PIPELINING_ENABLED;
  35. //! If gzip compression is enabled
  36. static bool GZIP_IS_ENABLED;
  37. //! Minimum size of content to compress
  38. static size_t GZIP_MIN_SIZE;
  39. //! Default gzip compression level
  40. static int GZIP_DEFAULT_LEVEL;
  41. //! Sleep time when flushing data and socket is unavailable
  42. static int UNAVAILABLE_SLEEP_TIME;
  43. //! Number of retries before considering socket unavailable for too long
  44. static int UNAVAILABLE_RETRIES;
  45. //! Class name
  46. static const AString CLASS;
  47. //! Map of locale to directory
  48. //! getPrev() points to parent locale as fallthrough, getNext() is always NULL
  49. class LocaleDirInfo : public ABase
  50. {
  51. public:
  52. LocaleDirInfo() {}
  53. LocaleDirInfo(AFilename& f) : dir(f) {}
  54. LocaleDirInfo(const LocaleDirInfo& that) : dir(that.dir) {}
  55. LocaleDirInfo& operator=(const LocaleDirInfo& that) { dir.set(that.dir); return *this; }
  56. bool operator< (const LocaleDirInfo& that) { return dir < that.dir; }
  57. AFilename dir;
  58. };
  59. //! Map of locale name to a directory
  60. typedef std::map<AString, LocaleDirInfo> MAP_LOCALE_DIRS;
  61. public:
  62. /*!
  63. ctor
  64. @param baseDir where aos_root directory is to be found
  65. @param services reference to AOSServices object
  66. */
  67. AOSConfiguration(const AFilename& baseDir, AOSServices& services);
  68. //! virtual dtor
  69. virtual ~AOSConfiguration();
  70. //! Path to the database URL
  71. static const AString DATABASE_URL;
  72. //! Path to the database connection to create per URL
  73. static const AString DATABASE_CONNECTIONS;
  74. /*!
  75. Configuration XML document's root element
  76. Read only
  77. @return constant reference to AXmlElement that is the configuration root
  78. */
  79. const AXmlElement& getConfigRoot() const;
  80. /*!
  81. Configuration XML document's root element
  82. Modifiable
  83. @return reference to AXmlElement that is the configuration root
  84. */
  85. AXmlElement& useConfigRoot();
  86. /*!
  87. Base directory
  88. @return constant reference to AFilename
  89. */
  90. const AFilename& getBaseDir() const;
  91. /*!
  92. Base website directory
  93. @return constant reference to AFilename
  94. */
  95. const AFilename& getBaseWebSiteDir() const;
  96. /*!
  97. Base locale for directories without locale specification
  98. Used for data/ static/ directories
  99. @return constant reference to the base locale string (defaults to en-us)
  100. */
  101. const AString& getBaseLocale() const;
  102. /*!
  103. Get a map of locales to static directories
  104. @return map of locales to static directories
  105. */
  106. const AOSConfiguration::MAP_LOCALE_DIRS& getLocaleStaticDirs() const;
  107. /*!
  108. Get a map of locales to data directories
  109. @return map of locales to data directories
  110. */
  111. const AOSConfiguration::MAP_LOCALE_DIRS& getLocaleDataDirs() const;
  112. /*!
  113. Load XML configuration for a given module in config directory
  114. Attached XML contents to the /config root
  115. @param filename of the module config to load
  116. */
  117. void loadConfig(const AFilename& filename);
  118. /*!
  119. Load all the controllers from the file system
  120. Checks dynamic and static directories and generates the website based on them
  121. */
  122. void loadControllers();
  123. /*!
  124. Checks all the loaded controllers against executors in the AOSServices
  125. */
  126. bool validateControllers();
  127. /*!
  128. Convert a given URl to the reported values
  129. server and port are used as per configuration
  130. @param url to convert
  131. */
  132. void convertUrlToReportedServerAndPort(AUrl& url) const;
  133. /*!
  134. Reported hostname
  135. Used when load balanced and a common DNS entry is desired
  136. @return constant reference to reported hostname
  137. */
  138. const AString& getReportedHostname() const;
  139. /*!
  140. Reported HTTP port
  141. Used when load balanced and a common DNS entry is desired
  142. @return reported HTTP port
  143. */
  144. int getReportedHttpPort() const;
  145. /*!
  146. Reported HTTPS port
  147. Used when load balanced and a common DNS entry is desired
  148. @return reported HTTPS port
  149. */
  150. int getReportedHttpsPort() const;
  151. /*!
  152. Set reported hostname
  153. @param configPath path in the config file to use content as hostname
  154. */
  155. void setReportedHostname(const AString& configPath);
  156. /*!
  157. Set reported port
  158. @param configPath path in the config file to use content as port
  159. */
  160. void setReportedHttpPort(const AString& configPath);
  161. /*!
  162. Set reported secure port
  163. @param configPath path in the config file to use content as port
  164. */
  165. void setReportedHttpsPort(const AString& configPath);
  166. /*!
  167. Reported 'Server:' value in the HTTP response header
  168. @return constant reference to the server name
  169. */
  170. const AString& getReportedServer() const;
  171. /*!
  172. Set server name
  173. @param configPath path in the config file to use content as server name
  174. */
  175. void setReportedServer(const AString& configPath);
  176. /*!
  177. Admin base directory
  178. @return constant reference to the AFilename
  179. */
  180. const AFilename& getAdminBaseHttpDir() const;
  181. /*!
  182. Set default filename
  183. @param filename to use as default (index.html is default)
  184. */
  185. void setAosDefaultFilename(const AString& filename);
  186. /*!
  187. Get default filename
  188. @return constant reference to the AFilename
  189. */
  190. const AString& getAosDefaultFilename() const;
  191. /*!
  192. Get default input processor registered class name
  193. @return constant reference to the AFilename
  194. */
  195. const AString& getAosDefaultInputProcessor() const;
  196. /*!
  197. Set default input processor
  198. @param name of the registered input processor (no default)
  199. */
  200. void setAosDefaultInputProcessor(const AString& name);
  201. /*!
  202. Get default ouput generator registered class name
  203. @return constant reference to the AFilename
  204. */
  205. const AString& getAosDefaultOutputGenerator() const;
  206. /*!
  207. Set default output generator
  208. @param name of the registered output generator (xml is default)
  209. */
  210. void setAosDefaultOutputGenerator(const AString& name);
  211. /*!
  212. Get the default mime type when it cannot be matched with extension
  213. @return configured default MIME type
  214. */
  215. const AString& getDefaultMimeType() const;
  216. /*!
  217. Map mime type from extension
  218. @param ext Extension to try and map
  219. @param target appended with extension if found
  220. @return true if extension is mapped and value was found, false if extension is not mapped
  221. */
  222. bool getMimeTypeFromExt(const AString& ext, AString& target) const;
  223. /*!
  224. Set the mime type based on extension or use configured default if extension is not found
  225. @param ext Extension to try and map, if not found will use default
  226. @param responseHeader AHTTPResponseHeader that will get set
  227. */
  228. void setMimeTypeFromExt(const AString& ext, AHTTPResponseHeader& responseHeader);
  229. /*!
  230. Set the mime type based on extension or use configured default if extension is not found
  231. @param ext Extension to try and map, if not found will use default
  232. @param context AOSContext contains the HTTP response header that will get set
  233. */
  234. void setMimeTypeFromExt(const AString& ext, AOSContext& context);
  235. /*!
  236. Set the MIME type in response header based on the extension found in the request header's URL
  237. NOTE: If extension mapping not found then default is set
  238. @param context AOSContext contains both request and response header
  239. */
  240. void setMimeTypeFromRequestExt(AOSContext& context);
  241. /*!
  242. Location of the config directory
  243. This directory is not intended to be localized (see data/ and static/)
  244. @return constant reference to AFilename object
  245. */
  246. const AFilename& getAosBaseConfigDirectory() const;
  247. /*!
  248. Location of the dynamic directory (controllers)
  249. This directory is not intended to be localized (see data/ and static/)
  250. @return constant reference to AFilename object
  251. */
  252. const AFilename& getAosBaseDynamicDirectory() const;
  253. /*!
  254. Location of the data directory based on default locale
  255. @return constant reference to AFilename object
  256. */
  257. const AFilename& getAosBaseDataDirectory() const;
  258. /*!
  259. Location of the static directory based on default locale
  260. @return constant reference to AFilename object
  261. */
  262. const AFilename& getAosBaseStaticDirectory() const;
  263. /*!
  264. Location of the data directory based on locale, if no locale specific one found, base returned instead
  265. @param request to check HTTP request for Accept-Languages: pair for correct directory
  266. @param target to receive localized directory
  267. */
  268. void getAosDataDirectory(AHTTPRequestHeader& request, AFilename& target) const;
  269. /*!
  270. Location of the static directory based on locale, if no locale specific one found, base returned instead
  271. @param request to check HTTP request for Accept-Languages: pair for correct directory
  272. @param target to receive localized directory
  273. */
  274. void getAosStaticDirectory(AHTTPRequestHeader& request, AFilename& target) const;
  275. /*!
  276. Location of the static directories based on locale, default is appended to end
  277. @param request to check HTTP request for Accept-Languages: pair for correct directory
  278. @param directories to add to, last entry is always the default locale location
  279. */
  280. void getAosStaticDirectoryChain(AHTTPRequestHeader& request, LIST_AFilename& directories);
  281. /*!
  282. Location of the data directories based on locale, default is appended to end
  283. @param request to check HTTP request for Accept-Languages: pair for correct directory
  284. @param directories to add to, last entry is always the default locale location
  285. */
  286. void getAosDataDirectoryChain(AHTTPRequestHeader& request, LIST_AFilename& directories);
  287. /*!
  288. Check is dumpContext is allowed in the configuration file
  289. @return true if allowed
  290. */
  291. bool isDumpContextAllowed() const;
  292. /*!
  293. Check if overrideInput is allowed as parameter
  294. @return true if allowed
  295. */
  296. bool isInputOverrideAllowed() const;
  297. /*!
  298. Check if overrideOutput is allowed as parameter
  299. @return true if allowed
  300. */
  301. bool isOutputOverrideAllowed() const;
  302. /*!
  303. Get a list of XML config files in conf/load from which dynamic libraries to load
  304. @param container for AFileInfo objects for xml config files corresponding to libraries to load
  305. */
  306. void getDynamicModuleConfigsToLoad(AFileSystem::FileInfos& target) const;
  307. /*!
  308. Controller for a given filename (should be absolute based on / as root, as if it was a URL)
  309. Returned pointer should never be deleted by the caller
  310. @param f AFilename
  311. @return constant pointer AOSController for a given AFilename
  312. */
  313. const AOSController * const getController(const AFilename& f) const;
  314. /*!
  315. Controller for a given URL
  316. Returned pointer should never be deleted by the caller
  317. @param url AUrl
  318. @return constant pointer AOSController for a given URL
  319. */
  320. const AOSController* const getController(const AUrl& url) const;
  321. /*!
  322. Directory config for a given URL
  323. Returned pointer should never be deleted by the caller
  324. @param f AFilename
  325. @return constant pointer AOSDirectoryConfig for a given URL
  326. */
  327. const AOSDirectoryConfig* const getDirectoryConfig(const AFilename& f) const;
  328. /*!
  329. Directory config for a given URL
  330. Returned pointer should never be deleted by the caller
  331. @param url AUrl
  332. @return constant pointer AOSDirectoryConfig for a given URL
  333. */
  334. const AOSDirectoryConfig* const getDirectoryConfig(const AUrl& url) const;
  335. /*!
  336. Get a set of extensions that are to be compressed based on config
  337. @return constant reference to a set of extension that should be compressed
  338. */
  339. const SET_AString& getCompressedExtensions() const;
  340. /*!
  341. Admin interface
  342. */
  343. virtual void adminEmitXml(AXmlElement& eBase, const AHTTPRequestHeader& request);
  344. virtual void adminProcessAction(AXmlElement& eBase, const AHTTPRequestHeader& request);
  345. virtual const AString& getClass() const;
  346. /*!
  347. ADebugDumpable
  348. */
  349. virtual void debugDump(std::ostream& os = std::cerr, int indent = 0x0) const;
  350. private:
  351. // Services
  352. AOSServices& m_Services;
  353. // Directory where all info lives for this instance
  354. AFilename m_BaseDir;
  355. // Directory where all website content info lives for this instance
  356. AFilename m_BaseWebSiteDir;
  357. // Configuration bits and Ini profile
  358. ABitArray m_ConfigBits;
  359. // Controllers
  360. typedef std::map<AString, AOSController *> MAP_ASTRING_CONTROLLERPTR;
  361. MAP_ASTRING_CONTROLLERPTR m_ControllerPtrs;
  362. // Directory configs
  363. // Maps absolute path (relative to dynamic root) to AXmlDocument with config
  364. typedef std::map<AString, AOSDirectoryConfig *> MAP_ASTRING_CONFIG;
  365. MAP_ASTRING_CONFIG m_DirectoryConfigs;
  366. // Extensions that should be compressed by default (assuming min size criterion is met)
  367. SET_AString m_CompressedExtensions;
  368. // Initialize statics
  369. void _initStatics();
  370. // Populate locale specific directories
  371. // if ./static/ is used
  372. // ./static.en/ is for en-* locales that don't have their own directory
  373. // ./static.en-gb/ is for en-GB locale and falls through to ./static.en/ and then ./static/
  374. void _populateLocaleDirectories(const AFilename&, AOSConfiguration::MAP_LOCALE_DIRS&);
  375. // Locale maps
  376. MAP_LOCALE_DIRS m_LocaleStaticDirs;
  377. MAP_LOCALE_DIRS m_LocaleDataDirs;
  378. // Locale equivalents map
  379. // loaded from config file and maps language to languare-locale combo (usually when more than one may exist)
  380. MAP_AString_AString m_LocaleRemap;
  381. // Default locale that base directories are in
  382. AString m_BaseLocale;
  383. // Load locale info
  384. void _loadLocaleInfo();
  385. // Get locale specific directory
  386. void _getLocaleAosDirectory(AHTTPRequestHeader& request, AFilename& filename, const AOSConfiguration::MAP_LOCALE_DIRS& dirs, const AFilename& defaultDir) const;
  387. // Get chain of locale directries to look up, last entry is always the default
  388. void _getLocaleAosDirectoryChain(AHTTPRequestHeader& request, LIST_AFilename& directories, const AOSConfiguration::MAP_LOCALE_DIRS& dirs) const;
  389. // Populate the extension set
  390. void _populateGzipCompressionExtensions();
  391. /*!
  392. Load commands
  393. */
  394. void _readController(AFilename&); // May alter the passed filename
  395. void _readDirectoryConfig(AFilename&); // May alter the passed filename
  396. void _postProcessControllerAndConfig(AFileSystem::FileInfos &);
  397. // MIME type lookup
  398. MAP_AString_AString m_ExtToMimeType;
  399. void _readMIMETypes();
  400. // Default MIME type
  401. AString m_DefaultMimeType;
  402. // AOS directories from base
  403. AFilename m_AosBaseConfigDir;
  404. AFilename m_AosBaseStaticDir;
  405. AFilename m_AosBaseDynamicDir;
  406. AFilename m_AosBaseDataDir;
  407. // Internals
  408. AString m_AosDefaultFilename;
  409. AString m_AosDefaultInputProcessor;
  410. AString m_AosDefaultOutputGenerator;
  411. // Admin
  412. AFilename m_AdminBaseHttpDir;
  413. // XML document with all config files overlayed based on load order
  414. AXmlDocument m_Config;
  415. // Reported statics
  416. AString m_ReportedServer;
  417. AString m_ReportedHostname;
  418. int m_ReportedHttpPort;
  419. int m_ReportedHttpsPort;
  420. //Overrides
  421. bool m_IsOverrideInputAllowed;
  422. bool m_IsOverrideOutputAllowed;
  423. bool m_IsDumpContextAllowed;
  424. };
  425. #endif //INCLUDED__AOSConfiguration_HPP__