Skip to content

Add support for PATCH method #881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 115 additions & 40 deletions boost/network/protocol/http/client/facade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,8 @@ class basic_client_facade {
body_callback_function_type body_handler = body_callback_function_type(),
body_generator_function_type body_generator =
body_generator_function_type()) {
if (body != string_type()) {
request << remove_header("Content-Length")
<< header("Content-Length", std::to_string(body.size()))
<< boost::network::body(body);
}
typename headers_range<basic_request<Tag> >::type content_type_headers =
headers(request)["Content-Type"];
if (content_type != string_type()) {
if (!boost::empty(content_type_headers))
request << remove_header("Content-Type");
request << header("Content-Type", content_type);
} else {
if (boost::empty(content_type_headers)) {
typedef typename char_<Tag>::type char_type;
static char_type const content_type[] = "x-application/octet-stream";
request << header("Content-Type", content_type);
}
}
return pimpl->request_skeleton(request, "POST", true, body_handler,
body_generator);
return perform_request(request, "POST", body, content_type,
body_handler, body_generator);
}

/**
Expand Down Expand Up @@ -225,26 +207,8 @@ class basic_client_facade {
body_callback_function_type body_handler = body_callback_function_type(),
body_generator_function_type body_generator =
body_generator_function_type()) {
if (body != string_type()) {
request << remove_header("Content-Length")
<< header("Content-Length", std::to_string(body.size()))
<< boost::network::body(body);
}
typename headers_range<basic_request<Tag> >::type content_type_headers =
headers(request)["Content-Type"];
if (content_type != string_type()) {
if (!boost::empty(content_type_headers))
request << remove_header("Content-Type");
request << header("Content-Type", content_type);
} else {
if (boost::empty(content_type_headers)) {
typedef typename char_<Tag>::type char_type;
static char_type const content_type[] = "x-application/octet-stream";
request << header("Content-Type", content_type);
}
}
return pimpl->request_skeleton(request, "PUT", true, body_handler,
body_generator);
return perform_request(request, "PUT", body, content_type,
body_handler, body_generator);
}

/**
Expand Down Expand Up @@ -284,6 +248,117 @@ class basic_client_facade {
return put(request, body, string_type(), body_handler, body_generator);
}

/**
* Perform a PATCH request.
*
* @param[in] request A copy of the request object including the URI and
* headers.
* @param[in] body The whole contents of the body. If provided, this overrides
* the body in the `request`.
* @param[in] content_type The content type for the request. This overrides
* the content type in the `request`.
* @param[in] body_handler The callback invoked for parts of the response body
* as they come in.
* @param[in] body_generator If provided, is invoked to generate parts of the
* request's body as it is being sent.
* @returns A response object.
* @throws std::exception May throw exceptions on errors, derived from
* `std::exception`.
*/
response patch(
request request, string_type const& body = string_type(),
string_type const& content_type = string_type(),
body_callback_function_type body_handler = body_callback_function_type(),
body_generator_function_type body_generator =
body_generator_function_type()) {
return perform_request(request, "PATCH", body, content_type,
body_handler, body_generator);
}

/**
* Perform a PATCH request.
*
* @param[in] request The request including the URI and headers.
* @param[in] callback If provided, the function to call for parts of the
* response's body as they come in.
* @param[in] body_generator The function to call to generate part of the body
* while the request is being performed.
* @returns A response object.
* @throws std::exception May throw exceptions derived from std::exception in
* case of errors.
*/
response patch(request const& request, body_callback_function_type callback,
body_generator_function_type body_generator =
body_generator_function_type()) {
return patch(request, string_type(), string_type(), callback, body_generator);
}

/**
* Perform a PATCH request.
*
* @param[in] request The request object including the URI and headers.
* @param[in] body The whole contents of the body.
* @param[in] body_handler The callback invoked for parts of the response body
* as they come in.
* @param[in] body_generator If provided, is invoked to generate parts of the
* request's body as it is being sent.
* @returns A response object.
* @throws std::exception May throw exceptions on errors, derived from
* `std::exception`.
*/
response patch(request const& request, string_type body,
body_callback_function_type body_handler,
body_generator_function_type body_generator = {}) {
return patch(request, body, string_type(), body_handler, body_generator);
}

/**
* Perform a request.
*
* @param[in] request A copy of the request object including the URI and
* headers.
* @param[in] method The HTTP method
* @param[in] body The whole contents of the body. If provided, this overrides
* the body in the `request`.
* @param[in] content_type The content type for the request. This overrides
* the content type in the `request`.
* @param[in] body_handler The callback invoked for parts of the response body
* as they come in.
* @param[in] body_generator If provided, is invoked to generate parts of the
* request's body as it is being sent.
* @returns A response object.
* @throws std::exception May throw exceptions on errors, derived from
* `std::exception`.
*/
response perform_request(request request, string_type const& method,
string_type const& body = string_type(),
string_type const& content_type = string_type(),
body_callback_function_type body_handler =
body_callback_function_type(),
body_generator_function_type body_generator =
body_generator_function_type()) {
if (body != string_type()) {
request << remove_header("Content-Length")
<< header("Content-Length", std::to_string(body.size()))
<< boost::network::body(body);
}
typename headers_range<basic_request<Tag> >::type content_type_headers =
headers(request)["Content-Type"];
if (content_type != string_type()) {
if (!boost::empty(content_type_headers))
request << remove_header("Content-Type");
request << header("Content-Type", content_type);
} else {
if (boost::empty(content_type_headers)) {
typedef typename char_<Tag>::type char_type;
static char_type const content_type[] = "x-application/octet-stream";
request << header("Content-Type", content_type);
}
}
return pimpl->request_skeleton(request, method, true, body_handler,
body_generator);
}

/**
* Perform a DELETE request.
*
Expand Down
5 changes: 5 additions & 0 deletions boost/network/protocol/http/traits/impl/request_methods.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ struct request_methods<tags::http_default_8bit_tcp_resolve> {
return PUT;
};

static char const* patch() {
static char const* const PATCH = "PATCH";
return PATCH;
};

static char const* post() {
static char const* const POST = "POST";
return POST;
Expand Down