1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4  *
5  * All rights reserved.
6  */
7 
8 #ifndef HTTP_CONFIGURATION_HPP
9 #define HTTP_CONFIGURATION_HPP
10 
11 #include <exception>
12 #include <string>
13 #include <boost/program_options.hpp>
14 #include <boost/cstdint.hpp>
15 
16 // For ::int64_t and ::uint64_t on Windows only
17 #include "Wt/WDllDefs.h"
18 
19 namespace po = boost::program_options;
20 
21 namespace boost {
22   namespace program_options {
23     class variables_map;
24   }
25 }
26 
27 namespace Wt {
28   class WLogger;
29   class WLogEntry;
30 }
31 
32 namespace http {
33 namespace server {
34 
35 class Configuration
36 {
37 public:
38   Configuration(Wt::WLogger& logger, bool silent = false);
39   ~Configuration();
40 
41   void setOptions(const std::string &applicationPath,
42                   const std::vector<std::string> &args,
43                   const std::string &configurationFile);
44 
45   std::vector<std::string> options() const;
46 
threads()47   int threads() const { return threads_; }
docRoot()48   const std::string& docRoot() const { return docRoot_; }
resourcesDir()49   const std::string& resourcesDir() const { return resourcesDir_; }
appRoot()50   const std::string& appRoot() const { return appRoot_; }
defaultStatic()51   bool defaultStatic() const { return defaultStatic_; }
staticPaths()52   const std::vector<std::string>& staticPaths() const
53   { return staticPaths_; }
errRoot()54   const std::string& errRoot() const { return errRoot_; }
deployPath()55   const std::string& deployPath() const { return deployPath_; }
pidPath()56   const std::string& pidPath() const { return pidPath_; }
serverName()57   const std::string& serverName() const { return serverName_; }
compression()58   bool compression() const { return compression_; }
gdb()59   bool gdb() const { return gdb_; }
configPath()60   const std::string& configPath() const { return configPath_; }
61 
httpListen()62   const std::vector<std::string>& httpListen() const { return httpListen_; }
httpAddress()63   const std::string& httpAddress() const { return httpAddress_; }
httpPort()64   const std::string& httpPort() const { return httpPort_; }
65 
httpsListen()66   const std::vector<std::string>& httpsListen() const { return httpsListen_; }
httpsAddress()67   const std::string& httpsAddress() const { return httpsAddress_; }
httpsPort()68   const std::string& httpsPort() const { return httpsPort_; }
sslCertificateChainFile()69   const std::string& sslCertificateChainFile() const
70     { return sslCertificateChainFile_; }
sslPrivateKeyFile()71   const std::string& sslPrivateKeyFile() const { return sslPrivateKeyFile_; }
sslTmpDHFile()72   const std::string& sslTmpDHFile() const { return sslTmpDHFile_; }
sslEnableV3()73   bool  sslEnableV3() const { return sslEnableV3_; }
sslClientVerification()74   const std::string& sslClientVerification() const
75   { return sslClientVerification_; }
sslVerifyDepth()76   int sslVerifyDepth() const { return sslVerifyDepth_; }
sslCaCertificates()77   const std::string& sslCaCertificates() const { return sslCaCertificates_; }
sslCipherList()78   const std::string& sslCipherList() const { return sslCipherList_; }
sslPreferServerCiphers()79   bool sslPreferServerCiphers() const { return sslPreferServerCiphers_; }
80 
sessionIdPrefix()81   const std::string& sessionIdPrefix() const { return sessionIdPrefix_; }
accessLog()82   const std::string& accessLog() const { return accessLog_; }
83 
parentPort()84   int parentPort() const { return parentPort_; }
85 
maxMemoryRequestSize()86   ::int64_t maxMemoryRequestSize() const { return maxMemoryRequestSize_; }
87 
88   typedef std::function<std::string (std::size_t max_length, int purpose)>
89     SslPasswordCallback;
90 
91   // ssl Password callback is not configurable from a file but we store it
92   // here because it's used in the Server constructor (inside start())
setSslPasswordCallback(const SslPasswordCallback & cb)93   void setSslPasswordCallback(const SslPasswordCallback& cb) {
94     sslPasswordCallback_ = cb;
95   }
sslPasswordCallback()96   SslPasswordCallback sslPasswordCallback() { return sslPasswordCallback_; }
hasSslPasswordCallback()97   bool hasSslPasswordCallback() { return (bool)sslPasswordCallback_; }
98 
99 private:
100   std::vector<std::string> options_;
101 
102   Wt::WLogger& logger_;
103   bool silent_;
104 
105   int threads_;
106   std::string docRoot_, appRoot_, resourcesDir_;
107   bool defaultStatic_;
108   std::vector<std::string> staticPaths_;
109   std::string errRoot_;
110   std::string deployPath_;
111   std::string pidPath_;
112   std::string serverName_;
113   bool compression_;
114   bool gdb_;
115   std::string configPath_;
116 
117   std::vector<std::string> httpListen_;
118   std::string httpAddress_;
119   std::string httpPort_;
120 
121   std::vector<std::string> httpsListen_;
122   std::string httpsAddress_;
123   std::string httpsPort_;
124   std::string sslCertificateChainFile_;
125   std::string sslPrivateKeyFile_;
126   std::string sslTmpDHFile_;
127   bool sslEnableV3_;
128   std::string sslClientVerification_;
129   int sslVerifyDepth_;
130   std::string sslCaCertificates_;
131   std::string sslCipherList_;
132   bool sslPreferServerCiphers_;
133 
134   std::string sessionIdPrefix_;
135   std::string accessLog_;
136 
137   int parentPort_;
138 
139   ::int64_t maxMemoryRequestSize_;
140 
141   SslPasswordCallback sslPasswordCallback_;
142 
143   void createOptions(po::options_description& options,
144 		     po::options_description& visible_options);
145   void readOptions(const po::variables_map& vm);
146 
147   void checkPath(const boost::program_options::variables_map& vm,
148 		 std::string varName, std::string varDescription,
149 		 std::string& result, int options);
150   void checkPath(std::string& result, std::string varDescription,
151 		 int options);
152 
153   enum PathOptions { RegularFile = 0x1,
154 		     Directory = 0x2,
155 		     Private = 0x4 };
156 
157   Wt::WLogEntry log(const std::string& type) const;
158 };
159 
160 } // namespace server
161 } // namespace http
162 
163 #endif // HTTP_CONFIGURATION_HPP
164