Skip to content

Commit c91e241

Browse files
committed
Add support for PATCH method
1 parent 208f188 commit c91e241

File tree

6 files changed

+30
-0
lines changed

6 files changed

+30
-0
lines changed

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,11 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
789789
mr->callback = &http_resource::render_DELETE;
790790
body = true;
791791
}
792+
else if (0 == strcasecmp(method, http_utils::http_method_patch.c_str()))
793+
{
794+
mr->callback = &http_resource::render_PATCH;
795+
body = true;
796+
}
792797
else if (0 == strcasecmp(method, http_utils::http_method_head.c_str()))
793798
{
794799
mr->callback = &http_resource::render_HEAD;

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)