Skip to content

Commit b5f5ef9

Browse files
committed
Changed http_endpoint to be an inner class of webserver.
This avoids the class being publicly accessible in a cleaner way because it avoids using friends as a mechanism to allow instantiation within stl structures
1 parent 562a0eb commit b5f5ef9

File tree

8 files changed

+43
-49
lines changed

8 files changed

+43
-49
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Sun Nov 04 19:28:00 2018 -0800
2+
Moved http_endpoint as a sub-class of webserver. This avoids usage of friends.
3+
14
Wed Feb 26 21:31:00 2017 +0000
25
Fixed problem with segfault when copying http_response object
36

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
AC_PREREQ(2.57)
2323
m4_define([libhttpserver_MAJOR_VERSION],[0])dnl
24-
m4_define([libhttpserver_MINOR_VERSION],[13])dnl
24+
m4_define([libhttpserver_MINOR_VERSION],[14])dnl
2525
m4_define([libhttpserver_REVISION],[0])dnl
2626
m4_define([libhttpserver_PKG_VERSION],[libhttpserver_MAJOR_VERSION.libhttpserver_MINOR_VERSION.libhttpserver_REVISION])dnl
2727
m4_define([libhttpserver_LDF_VERSION],[libhttpserver_MAJOR_VERSION:libhttpserver_MINOR_VERSION:libhttpserver_REVISION])dnl

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 details/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 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/details/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/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: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
USA
1919
*/
2020

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

@@ -29,18 +29,15 @@ namespace httpserver
2929

3030
using namespace http;
3131

32-
namespace details
33-
{
34-
35-
http_endpoint::~http_endpoint()
32+
webserver::http_endpoint::~http_endpoint()
3633
{
3734
if(reg_compiled)
3835
{
3936
regfree(&(this->re_url_modded));
4037
}
4138
}
4239

43-
http_endpoint::http_endpoint
40+
webserver::http_endpoint::http_endpoint
4441
(
4542
const string& url,
4643
bool family,
@@ -95,7 +92,7 @@ http_endpoint::http_endpoint
9592
}
9693

9794
if((parts[i].size() < 3) || (parts[i][0] != '{') || (parts[i][parts[i].size() - 1] != '}'))
98-
throw bad_http_endpoint();
95+
throw webserver::http_endpoint::bad_http_endpoint();
9996

10097
std::string::size_type bar = parts[i].find_first_of('|');
10198
this->url_pars.push_back(parts[i].substr(1, bar != string::npos ? bar - 1 : parts[i].size() - 2));
@@ -118,7 +115,7 @@ http_endpoint::http_endpoint
118115
}
119116
}
120117

121-
http_endpoint::http_endpoint(const http_endpoint& h):
118+
webserver::http_endpoint::http_endpoint(const webserver::http_endpoint& h):
122119
url_complete(h.url_complete),
123120
url_modded(h.url_modded),
124121
url_pars(h.url_pars),
@@ -133,7 +130,7 @@ http_endpoint::http_endpoint(const http_endpoint& h):
133130
);
134131
}
135132

136-
http_endpoint& http_endpoint::operator =(const http_endpoint& h)
133+
webserver::http_endpoint& webserver::http_endpoint::operator =(const webserver::http_endpoint& h)
137134
{
138135
this->url_complete = h.url_complete;
139136
this->url_modded = h.url_modded;
@@ -149,12 +146,12 @@ http_endpoint& http_endpoint::operator =(const http_endpoint& h)
149146
return *this;
150147
}
151148

152-
bool http_endpoint::operator <(const http_endpoint& b) const
149+
bool webserver::http_endpoint::operator <(const webserver::http_endpoint& b) const
153150
{
154151
COMPARATOR(this->url_modded, b.url_modded, std::toupper);
155152
}
156153

157-
bool http_endpoint::match(const http_endpoint& url) const
154+
bool webserver::http_endpoint::match(const webserver::http_endpoint& url) const
158155
{
159156

160157
if(!this->family_url || url.url_pieces.size() < this->url_pieces.size())
@@ -172,4 +169,3 @@ bool http_endpoint::match(const http_endpoint& url) const
172169

173170
};
174171

175-
};

src/httpserver.hpp

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

2626
#include "httpserver/http_utils.hpp"
27-
#include "httpserver/details/http_endpoint.hpp"
27+
#include "httpserver/http_endpoint.hpp"
2828
#include "httpserver/http_resource.hpp"
2929
#include "httpserver/http_response.hpp"
3030
#include "httpserver/http_response_builder.hpp"
Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <regex.h>
3131
#include <string>
3232

33+
#include "webserver.hpp"
34+
3335
namespace httpserver
3436
{
3537

@@ -42,24 +44,27 @@ namespace details
4244
/**
4345
* Exception class throwed when a bad formatted http url is used
4446
**/
45-
class bad_http_endpoint : public std::exception
46-
{
47-
/**
48-
* Method used to see error details
49-
* @return a const char* containing the error message
50-
**/
51-
virtual const char* what() const throw()
52-
{
53-
return "Bad url format!";
54-
}
47+
5548
};
5649

5750
/**
5851
* Class representing an Http Endpoint. It is an abstraction used by the APIs.
5952
**/
60-
class http_endpoint
53+
class webserver::http_endpoint
6154
{
62-
private:
55+
public:
56+
class bad_http_endpoint : public std::exception
57+
{
58+
/**
59+
* Method used to see error details
60+
* @return a const char* containing the error message
61+
**/
62+
virtual const char* what() const throw()
63+
{
64+
return "Bad url format!";
65+
}
66+
};
67+
6368
/**
6469
* Copy constructor. It is useful expecially to copy regex_t structure that contains dinamically allocated data.
6570
* @param h The http_endpoint to copy
@@ -232,17 +237,6 @@ class http_endpoint
232237
* Boolean indicating if the regex is compiled
233238
**/
234239
bool reg_compiled;
235-
friend class httpserver::webserver;
236-
friend void _register_resource(
237-
webserver*,
238-
const std::string&,
239-
const http_resource&,
240-
bool
241-
);
242-
template<typename, typename> friend struct std::pair;
243-
template<typename> friend struct std::less;
244-
};
245-
246240
};
247241

248242
};

src/httpserver/webserver.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ struct httpserver_ska;
5757
};
5858

5959
namespace details {
60-
class http_endpoint;
6160
struct daemon_item;
6261
struct modded_request;
6362
struct cache_entry;
@@ -174,6 +173,8 @@ class webserver
174173
webserver& operator=(const webserver& other);
175174

176175
private:
176+
class http_endpoint;
177+
177178
const uint16_t port;
178179
http::http_utils::start_method_T start_method;
179180
const int max_threads;
@@ -218,7 +219,7 @@ class webserver
218219
render_ptr method_not_allowed_resource;
219220
render_ptr method_not_acceptable_resource;
220221
render_ptr internal_error_resource;
221-
std::map<details::http_endpoint, http_resource*> registered_resources;
222+
std::map<http_endpoint, http_resource*> registered_resources;
222223
std::map<std::string, http_resource*> registered_resources_str;
223224

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

src/webserver.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#include "http_response.hpp"
4949
#include "http_request.hpp"
5050
#include "http_response_builder.hpp"
51-
#include "details/http_endpoint.hpp"
51+
#include "http_endpoint.hpp"
5252
#include "string_utilities.hpp"
5353
#include "create_webserver.hpp"
5454
#include "details/comet_manager.hpp"
@@ -224,10 +224,10 @@ void webserver::request_completed (
224224

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

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

233233
if(result.second)
@@ -435,7 +435,7 @@ bool webserver::stop()
435435

436436
void webserver::unregister_resource(const string& resource)
437437
{
438-
details::http_endpoint he(resource);
438+
http_endpoint he(resource);
439439
this->registered_resources.erase(he);
440440
this->registered_resources.erase(he.url_complete);
441441
}
@@ -813,11 +813,11 @@ int webserver::finalize_answer(
813813
if(regex_checking)
814814
{
815815

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

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

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

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

0 commit comments

Comments
 (0)