libutil++  1.9.3
 All Classes Functions Variables
HTTP.h
1 /*
2 ** libutil++
3 ** $Id: HTTP.h 1664 2016-04-04 03:57:40Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__server__HTTP_H__
10 #define __libutilxx__sella__server__HTTP_H__
11 
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"
20 
21 #include <set>
22 #include <mutex>
23 #include <memory>
24 #include <queue>
25 #include <vector>
26 #include <functional>
27 #include <unordered_set>
28 
29 namespace sella {
30  namespace server {
31  class HTTP;
32  }
33 
34  namespace util {
35  class ThreadPool;
36  }
37 }
38 
40  public:
41  typedef std::shared_ptr<HTTP> shared;
42  typedef HTTP *ptr;
43 
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;
48 
49  public:
50  static const sella::net::IPAddressPort::vector& ListenersDefault(void) { static sella::net::IPAddressPort::vector v({ sella::net::IPAddressPort("0.0.0.0:8080"), sella::net::IPAddressPort("[::]:8080") }); return v; }
51  static const sella::net::IPAddressPort::vector& SecureListenersDefault(void) { static sella::net::IPAddressPort::vector v({ sella::net::IPAddressPort("0.0.0.0:8443"), sella::net::IPAddressPort("[::]:8443") }); return v; }
52  static const size_t RequestBodyLimitDefault = 25 * 102400; // 25KB
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; // 1KB - Compress as we near the size of a packet.
61  static const int CompressionLevelDefault = 1; // Best level for low latency.
62 
63  public:
64  static const size_t MaximumWorkersDefault = 0; /* 0 to auto assign available core count */
65  static const size_t MinimumWorkersDefault = 2;
66  static const size_t SpareWorkersDefault = 2; /* Number of non-active workers. */
67 
68  public:
69  HTTP() throw (ServerException);
70  HTTP(sella::util::Log::shared log) throw (ServerException);
71  virtual ~HTTP() throw ();
72 
73  /* Async */
74  virtual bool receive(void) throw (ServerException); /* Process incoming packets return. */
75  virtual bool receive(timeval *tv) throw (ServerException); /* Process incoming packets and return. */
76 
77  /* Threaded */
78  virtual bool run(size_t workers = MaximumWorkersDefault) throw (ServerException); /* Simple blocking non-scaling internal pool */
79  virtual bool run(size_t maximumWorkers, size_t minimumWorkers, size_t spareWorkers = SpareWorkersDefault) throw (ServerException); /* Auto-scaling blocking internal pool. */
80  virtual void start(size_t workers = MaximumWorkersDefault) throw (ServerException); /* Simple non-blocking non-scaling internal pool */
81  virtual void start(size_t maximumWorkers, size_t minimumWorkers, size_t spareWorkers = SpareWorkersDefault) throw (ServerException); /* Auto-scaling non-blocking internal pool */
82  virtual void start(sella::util::ThreadPool::shared pool) throw (ServerException); /* Custom non-blocking pool */
83  virtual bool stop(void) throw ();
84 
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 (); /* Preferred over set password */
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 (); /* Set 0 to disable. */
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 ();
115 
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 ();
130 
131  void clearAllow(HTTPRequest::Method method) throw ();
132 
133  bool isRunning(void) const throw ();
134 
135  public:
136  virtual void clear(void);
137  virtual void print(void);
138 
139  static const HTTP::shared shared_ptr() throw (ServerException);
140  static const HTTP::shared shared_ptr(sella::util::Log::shared log) throw (ServerException);
141 
142  protected:
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 ();
150 
151  private:
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;
157  bool running;
158  std::mutex mutex;
159  std::thread manager;
161 
162  sella::net::IPAddressPort::vector listeners;
163  sella::net::IPAddressPort::vector secureListeners;
164  std::set<net::SecureTCPSocket::Protocol> protocols;
165  std::string ciphers;
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;
175  std::string root;
176  std::vector<std::string> indexes;
177  std::string banner;
178  std::vector<std::string> headers;
179  std::set<HTTPRequest::Method> allow;
180  HTTPRequest::AcceptType fallbackAcceptType;
181  size_t compressionThreshold;
182  int compressionLevel;
183 
184  acl_callback aclCallback;
185  authentication_callback authCallback;
186  request_callback reqCallback;
187  error_callback errorCallback;
188 };
189 
190 #endif
191 
192 /*
193 ** vim: noet ts=3 sw=3
194 */