Skip to content

Commit 6d77ac5

Browse files
authored
Merge pull request #151 from etr/body_methods_2
Enable body for delete requests and enable patch requests
2 parents 7145168 + 088c9d0 commit 6d77ac5

File tree

7 files changed

+37
-2
lines changed

7 files changed

+37
-2
lines changed

ChangeLog

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
Sat Jan 27 21:59:11 2018 -0800
1+
Tue Aug 06 22:22:14 2019 -0800
2+
Added support for body parsing in DELETE requests.
3+
Added support for PATCH method
4+
5+
Sat Jan 27 21:59:11 2019 -0800
26
libhttpserver now includes set of examples to demonstrate the main capabilities of the library
37
"examples" are now optionally disabled.
48
Adds valgrind memcheck to the build system on travis
@@ -7,7 +11,7 @@ Sat Jan 27 21:59:11 2018 -0800
711
All classes now implement move constructor and move assignment operator
812
The library now avoids collecting connection properties (headers, arguments, footers, cookies, etc...) unless explicitly asked by the client code.
913

10-
Sat Jan 12 00:51:00 2018 -0800
14+
Sat Jan 12 00:51:00 2019 -0800
1115
Removed the support for integrated COMET logic.
1216
Removed the support for caching logic.
1317
Added integ tests.

src/http_resource.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void resource_init(map<string, bool>& allowed_methods)
4343
allowed_methods[MHD_HTTP_METHOD_TRACE] = true;
4444
allowed_methods[MHD_HTTP_METHOD_CONNECT] = true;
4545
allowed_methods[MHD_HTTP_METHOD_OPTIONS] = true;
46+
allowed_methods[MHD_HTTP_METHOD_PATCH] = true;
4647
}
4748

4849
namespace details

src/http_utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ const std::string http_utils::http_method_options = MHD_HTTP_METHOD_OPTIONS;
205205
const std::string http_utils::http_method_post = MHD_HTTP_METHOD_POST;
206206
const std::string http_utils::http_method_put = MHD_HTTP_METHOD_PUT;
207207
const std::string http_utils::http_method_trace = MHD_HTTP_METHOD_TRACE;
208+
const std::string http_utils::http_method_patch = MHD_HTTP_METHOD_PATCH;
208209

209210
const std::string http_utils::http_post_encoding_form_urlencoded =
210211
MHD_HTTP_POST_ENCODING_FORM_URLENCODED;

src/httpserver/http_resource.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ class http_resource
136136
{
137137
return render(req);
138138
}
139+
/**
140+
* Method used to answer to a PATCH request
141+
* @param req Request passed through http
142+
* @return A http_response object
143+
**/
144+
virtual const std::shared_ptr<http_response> render_PATCH(const http_request& req)
145+
{
146+
return render(req);
147+
}
139148
/**
140149
* Method used to answer to a CONNECT request
141150
* @param req Request passed through http

src/httpserver/http_utils.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class http_utils
102102
static const short http_method_post_code;
103103
static const short http_method_put_code;
104104
static const short http_method_trace_code;
105+
static const short http_method_patch_code;
105106
static const short http_method_unknown_code;
106107

107108
static const int http_continue;
@@ -224,6 +225,7 @@ class http_utils
224225
static const std::string http_method_post;
225226
static const std::string http_method_put;
226227
static const std::string http_method_trace;
228+
static const std::string http_method_patch;
227229

228230
static const std::string http_post_encoding_form_urlencoded;
229231
static const std::string http_post_encoding_multipart_formdata;

src/webserver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,12 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
787787
else if (0 == strcasecmp(method,http_utils::http_method_delete.c_str()))
788788
{
789789
mr->callback = &http_resource::render_DELETE;
790+
body = true;
791+
}
792+
else if (0 == strcasecmp(method, http_utils::http_method_patch.c_str()))
793+
{
794+
mr->callback = &http_resource::render_PATCH;
795+
body = true;
790796
}
791797
else if (0 == strcasecmp(method, http_utils::http_method_head.c_str()))
792798
{

test/integ/basic.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ class complete_test_resource : public http_resource
158158
{
159159
return shared_ptr<string_response>(new string_response("OK", 200, "text/plain"));
160160
}
161+
const shared_ptr<http_response> render_PATCH(const http_request& req)
162+
{
163+
return shared_ptr<string_response>(new string_response("OK", 200, "text/plain"));
164+
}
161165
};
162166

163167
class only_render_resource : public http_resource
@@ -483,6 +487,14 @@ LT_BEGIN_AUTO_TEST(basic_suite, complete)
483487
curl_easy_cleanup(curl);
484488
}
485489

490+
{
491+
CURL* curl = curl_easy_init();
492+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
493+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
494+
CURLcode res = curl_easy_perform(curl);
495+
LT_ASSERT_EQ(res, 0);
496+
curl_easy_cleanup(curl);
497+
}
486498
/*
487499
{
488500
CURL* curl = curl_easy_init();

0 commit comments

Comments
 (0)