diff --git a/http_examples.cpp b/http_examples.cpp index ad206fd..26ff6eb 100644 --- a/http_examples.cpp +++ b/http_examples.cpp @@ -30,7 +30,8 @@ int main() { //HTTP-server at port 8080 using 1 thread //Unless you do more heavy non-threaded processing in the resources, //1 thread is usually faster than several threads - HttpServer server(8080, 1); + HttpServer server; + server.config.port=8080; //Add resources using path-regex and method-string, and an anonymous function //POST-example for the path /string, responds the posted string diff --git a/https_examples.cpp b/https_examples.cpp index 95a82b9..8862170 100644 --- a/https_examples.cpp +++ b/https_examples.cpp @@ -28,7 +28,8 @@ int main() { //HTTPS-server at port 8080 using 1 thread //Unless you do more heavy non-threaded processing in the resources, //1 thread is usually faster than several threads - HttpsServer server(8080, 1, "server.crt", "server.key"); + HttpsServer server("server.crt", "server.key"); + server.config.port=8080; //Add resources using path-regex and method-string, and an anonymous function //POST-example for the path /string, responds the posted string diff --git a/server_http.hpp b/server_http.hpp index f0cf9a1..874a5f0 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -20,6 +20,15 @@ #define REGEX_NS std #endif +// TODO when switching to c++14, use [[deprecated]] instead +#ifdef __GNUC__ +#define DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) +#define DEPRECATED __declspec(deprecated) +#else +#define DEPRECATED +#endif + namespace SimpleWeb { template class Server; @@ -107,15 +116,21 @@ namespace SimpleWeb { class Config { friend class ServerBase; - Config(unsigned short port, size_t num_threads): num_threads(num_threads), port(port), reuse_address(true) {} - size_t num_threads; + Config(unsigned short port): port(port) {} public: + /// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS. unsigned short port; - ///IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation. - ///If empty, the address will be any address. + /// Number of threads that the server will use when start() is called. Defaults to 1 thread. + size_t thread_pool_size=1; + /// Timeout on request handling. Defaults to 5 seconds. + size_t timeout_request=5; + /// Timeout on content handling. Defaults to 300 seconds. + size_t timeout_content=300; + /// IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation. + /// If empty, the address will be any address. std::string address; - ///Set to false to avoid binding the socket to an address that is already in use. - bool reuse_address; + /// Set to false to avoid binding the socket to an address that is already in use. Defaults to true. + bool reuse_address=true; }; ///Set before calling start(). Config config; @@ -175,16 +190,16 @@ namespace SimpleWeb { accept(); - //If num_threads>1, start m_io_service.run() in (num_threads-1) threads for thread-pooling + //If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling threads.clear(); - for(size_t c=1;crun(); }); } //Main thread - if(config.num_threads>0) + if(config.thread_pool_size>0) io_service->run(); //Wait for the rest of the threads, if any, to finish as well @@ -195,7 +210,7 @@ namespace SimpleWeb { void stop() { acceptor->close(); - if(config.num_threads>0) + if(config.thread_pool_size>0) io_service->stop(); } @@ -208,17 +223,13 @@ namespace SimpleWeb { } /// If you have your own boost::asio::io_service, store its pointer here before running start(). - /// You might also want to set config.num_threads to 0. + /// You might also want to set config.thread_pool_size to 0. std::shared_ptr io_service; protected: std::unique_ptr acceptor; std::vector threads; - long timeout_request; - long timeout_content; - - ServerBase(unsigned short port, size_t num_threads, long timeout_request, long timeout_send_or_receive) : - config(port, num_threads), timeout_request(timeout_request), timeout_content(timeout_send_or_receive) {} + ServerBase(unsigned short port) : config(port) {} virtual void accept()=0; @@ -244,7 +255,7 @@ namespace SimpleWeb { std::shared_ptr request(new Request(*socket)); //Set timeout on the following boost::asio::async-read or write function - auto timer=this->get_timeout_timer(socket, timeout_request); + auto timer=this->get_timeout_timer(socket, config.timeout_request); boost::asio::async_read_until(*socket, request->streambuf, "\r\n\r\n", [this, socket, request, timer](const boost::system::error_code& ec, size_t bytes_transferred) { @@ -274,7 +285,7 @@ namespace SimpleWeb { } if(content_length>num_additional_bytes) { //Set timeout on the following boost::asio::async-read or write function - auto timer=this->get_timeout_timer(socket, timeout_content); + auto timer=this->get_timeout_timer(socket, config.timeout_content); boost::asio::async_read(*socket, request->streambuf, boost::asio::transfer_exactly(content_length-num_additional_bytes), [this, socket, request, timer] @@ -363,7 +374,7 @@ namespace SimpleWeb { std::function::Response>, std::shared_ptr::Request>)>& resource_function) { //Set timeout on the following boost::asio::async-read or write function - auto timer=this->get_timeout_timer(socket, timeout_content); + auto timer=this->get_timeout_timer(socket, config.timeout_content); auto response=std::shared_ptr(new Response(socket), [this, request, timer](Response *response_ptr) { auto response=std::shared_ptr(response_ptr); @@ -413,8 +424,15 @@ namespace SimpleWeb { template<> class Server : public ServerBase { public: - Server(unsigned short port, size_t num_threads=1, long timeout_request=5, long timeout_content=300) : - ServerBase::ServerBase(port, num_threads, timeout_request, timeout_content) {} + DEPRECATED Server(unsigned short port, size_t thread_pool_size=1, long timeout_request=5, long timeout_content=300) : + Server() { + config.port=port; + config.thread_pool_size=thread_pool_size; + config.timeout_request=timeout_request; + config.timeout_request=timeout_content; + } + + Server() : ServerBase::ServerBase(80) {} protected: void accept() { diff --git a/server_https.hpp b/server_https.hpp index 8e381f1..6fe0f6f 100644 --- a/server_https.hpp +++ b/server_https.hpp @@ -14,11 +14,18 @@ namespace SimpleWeb { std::string session_id_context; bool set_session_id_context=false; public: - Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file, + DEPRECATED Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file, long timeout_request=5, long timeout_content=300, const std::string& verify_file=std::string()) : - ServerBase::ServerBase(port, num_threads, timeout_request, timeout_content), - context(boost::asio::ssl::context::tlsv12) { // 2016/08/13 only use tls12, see https://www.ssllabs.com/ssltest + Server(cert_file, private_key_file, verify_file) { + config.port=port; + config.thread_pool_size=num_threads; + config.timeout_request=timeout_request; + config.timeout_content=timeout_content; + } + + Server(const std::string& cert_file, const std::string& private_key_file, const std::string& verify_file=std::string()): + ServerBase::ServerBase(443), context(boost::asio::ssl::context::tlsv12) { context.use_certificate_chain_file(cert_file); context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem); @@ -60,7 +67,7 @@ namespace SimpleWeb { socket->lowest_layer().set_option(option); //Set timeout on the following boost::asio::ssl::stream::async_handshake - auto timer=get_timeout_timer(socket, timeout_request); + auto timer=get_timeout_timer(socket, config.timeout_request); socket->async_handshake(boost::asio::ssl::stream_base::server, [this, socket, timer] (const boost::system::error_code& ec) { if(timer) diff --git a/tests/io_test.cpp b/tests/io_test.cpp index e2a4115..db999cc 100644 --- a/tests/io_test.cpp +++ b/tests/io_test.cpp @@ -9,7 +9,8 @@ typedef SimpleWeb::Server HttpServer; typedef SimpleWeb::Client HttpClient; int main() { - HttpServer server(8080, 1); + HttpServer server; + server.config.port=8080; server.resource["^/string$"]["POST"]=[](shared_ptr response, shared_ptr request) { auto content=request->content.string(); diff --git a/tests/parse_test.cpp b/tests/parse_test.cpp index f417da5..502db0b 100644 --- a/tests/parse_test.cpp +++ b/tests/parse_test.cpp @@ -8,7 +8,7 @@ using namespace SimpleWeb; class ServerTest : public ServerBase { public: ServerTest() : - ServerBase::ServerBase(8080, 1, 5, 300) {} + ServerBase::ServerBase(8080) {} void accept() {}