Skip to content

Commit 0649d3b

Browse files
committed
Add support for patch method
1 parent 2b8406e commit 0649d3b

File tree

2 files changed

+120
-40
lines changed

2 files changed

+120
-40
lines changed

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

Lines changed: 115 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,8 @@ 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,
124+
body_handler, body_generator);
143125
}
144126

145127
/**
@@ -225,26 +207,8 @@ class basic_client_facade {
225207
body_callback_function_type body_handler = body_callback_function_type(),
226208
body_generator_function_type body_generator =
227209
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);
210+
return perform_request(request, "PUT", body, content_type,
211+
body_handler, body_generator);
248212
}
249213

250214
/**
@@ -284,6 +248,117 @@ class basic_client_facade {
284248
return put(request, body, string_type(), body_handler, body_generator);
285249
}
286250

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

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)