Skip to content

Commit 49f954f

Browse files
committed
Added OPTIONS, HEAD and PATCH request types.
Closes #38
1 parent 3b7be1a commit 49f954f

8 files changed

Lines changed: 111 additions & 13 deletions

File tree

include/restc-cpp/RequestBuilder.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,33 @@ class RequestBuilder
7575
return *this;
7676
}
7777

78+
/*! Make a HTTP OPTIONS request */
79+
RequestBuilder& Options(std::string url) {
80+
assert(url_.empty());
81+
url_ = std::move(url);
82+
MAP_URL_FOR_TESTING(url_);
83+
type_ = Request::Type::OPTIONS;
84+
return *this;
85+
}
86+
87+
/*! Make a HTTP HEAD request */
88+
RequestBuilder& Head(std::string url) {
89+
assert(url_.empty());
90+
url_ = std::move(url);
91+
MAP_URL_FOR_TESTING(url_);
92+
type_ = Request::Type::HEAD;
93+
return *this;
94+
}
95+
96+
/*! Make a HTTP PATCH request */
97+
RequestBuilder& Patch(std::string url) {
98+
assert(url_.empty());
99+
url_ = std::move(url);
100+
MAP_URL_FOR_TESTING(url_);
101+
type_ = Request::Type::PATCH;
102+
return *this;
103+
}
104+
78105
/*! Set a header
79106
*
80107
* \param name Name of the header

include/restc-cpp/restc-cpp.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ class Request {
128128
GET,
129129
POST,
130130
PUT,
131-
DELETE
131+
DELETE,
132+
OPTIONS,
133+
HEAD,
134+
PATCH
132135
};
133136

134137
class Properties {
@@ -271,6 +274,15 @@ class Context {
271274
/*! Send a DELETE request asynchronously to the server. */
272275
virtual std::unique_ptr<Reply> Delete(std::string url) = 0;
273276

277+
/*! Send a OPTIONS request asynchronously to the server. */
278+
virtual std::unique_ptr<Reply> Options(std::string url) = 0;
279+
280+
/*! Send a OPTIONS request asynchronously to the server. */
281+
virtual std::unique_ptr<Reply> Head(std::string url) = 0;
282+
283+
/*! Send a OPTIONS request asynchronously to the server. */
284+
virtual std::unique_ptr<Reply> Patch(std::string url) = 0;
285+
274286
/*! Send a request asynchronously to the server.
275287
*
276288
* At the time the method returns, the request is sent, and the

src/ReplyImpl.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,27 @@ std::deque<std::string> ReplyImpl::GetHeaders(const std::string& name) {
4242
ReplyImpl::ReplyImpl(Connection::ptr_t connection,
4343
Context& ctx,
4444
RestClient& owner,
45-
Request::Properties::ptr_t& properties)
45+
Request::Properties::ptr_t& properties,
46+
Request::Type type)
4647
: connection_{move(connection)}, ctx_{ctx}
4748
, properties_{properties}
4849
, owner_{owner}
4950
, connection_id_(connection_ ? connection_->GetId()
5051
: boost::uuids::random_generator()())
52+
, request_type_{type}
5153
{
5254
}
5355

5456
ReplyImpl::ReplyImpl(Connection::ptr_t connection,
5557
Context& ctx,
56-
RestClient& owner)
58+
RestClient& owner,
59+
Request::Type type)
5760
: connection_{move(connection)}, ctx_{ctx}
5861
, properties_{owner.GetConnectionProperties()}
5962
, owner_{owner}
6063
, connection_id_(connection_ ? connection_->GetId()
6164
: boost::uuids::random_generator()())
65+
, request_type_{type}
6266
{
6367
}
6468

@@ -107,7 +111,9 @@ void ReplyImpl::HandleContentType(unique_ptr<DataReaderStream>&& stream) {
107111
static const std::string transfer_encoding_name{"Transfer-Encoding"};
108112
static const std::string chunked_name{"chunked"};
109113

110-
if (const auto cl = GetHeader(content_len_name)) {
114+
if (request_type_ == Request::Type::HEAD) {
115+
reader_ = DataReader::CreateNoBodyReader();
116+
} else if (const auto cl = GetHeader(content_len_name)) {
111117
content_length_ = stoi(*cl);
112118
reader_ = DataReader::CreatePlainReader(*content_length_, move(stream));
113119
} else {
@@ -225,9 +231,10 @@ std::unique_ptr<ReplyImpl>
225231
ReplyImpl::Create(Connection::ptr_t connection,
226232
Context& ctx,
227233
RestClient& owner,
228-
Request::Properties::ptr_t& properties) {
234+
Request::Properties::ptr_t& properties,
235+
Request::Type type) {
229236

230-
return make_unique<ReplyImpl>(move(connection), ctx, owner, properties);
237+
return make_unique<ReplyImpl>(move(connection), ctx, owner, properties, type);
231238
}
232239

233240
} // restc_cpp

src/ReplyImpl.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ class ReplyImpl : public Reply {
2424
{ NOT_CHUNKED, GET_SIZE, IN_SEGMENT, IN_TRAILER, DONE };
2525

2626
ReplyImpl(Connection::ptr_t connection, Context& ctx,
27-
RestClient& owner, Request::Properties::ptr_t& properties);
27+
RestClient& owner, Request::Properties::ptr_t& properties,
28+
Request::Type type);
2829

2930
ReplyImpl(Connection::ptr_t connection, Context& ctx,
30-
RestClient& owner);
31+
RestClient& owner, Request::Type type);
3132

3233
~ReplyImpl();
3334

@@ -61,7 +62,8 @@ class ReplyImpl : public Reply {
6162
Create(Connection::ptr_t connection,
6263
Context& ctx,
6364
RestClient& owner,
64-
Request::Properties::ptr_t& properties);
65+
Request::Properties::ptr_t& properties,
66+
Request::Type type);
6567

6668
static boost::string_ref b2sr(boost::asio::const_buffers_1 buffer) {
6769
return { boost::asio::buffer_cast<const char*>(buffer),
@@ -90,6 +92,7 @@ class ReplyImpl : public Reply {
9092
boost::optional<size_t> content_length_;
9193
const boost::uuids::uuid connection_id_;
9294
std::unique_ptr<DataReader> reader_;
95+
const Request::Type request_type_;
9396
};
9497

9598

src/RequestImpl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <fstream>
33
#include <thread>
44
#include <future>
5+
#include <array>
56

67
#include <boost/utility/string_ref.hpp>
78

@@ -106,8 +107,10 @@ class RequestImpl : public Request {
106107
}
107108

108109
const std::string& Verb(const Type requestType) {
109-
static const std::vector<std::string> names =
110-
{ "GET", "POST", "PUT", "DELETE" };
110+
static const std::array<std::string, 7> names =
111+
{{ "GET", "POST", "PUT", "DELETE", "OPTIONS",
112+
"HEAD", "PATCH"
113+
}};
111114

112115
return names[static_cast<int>(requestType)];
113116
}
@@ -424,7 +427,8 @@ class RequestImpl : public Request {
424427

425428
DataReader::ReadConfig cfg;
426429
cfg.msReadTimeout = properties_->recvTimeout;
427-
auto reply = ReplyImpl::Create(connection_, ctx, owner_, properties_);
430+
auto reply = ReplyImpl::Create(connection_, ctx, owner_, properties_,
431+
request_type_);
428432
reply->StartReceiveFromServer(
429433
DataReader::CreateIoReader(connection_, ctx, cfg));
430434

src/RestClientImpl.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ class RestClientImpl : public RestClient {
7575
return Request(*req);
7676
}
7777

78+
unique_ptr< Reply > Options(string url) override {
79+
auto req = Request::Create(url, restc_cpp::Request::Type::OPTIONS, rc_);
80+
return Request(*req);
81+
}
82+
83+
unique_ptr< Reply > Head(string url) override {
84+
auto req = Request::Create(url, restc_cpp::Request::Type::HEAD, rc_);
85+
return Request(*req);
86+
}
87+
88+
unique_ptr< Reply > Patch(string url) override {
89+
auto req = Request::Create(url, restc_cpp::Request::Type::PATCH, rc_);
90+
return Request(*req);
91+
}
92+
7893
unique_ptr<Reply> Request(restc_cpp::Request& req) override {
7994
return req.Execute(*this);
8095
}

tests/functional/CRUD_test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,36 @@ STARTCASE(TestCRUD) {
9494

9595
} ENDCASE
9696

97+
STARTCASE(TestOptions) {
98+
99+
auto rest_client = RestClient::Create();
100+
rest_client->ProcessWithPromise([&](Context& ctx) {
101+
102+
auto reply = RequestBuilder(ctx)
103+
.Options(GetDockerUrl(http_url)) // URL
104+
.Execute(); // Do it!
105+
106+
CHECK_EQUAL(204, reply->GetResponseCode());
107+
108+
}).get();
109+
110+
} ENDCASE
111+
112+
STARTCASE(TestHEAD) {
113+
114+
auto rest_client = RestClient::Create();
115+
rest_client->ProcessWithPromise([&](Context& ctx) {
116+
117+
auto reply = RequestBuilder(ctx)
118+
.Head(GetDockerUrl(http_url)) // URL
119+
.Execute(); // Do it!
120+
121+
CHECK_EQUAL(200, reply->GetResponseCode());
122+
123+
}).get();
124+
125+
} ENDCASE
126+
97127
}; //lest
98128

99129
int main( int argc, char * argv[] )

tests/unit/HttpReplyTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class TestReply : public ReplyImpl
5151
{
5252
public:
5353
TestReply(Context& ctx, RestClient& owner, test_buffers_t& buffers)
54-
: ReplyImpl(nullptr, ctx, owner), buffers_{buffers}
54+
: ReplyImpl(nullptr, ctx, owner, Request::Type::GET), buffers_{buffers}
5555
{
5656
}
5757

0 commit comments

Comments
 (0)