From ddba5b21b3ef163bd14369dfa8a9bd345d0a5601 Mon Sep 17 00:00:00 2001 From: Naim A Date: Sun, 18 Aug 2013 02:49:49 +0300 Subject: [PATCH] Ability to customize bind for HTTP --- src/http/httpserver.cpp | 51 ++++++++++++++++++++++++++++++++++++----- src/http/httpserver.hpp | 4 ++++ src/main.cpp | 13 ++--------- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/http/httpserver.cpp b/src/http/httpserver.cpp index c32a2b9..c8d774f 100644 --- a/src/http/httpserver.cpp +++ b/src/http/httpserver.cpp @@ -33,9 +33,52 @@ namespace UDPT /* HTTPServer */ HTTPServer::HTTPServer (uint16_t port, int threads) { - int r; SOCKADDR_IN sa; + memset((void*)&sa, 0, sizeof(sa)); + sa.sin_addr.s_addr = 0L; + sa.sin_family = AF_INET; + sa.sin_port = htons (port); + + this->init(sa, threads); + } + + HTTPServer::HTTPServer(Settings *s) + { + Settings::SettingClass *sc = s->getClass("apiserver"); + list localEndpoints; + uint16_t port; + int threads; + + port = 6969; + threads = 1; + + if (sc != NULL) + { + port = sc->getInt("port", 6969); + threads = sc->getInt("threads", 1); + sc->getIPs("bind", localEndpoints); + } + + if (threads <= 0) + threads = 1; + + if (localEndpoints.empty()) + { + SOCKADDR_IN sa; + memset((void*)&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_port = htons (port); + sa.sin_addr.s_addr = 0L; + localEndpoints.push_front(sa); + } + + this->init(localEndpoints.front(), threads); + } + + void HTTPServer::init (SOCKADDR_IN &localEndpoint, int threads) + { + int r; this->thread_count = threads; this->threads = new HANDLE[threads]; this->isRunning = false; @@ -48,11 +91,7 @@ namespace UDPT throw ServerException (1, "Failed to create Socket"); } - sa.sin_addr.s_addr = 0L; - sa.sin_family = AF_INET; - sa.sin_port = htons (port); - - r = bind (this->srv, (SOCKADDR*)&sa, sizeof(sa)); + r = bind (this->srv, (SOCKADDR*)&localEndpoint, sizeof(localEndpoint)); if (r == SOCKET_ERROR) { throw ServerException (2, "Failed to bind socket"); diff --git a/src/http/httpserver.hpp b/src/http/httpserver.hpp index 4a32e0a..e4b1d16 100644 --- a/src/http/httpserver.hpp +++ b/src/http/httpserver.hpp @@ -25,6 +25,7 @@ #include #include #include "../multiplatform.h" +#include "../settings.hpp" using namespace std; #define REQUEST_BUFFER_SIZE 2048 @@ -130,6 +131,7 @@ namespace UDPT typedef void (reqCallback)(HTTPServer*,Request*,Response*); HTTPServer (uint16_t port, int threads); + HTTPServer (Settings *s); void addApp (list *path, reqCallback *); @@ -152,6 +154,8 @@ namespace UDPT appNode rootNode; map customData; + void init (SOCKADDR_IN &localEndpoint, int threads); + static void handleConnections (HTTPServer *); #ifdef WIN32 diff --git a/src/main.cpp b/src/main.cpp index 357be40..67ba4fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,23 +54,14 @@ static void _doAPIStart (Settings *settings, WebApp **wa, HTTPServer **srv, Data if (sc == NULL) return; // no settings set! - if (sc->get("enable") != "1") + if (!sc->getBool("enable", false)) { cerr << "API Server not enabled." << endl; return; } - string s_port = sc->get("port"); - string s_threads = sc->get("threads"); - - uint16_t port = (s_port == "" ? 6969 : atoi (s_port.c_str())); - uint16_t threads = (s_threads == "" ? 1 : atoi (s_threads.c_str())); - - if (threads <= 0) - threads = 1; - try { - *srv = Instance.httpserver = new HTTPServer (port, threads); + *srv = Instance.httpserver = new HTTPServer (settings); *wa = Instance.wa = new WebApp (*srv, drvr, settings); (*wa)->deploy(); } catch (ServerException &e)