9 #ifndef __libutilxx__sella__server__HTTP_H__
10 #define __libutilxx__sella__server__HTTP_H__
12 #include "../../common.h"
13 #include "ServerException.h"
14 #include "HTTPConnection.h"
15 #include "../util/Log.h"
16 #include "../util/ThreadPool.h"
17 #include "../net/IPAddressPort.h"
18 #include "../net/SocketMux.h"
19 #include "../net/SocketTrigger.h"
27 #include <unordered_set>
41 typedef std::shared_ptr<HTTP> shared;
44 typedef std::function<bool(HTTPConnection::shared)> acl_callback;
45 typedef std::function<bool(HTTPConnection::shared)> request_callback;
46 typedef std::function<bool(HTTPConnection::shared)> authentication_callback;
47 typedef std::function<bool(HTTPConnection::shared, int, const std::string&)> error_callback;
52 static const size_t RequestBodyLimitDefault = 25 * 102400;
53 static const size_t ConnectionLimitDefault = 256;
54 static const size_t DelaySecondsDefault = 7200;
55 static const std::string& RootDefault(
void) {
static std::string v(
".");
return v; }
56 static const std::vector<std::string>& DirectoryIndexesDefault(
void) {
static std::vector<std::string> v({
"index.html",
"index.htm" });
return v; }
57 static const std::string& BannerDefault(
void) {
static std::string v(
"Digital Genesis HTTP Server " PACKAGE
"/" VERSION);
return v; }
58 static const std::set<HTTPRequest::Method>& AllowDefault(
void) {
static std::set<HTTPRequest::Method> v({ HTTPRequest::Options, HTTPRequest::Get, HTTPRequest::Head, HTTPRequest::Trace });
return v; }
59 static const HTTPRequest::AcceptType FallbackAcceptTypeDefault = HTTPRequest::AcceptType::HTML;
60 static const int CompressionThresholdDefault = 1024;
61 static const int CompressionLevelDefault = 1;
64 static const size_t MaximumWorkersDefault = 0;
65 static const size_t MinimumWorkersDefault = 2;
66 static const size_t SpareWorkersDefault = 2;
69 HTTP()
throw (ServerException);
70 HTTP(sella::util::Log::shared log)
throw (ServerException);
71 virtual ~
HTTP()
throw ();
74 virtual bool receive(
void)
throw (ServerException);
75 virtual bool receive(timeval *tv)
throw (ServerException);
78 virtual bool run(
size_t workers = MaximumWorkersDefault)
throw (ServerException);
79 virtual bool run(
size_t maximumWorkers,
size_t minimumWorkers,
size_t spareWorkers = SpareWorkersDefault)
throw (ServerException);
80 virtual void start(
size_t workers = MaximumWorkersDefault)
throw (ServerException);
81 virtual void start(
size_t maximumWorkers,
size_t minimumWorkers,
size_t spareWorkers = SpareWorkersDefault)
throw (ServerException);
82 virtual void start(sella::util::ThreadPool::shared pool)
throw (ServerException);
83 virtual bool stop(
void)
throw ();
85 void setListeners(
const sella::net::IPAddressPort::vector &listeners)
throw ();
86 void setListeners(
const std::string &listeners)
throw ();
87 void setSecureListeners(
const sella::net::IPAddressPort::vector &listeners)
throw ();
88 void setSecureListeners(
const std::string &listeners)
throw ();
89 void setSecureProtocols(
const std::set<net::SecureTCPSocket::Protocol> &protocols = net::SecureTCPSocket::DefaultProtocols)
throw ();
90 void setSecureProtocol(net::SecureTCPSocket::Protocol protocol,
bool on =
true)
throw ();
91 void setSecureCipher(
const std::string &ciphers)
throw ();
92 void setSecurePreferServerCipher(
bool on =
true)
throw ();
93 void setCertificateFile(
const std::string &certificateFile)
throw ();
94 void setPrivateKeyFile(
const std::string &privateKeyFile)
throw ();
95 void setPasswordFile(
const std::string &passwordFile)
throw ();
96 void setPassword(
const std::string &password)
throw ();
97 void setCertificateChainFile(
const std::string &certificateChainFile)
throw ();
98 void setCACertificateFile(
const std::string &caCertificateFile)
throw ();
99 void setRequestBodyLimit(
size_t requestBodyLimit)
throw ();
100 void setConnectionLimit(
size_t connectionLimit)
throw ();
101 void setRoot(
const std::string &root)
throw (ServerException);
102 void setDirectoryIndex(
const std::vector<std::string> &indexes)
throw ();
103 void addDirectoryIndex(
const std::string &index)
throw ();
104 void setBanner(
const std::string &banner)
throw ();
105 void setCustomHeaders(
const std::vector<std::string> &headers)
throw ();
106 void setAllows(
const std::set<HTTPRequest::Method> &methods = AllowDefault())
throw ();
107 void setAllow(HTTPRequest::Method method)
throw ();
108 void setFallbackAcceptType(HTTPRequest::AcceptType fallbackAcceptType)
throw ();
109 void setCompressionThreshold(
size_t threshold = CompressionThresholdDefault)
throw ();
110 void setCompressionLevel(
int level = CompressionLevelDefault)
throw ();
111 void setACLCallback(
const acl_callback &callback)
throw ();
112 void setAuthenticationCallback(
const authentication_callback &callback)
throw ();
113 void setRequestCallback(
const request_callback &callback)
throw ();
114 void setErrorCallback(
const error_callback &callback)
throw ();
116 sella::net::IPAddressPort::vector getListeners(
void)
const throw ();
117 sella::net::IPAddressPort::vector getSecureListeners(
void)
const throw ();
118 std::string getListenersString(
void)
const throw ();
119 std::string getSecureListenersString(
void)
const throw ();
120 size_t getRequestBodyLimit(
void)
const throw ();
121 size_t getConnectionLimit(
void)
const throw ();
122 const std::string& getRoot(
void)
const throw ();
123 const std::vector<std::string>& getDirectoryIndexes(
void)
const throw ();
124 const std::string& getBanner(
void)
const throw ();
125 const std::vector<std::string>& getCustomHeaders(
void)
const throw ();
126 std::set<HTTPRequest::Method> getAllow(
void)
const throw ();
127 HTTPRequest::AcceptType getFallbackAcceptType(
void)
const throw ();
128 size_t getCompressionThreshold(
void)
const throw ();
129 int getCompressionLevel(
void)
const throw ();
131 void clearAllow(HTTPRequest::Method method)
throw ();
133 bool isRunning(
void)
const throw ();
136 virtual void clear(
void);
137 virtual void print(
void);
139 static const HTTP::shared shared_ptr()
throw (ServerException);
140 static const HTTP::shared shared_ptr(sella::util::Log::shared log)
throw (ServerException);
143 void startup(
void)
throw (ServerException);
144 void shutdown(
void)
throw ();
145 int select(timeval *tv)
throw (ServerException);
146 HTTPConnection::shared accept(
const sella::net::Socket::shared &socket)
throw (ServerException);
147 void process(HTTPConnection::shared &connection)
throw ();
148 void server(
void)
throw ();
149 void worker(HTTPConnection::shared &connection)
throw ();
152 sella::util::Log::shared libutilxx__log;
153 sella::util::ThreadPool::shared pool;
155 std::unordered_set<int> servers;
156 std::queue<HTTPConnection::shared> pending;
162 sella::net::IPAddressPort::vector listeners;
163 sella::net::IPAddressPort::vector secureListeners;
164 std::set<net::SecureTCPSocket::Protocol> protocols;
166 bool preferServerCiphers;
167 std::string certificateFile;
168 std::string privateKeyFile;
169 std::string passwordFile;
170 std::string password;
171 std::string certificateChainFile;
172 std::string caCertificateFile;
173 size_t requestBodyLimit;
174 size_t connectionLimit;
176 std::vector<std::string> indexes;
178 std::vector<std::string> headers;
179 std::set<HTTPRequest::Method> allow;
180 HTTPRequest::AcceptType fallbackAcceptType;
181 size_t compressionThreshold;
182 int compressionLevel;
184 acl_callback aclCallback;
185 authentication_callback authCallback;
186 request_callback reqCallback;
187 error_callback errorCallback;