Skip to content

Commit 75010ba

Browse files
committed
Added tests to http_endpoint.
Moved http_endpoint to details subpackage to allow for tests
1 parent bf90c01 commit 75010ba

File tree

8 files changed

+292
-46
lines changed

8 files changed

+292
-46
lines changed

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
AM_CPPFLAGS = -I../ -I$(srcdir)/httpserver/
2020
METASOURCES = AUTO
2121
lib_LTLIBRARIES = libhttpserver.la
22-
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp http_request.cpp http_response.cpp http_resource.cpp details/comet_manager.cpp http_endpoint.cpp
22+
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp http_request.cpp http_response.cpp http_resource.cpp details/comet_manager.cpp details/http_endpoint.cpp
2323
noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.hpp httpserver/details/http_response_ptr.hpp httpserver/details/cache_entry.hpp httpserver/details/comet_manager.hpp gettext.h
24-
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/binders.hpp httpserver/http_response_builder.hpp
24+
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/details/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/binders.hpp httpserver/http_response_builder.hpp
2525

2626
AM_CXXFLAGS += -fPIC -Wall
2727

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
USA
1919
*/
2020

21-
#include "http_endpoint.hpp"
21+
#include "details/http_endpoint.hpp"
2222
#include "http_utils.hpp"
2323
#include "string_utilities.hpp"
2424

@@ -27,17 +27,20 @@ using namespace std;
2727
namespace httpserver
2828
{
2929

30+
namespace details
31+
{
32+
3033
using namespace http;
3134

32-
webserver::http_endpoint::~http_endpoint()
35+
http_endpoint::~http_endpoint()
3336
{
3437
if(reg_compiled)
3538
{
36-
regfree(&(this->re_url_modded));
39+
regfree(&(this->re_url_normalized));
3740
}
3841
}
3942

40-
webserver::http_endpoint::http_endpoint
43+
http_endpoint::http_endpoint
4144
(
4245
const string& url,
4346
bool family,
@@ -47,7 +50,7 @@ webserver::http_endpoint::http_endpoint
4750
family_url(family),
4851
reg_compiled(false)
4952
{
50-
this->url_modded = use_regex ? "^/" : "/";
53+
this->url_normalized = use_regex ? "^/" : "/";
5154
vector<string> parts;
5255

5356
#ifdef CASE_INSENSITIVE
@@ -67,7 +70,7 @@ webserver::http_endpoint::http_endpoint
6770
{
6871
if(!registration)
6972
{
70-
this->url_modded += (first ? "" : "/") + parts[i];
73+
this->url_normalized += (first ? "" : "/") + parts[i];
7174
first = false;
7275

7376
this->url_pieces.push_back(parts[i]);
@@ -79,12 +82,12 @@ webserver::http_endpoint::http_endpoint
7982
{
8083
if(first)
8184
{
82-
this->url_modded = (parts[i][0] == '^' ? "" : this->url_modded) + parts[i];
85+
this->url_normalized = (parts[i][0] == '^' ? "" : this->url_normalized) + parts[i];
8386
first = false;
8487
}
8588
else
8689
{
87-
this->url_modded += "/" + parts[i];
90+
this->url_normalized += "/" + parts[i];
8891
}
8992
this->url_pieces.push_back(parts[i]);
9093

@@ -96,7 +99,7 @@ webserver::http_endpoint::http_endpoint
9699

97100
std::string::size_type bar = parts[i].find_first_of('|');
98101
this->url_pars.push_back(parts[i].substr(1, bar != string::npos ? bar - 1 : parts[i].size() - 2));
99-
this->url_modded += (first ? "" : "/") + (bar != string::npos ? parts[i].substr(bar + 1, parts[i].size() - bar - 2) : "([^\\/]+)");
102+
this->url_normalized += (first ? "" : "/") + (bar != string::npos ? parts[i].substr(bar + 1, parts[i].size() - bar - 2) : "([^\\/]+)");
100103

101104
first = false;
102105

@@ -107,37 +110,37 @@ webserver::http_endpoint::http_endpoint
107110

108111
if(use_regex)
109112
{
110-
this->url_modded += "$";
111-
regcomp(&(this->re_url_modded), url_modded.c_str(),
113+
this->url_normalized += "$";
114+
regcomp(&(this->re_url_normalized), url_normalized.c_str(),
112115
REG_EXTENDED|REG_ICASE|REG_NOSUB
113116
);
114117
this->reg_compiled = true;
115118
}
116119
}
117120

118-
webserver::http_endpoint::http_endpoint(const webserver::http_endpoint& h):
121+
http_endpoint::http_endpoint(const http_endpoint& h):
119122
url_complete(h.url_complete),
120-
url_modded(h.url_modded),
123+
url_normalized(h.url_normalized),
121124
url_pars(h.url_pars),
122125
url_pieces(h.url_pieces),
123126
chunk_positions(h.chunk_positions),
124127
family_url(h.family_url),
125128
reg_compiled(h.reg_compiled)
126129
{
127130
if(this->reg_compiled)
128-
regcomp(&(this->re_url_modded), url_modded.c_str(),
131+
regcomp(&(this->re_url_normalized), url_normalized.c_str(),
129132
REG_EXTENDED|REG_ICASE|REG_NOSUB
130133
);
131134
}
132135

133-
webserver::http_endpoint& webserver::http_endpoint::operator =(const webserver::http_endpoint& h)
136+
http_endpoint& http_endpoint::operator =(const http_endpoint& h)
134137
{
135138
this->url_complete = h.url_complete;
136-
this->url_modded = h.url_modded;
139+
this->url_normalized = h.url_normalized;
137140
this->family_url = h.family_url;
138141
this->reg_compiled = h.reg_compiled;
139142
if(this->reg_compiled)
140-
regcomp(&(this->re_url_modded), url_modded.c_str(),
143+
regcomp(&(this->re_url_normalized), url_normalized.c_str(),
141144
REG_EXTENDED|REG_ICASE|REG_NOSUB
142145
);
143146
this->url_pars = h.url_pars;
@@ -146,16 +149,16 @@ webserver::http_endpoint& webserver::http_endpoint::operator =(const webserver::
146149
return *this;
147150
}
148151

149-
bool webserver::http_endpoint::operator <(const webserver::http_endpoint& b) const
152+
bool http_endpoint::operator <(const http_endpoint& b) const
150153
{
151-
COMPARATOR(this->url_modded, b.url_modded, std::toupper);
154+
COMPARATOR(this->url_normalized, b.url_normalized, std::toupper);
152155
}
153156

154-
bool webserver::http_endpoint::match(const webserver::http_endpoint& url) const
157+
bool http_endpoint::match(const http_endpoint& url) const
155158
{
156159

157-
if(!this->family_url || url.url_pieces.size() < this->url_pieces.size())
158-
return regexec(&(this->re_url_modded), url.url_complete.c_str(), 0, NULL, 0) == 0;
160+
if(!this->family_url || url.url_pieces.size() < this->url_pieces.size())
161+
return regexec(&(this->re_url_normalized), url.url_complete.c_str(), 0, NULL, 0) == 0;
159162

160163
string nn = "/";
161164
bool first = true;
@@ -164,8 +167,9 @@ bool webserver::http_endpoint::match(const webserver::http_endpoint& url) const
164167
nn += (first ? "" : "/") + url.url_pieces[i];
165168
first = false;
166169
}
167-
return regexec(&(this->re_url_modded), nn.c_str(), 0, NULL, 0) == 0;
170+
return regexec(&(this->re_url_normalized), nn.c_str(), 0, NULL, 0) == 0;
168171
}
169172

170173
};
171174

175+
};

src/httpserver.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#define _HTTPSERVER_HPP_INSIDE_
2525

2626
#include "httpserver/http_utils.hpp"
27-
#include "httpserver/http_endpoint.hpp"
2827
#include "httpserver/http_resource.hpp"
2928
#include "httpserver/http_response.hpp"
3029
#include "httpserver/http_response_builder.hpp"
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@
3131
#include <string>
3232
#include <stdexcept>
3333

34-
#include "webserver.hpp"
35-
3634
namespace httpserver
3735
{
3836

39-
class webserver;
37+
namespace details
38+
{
39+
4040
class http_resource;
4141

4242
/**
4343
* Class representing an Http Endpoint. It is an abstraction used by the APIs.
4444
**/
45-
class webserver::http_endpoint
45+
class http_endpoint
4646
{
4747
public:
4848
/**
@@ -88,6 +88,11 @@ class webserver::http_endpoint
8888
return this->url_complete;
8989
}
9090

91+
const std::string& get_url_normalized() const
92+
{
93+
return this->url_normalized;
94+
}
95+
9196
/**
9297
* Method used to get all pars defined inside an url.
9398
* @return a vector of strings representing all found pars.
@@ -115,6 +120,16 @@ class webserver::http_endpoint
115120
return this->chunk_positions;
116121
}
117122

123+
const bool is_family_url() const
124+
{
125+
return this->family_url;
126+
}
127+
128+
const bool is_regex_compiled() const
129+
{
130+
return this->reg_compiled;
131+
}
132+
118133
/**
119134
* Default constructor of the class.
120135
* @param family boolean that indicates if the endpoint is a family endpoint.
@@ -124,7 +139,7 @@ class webserver::http_endpoint
124139
**/
125140
http_endpoint(bool family = false):
126141
url_complete("/"),
127-
url_modded("/"),
142+
url_normalized("/"),
128143
family_url(family),
129144
reg_compiled(false)
130145
{
@@ -147,6 +162,7 @@ class webserver::http_endpoint
147162
bool use_regex = true
148163
);
149164

165+
private:
150166
/**
151167
* The complete url extracted
152168
**/
@@ -155,7 +171,7 @@ class webserver::http_endpoint
155171
/**
156172
* The url standardized in order to use standard comparisons or regexes
157173
**/
158-
std::string url_modded;
174+
std::string url_normalized;
159175

160176
/**
161177
* Vector containing parameters extracted from url
@@ -175,7 +191,7 @@ class webserver::http_endpoint
175191
/**
176192
* Regex used in comparisons
177193
**/
178-
regex_t re_url_modded;
194+
regex_t re_url_normalized;
179195

180196
/**
181197
* Boolean indicating wheter the endpoint represents a family
@@ -188,5 +204,7 @@ class webserver::http_endpoint
188204
bool reg_compiled;
189205
};
190206

207+
};
208+
191209
};
192210
#endif

src/httpserver/webserver.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#include "httpserver/create_webserver.hpp"
4747
#include "httpserver/http_response.hpp"
4848

49+
#include "details/http_endpoint.hpp"
50+
4951
namespace httpserver {
5052

5153
class http_resource;
@@ -173,8 +175,6 @@ class webserver
173175
webserver& operator=(const webserver& other);
174176

175177
private:
176-
class http_endpoint;
177-
178178
const uint16_t port;
179179
http::http_utils::start_method_T start_method;
180180
const int max_threads;
@@ -219,7 +219,7 @@ class webserver
219219
render_ptr method_not_allowed_resource;
220220
render_ptr method_not_acceptable_resource;
221221
render_ptr internal_error_resource;
222-
std::map<http_endpoint, http_resource*> registered_resources;
222+
std::map<details::http_endpoint, http_resource*> registered_resources;
223223
std::map<std::string, http_resource*> registered_resources_str;
224224

225225
std::map<std::string, details::cache_entry*> response_cache;

src/webserver.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#include "http_response.hpp"
5050
#include "http_request.hpp"
5151
#include "http_response_builder.hpp"
52-
#include "http_endpoint.hpp"
52+
#include "details/http_endpoint.hpp"
5353
#include "string_utilities.hpp"
5454
#include "create_webserver.hpp"
5555
#include "details/comet_manager.hpp"
@@ -225,10 +225,10 @@ void webserver::request_completed (
225225

226226
bool webserver::register_resource(const std::string& resource, http_resource* hrm, bool family)
227227
{
228-
http_endpoint idx(resource, family, true, regex_checking);
228+
details::http_endpoint idx(resource, family, true, regex_checking);
229229

230-
pair<map<http_endpoint, http_resource*>::iterator, bool> result = registered_resources.insert(
231-
map<http_endpoint, http_resource*>::value_type(idx, hrm)
230+
pair<map<details::http_endpoint, http_resource*>::iterator, bool> result = registered_resources.insert(
231+
map<details::http_endpoint, http_resource*>::value_type(idx, hrm)
232232
);
233233

234234
if(result.second)
@@ -436,9 +436,9 @@ bool webserver::stop()
436436

437437
void webserver::unregister_resource(const string& resource)
438438
{
439-
http_endpoint he(resource);
439+
details::http_endpoint he(resource);
440440
this->registered_resources.erase(he);
441-
this->registered_resources.erase(he.url_complete);
441+
this->registered_resources.erase(he.get_url_complete());
442442
}
443443

444444
void webserver::ban_ip(const string& ip)
@@ -813,11 +813,11 @@ int webserver::finalize_answer(
813813
if(regex_checking)
814814
{
815815

816-
map<http_endpoint, http_resource*>::iterator found_endpoint;
816+
map<details::http_endpoint, http_resource*>::iterator found_endpoint;
817817

818-
http_endpoint endpoint(st_url, false, false, regex_checking);
818+
details::http_endpoint endpoint(st_url, false, false, regex_checking);
819819

820-
map<http_endpoint, http_resource*>::iterator it;
820+
map<details::http_endpoint, http_resource*>::iterator it;
821821

822822
size_t len = 0;
823823
size_t tot_len = 0;

test/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
LDADD = $(top_builddir)/src/libhttpserver.la
2020
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
2121
METASOURCES = AUTO
22-
check_PROGRAMS = basic http_utils threaded string_utilities
22+
check_PROGRAMS = basic http_utils threaded string_utilities http_endpoint
2323

2424
MOSTLYCLEANFILES = *.gcda *.gcno *.gcov
2525

2626
basic_SOURCES = integ/basic.cpp
2727
threaded_SOURCES = integ/threaded.cpp
2828
http_utils_SOURCES = unit/http_utils_test.cpp
2929
string_utilities_SOURCES = unit/string_utilities_test.cpp
30+
http_endpoint_SOURCES = unit/http_endpoint_test.cpp
3031

3132
noinst_HEADERS = littletest.hpp
3233
AM_CXXFLAGS += -lcurl -Wall -fPIC

0 commit comments

Comments
 (0)