Skip to content

Commit afa67ef

Browse files
committed
Normalized structure of http_response.
Now it has to use pointers to make use of polymorphism. Used shared_ptr to guarantee memory management. Removed internal smart pointer implementation. Removed internal binder implementation.
1 parent 40e5f6d commit afa67ef

36 files changed

+887
-1191
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Sat Jan 12 00:51:00 2018 -0800
2+
Removed the support for integrated COMET logic.
3+
Removed the support for caching logic.
4+
Added integ tests.
5+
Changed http_resource interface to use shared_ptr.
6+
Improved interface of the http_response object.
7+
Deprecated http_response_builder object.
8+
19
Thu Dec 26 10:00:30 2018 -0800
210
Fixed IPV6 parsing logic.
311
Added tests to support IP parsing, URL parsing and utilities

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],[16])dnl
24+
m4_define([libhttpserver_MINOR_VERSION],[17])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

examples/hello_world.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ using namespace httpserver;
2525

2626
class hello_world_resource : public http_resource {
2727
public:
28-
const http_response render(const http_request&);
28+
const std::shared_ptr<http_response> render(const http_request&);
2929
void set_some_data(const std::string &s) {data = s;}
3030
std::string data;
3131
};
3232

3333
//using the render method you are able to catch each type of request you receive
34-
const http_response hello_world_resource::render(const http_request& req)
34+
const std::shared_ptr<http_response> hello_world_resource::render(const http_request& req)
3535
{
3636
//it is possible to store data inside the resource object that can be altered
3737
//through the requests
@@ -42,7 +42,7 @@ const http_response hello_world_resource::render(const http_request& req)
4242

4343
//it is possible to send a response initializing an http_string_response
4444
//that reads the content to send in response from a string.
45-
return http_response_builder("Hello World!!!", 200).string_response();
45+
return std::shared_ptr<http_response>(new string_response("Hello World!!!", 200));
4646
}
4747

4848
int main()

examples/service.cpp

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ class service_resource: public http_resource {
3232
service_resource();
3333

3434
~service_resource();
35-
36-
const http_response render_GET(const http_request &req);
37-
const http_response render_PUT(const http_request &req);
38-
const http_response render_POST(const http_request &req);
39-
const http_response render(const http_request &req);
40-
const http_response render_HEAD(const http_request &req);
41-
const http_response render_OPTIONS(const http_request &req);
42-
const http_response render_CONNECT(const http_request &req);
43-
const http_response render_DELETE(const http_request &req);
35+
36+
const std::shared_ptr<http_response> render_GET(const http_request &req);
37+
const std::shared_ptr<http_response> render_PUT(const http_request &req);
38+
const std::shared_ptr<http_response> render_POST(const http_request &req);
39+
const std::shared_ptr<http_response> render(const http_request &req);
40+
const std::shared_ptr<http_response> render_HEAD(const http_request &req);
41+
const std::shared_ptr<http_response> render_OPTIONS(const http_request &req);
42+
const std::shared_ptr<http_response> render_CONNECT(const http_request &req);
43+
const std::shared_ptr<http_response> render_DELETE(const http_request &req);
4444

4545
private:
4646

@@ -53,120 +53,118 @@ service_resource::service_resource()
5353
service_resource::~service_resource()
5454
{}
5555

56-
const http_response
56+
const std::shared_ptr<http_response>
5757
service_resource::render_GET(const http_request &req)
5858
{
5959
std::cout << "service_resource::render_GET()" << std::endl;
6060

6161
if (verbose) std::cout << req;
62+
string_response* res = new string_response("GET response", 200);
6263

63-
http_response res(http_response_builder("GET response", 200).string_response());
64-
65-
if (verbose) std::cout << res;
66-
67-
return res;
64+
if (verbose) std::cout << *res;
6865

66+
return std::shared_ptr<http_response>(res);
6967
}
7068

7169

72-
const http_response
70+
const std::shared_ptr<http_response>
7371
service_resource::render_PUT(const http_request &req)
7472
{
75-
std::cout << "service_resource::render_PUT()" << std::endl;
73+
std::cout << "service_resource::render_PUT()" << std::endl;
7674

7775
if (verbose) std::cout << req;
78-
79-
http_response res(http_response_builder("PUT response", 200).string_response());
8076

81-
if (verbose) std::cout << res;
77+
string_response* res = new string_response("PUT response", 200);
8278

83-
return res;
79+
if (verbose) std::cout << *res;
80+
81+
return std::shared_ptr<http_response>(res);
8482
}
8583

8684

87-
const http_response
85+
const std::shared_ptr<http_response>
8886
service_resource::render_POST(const http_request &req)
8987
{
90-
std::cout << "service_resource::render_POST()" << std::endl;
88+
std::cout << "service_resource::render_POST()" << std::endl;
9189

9290
if (verbose) std::cout << req;
93-
94-
http_response res(http_response_builder("POST response", 200).string_response());
9591

96-
if (verbose) std::cout << res;
92+
string_response* res = new string_response("POST response", 200);
93+
94+
if (verbose) std::cout << *res;
9795

98-
return res;
96+
return std::shared_ptr<http_response>(res);
9997
}
10098

101-
const http_response
99+
const std::shared_ptr<http_response>
102100
service_resource::render(const http_request &req)
103101
{
104-
std::cout << "service_resource::render()" << std::endl;
102+
std::cout << "service_resource::render()" << std::endl;
105103

106104
if (verbose) std::cout << req;
107105

108-
http_response res(http_response_builder("generic response", 200).string_response());
106+
string_response* res = new string_response("generic response", 200);
109107

110-
if (verbose) std::cout << res;
108+
if (verbose) std::cout << *res;
111109

112-
return res;
110+
return std::shared_ptr<http_response>(res);
113111
}
114112

115113

116-
const http_response
114+
const std::shared_ptr<http_response>
117115
service_resource::render_HEAD(const http_request &req)
118116
{
119117
std::cout << "service_resource::render_HEAD()" << std::endl;
120118

121119
if (verbose) std::cout << req;
122-
123-
http_response res(http_response_builder("HEAD response", 200).string_response());
124120

125-
if (verbose) std::cout << res;
121+
string_response* res = new string_response("HEAD response", 200);
126122

127-
return res;
123+
if (verbose) std::cout << *res;
124+
125+
return std::shared_ptr<http_response>(res);
128126
}
129127

130-
const http_response
128+
const std::shared_ptr<http_response>
131129
service_resource::render_OPTIONS(const http_request &req)
132130
{
133131
std::cout << "service_resource::render_OPTIONS()" << std::endl;
134132

135133
if (verbose) std::cout << req;
136-
137-
http_response res(http_response_builder("OPTIONS response", 200).string_response());
138134

139-
if (verbose) std::cout << res;
135+
string_response* res = new string_response("OPTIONS response", 200);
136+
137+
if (verbose) std::cout << *res;
140138

141-
return res;
139+
return std::shared_ptr<http_response>(res);
142140
}
143141

144-
const http_response
142+
const std::shared_ptr<http_response>
145143
service_resource::render_CONNECT(const http_request &req)
146144
{
147-
std::cout << "service_resource::render_CONNECT()" << std::endl;
145+
std::cout << "service_resource::render_CONNECT()" << std::endl;
148146

149147
if (verbose) std::cout << req;
150-
151-
http_response res(http_response_builder("CONNECT response", 200).string_response());
152148

153-
if (verbose) std::cout << res;
149+
string_response* res = new string_response("CONNECT response", 200);
154150

155-
return res;
151+
if (verbose) std::cout << *res;
152+
153+
return std::shared_ptr<http_response>(res);
156154
}
157155

158-
const http_response
156+
const std::shared_ptr<http_response>
159157
service_resource::render_DELETE(const http_request &req)
160158
{
161-
std::cout << "service_resource::render_DELETE()" << std::endl;
159+
std::cout << "service_resource::render_DELETE()" << std::endl;
162160

163161
if (verbose) std::cout << req;
164-
165-
http_response res(http_response_builder("DELETE response", 200).string_response());
166162

167-
if (verbose) std::cout << res;
163+
string_response* res = new string_response("DELETE response", 200);
164+
165+
if (verbose) std::cout << *res;
168166

169-
return res;
167+
return std::shared_ptr<http_response>(res);
170168
}
171169

172170
void usage()

src/Makefile.am

Lines changed: 3 additions & 3 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/http_endpoint.cpp
23-
noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.hpp httpserver/details/http_response_ptr.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
22+
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp http_request.cpp http_response.cpp string_response.cpp basic_auth_fail_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp details/http_endpoint.cpp
23+
noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.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/string_response.hpp httpserver/basic_auth_fail_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp
2525

2626
AM_CXXFLAGS += -fPIC -Wall
2727

src/basic_auth_fail_response.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011-2019 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#include "basic_auth_fail_response.hpp"
22+
23+
using namespace std;
24+
25+
namespace httpserver
26+
{
27+
28+
int basic_auth_fail_response::enqueue_response(MHD_Connection* connection, MHD_Response* response)
29+
{
30+
return MHD_queue_basic_auth_fail_response(
31+
connection,
32+
realm.c_str(),
33+
response
34+
);
35+
}
36+
37+
}

src/deferred_response.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011-2019 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#include "deferred_response.hpp"
22+
23+
using namespace std;
24+
25+
namespace httpserver
26+
{
27+
28+
namespace details
29+
{
30+
31+
ssize_t cb(void* cls, uint64_t pos, char* buf, size_t max)
32+
{
33+
ssize_t val = static_cast<deferred_response*>(cls)->cycle_callback(buf, max);
34+
if(val == -1)
35+
{
36+
static_cast<deferred_response*>(cls)->completed = true;
37+
}
38+
39+
return val;
40+
}
41+
42+
}
43+
44+
MHD_Response* deferred_response::get_raw_response()
45+
{
46+
if(!completed)
47+
{
48+
return MHD_create_response_from_callback(
49+
MHD_SIZE_UNKNOWN,
50+
1024,
51+
&details::cb,
52+
this,
53+
NULL
54+
);
55+
}
56+
else
57+
{
58+
return static_cast<string_response*>(this)->get_raw_response();
59+
}
60+
}
61+
62+
void deferred_response::decorate_response(MHD_Response* response)
63+
{
64+
if(completed)
65+
{
66+
static_cast<string_response*>(this)->decorate_response(response);
67+
}
68+
}
69+
70+
}
71+

src/digest_auth_fail_response.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011-2019 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#include "digest_auth_fail_response.hpp"
22+
23+
using namespace std;
24+
25+
namespace httpserver
26+
{
27+
28+
int digest_auth_fail_response::enqueue_response(MHD_Connection* connection, MHD_Response* response)
29+
{
30+
return MHD_queue_auth_fail_response(
31+
connection,
32+
realm.c_str(),
33+
opaque.c_str(),
34+
response,
35+
reload_nonce ? MHD_YES : MHD_NO
36+
);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)