Skip to content

Commit 1f53fb0

Browse files
author
Sebastiano Merlino
committed
Solved a logical error in http_resource route
Added some debug prints
1 parent 838cede commit 1f53fb0

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

src/http_resource.cpp

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ http_resource::http_resource()
3939
this->allowed_methods[MHD_HTTP_METHOD_TRACE] = true;
4040
this->allowed_methods[MHD_HTTP_METHOD_CONNECT] = true;
4141
this->allowed_methods[MHD_HTTP_METHOD_OPTIONS] = true;
42+
#ifdef DEBUG
43+
std::map<std::string, bool>::iterator it;
44+
for(it = allowed_methods.begin(); it != allowed_methods.end(); it++)
45+
{
46+
std::cout << (*it).first << " -> " << (*it).second << std::endl;
47+
}
48+
#endif //DEBUG
49+
4250
}
4351

4452
http_resource::~http_resource()
@@ -47,14 +55,7 @@ http_resource::~http_resource()
4755

4856
http_response http_resource::render(const http_request& r)
4957
{
50-
if(this->is_allowed(r.get_method()))
51-
{
52-
return this->render_404();
53-
}
54-
else
55-
{
56-
return this->render_405();
57-
}
58+
return this->render_404();
5859
}
5960

6061
http_response http_resource::render_404()
@@ -120,41 +121,48 @@ http_response http_resource::route_request(const http_request& r)
120121

121122
http_response res;
122123

123-
if(method == MHD_HTTP_METHOD_GET)
124-
{
125-
res = this->render_GET(r);
126-
}
127-
else if (method == MHD_HTTP_METHOD_POST)
124+
if(this->is_allowed(method))
128125
{
129-
res = this->render_POST(r);
130-
}
131-
else if (method == MHD_HTTP_METHOD_PUT)
132-
{
133-
res = this->render_PUT(r);
134-
}
135-
else if (method == MHD_HTTP_METHOD_DELETE)
136-
{
137-
res = this->render_DELETE(r);
138-
}
139-
else if (method == MHD_HTTP_METHOD_HEAD)
140-
{
141-
res = this->render_HEAD(r);
142-
}
143-
else if (method == MHD_HTTP_METHOD_TRACE)
144-
{
145-
res = this->render_TRACE(r);
146-
}
147-
else if (method == MHD_HTTP_METHOD_OPTIONS)
148-
{
149-
res = this->render_OPTIONS(r);
150-
}
151-
else if (method == MHD_HTTP_METHOD_CONNECT)
152-
{
153-
res = this->render_CONNECT(r);
154-
}
155-
else
126+
if(method == MHD_HTTP_METHOD_GET)
127+
{
128+
res = this->render_GET(r);
129+
}
130+
else if (method == MHD_HTTP_METHOD_POST)
131+
{
132+
res = this->render_POST(r);
133+
}
134+
else if (method == MHD_HTTP_METHOD_PUT)
135+
{
136+
res = this->render_PUT(r);
137+
}
138+
else if (method == MHD_HTTP_METHOD_DELETE)
139+
{
140+
res = this->render_DELETE(r);
141+
}
142+
else if (method == MHD_HTTP_METHOD_HEAD)
143+
{
144+
res = this->render_HEAD(r);
145+
}
146+
else if (method == MHD_HTTP_METHOD_TRACE)
147+
{
148+
res = this->render_TRACE(r);
149+
}
150+
else if (method == MHD_HTTP_METHOD_OPTIONS)
151+
{
152+
res = this->render_OPTIONS(r);
153+
}
154+
else if (method == MHD_HTTP_METHOD_CONNECT)
155+
{
156+
res = this->render_CONNECT(r);
157+
}
158+
else
159+
{
160+
res = this->render(r);
161+
}
162+
}
163+
else
156164
{
157-
res = this->render(r);
165+
res = this->render_405();
158166
}
159167
return res;
160168
}

src/httpserver/http_resource.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <map>
2323
#include <string>
2424

25+
#ifdef DEBUG
26+
#include <iostream>
27+
#endif
28+
2529
namespace httpserver
2630
{
2731

@@ -157,6 +161,13 @@ class http_resource
157161
}
158162
else
159163
{
164+
#ifdef DEBUG
165+
std::map<std::string, bool>::iterator it;
166+
for(it = allowed_methods.begin(); it != allowed_methods.end(); it++)
167+
{
168+
std::cout << (*it).first << " -> " << (*it).second << std::endl;
169+
}
170+
#endif //DEBUG
160171
return false;
161172
}
162173
}

0 commit comments

Comments
 (0)