Skip to content

Commit dfe3664

Browse files
author
Sebastiano Merlino
committed
Avoid to expose dependencies toward detail classes
1 parent 35dc94f commit dfe3664

File tree

3 files changed

+48
-190
lines changed

3 files changed

+48
-190
lines changed

src/httpserver/create_webserver.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727

2828
#include "httpserver/http_utils.hpp"
2929

30+
#define DEFAULT_WS_TIMEOUT 180
31+
#define DEFAULT_WS_PORT 9898
32+
3033
namespace httpserver {
3134

3235
class webserver;
3336
class http_request;
3437
class http_response;
3538

36-
using namespace http;
37-
3839
typedef void(*render_ptr)(const http_request&, http_response**);
3940
typedef bool(*validator_ptr)(const std::string&);
4041
typedef void(*unescaper_ptr)(char*);
@@ -46,7 +47,7 @@ class create_webserver
4647
public:
4748
create_webserver():
4849
_port(DEFAULT_WS_PORT),
49-
_start_method(http_utils::INTERNAL_SELECT),
50+
_start_method(http::http_utils::INTERNAL_SELECT),
5051
_max_threads(0),
5152
_max_connections(0),
5253
_memory_limit(0),
@@ -67,10 +68,10 @@ class create_webserver
6768
_https_mem_cert(""),
6869
_https_mem_trust(""),
6970
_https_priorities(""),
70-
_cred_type(http_utils::NONE),
71+
_cred_type(http::http_utils::NONE),
7172
_digest_auth_random(""),
7273
_nonce_nc_size(0),
73-
_default_policy(http_utils::ACCEPT),
74+
_default_policy(http::http_utils::ACCEPT),
7475
_basic_auth_enabled(true),
7576
_digest_auth_enabled(true),
7677
_regex_checking(true),
@@ -86,7 +87,7 @@ class create_webserver
8687

8788
explicit create_webserver(int port):
8889
_port(port),
89-
_start_method(http_utils::INTERNAL_SELECT),
90+
_start_method(http::http_utils::INTERNAL_SELECT),
9091
_max_threads(0),
9192
_max_connections(0),
9293
_memory_limit(0),
@@ -107,10 +108,10 @@ class create_webserver
107108
_https_mem_cert(""),
108109
_https_mem_trust(""),
109110
_https_priorities(""),
110-
_cred_type(http_utils::NONE),
111+
_cred_type(http::http_utils::NONE),
111112
_digest_auth_random(""),
112113
_nonce_nc_size(0),
113-
_default_policy(http_utils::ACCEPT),
114+
_default_policy(http::http_utils::ACCEPT),
114115
_basic_auth_enabled(true),
115116
_digest_auth_enabled(true),
116117
_regex_checking(true),
@@ -126,7 +127,7 @@ class create_webserver
126127

127128
create_webserver& port(int port) { _port = port; return *this; }
128129
create_webserver& start_method(
129-
const http_utils::start_method_T& start_method
130+
const http::http_utils::start_method_T& start_method
130131
)
131132
{
132133
_start_method = start_method; return *this;
@@ -226,7 +227,7 @@ class create_webserver
226227
{
227228
_https_priorities = https_priorities; return *this;
228229
}
229-
create_webserver& cred_type(const http_utils::cred_type_T& cred_type)
230+
create_webserver& cred_type(const http::http_utils::cred_type_T& cred_type)
230231
{
231232
_cred_type = cred_type; return *this;
232233
}
@@ -241,7 +242,7 @@ class create_webserver
241242
_nonce_nc_size = nonce_nc_size; return *this;
242243
}
243244
create_webserver& default_policy(
244-
const http_utils::policy_T& default_policy
245+
const http::http_utils::policy_T& default_policy
245246
)
246247
{
247248
_default_policy = default_policy; return *this;
@@ -317,7 +318,7 @@ class create_webserver
317318

318319
private:
319320
int _port;
320-
http_utils::start_method_T _start_method;
321+
http::http_utils::start_method_T _start_method;
321322
int _max_threads;
322323
int _max_connections;
323324
int _memory_limit;
@@ -338,10 +339,10 @@ class create_webserver
338339
std::string _https_mem_cert;
339340
std::string _https_mem_trust;
340341
std::string _https_priorities;
341-
http_utils::cred_type_T _cred_type;
342+
http::http_utils::cred_type_T _cred_type;
342343
std::string _digest_auth_random;
343344
int _nonce_nc_size;
344-
http_utils::policy_T _default_policy;
345+
http::http_utils::policy_T _default_policy;
345346
bool _basic_auth_enabled;
346347
bool _digest_auth_enabled;
347348
bool _regex_checking;

src/httpserver/webserver.hpp

Lines changed: 30 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
#define METHOD_ERROR "Method not Allowed"
3030
#define NOT_METHOD_ERROR "Method not Acceptable"
3131
#define GENERIC_ERROR "Internal Error"
32-
#define DEFAULT_WS_PORT 9898
33-
#define DEFAULT_WS_TIMEOUT 180
3432

3533
#include <cstring>
3634
#include <map>
@@ -47,8 +45,6 @@
4745

4846
#include <pthread.h>
4947

50-
#include "httpserver/details/http_resource_mirror.hpp"
51-
#include "httpserver/details/event_tuple.hpp"
5248
#include "httpserver/create_webserver.hpp"
5349

5450
namespace httpserver {
@@ -57,76 +53,27 @@ template<typename CHILD> class http_resource;
5753
class http_response;
5854
struct cache_entry;
5955
template<typename CHILD> class event_supplier;
56+
class create_webserver;
6057

6158
namespace http {
6259
struct ip_representation;
60+
struct httpserver_ska;
6361
};
6462

63+
namespace details {
64+
class http_resource_mirror;
65+
class event_tuple;
66+
class http_endpoint;
67+
class daemon_item;
68+
class modded_request;
69+
}
70+
6571
/**
6672
* Class representing the webserver. Main class of the apis.
6773
**/
6874
class webserver
6975
{
7076
public:
71-
/**
72-
* Constructor of the class.
73-
* @param port Integer representing the port the webserver is listening on.
74-
* @param start_method
75-
* @param max_threads max number of serving threads (0 -> infty)
76-
* @param max_connections max number of allowed connections (0 -> infty).
77-
* @param memory_limit max memory allocated to serve requests (0 -> infty).
78-
* @param per_IP_connection_limit max number of connections allowed for a single IP (0 -> infty).
79-
* @param log_delegate logging_delegate object used to log
80-
* @param validator request_validator object used to validate requestors
81-
* @param unescaper unescaper object used to unescape urls.
82-
* @param max_thread_stack_size max dimesion of request stack
83-
* @param https_mem_key used with https. Private key used for requests.
84-
* @param https_mem_cert used with https. Certificate used for requests.
85-
* @param https_mem_trust used with https. CA Certificate used to trust request certificates.
86-
* @param https_priorities used with https. Determinates the SSL/TLS version to be used.
87-
* @param cred_type used with https. Daemon credential type. Can be NONE, certificate or anonymous.
88-
* @param digest_auth_random used with https. Digest authentication nonce's seed.
89-
* @param nonce_nc_size used with https. Size of an array of nonce and nonce counter map.
90-
**/
91-
explicit webserver
92-
(
93-
int port = DEFAULT_WS_PORT,
94-
const http_utils::start_method_T& start_method = http_utils::INTERNAL_SELECT,
95-
int max_threads = 0,
96-
int max_connections = 0,
97-
int memory_limit = 0,
98-
int connection_timeout = DEFAULT_WS_TIMEOUT,
99-
int per_IP_connection_limit = 0,
100-
log_access_ptr log_access = 0x0,
101-
log_error_ptr log_error = 0x0,
102-
validator_ptr validator = 0x0,
103-
unescaper_ptr unescaper = 0x0,
104-
const struct sockaddr* bind_address = 0x0,
105-
int bind_socket = 0,
106-
int max_thread_stack_size = 0,
107-
bool use_ssl = false,
108-
bool use_ipv6 = false,
109-
bool debug = false,
110-
bool pedantic = false,
111-
const std::string& https_mem_key = "",
112-
const std::string& https_mem_cert = "",
113-
const std::string& https_mem_trust = "",
114-
const std::string& https_priorities = "",
115-
const http_utils::cred_type_T& cred_type= http_utils::NONE,
116-
const std::string digest_auth_random = "", //IT'S CORRECT TO PASS THIS PARAMETER BY VALUE
117-
int nonce_nc_size = 0,
118-
const http_utils::policy_T& default_policy = http_utils::ACCEPT,
119-
bool basic_auth_enabled = true,
120-
bool digest_auth_enabled = true,
121-
bool regex_checking = true,
122-
bool ban_system_enabled = true,
123-
bool post_process_enabled = true,
124-
render_ptr single_resource = 0x0,
125-
render_ptr not_found_resource = 0x0,
126-
render_ptr method_not_allowed_resource = 0x0,
127-
render_ptr method_not_acceptable_resource = 0x0,
128-
render_ptr internal_error_resource = 0x0
129-
);
13077
webserver(const create_webserver& params);
13178
/**
13279
* Destructor of the class
@@ -174,20 +121,20 @@ class webserver
174121
void send_message_to_topic(const std::string& topic,
175122
const std::string& message
176123
);
177-
void send_message_to_consumer(const httpserver_ska& connection_id,
124+
void send_message_to_consumer(const http::httpserver_ska& connection_id,
178125
const std::string& message, bool to_lock = true
179126
);
180127
void register_to_topics(const std::vector<std::string>& topics,
181-
const httpserver_ska& connection_id, int keepalive_secs = -1,
128+
const http::httpserver_ska& connection_id, int keepalive_secs = -1,
182129
std::string keepalive_msg = ""
183130
);
184-
size_t read_message(const httpserver_ska& connection_id,
131+
size_t read_message(const http::httpserver_ska& connection_id,
185132
std::string& message
186133
);
187134
size_t get_topic_consumers(const std::string& topic,
188-
std::set<httpserver_ska>& consumers
135+
std::set<http::httpserver_ska>& consumers
189136
);
190-
bool pop_signaled(const httpserver_ska& consumer);
137+
bool pop_signaled(const http::httpserver_ska& consumer);
191138

192139
http_response* get_from_cache(const std::string& key, bool* valid,
193140
bool lock = false, bool write = false
@@ -250,13 +197,7 @@ class webserver
250197
event_supplier<T>* ev_supplier
251198
)
252199
{
253-
pthread_rwlock_wrlock(&runguard);
254-
std::map<std::string, details::event_tuple>::iterator it =
255-
event_suppliers.find(id);
256-
if(it != event_suppliers.end())
257-
delete it->second;
258-
event_suppliers[id] = details::event_tuple(&ev_supplier);
259-
pthread_rwlock_unlock(&runguard);
200+
//TODO: implement method
260201
}
261202

262203
void remove_event_supplier(const std::string& id);
@@ -267,7 +208,7 @@ class webserver
267208
void sweet_kill();
268209
private:
269210
const int port;
270-
http_utils::start_method_T start_method;
211+
http::http_utils::start_method_T start_method;
271212
const int max_threads;
272213
const int max_connections;
273214
const int memory_limit;
@@ -288,11 +229,11 @@ class webserver
288229
const std::string https_mem_cert;
289230
const std::string https_mem_trust;
290231
const std::string https_priorities;
291-
const http_utils::cred_type_T cred_type;
232+
const http::http_utils::cred_type_T cred_type;
292233
const std::string digest_auth_random;
293234
const int nonce_nc_size;
294235
bool running;
295-
const http_utils::policy_T default_policy;
236+
const http::http_utils::policy_T default_policy;
296237
const bool basic_auth_enabled;
297238
const bool digest_auth_enabled;
298239
const bool regex_checking;
@@ -315,19 +256,19 @@ class webserver
315256
int next_to_choose;
316257
pthread_rwlock_t cache_guard;
317258
#ifdef USE_CPP_ZEROX
318-
std::unordered_set<ip_representation> bans;
319-
std::unordered_set<ip_representation> allowances;
259+
std::unordered_set<http::ip_representation> bans;
260+
std::unordered_set<http::ip_representation> allowances;
320261
#else
321-
std::set<ip_representation> bans;
322-
std::set<ip_representation> allowances;
262+
std::set<http::ip_representation> bans;
263+
std::set<http::ip_representation> allowances;
323264
#endif
324265

325-
std::map<httpserver_ska, std::deque<std::string> > q_messages;
326-
std::map<std::string, std::set<httpserver_ska> > q_waitings;
327-
std::map<httpserver_ska, std::pair<pthread_mutex_t, pthread_cond_t> > q_blocks;
328-
std::set<httpserver_ska> q_signal;
329-
std::map<httpserver_ska, long> q_keepalives;
330-
std::map<httpserver_ska, std::pair<int, std::string> > q_keepalives_mem;
266+
std::map<http::httpserver_ska, std::deque<std::string> > q_messages;
267+
std::map<std::string, std::set<http::httpserver_ska> > q_waitings;
268+
std::map<http::httpserver_ska, std::pair<pthread_mutex_t, pthread_cond_t> > q_blocks;
269+
std::set<http::httpserver_ska> q_signal;
270+
std::map<http::httpserver_ska, long> q_keepalives;
271+
std::map<http::httpserver_ska, std::pair<int, std::string> > q_keepalives_mem;
331272
pthread_rwlock_t comet_guard;
332273

333274
std::vector<details::daemon_item*> daemons;
@@ -337,7 +278,6 @@ class webserver
337278

338279
webserver& operator=(const webserver& b);
339280

340-
void init(render_ptr single_resource);
341281
static void* select(void* self);
342282
static void* cleaner(void* self);
343283

@@ -432,7 +372,7 @@ class webserver
432372

433373
bool use_internal_select()
434374
{
435-
return this->start_method == http_utils::INTERNAL_SELECT;
375+
return this->start_method == http::http_utils::INTERNAL_SELECT;
436376
}
437377

438378
friend int policy_callback (void *cls,

0 commit comments

Comments
 (0)