|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION) |
| 22 | +#error "Only <httpserver.hpp> or <httpserverpp> can be included directly." |
| 23 | +#endif |
| 24 | + |
| 25 | +#ifndef _CREATE_WEBSERVER_HPP_ |
| 26 | +#define _CREATE_WEBSERVER_HPP_ |
| 27 | + |
| 28 | +#include "httpserver/http_utils.hpp" |
| 29 | + |
| 30 | +namespace httpserver { |
| 31 | + |
| 32 | +class webserver; |
| 33 | +class http_request; |
| 34 | +class http_response; |
| 35 | + |
| 36 | +using namespace http; |
| 37 | + |
| 38 | +typedef void(*render_ptr)(const http_request&, http_response**); |
| 39 | +typedef bool(*validator_ptr)(const std::string&); |
| 40 | +typedef void(*unescaper_ptr)(char*); |
| 41 | +typedef void(*log_access_ptr)(const std::string&); |
| 42 | +typedef void(*log_error_ptr)(const std::string&); |
| 43 | + |
| 44 | +class create_webserver |
| 45 | +{ |
| 46 | + public: |
| 47 | + create_webserver(): |
| 48 | + _port(DEFAULT_WS_PORT), |
| 49 | + _start_method(http_utils::INTERNAL_SELECT), |
| 50 | + _max_threads(0), |
| 51 | + _max_connections(0), |
| 52 | + _memory_limit(0), |
| 53 | + _connection_timeout(DEFAULT_WS_TIMEOUT), |
| 54 | + _per_IP_connection_limit(0), |
| 55 | + _log_access(0x0), |
| 56 | + _log_error(0x0), |
| 57 | + _validator(0x0), |
| 58 | + _unescaper(0x0), |
| 59 | + _bind_address(0x0), |
| 60 | + _bind_socket(0), |
| 61 | + _max_thread_stack_size(0), |
| 62 | + _use_ssl(false), |
| 63 | + _use_ipv6(false), |
| 64 | + _debug(false), |
| 65 | + _pedantic(false), |
| 66 | + _https_mem_key(""), |
| 67 | + _https_mem_cert(""), |
| 68 | + _https_mem_trust(""), |
| 69 | + _https_priorities(""), |
| 70 | + _cred_type(http_utils::NONE), |
| 71 | + _digest_auth_random(""), |
| 72 | + _nonce_nc_size(0), |
| 73 | + _default_policy(http_utils::ACCEPT), |
| 74 | + _basic_auth_enabled(true), |
| 75 | + _digest_auth_enabled(true), |
| 76 | + _regex_checking(true), |
| 77 | + _ban_system_enabled(true), |
| 78 | + _post_process_enabled(true), |
| 79 | + _single_resource(0x0), |
| 80 | + _not_found_resource(0x0), |
| 81 | + _method_not_allowed_resource(0x0), |
| 82 | + _method_not_acceptable_resource(0x0), |
| 83 | + _internal_error_resource(0x0) |
| 84 | + { |
| 85 | + } |
| 86 | + |
| 87 | + explicit create_webserver(int port): |
| 88 | + _port(port), |
| 89 | + _start_method(http_utils::INTERNAL_SELECT), |
| 90 | + _max_threads(0), |
| 91 | + _max_connections(0), |
| 92 | + _memory_limit(0), |
| 93 | + _connection_timeout(DEFAULT_WS_TIMEOUT), |
| 94 | + _per_IP_connection_limit(0), |
| 95 | + _log_access(0x0), |
| 96 | + _log_error(0x0), |
| 97 | + _validator(0x0), |
| 98 | + _unescaper(0x0), |
| 99 | + _bind_address(0x0), |
| 100 | + _bind_socket(0), |
| 101 | + _max_thread_stack_size(0), |
| 102 | + _use_ssl(false), |
| 103 | + _use_ipv6(false), |
| 104 | + _debug(false), |
| 105 | + _pedantic(false), |
| 106 | + _https_mem_key(""), |
| 107 | + _https_mem_cert(""), |
| 108 | + _https_mem_trust(""), |
| 109 | + _https_priorities(""), |
| 110 | + _cred_type(http_utils::NONE), |
| 111 | + _digest_auth_random(""), |
| 112 | + _nonce_nc_size(0), |
| 113 | + _default_policy(http_utils::ACCEPT), |
| 114 | + _basic_auth_enabled(true), |
| 115 | + _digest_auth_enabled(true), |
| 116 | + _regex_checking(true), |
| 117 | + _ban_system_enabled(true), |
| 118 | + _post_process_enabled(true), |
| 119 | + _single_resource(0x0), |
| 120 | + _not_found_resource(0x0), |
| 121 | + _method_not_allowed_resource(0x0), |
| 122 | + _method_not_acceptable_resource(0x0), |
| 123 | + _internal_error_resource(0x0) |
| 124 | + { |
| 125 | + } |
| 126 | + |
| 127 | + create_webserver& port(int port) { _port = port; return *this; } |
| 128 | + create_webserver& start_method( |
| 129 | + const http_utils::start_method_T& start_method |
| 130 | + ) |
| 131 | + { |
| 132 | + _start_method = start_method; return *this; |
| 133 | + } |
| 134 | + create_webserver& max_threads(int max_threads) |
| 135 | + { |
| 136 | + _max_threads = max_threads; return *this; |
| 137 | + } |
| 138 | + create_webserver& max_connections(int max_connections) |
| 139 | + { |
| 140 | + _max_connections = max_connections; return *this; |
| 141 | + } |
| 142 | + create_webserver& memory_limit(int memory_limit) |
| 143 | + { |
| 144 | + _memory_limit = memory_limit; return *this; |
| 145 | + } |
| 146 | + create_webserver& connection_timeout(int connection_timeout) |
| 147 | + { |
| 148 | + _connection_timeout = connection_timeout; return *this; |
| 149 | + } |
| 150 | + create_webserver& per_IP_connection_limit(int per_IP_connection_limit) |
| 151 | + { |
| 152 | + _per_IP_connection_limit = per_IP_connection_limit; return *this; |
| 153 | + } |
| 154 | + create_webserver& log_access(log_access_ptr log_access) |
| 155 | + { |
| 156 | + _log_access = log_access; return *this; |
| 157 | + } |
| 158 | + create_webserver& log_error(log_error_ptr log_error) |
| 159 | + { |
| 160 | + _log_error = log_error; return *this; |
| 161 | + } |
| 162 | + create_webserver& validator(validator_ptr validator) |
| 163 | + { |
| 164 | + _validator = validator; return *this; |
| 165 | + } |
| 166 | + create_webserver& unescaper(unescaper_ptr unescaper) |
| 167 | + { |
| 168 | + _unescaper = unescaper; return *this; |
| 169 | + } |
| 170 | + create_webserver& bind_address(const struct sockaddr* bind_address) |
| 171 | + { |
| 172 | + _bind_address = bind_address; return *this; |
| 173 | + } |
| 174 | + create_webserver& bind_socket(int bind_socket) |
| 175 | + { |
| 176 | + _bind_socket = bind_socket; return *this; |
| 177 | + } |
| 178 | + create_webserver& max_thread_stack_size(int max_thread_stack_size) |
| 179 | + { |
| 180 | + _max_thread_stack_size = max_thread_stack_size; return *this; |
| 181 | + } |
| 182 | + create_webserver& use_ssl() { _use_ssl = true; return *this; } |
| 183 | + create_webserver& no_ssl() { _use_ssl = false; return *this; } |
| 184 | + create_webserver& use_ipv6() { _use_ipv6 = true; return *this; } |
| 185 | + create_webserver& no_ipv6() { _use_ipv6 = false; return *this; } |
| 186 | + create_webserver& debug() { _debug = true; return *this; } |
| 187 | + create_webserver& no_debug() { _debug = false; return *this; } |
| 188 | + create_webserver& pedantic() { _pedantic = true; return *this; } |
| 189 | + create_webserver& no_pedantic() { _pedantic = false; return *this; } |
| 190 | + create_webserver& https_mem_key(const std::string& https_mem_key) |
| 191 | + { |
| 192 | + char* _https_mem_key_pt = http::load_file(https_mem_key.c_str()); |
| 193 | + _https_mem_key = _https_mem_key_pt; |
| 194 | + free(_https_mem_key_pt); |
| 195 | + return *this; |
| 196 | + } |
| 197 | + create_webserver& https_mem_cert(const std::string& https_mem_cert) |
| 198 | + { |
| 199 | + char* _https_mem_cert_pt = http::load_file(https_mem_cert.c_str()); |
| 200 | + _https_mem_cert = _https_mem_cert_pt; |
| 201 | + free(_https_mem_cert_pt); |
| 202 | + return *this; |
| 203 | + } |
| 204 | + create_webserver& https_mem_trust(const std::string& https_mem_trust) |
| 205 | + { |
| 206 | + char* _https_mem_trust_pt = http::load_file(https_mem_trust.c_str()); |
| 207 | + _https_mem_trust = _https_mem_trust_pt; |
| 208 | + free(_https_mem_trust_pt); |
| 209 | + return *this; |
| 210 | + } |
| 211 | + create_webserver& raw_https_mem_key(const std::string& https_mem_key) |
| 212 | + { |
| 213 | + _https_mem_key = https_mem_key; return *this; |
| 214 | + } |
| 215 | + create_webserver& raw_https_mem_cert(const std::string& https_mem_cert) |
| 216 | + { |
| 217 | + _https_mem_cert = https_mem_cert; return *this; |
| 218 | + } |
| 219 | + create_webserver& raw_https_mem_trust( |
| 220 | + const std::string& https_mem_trust |
| 221 | + ) |
| 222 | + { |
| 223 | + _https_mem_trust = https_mem_trust; return *this; |
| 224 | + } |
| 225 | + create_webserver& https_priorities(const std::string& https_priorities) |
| 226 | + { |
| 227 | + _https_priorities = https_priorities; return *this; |
| 228 | + } |
| 229 | + create_webserver& cred_type(const http_utils::cred_type_T& cred_type) |
| 230 | + { |
| 231 | + _cred_type = cred_type; return *this; |
| 232 | + } |
| 233 | + create_webserver& digest_auth_random( |
| 234 | + const std::string& digest_auth_random |
| 235 | + ) |
| 236 | + { |
| 237 | + _digest_auth_random = digest_auth_random; return *this; |
| 238 | + } |
| 239 | + create_webserver& nonce_nc_size(int nonce_nc_size) |
| 240 | + { |
| 241 | + _nonce_nc_size = nonce_nc_size; return *this; |
| 242 | + } |
| 243 | + create_webserver& default_policy( |
| 244 | + const http_utils::policy_T& default_policy |
| 245 | + ) |
| 246 | + { |
| 247 | + _default_policy = default_policy; return *this; |
| 248 | + } |
| 249 | + create_webserver& basic_auth() |
| 250 | + { |
| 251 | + _basic_auth_enabled = true; return *this; |
| 252 | + } |
| 253 | + create_webserver& no_basic_auth() |
| 254 | + { |
| 255 | + _basic_auth_enabled = false; return *this; |
| 256 | + } |
| 257 | + create_webserver& digest_auth() |
| 258 | + { |
| 259 | + _digest_auth_enabled = true; return *this; |
| 260 | + } |
| 261 | + create_webserver& no_digest_auth() |
| 262 | + { |
| 263 | + _digest_auth_enabled = false; return *this; |
| 264 | + } |
| 265 | + create_webserver& regex_checking() |
| 266 | + { |
| 267 | + _regex_checking = true; return *this; |
| 268 | + } |
| 269 | + create_webserver& no_regex_checking() |
| 270 | + { |
| 271 | + _regex_checking = false; return *this; |
| 272 | + } |
| 273 | + create_webserver& ban_system() |
| 274 | + { |
| 275 | + _ban_system_enabled = true; return *this; |
| 276 | + } |
| 277 | + create_webserver& no_ban_system() |
| 278 | + { |
| 279 | + _ban_system_enabled = false; return *this; |
| 280 | + } |
| 281 | + create_webserver& post_process() |
| 282 | + { |
| 283 | + _post_process_enabled = true; return *this; |
| 284 | + } |
| 285 | + create_webserver& no_post_process() |
| 286 | + { |
| 287 | + _post_process_enabled = false; return *this; |
| 288 | + } |
| 289 | + create_webserver& single_resource(render_ptr single_resource) |
| 290 | + { |
| 291 | + _single_resource = single_resource; return *this; |
| 292 | + } |
| 293 | + create_webserver& not_found_resource(render_ptr not_found_resource) |
| 294 | + { |
| 295 | + _not_found_resource = not_found_resource; return *this; |
| 296 | + } |
| 297 | + create_webserver& method_not_allowed_resource( |
| 298 | + render_ptr method_not_allowed_resource |
| 299 | + ) |
| 300 | + { |
| 301 | + _method_not_allowed_resource = method_not_allowed_resource; |
| 302 | + return *this; |
| 303 | + } |
| 304 | + create_webserver& method_not_acceptable_resource( |
| 305 | + render_ptr method_not_acceptable_resource |
| 306 | + ) |
| 307 | + { |
| 308 | + _method_not_acceptable_resource = method_not_acceptable_resource; |
| 309 | + return *this; |
| 310 | + } |
| 311 | + create_webserver& internal_error_resource( |
| 312 | + render_ptr internal_error_resource |
| 313 | + ) |
| 314 | + { |
| 315 | + _internal_error_resource = internal_error_resource; return *this; |
| 316 | + } |
| 317 | + |
| 318 | + private: |
| 319 | + int _port; |
| 320 | + http_utils::start_method_T _start_method; |
| 321 | + int _max_threads; |
| 322 | + int _max_connections; |
| 323 | + int _memory_limit; |
| 324 | + int _connection_timeout; |
| 325 | + int _per_IP_connection_limit; |
| 326 | + log_access_ptr _log_access; |
| 327 | + log_error_ptr _log_error; |
| 328 | + validator_ptr _validator; |
| 329 | + unescaper_ptr _unescaper; |
| 330 | + const struct sockaddr* _bind_address; |
| 331 | + int _bind_socket; |
| 332 | + int _max_thread_stack_size; |
| 333 | + bool _use_ssl; |
| 334 | + bool _use_ipv6; |
| 335 | + bool _debug; |
| 336 | + bool _pedantic; |
| 337 | + std::string _https_mem_key; |
| 338 | + std::string _https_mem_cert; |
| 339 | + std::string _https_mem_trust; |
| 340 | + std::string _https_priorities; |
| 341 | + http_utils::cred_type_T _cred_type; |
| 342 | + std::string _digest_auth_random; |
| 343 | + int _nonce_nc_size; |
| 344 | + http_utils::policy_T _default_policy; |
| 345 | + bool _basic_auth_enabled; |
| 346 | + bool _digest_auth_enabled; |
| 347 | + bool _regex_checking; |
| 348 | + bool _ban_system_enabled; |
| 349 | + bool _post_process_enabled; |
| 350 | + render_ptr _single_resource; |
| 351 | + render_ptr _not_found_resource; |
| 352 | + render_ptr _method_not_allowed_resource; |
| 353 | + render_ptr _method_not_acceptable_resource; |
| 354 | + render_ptr _internal_error_resource; |
| 355 | + |
| 356 | + friend class webserver; |
| 357 | +}; |
| 358 | + |
| 359 | +} //httpserver |
| 360 | + |
| 361 | +#endif //_CREATE_WEBSERVER_HPP_ |
0 commit comments