Skip to content

Commit 4b9974a

Browse files
committed
Add support for patch method
1 parent 2bef8be commit 4b9974a

File tree

2 files changed

+119
-40
lines changed

2 files changed

+119
-40
lines changed

boost/network/protocol/http/client/facade.hpp

Lines changed: 114 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,7 @@ class basic_client_facade {
120120
body_callback_function_type body_handler = body_callback_function_type(),
121121
body_generator_function_type body_generator =
122122
body_generator_function_type()) {
123-
if (body != string_type()) {
124-
request << remove_header("Content-Length")
125-
<< header("Content-Length", std::to_string(body.size()))
126-
<< boost::network::body(body);
127-
}
128-
typename headers_range<basic_request<Tag> >::type content_type_headers =
129-
headers(request)["Content-Type"];
130-
if (content_type != string_type()) {
131-
if (!boost::empty(content_type_headers))
132-
request << remove_header("Content-Type");
133-
request << header("Content-Type", content_type);
134-
} else {
135-
if (boost::empty(content_type_headers)) {
136-
typedef typename char_<Tag>::type char_type;
137-
static char_type const content_type[] = "x-application/octet-stream";
138-
request << header("Content-Type", content_type);
139-
}
140-
}
141-
return pimpl->request_skeleton(request, "POST", true, body_handler,
142-
body_generator);
123+
return perform_request(request, "POST", body, content_type, body_handler, body_generator);
143124
}
144125

145126
/**
@@ -225,26 +206,7 @@ class basic_client_facade {
225206
body_callback_function_type body_handler = body_callback_function_type(),
226207
body_generator_function_type body_generator =
227208
body_generator_function_type()) {
228-
if (body != string_type()) {
229-
request << remove_header("Content-Length")
230-
<< header("Content-Length", std::to_string(body.size()))
231-
<< boost::network::body(body);
232-
}
233-
typename headers_range<basic_request<Tag> >::type content_type_headers =
234-
headers(request)["Content-Type"];
235-
if (content_type != string_type()) {
236-
if (!boost::empty(content_type_headers))
237-
request << remove_header("Content-Type");
238-
request << header("Content-Type", content_type);
239-
} else {
240-
if (boost::empty(content_type_headers)) {
241-
typedef typename char_<Tag>::type char_type;
242-
static char_type const content_type[] = "x-application/octet-stream";
243-
request << header("Content-Type", content_type);
244-
}
245-
}
246-
return pimpl->request_skeleton(request, "PUT", true, body_handler,
247-
body_generator);
209+
return perform_request(request, "PUT", body, content_type, body_handler, body_generator);
248210
}
249211

250212
/**
@@ -284,6 +246,118 @@ class basic_client_facade {
284246
return put(request, body, string_type(), body_handler, body_generator);
285247
}
286248

249+
250+
/**
251+
* Perform a PATCH request.
252+
*
253+
* @param[in] request A copy of the request object including the URI and
254+
* headers.
255+
* @param[in] body The whole contents of the body. If provided, this overrides
256+
* the body in the `request`.
257+
* @param[in] content_type The content type for the request. This overrides
258+
* the content type in the `request`.
259+
* @param[in] body_handler The callback invoked for parts of the response body
260+
* as they come in.
261+
* @param[in] body_generator If provided, is invoked to generate parts of the
262+
* request's body as it is being sent.
263+
* @returns A response object.
264+
* @throws std::exception May throw exceptions on errors, derived from
265+
* `std::exception`.
266+
*/
267+
response patch(
268+
request request, string_type const& body = string_type(),
269+
string_type const& content_type = string_type(),
270+
body_callback_function_type body_handler = body_callback_function_type(),
271+
body_generator_function_type body_generator =
272+
body_generator_function_type()) {
273+
return perform_request(request, "PATCH", body, content_type, body_handler, body_generator);
274+
}
275+
276+
/**
277+
* Perform a PATCH request.
278+
*
279+
* @param[in] request The request including the URI and headers.
280+
* @param[in] callback If provided, the function to call for parts of the
281+
* response's body as they come in.
282+
* @param[in] body_generator The function to call to generate part of the body
283+
* while the request is being performed.
284+
* @returns A response object.
285+
* @throws std::exception May throw exceptions derived from std::exception in
286+
* case of errors.
287+
*/
288+
response patch(request const& request, body_callback_function_type callback,
289+
body_generator_function_type body_generator =
290+
body_generator_function_type()) {
291+
return patch(request, string_type(), string_type(), callback, body_generator);
292+
}
293+
294+
/**
295+
* Perform a PATCH request.
296+
*
297+
* @param[in] request The request object including the URI and headers.
298+
* @param[in] body The whole contents of the body.
299+
* @param[in] body_handler The callback invoked for parts of the response body
300+
* as they come in.
301+
* @param[in] body_generator If provided, is invoked to generate parts of the
302+
* request's body as it is being sent.
303+
* @returns A response object.
304+
* @throws std::exception May throw exceptions on errors, derived from
305+
* `std::exception`.
306+
*/
307+
response patch(request const& request, string_type body,
308+
body_callback_function_type body_handler,
309+
body_generator_function_type body_generator = {}) {
310+
return patch(request, body, string_type(), body_handler, body_generator);
311+
}
312+
313+
/**
314+
* Perform a request.
315+
*
316+
* @param[in] request A copy of the request object including the URI and
317+
* headers.
318+
* @param[in] method The HTTP method
319+
* @param[in] body The whole contents of the body. If provided, this overrides
320+
* the body in the `request`.
321+
* @param[in] content_type The content type for the request. This overrides
322+
* the content type in the `request`.
323+
* @param[in] body_handler The callback invoked for parts of the response body
324+
* as they come in.
325+
* @param[in] body_generator If provided, is invoked to generate parts of the
326+
* request's body as it is being sent.
327+
* @returns A response object.
328+
* @throws std::exception May throw exceptions on errors, derived from
329+
* `std::exception`.
330+
*/
331+
response perform_request(request request, string_type const& method,
332+
string_type const& body = string_type(),
333+
string_type const& content_type = string_type(),
334+
body_callback_function_type body_handler =
335+
body_callback_function_type(),
336+
body_generator_function_type body_generator =
337+
body_generator_function_type()) {
338+
{
339+
if (body != string_type()) {
340+
request << remove_header("Content-Length")
341+
<< header("Content-Length", std::to_string(body.size()))
342+
<< boost::network::body(body);
343+
}
344+
typename headers_range<basic_request<Tag> >::type content_type_headers =
345+
headers(request)["Content-Type"];
346+
if (content_type != string_type()) {
347+
if (!boost::empty(content_type_headers))
348+
request << remove_header("Content-Type");
349+
request << header("Content-Type", content_type);
350+
} else {
351+
if (boost::empty(content_type_headers)) {
352+
typedef typename char_<Tag>::type char_type;
353+
static char_type const content_type[] = "x-application/octet-stream";
354+
request << header("Content-Type", content_type);
355+
}
356+
}
357+
return pimpl->request_skeleton(request, method, true, body_handler,
358+
body_generator);
359+
}
360+
287361
/**
288362
* Perform a DELETE request.
289363
*

boost/network/protocol/http/traits/impl/request_methods.ipp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ struct request_methods<tags::http_default_8bit_tcp_resolve> {
3030
return PUT;
3131
};
3232

33+
static char const* patch() {
34+
static char const* const PATCH = "PATCH";
35+
return PATCH;
36+
};
37+
3338
static char const* post() {
3439
static char const* const POST = "POST";
3540
return POST;

0 commit comments

Comments
 (0)