Skip to content

Commit 4389238

Browse files
committed
Fixes cpp-netlib#41 : Use Boost.Parameter in HTTP Client API member functions.
1 parent ead53cf commit 4389238

File tree

2 files changed

+70
-48
lines changed

2 files changed

+70
-48
lines changed

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

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,62 +41,80 @@ namespace boost { namespace network { namespace http {
4141
>::type());
4242
}
4343

44-
response const head (request const & request_) {
45-
return pimpl->request_skeleton(request_, "HEAD", false);
44+
BOOST_PARAMETER_MEMBER_FUNCTION((response const), head, tag, (required (request,(request const &)))) {
45+
return pimpl->request_skeleton(request, "HEAD", false);
4646
}
4747

48-
response const get (request const & request_) {
49-
return pimpl->request_skeleton(request_, "GET", true);
48+
BOOST_PARAMETER_MEMBER_FUNCTION((response const), get , tag, (required (request,(request const &)))) {
49+
return pimpl->request_skeleton(request, "GET", true);
5050
}
5151

52-
response const post (request const & request_) {
53-
return pimpl->request_skeleton(request_, "POST", true);
52+
BOOST_PARAMETER_MEMBER_FUNCTION((response const), post, tag,
53+
(required
54+
(request,(request)) // yes sir, we make a copy of the original request.
55+
)
56+
(optional
57+
(body,(string_type const &),string_type())
58+
(content_type,(string_type const &),string_type())
59+
)
60+
) {
61+
if (body != string_type()) {
62+
request << remove_header("Content-Length")
63+
<< header("Content-Length", boost::lexical_cast<string_type>(body.size()))
64+
<< boost::network::body(body);
65+
}
66+
typename headers_range<basic_request<Tag> >::type content_type_headers =
67+
headers(request)["Content-Type"];
68+
if (content_type != string_type()) {
69+
if (!boost::empty(content_type_headers))
70+
request << remove_header("Content-Type");
71+
request << header("Content-Type", content_type);
72+
} else {
73+
if (boost::empty(content_type_headers)) {
74+
typedef typename char_<Tag>::type char_type;
75+
static char_type content_type[] = "x-application/octet-stream";
76+
request << header("Content-Type", content_type);
77+
}
78+
}
79+
return pimpl->request_skeleton(request, "POST", true);
5480
}
5581

56-
response const post (request request_, string_type const & content_type, string_type const & body_) {
57-
if (!boost::empty(headers(request_)["Content-Type"]))
58-
request_ << remove_header("Content-Type");
59-
60-
request_ << ::boost::network::body(body_)
61-
<< header("Content-Type", content_type)
62-
<< header("Content-Length", boost::lexical_cast<string_type>(body_.size()));
63-
return post(request_);
64-
}
65-
66-
response const post (request const & request_, string_type const & body_) {
67-
string_type content_type = "x-application/octet-stream";
68-
typename headers_range<request>::type content_type_headers =
69-
headers(request_)["Content-Type"];
70-
if (!boost::empty(content_type_headers))
71-
content_type = boost::begin(content_type_headers)->second;
72-
return post(request_, content_type, body_);
73-
}
74-
75-
response const put (request const & request_) {
76-
return pimpl->request_skeleton(request_, "PUT", true);
77-
}
78-
79-
response const put (request const & request_, string_type const & body_) {
80-
string_type content_type = "x-application/octet-stream";
81-
typename headers_range<request>::type content_type_headers =
82-
headers(request_)["Content-Type"];
83-
if (!boost::empty(content_type_headers))
84-
content_type = boost::begin(content_type_headers)->second;
85-
return put(request_, content_type, body_);
86-
}
87-
88-
response const put (request request_, string_type const & content_type, string_type const & body_) {
89-
if (!boost::empty(headers(request_)["Content-Type"]))
90-
request_ << remove_header("Content-Type");
91-
92-
request_ << ::boost::network::body(body_)
93-
<< header("Content-Type", content_type)
94-
<< header("Content-Length", boost::lexical_cast<string_type>(body_.size()));
95-
return put(request_);
82+
BOOST_PARAMETER_MEMBER_FUNCTION((response const), put , tag,
83+
(required
84+
(request,(request)) // yes sir, we make a copy of the original request.
85+
)
86+
(optional
87+
(body,(string_type const &),string_type())
88+
(content_type,(string_type const &),string_type())
89+
)
90+
) {
91+
if (body != string_type()) {
92+
request << remove_header("Content-Length")
93+
<< header("Content-Length", boost::lexical_cast<string_type>(body.size()))
94+
<< boost::network::body(body);
95+
}
96+
typename headers_range<basic_request<Tag> >::type content_type_headers =
97+
headers(request)["Content-Type"];
98+
if (content_type != string_type()) {
99+
if (!boost::empty(content_type_headers))
100+
request << remove_header("Content-Type");
101+
request << header("Content-Type", content_type);
102+
} else {
103+
if (boost::empty(content_type_headers)) {
104+
typedef typename char_<Tag>::type char_type;
105+
static char_type content_type[] = "x-application/octet-stream";
106+
request << header("Content-Type", content_type);
107+
}
108+
}
109+
return pimpl->request_skeleton(request, "PUT", true);
96110
}
97111

98-
response const delete_ (request const & request_) {
99-
return pimpl->request_skeleton(request_, "DELETE", true);
112+
BOOST_PARAMETER_MEMBER_FUNCTION((response const), delete_, tag,
113+
(required
114+
(request,(request const &))
115+
)
116+
) {
117+
return pimpl->request_skeleton(request, "DELETE", true);
100118
}
101119

102120
void clear_resolved_cache() {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace boost { namespace network { namespace http {
1515
BOOST_PARAMETER_NAME(openssl_certificate)
1616
BOOST_PARAMETER_NAME(openssl_verify_path)
1717

18+
BOOST_PARAMETER_NAME(request)
19+
BOOST_PARAMETER_NAME(body)
20+
BOOST_PARAMETER_NAME(content_type)
21+
1822
} /* http */
1923

2024
} /* network */

0 commit comments

Comments
 (0)