Skip to content

Commit 838cede

Browse files
author
Sebastiano Merlino
committed
Changed in order to add optional optimizations on ws
1 parent 11f3c51 commit 838cede

File tree

5 files changed

+238
-136
lines changed

5 files changed

+238
-136
lines changed

src/http_endpoint.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ using namespace std;
2626
namespace httpserver
2727
{
2828
using namespace http;
29+
2930
//ENDPOINT
3031
http_endpoint::~http_endpoint()
3132
{
@@ -34,19 +35,21 @@ http_endpoint::~http_endpoint()
3435
regfree(&(this->re_url_modded));
3536
}
3637
}
37-
http_endpoint::http_endpoint(const string& url, bool family, bool registration):
38-
url_modded("/"),
38+
http_endpoint::http_endpoint(const string& url, bool family, bool registration, bool use_regex):
3939
family_url(family),
4040
reg_compiled(false)
4141
{
42+
if(use_regex)
43+
this->url_modded = "^/";
44+
else
45+
this->url_modded = "/";
4246
vector<string> parts;
4347
string_utilities::to_lower_copy(url, url_complete);
4448
http_utils::tokenize_url(url, parts);
4549
string buffered;
4650
bool first = true;
4751
if(registration)
4852
{
49-
this->url_modded = "^/";
5053
for(unsigned int i = 0; i< parts.size(); i++)
5154
{
5255
if((parts[i] != "") && (parts[i][0] != '{'))
@@ -103,14 +106,11 @@ http_endpoint::http_endpoint(const string& url, bool family, bool registration):
103106
}
104107
else
105108
{
106-
// RITORNARE ECCEZIONE
109+
throw bad_http_endpoint();
107110
}
108111
}
109112
this->url_pieces.push_back(parts[i]);
110113
}
111-
this->url_modded += "$";
112-
regcomp(&(this->re_url_modded), url_modded.c_str(), REG_EXTENDED|REG_ICASE|REG_NOSUB);
113-
reg_compiled = true;
114114
}
115115
else
116116
{
@@ -128,6 +128,12 @@ http_endpoint::http_endpoint(const string& url, bool family, bool registration):
128128
this->url_pieces.push_back(parts[i]);
129129
}
130130
}
131+
if(use_regex)
132+
{
133+
this->url_modded += "$";
134+
regcomp(&(this->re_url_modded), url_modded.c_str(), REG_EXTENDED|REG_ICASE|REG_NOSUB);
135+
reg_compiled = true;
136+
}
131137
}
132138

133139
http_endpoint::http_endpoint(const http_endpoint& h):

src/httpserver/http_endpoint.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ namespace httpserver
3030

3131
class webserver;
3232

33+
class bad_http_endpoint : public std::exception
34+
{
35+
virtual const char* what() const throw()
36+
{
37+
return "Bad url format!";
38+
}
39+
};
40+
3341
/**
3442
* Class representing an Http Endpoint. It is an abstraction used by the APIs.
3543
**/
@@ -149,7 +157,7 @@ class http_endpoint
149157
* @param registration boolean that indicates to the system if this is an endpoint that need to be registered to a webserver
150158
* or it is simply an endpoint to be used for comparisons.
151159
**/
152-
http_endpoint(const std::string& url, bool family = false, bool registration = false);
160+
http_endpoint(const std::string& url, bool family = false, bool registration = false, bool use_regex = true);
153161
/**
154162
* Destructor of the class. Essentially it frees the regex dinamically allocated pattern
155163
**/

src/httpserver/http_resource.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ class http_resource
170170
**/
171171
http_resource(const http_resource& b) : allowed_methods(b.allowed_methods) { }
172172

173+
void operator = (const http_resource& b)
174+
{
175+
allowed_methods = b.allowed_methods;
176+
}
177+
173178
private:
174179
friend class webserver;
175180
std::map<std::string, bool> allowed_methods;

src/httpserver/webserver.hpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,13 @@ class webserver
175175
const http_utils::cred_type_T& cred_type= http_utils::NONE,
176176
const std::string digest_auth_random = "", //IT'S CORRECT TO PASS THIS PARAMETER BY VALUE
177177
int nonce_nc_size = 0,
178-
const http_utils::policy_T& default_policy = http_utils::ACCEPT
178+
const http_utils::policy_T& default_policy = http_utils::ACCEPT,
179+
bool basic_auth_enabled = true,
180+
bool digest_auth_enabled = true,
181+
bool regex_checking = true,
182+
bool ban_system_enabled = true,
183+
bool post_process_enabled = true,
184+
http_resource* single_resource = 0x0
179185
);
180186
webserver(const create_webserver& params);
181187
/**
@@ -243,6 +249,12 @@ class webserver
243249
int nonce_nc_size;
244250
bool running;
245251
http_utils::policy_T default_policy;
252+
bool basic_auth_enabled;
253+
bool digest_auth_enabled;
254+
bool regex_checking;
255+
bool ban_system_enabled;
256+
bool post_process_enabled;
257+
bool single_resource;
246258

247259
std::map<http_endpoint, http_resource* > registered_resources;
248260
#ifdef USE_CPP_ZEROX
@@ -326,7 +338,13 @@ class create_webserver
326338
_cred_type(http_utils::NONE),
327339
_digest_auth_random(""),
328340
_nonce_nc_size(0),
329-
_default_policy(http_utils::ACCEPT)
341+
_default_policy(http_utils::ACCEPT),
342+
_basic_auth_enabled(true),
343+
_digest_auth_enabled(true),
344+
_regex_checking(true),
345+
_ban_system_enabled(true),
346+
_post_process_enabled(true),
347+
_single_resource(0x0)
330348
{
331349
}
332350

@@ -355,7 +373,13 @@ class create_webserver
355373
_cred_type(http_utils::NONE),
356374
_digest_auth_random(""),
357375
_nonce_nc_size(0),
358-
_default_policy(http_utils::ACCEPT)
376+
_default_policy(http_utils::ACCEPT),
377+
_basic_auth_enabled(true),
378+
_digest_auth_enabled(true),
379+
_regex_checking(true),
380+
_ban_system_enabled(true),
381+
_post_process_enabled(true),
382+
_single_resource(0x0)
359383
{
360384
}
361385

@@ -391,6 +415,17 @@ class create_webserver
391415
create_webserver& digest_auth_random(const std::string& digest_auth_random) { _digest_auth_random = digest_auth_random; return *this; }
392416
create_webserver& nonce_nc_size(int nonce_nc_size) { _nonce_nc_size = nonce_nc_size; return *this; }
393417
create_webserver& default_policy(const http_utils::policy_T& default_policy) { _default_policy = default_policy; return *this; }
418+
create_webserver& basic_auth() { _basic_auth_enabled = true; return *this; }
419+
create_webserver& no_basic_auth() { _basic_auth_enabled = false; return *this; }
420+
create_webserver& digest_auth() { _digest_auth_enabled = true; return *this; }
421+
create_webserver& no_digest_auth() { _digest_auth_enabled = false; return *this; }
422+
create_webserver& regex_checking() { _regex_checking = true; return *this; }
423+
create_webserver& no_regex_checking() { _regex_checking = false; return *this; }
424+
create_webserver& ban_system() { _ban_system_enabled = true; return *this; }
425+
create_webserver& no_ban_system() { _ban_system_enabled = false; return *this; }
426+
create_webserver& post_process() { _post_process_enabled = true; return *this; }
427+
create_webserver& no_post_process() { _post_process_enabled = false; return *this; }
428+
create_webserver& single_resource(http_resource* single_resource) { _single_resource = single_resource; return *this; }
394429
private:
395430
int _port;
396431
http_utils::start_method_T _start_method;
@@ -417,6 +452,12 @@ class create_webserver
417452
std::string _digest_auth_random;
418453
int _nonce_nc_size;
419454
http_utils::policy_T _default_policy;
455+
bool _basic_auth_enabled;
456+
bool _digest_auth_enabled;
457+
bool _regex_checking;
458+
bool _ban_system_enabled;
459+
bool _post_process_enabled;
460+
http_resource* _single_resource;
420461

421462
friend class webserver;
422463
};

0 commit comments

Comments
 (0)