X Tutup
Skip to content

Commit 3850f99

Browse files
author
Sebastiano Merlino
committed
Changed http_endpoint, http_request, http_resource, http_response visibility in order to avoid constructions.
Solved a problem (in ruby wrapper) due to the inlining of http_endpoint destructor Changed Test.hpp/Test.cpp in order to follow new interface
1 parent 6944b25 commit 3850f99

File tree

9 files changed

+120
-114
lines changed

9 files changed

+120
-114
lines changed

src/http_endpoint.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ namespace httpserver
2727
{
2828
using namespace http;
2929
//ENDPOINT
30+
http_endpoint::~http_endpoint()
31+
{
32+
if(reg_compiled)
33+
{
34+
regfree(&(this->re_url_modded));
35+
}
36+
}
3037
http_endpoint::http_endpoint(const string& url, bool family, bool registration):
3138
url_complete(string_utilities::to_lower_copy(url)),
3239
url_modded("/"),

src/httpserver.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ namespace std {
5353
%module(directors="1") libhttpserver_python
5454
#endif
5555

56-
%feature("director") webserver;
57-
%feature("director") http_request;
58-
%feature("director") http_response;
59-
%feature("director") http_string_response;
60-
%feature("director") http_file_response;
61-
%feature("director") http_basic_auth_fail_response;
62-
%feature("director") http_digest_auth_fail_response;
63-
%feature("director") shoutCAST_response;
56+
//%feature("director") webserver;
57+
//%feature("director") http_request;
58+
//%feature("director") http_response;
59+
//%feature("director") http_string_response;
60+
//%feature("director") http_file_response;
61+
//%feature("director") http_basic_auth_fail_response;
62+
//%feature("director") http_digest_auth_fail_response;
63+
//%feature("director") shoutCAST_response;
6464
%feature("director") http_resource;
65-
%feature("director") http_endpoint;
66-
%feature("director") http_utils;
65+
//%feature("director") http_endpoint;
66+
//%feature("director") http_utils;
6767

6868
#ifdef SWIGPYTHON
6969
%typemap(out) string {

src/httpserver/http_endpoint.hpp

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define _HTTP_ENDPOINT_HPP_
2222

2323
#include <vector>
24+
#include <utility>
2425
#include <regex.h>
2526
#include <string>
2627

@@ -35,48 +36,15 @@ class webserver;
3536
class http_endpoint
3637
{
3738
public:
38-
/**
39-
* Default constructor of the class.
40-
* @param family boolean that indicates if the endpoint is a family endpoint.
41-
* A family endpoint is an endpoint that identifies a root and all its child like the same resource.
42-
* For example, if I identify "/path/" like a family endpoint and I associate to it the resource "A", also
43-
* "/path/to/res/" is automatically associated to resource "A".
44-
**/
45-
http_endpoint(bool family = false):
46-
url_complete("/"),
47-
url_modded("/"),
48-
family_url(family),
49-
reg_compiled(false)
50-
{
51-
}
52-
/**
53-
* Constructor of the class http_endpoint. It is used to initialize an http_endpoint starting from a string form URL.
54-
* @param url The string representation of the endpoint. All endpoints are in the form "/path/to/resource".
55-
* @param family boolean that indicates if the endpoint is a family endpoint.
56-
* A family endpoint is an endpoint that identifies a root and all its child like the same resource.
57-
* For example, if I identify "/path/" like a family endpoint and I associate to it the resource "A", also
58-
* "/path/to/res/" is automatically associated to resource "A".
59-
* @param registration boolean that indicates to the system if this is an endpoint that need to be registered to a webserver
60-
* or it is simply an endpoint to be used for comparisons.
61-
**/
62-
http_endpoint(const std::string& url, bool family = false, bool registration = false);
6339
/**
6440
* Copy constructor. It is useful expecially to copy regex_t structure that contains dinamically allocated data.
6541
* @param h The http_endpoint to copy
6642
**/
6743
http_endpoint(const http_endpoint& h);
6844
/**
69-
* Destructor of the class. Essentially it frees the regex dinamically allocated pattern
45+
* Class Destructor
7046
**/
71-
~http_endpoint()
72-
{
73-
74-
if(reg_compiled)
75-
{
76-
regfree(&(this->re_url_modded));
77-
}
78-
79-
}
47+
~http_endpoint(); //if inlined it causes problems during ruby wrapper compiling
8048
/**
8149
* Operator overload for "less than operator". It is used to order endpoints in maps.
8250
* @param b The http_endpoint to compare to
@@ -130,6 +98,34 @@ class http_endpoint
13098
return this->chunk_positions;
13199
}
132100
private:
101+
/**
102+
* Default constructor of the class.
103+
* @param family boolean that indicates if the endpoint is a family endpoint.
104+
* A family endpoint is an endpoint that identifies a root and all its child like the same resource.
105+
* For example, if I identify "/path/" like a family endpoint and I associate to it the resource "A", also
106+
* "/path/to/res/" is automatically associated to resource "A".
107+
**/
108+
http_endpoint(bool family = false):
109+
url_complete("/"),
110+
url_modded("/"),
111+
family_url(family),
112+
reg_compiled(false)
113+
{
114+
}
115+
/**
116+
* Constructor of the class http_endpoint. It is used to initialize an http_endpoint starting from a string form URL.
117+
* @param url The string representation of the endpoint. All endpoints are in the form "/path/to/resource".
118+
* @param family boolean that indicates if the endpoint is a family endpoint.
119+
* A family endpoint is an endpoint that identifies a root and all its child like the same resource.
120+
* For example, if I identify "/path/" like a family endpoint and I associate to it the resource "A", also
121+
* "/path/to/res/" is automatically associated to resource "A".
122+
* @param registration boolean that indicates to the system if this is an endpoint that need to be registered to a webserver
123+
* or it is simply an endpoint to be used for comparisons.
124+
**/
125+
http_endpoint(const std::string& url, bool family = false, bool registration = false);
126+
/**
127+
* Destructor of the class. Essentially it frees the regex dinamically allocated pattern
128+
**/
133129
std::string url_complete;
134130
std::string url_modded;
135131
std::vector<std::string> url_pars;
@@ -139,6 +135,7 @@ class http_endpoint
139135
// boost::xpressive::sregex re_url_modded;
140136
bool family_url;
141137
bool reg_compiled;
138+
friend class webserver;
142139
};
143140

144141
};

src/httpserver/http_request.hpp

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,7 @@ using namespace http;
4646
class http_request
4747
{
4848
public:
49-
/**
50-
* Default constructor of the class. It is a specific responsibility of apis to initialize this type of objects.
51-
**/
52-
http_request():
53-
content("")
54-
{
55-
}
56-
/**
57-
* Copy constructor.
58-
* @param b http_request b to copy attributes from.
59-
**/
60-
http_request(const http_request& b):
61-
user(b.user),
62-
pass(b.pass),
63-
path(b.path),
64-
digested_user(b.digested_user),
65-
method(b.method),
66-
post_path(b.post_path),
67-
headers(b.headers),
68-
footers(b.footers),
69-
cookies(b.cookies),
70-
args(b.args),
71-
content(b.content),
72-
version(b.version),
73-
requestor(b.requestor),
74-
underlying_connection(b.underlying_connection)
75-
{
76-
}
49+
7750
/**
7851
* Method used to get the username eventually passed through basic authentication.
7952
* @return string representation of the username.
@@ -406,7 +379,34 @@ class http_request
406379
}
407380
bool check_digest_auth(const std::string& realm, const std::string& password, int nonce_timeout, bool& reload_nonce) const;
408381
private:
409-
friend class webserver;
382+
/**
383+
* Default constructor of the class. It is a specific responsibility of apis to initialize this type of objects.
384+
**/
385+
http_request():
386+
content("")
387+
{
388+
}
389+
/**
390+
* Copy constructor.
391+
* @param b http_request b to copy attributes from.
392+
**/
393+
http_request(const http_request& b):
394+
user(b.user),
395+
pass(b.pass),
396+
path(b.path),
397+
digested_user(b.digested_user),
398+
method(b.method),
399+
post_path(b.post_path),
400+
headers(b.headers),
401+
footers(b.footers),
402+
cookies(b.cookies),
403+
args(b.args),
404+
content(b.content),
405+
version(b.version),
406+
requestor(b.requestor),
407+
underlying_connection(b.underlying_connection)
408+
{
409+
}
410410
std::string user;
411411
std::string pass;
412412
std::string path;
@@ -427,6 +427,7 @@ class http_request
427427
{
428428
this->underlying_connection = conn;
429429
}
430+
friend class webserver;
430431
};
431432

432433
};

src/httpserver/http_resource.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ class http_response;
3535
class http_resource
3636
{
3737
public:
38-
/**
39-
* Constructor of the class
40-
**/
41-
http_resource();
42-
/**
43-
* Copy constructor
44-
**/
45-
http_resource(const http_resource& b) : allowed_methods(b.allowed_methods) { }
4638
/**
4739
* Class destructor
4840
**/
@@ -168,6 +160,16 @@ class http_resource
168160
return false;
169161
}
170162
}
163+
protected:
164+
/**
165+
* Constructor of the class
166+
**/
167+
http_resource();
168+
/**
169+
* Copy constructor
170+
**/
171+
http_resource(const http_resource& b) : allowed_methods(b.allowed_methods) { }
172+
171173
private:
172174
friend class webserver;
173175
std::map<std::string, bool> allowed_methods;

src/httpserver/http_response.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class http_response
5454
BASIC_AUTH_FAIL,
5555
SWITCH_PROTOCOL
5656
};
57-
57+
5858
/**
5959
* Constructor used to build an http_response with a content and a response_code
6060
* @param content The content to set for the request. (if the response_type is FILE_CONTENT, it represents the path to the file to read from).
@@ -82,7 +82,6 @@ class http_response
8282
{
8383
set_header(http_utils::http_header_content_type, content_type);
8484
}
85-
8685
/**
8786
* Copy constructor
8887
* @param b The http_response object to copy attributes value from.
@@ -231,7 +230,6 @@ class http_response
231230
return 0;
232231
}
233232
protected:
234-
friend class webserver;
235233
response_type_T response_type;
236234
std::string content;
237235
int response_code;
@@ -242,6 +240,7 @@ class http_response
242240
std::string filename;
243241
std::map<std::string, std::string, header_comparator> headers;
244242
std::map<std::string, std::string, arg_comparator> footers;
243+
friend class webserver;
245244
};
246245

247246
class http_string_response : public http_response

src/webserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
718718
{
719719
support_req.set_digested_user(digested_user);
720720
}
721-
http_endpoint endpoint = http_endpoint(st_url);
721+
http_endpoint endpoint(st_url);
722722
http_response dhrs;
723723
const http_endpoint* matching_endpoint = 0x0;
724724
if(!(dws->registered_resources.count(endpoint) > 0))

test/Test.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,59 @@
88
#include <stdio.h>
99
using namespace std;
1010

11-
Test::Test() : HttpResource()
11+
Test::Test() : http_resource()
1212
{
1313
}
1414

15-
Test2::Test2() : HttpResource()
15+
Test2::Test2() : http_resource()
1616
{
1717
}
1818

19-
HttpResponse Test::render_GET(const HttpRequest& r)
19+
http_response Test::render_GET(const http_request& r)
2020
{
21-
cout << r.getVersion() << endl;
22-
cout << r.getRequestor() << endl;
23-
cout << r.getRequestorPort() << endl;
24-
cout << "PROVA: " << r.getArg("prova") << endl;
25-
cout << "ALTRO: " << r.getArg("altro") << endl;
26-
string pp = r.getArg("prova");
27-
return HttpFileResponse("/home/etr/progs/libhttpserver/test/readme", 200);
21+
cout << r.get_version() << endl;
22+
cout << r.get_requestor() << endl;
23+
cout << r.get_requestor_port() << endl;
24+
cout << "PROVA: " << r.get_arg("prova") << endl;
25+
cout << "ALTRO: " << r.get_arg("altro") << endl;
26+
string pp = r.get_arg("prova");
27+
return http_file_response("/home/etr/progs/libhttpserver/test/readme", 200);
2828
}
2929

30-
HttpResponse Test::render_POST(const HttpRequest& r)
30+
http_response Test::render_POST(const http_request& r)
3131
{
3232
fstream filestr;
3333
filestr.open("test.txt", fstream::out | fstream::app);
34-
filestr << r.getContent() << endl;
34+
filestr << r.get_content() << endl;
3535
filestr.close();
3636
cout << "DOPO" << endl;
37-
vector<string> vv = r.getPathPieces();
37+
vector<string> vv = r.get_path_pieces();
3838
for(int i = 0; i < vv.size(); i++)
3939
{
4040
cout << vv[i] << endl;
4141
}
42-
return HttpStringResponse("OK",200);
42+
return http_string_response("OK",200);
4343
}
4444

45-
HttpResponse Test2::render_GET(const HttpRequest& r)
45+
http_response Test2::render_GET(const http_request& r)
4646
{
4747
cout << "D2" << endl;
48-
return HttpStringResponse("{\" var1 \" : \" "+r.getArg("var1")+" \", \" var2 \" : \" "+r.getArg("var2")+" \", \" var3 \" : \" "+r.getArg("var3")+" \"}", 200);
48+
return http_string_response("{\" var1 \" : \" "+r.get_arg("var1")+" \", \" var2 \" : \" "+r.get_arg("var2")+" \", \" var3 \" : \" "+r.get_arg("var3")+" \"}", 200);
4949
}
5050

51-
HttpResponse Test::render_PUT(const HttpRequest& r)
51+
http_response Test::render_PUT(const http_request& r)
5252
{
53-
return HttpStringResponse(r.getContent(), 200);
53+
return http_string_response(r.get_content(), 200);
5454
}
5555

5656
int main()
5757
{
58-
Webserver ws = CreateWebserver(9898);
58+
webserver ws = create_webserver(9898);
5959
Test dt = Test();
6060
Test2 dt2 = Test2();
61-
ws.registerResource(string("base/{var1}/{var2}/drop_test/{var3}/tail"), &dt2, true);
62-
ws.registerResource(string("other/side"), &dt, true);
63-
boost::thread* t1 = new boost::thread(boost::bind(&Webserver::start, ws, true));
61+
ws.register_resource(string("base/{var1}/{var2}/drop_test/{var3}/tail"), &dt2, true);
62+
ws.register_resource(string("other/side"), &dt, true);
63+
boost::thread* t1 = new boost::thread(boost::bind(&webserver::start, ws, true));
6464
t1->join();
6565
return 0;
6666
}

0 commit comments

Comments
 (0)
X Tutup