Skip to content

Commit 4c18ce7

Browse files
author
Sebastiano Merlino
committed
Experimenting on improving readability of http_response interface
1 parent c8d270e commit 4c18ce7

File tree

13 files changed

+185
-196
lines changed

13 files changed

+185
-196
lines changed

examples/benchmark.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ using namespace httpserver;
3636

3737
class hello_world_resource : public http_resource {
3838
public:
39-
void render(const http_request&, http_response**);
39+
const http_response render(const http_request&);
4040
};
4141

4242
//using the render method you are able to catch each type of request you receive
43-
void hello_world_resource::render(const http_request& req, http_response** res)
43+
const http_response hello_world_resource::render(const http_request& req)
4444
{
4545
//it is possible to send a response initializing an http_string_response
4646
//that reads the content to send in response from a string.
47-
*res = new http_response(http_response_builder(PAGE, 200).string_response());
47+
return http_response(http_response_builder(PAGE, 200).string_response());
4848
}
4949

5050
int main()

examples/comet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ std::vector<std::string> topics(topics_array, topics_array + sizeof(topics_array
2929

3030
class comet_send_resource : public http_resource {
3131
public:
32-
void render(const http_request& req, http_response** res)
32+
const http_response render(const http_request& req)
3333
{
34-
*res = new http_response(http_response_builder("Hi", 200).long_polling_send_response(topics_array[0]));
34+
return http_response(http_response_builder("Hi", 200).long_polling_send_response(topics_array[0]));
3535
}
3636
};
3737

3838
class comet_listen_resource : public http_resource {
3939
public:
40-
void render(const http_request& req, http_response** res)
40+
const http_response render(const http_request& req)
4141
{
42-
*res = new http_response(http_response_builder("OK", 200).long_polling_receive_response(topics));
42+
return http_response(http_response_builder("OK", 200).long_polling_receive_response(topics));
4343
}
4444
};
4545

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-
void render(const http_request&, http_response**);
28+
const 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-
void hello_world_resource::render(const http_request& req, http_response** res)
34+
const 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 @@ void hello_world_resource::render(const http_request& req, http_response** res)
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-
*res = new http_response(http_response_builder("Hello World!!!", 200).string_response());
45+
return http_response(http_response_builder("Hello World!!!", 200).string_response());
4646
}
4747

4848
int main()

examples/service.cpp

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,14 @@ class service_resource: public http_resource {
3333

3434
~service_resource();
3535

36-
void render_GET(const http_request &req, http_response** res);
37-
38-
void render_PUT(const http_request &req, http_response** res);
39-
40-
void render_POST(const http_request &req, http_response** res);
41-
42-
void render(const http_request &req, http_response** res);
43-
44-
void render_HEAD(const http_request &req, http_response** res);
45-
46-
void render_OPTIONS(const http_request &req, http_response** res);
47-
48-
void render_CONNECT(const http_request &req, http_response** res);
49-
50-
void render_DELETE(const http_request &req, http_response** res);
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);
5144

5245
private:
5346

@@ -60,102 +53,120 @@ service_resource::service_resource()
6053
service_resource::~service_resource()
6154
{}
6255

63-
void
64-
service_resource::render_GET(const http_request &req, http_response** res)
56+
const http_response
57+
service_resource::render_GET(const http_request &req)
6558
{
66-
std::cout << "service_resource::render_GET()" << std::endl;
59+
std::cout << "service_resource::render_GET()" << std::endl;
6760

6861
if (verbose) std::cout << req;
6962

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

72-
if (verbose) std::cout << **res;
7369
}
7470

7571

76-
void
77-
service_resource::render_PUT(const http_request &req, http_response** res)
72+
const http_response
73+
service_resource::render_PUT(const http_request &req)
7874
{
79-
std::cout << "service_resource::render_PUT()" << std::endl;
75+
std::cout << "service_resource::render_PUT()" << std::endl;
8076

8177
if (verbose) std::cout << req;
8278

83-
*res = new http_response(http_response_builder("PUT response", 200).string_response());
79+
http_response res(http_response_builder("PUT response", 200).string_response());
8480

85-
if (verbose) std::cout << **res;
81+
if (verbose) std::cout << res;
82+
83+
return res;
8684
}
8785

8886

89-
void
90-
service_resource::render_POST(const http_request &req, http_response** res)
87+
const http_response
88+
service_resource::render_POST(const http_request &req)
9189
{
92-
std::cout << "service_resource::render_POST()" << std::endl;
90+
std::cout << "service_resource::render_POST()" << std::endl;
9391

9492
if (verbose) std::cout << req;
9593

96-
*res = new http_response(http_response_builder("POST response", 200).string_response());
94+
http_response res(http_response_builder("POST response", 200).string_response());
95+
96+
if (verbose) std::cout << res;
9797

98-
if (verbose) std::cout << **res;
98+
return res;
9999
}
100-
void
101-
service_resource::render(const http_request &req, http_response** res)
100+
101+
const http_response
102+
service_resource::render(const http_request &req)
102103
{
103-
std::cout << "service_resource::render()" << std::endl;
104+
std::cout << "service_resource::render()" << std::endl;
104105

105106
if (verbose) std::cout << req;
106107

107-
*res = new http_response(http_response_builder("generic response", 200).string_response());
108+
http_response res(http_response_builder("generic response", 200).string_response());
109+
110+
if (verbose) std::cout << res;
108111

109-
if (verbose) std::cout << **res;
112+
return res;
110113
}
111114

112115

113-
void
114-
service_resource::render_HEAD(const http_request &req, http_response** res)
116+
const http_response
117+
service_resource::render_HEAD(const http_request &req)
115118
{
116-
std::cout << "service_resource::render_HEAD()" << std::endl;
119+
std::cout << "service_resource::render_HEAD()" << std::endl;
117120

118121
if (verbose) std::cout << req;
119122

120-
*res = new http_response(http_response_builder("HEAD response", 200).string_response());
123+
http_response res(http_response_builder("HEAD response", 200).string_response());
121124

122-
if (verbose) std::cout << **res;
125+
if (verbose) std::cout << res;
126+
127+
return res;
123128
}
124129

125-
void
126-
service_resource::render_OPTIONS(const http_request &req, http_response** res)
130+
const http_response
131+
service_resource::render_OPTIONS(const http_request &req)
127132
{
128-
std::cout << "service_resource::render_OPTIONS()" << std::endl;
133+
std::cout << "service_resource::render_OPTIONS()" << std::endl;
129134

130135
if (verbose) std::cout << req;
131136

132-
*res = new http_response(http_response_builder("OPTIONS response", 200).string_response());
137+
http_response res(http_response_builder("OPTIONS response", 200).string_response());
138+
139+
if (verbose) std::cout << res;
133140

134-
if (verbose) std::cout << **res;
141+
return res;
135142
}
136143

137-
void
138-
service_resource::render_CONNECT(const http_request &req, http_response** res)
144+
const http_response
145+
service_resource::render_CONNECT(const http_request &req)
139146
{
140-
std::cout << "service_resource::render_CONNECT()" << std::endl;
147+
std::cout << "service_resource::render_CONNECT()" << std::endl;
141148

142149
if (verbose) std::cout << req;
143150

144-
*res = new http_response(http_response_builder("CONNECT response", 200).string_response());
151+
http_response res(http_response_builder("CONNECT response", 200).string_response());
145152

146-
if (verbose) std::cout << **res;
153+
if (verbose) std::cout << res;
154+
155+
return res;
147156
}
148157

149-
void
150-
service_resource::render_DELETE(const http_request &req, http_response** res)
158+
const http_response
159+
service_resource::render_DELETE(const http_request &req)
151160
{
152-
std::cout << "service_resource::render_DELETE()" << std::endl;
161+
std::cout << "service_resource::render_DELETE()" << std::endl;
153162

154163
if (verbose) std::cout << req;
155164

156-
*res = new http_response(http_response_builder("DELETE response", 200).string_response());
165+
http_response res(http_response_builder("DELETE response", 200).string_response());
166+
167+
if (verbose) std::cout << res;
157168

158-
if (verbose) std::cout << **res;
169+
return res;
159170
}
160171

161172
void usage()

src/http_resource.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ void resource_init(map<string, bool>& allowed_methods)
4848
namespace details
4949
{
5050

51-
void empty_render(const http_request& r, http_response** res)
51+
http_response empty_render(const http_request& r)
5252
{
53-
*res = new http_response(http_response_builder("", 200).string_response());
53+
return http_response_builder("", 200).string_response();
5454
}
5555

5656
};

src/httpserver/create_webserver.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <stdlib.h>
2929
#include "httpserver/http_utils.hpp"
30+
#include "httpserver/http_response.hpp"
3031

3132
#define DEFAULT_WS_TIMEOUT 180
3233
#define DEFAULT_WS_PORT 9898
@@ -35,9 +36,8 @@ namespace httpserver {
3536

3637
class webserver;
3738
class http_request;
38-
class http_response;
3939

40-
typedef void(*render_ptr)(const http_request&, http_response**);
40+
typedef http_response(*render_ptr)(const http_request&);
4141
typedef bool(*validator_ptr)(const std::string&);
4242
typedef void(*unescaper_ptr)(char*);
4343
typedef void(*log_access_ptr)(const std::string&);

src/httpserver/details/modded_request.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct modded_request
4141
std::string* standardized_url;
4242
webserver* ws;
4343

44-
void (httpserver::http_resource::*callback)(const httpserver::http_request&, httpserver::http_response**);
44+
const http_response (httpserver::http_resource::*callback)(const httpserver::http_request&);
4545

4646
http_request* dhr;
4747
http_response_ptr dhrs;

0 commit comments

Comments
 (0)