Skip to content

Commit 5ecb5c7

Browse files
committed
Next iteration of improvements, still a long way from a working HTTP client.
1 parent 06cd005 commit 5ecb5c7

File tree

6 files changed

+647
-599
lines changed

6 files changed

+647
-599
lines changed

http/src/http/v2/client/client.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,36 @@
88
namespace network {
99
namespace http {
1010
namespace v2 {
11-
client::client() {
11+
client::client(client_options options)
12+
: options_(options) {
1213

1314
}
1415

15-
std::future<response> client::get(const request &req) {
16-
return std::future<response>();
16+
std::future<response> client::get(request req, request_options options) {
17+
return do_request("GET", req, options);
1718
}
1819

19-
std::future<response> client::post(const request &req) {
20-
return std::future<response>();
20+
std::future<response> client::post(request req, request_options options) {
21+
return do_request("POST", req, options);
2122
}
2223

23-
std::future<response> client::put(const request &req) {
24-
return std::future<response>();
24+
std::future<response> client::put(request req, request_options options) {
25+
return do_request("PUT", req, options);
2526
}
2627

27-
std::future<response> client::delete_(const request &req) {
28-
return std::future<response>();
28+
std::future<response> client::delete_(request req, request_options options) {
29+
return do_request("DELETE", req, options);
30+
}
31+
32+
std::future<response> client::head(request req, request_options options) {
33+
return do_request("HEAD", req, options);
34+
}
35+
36+
std::future<response> client::options(request req, request_options options) {
37+
return do_request("OPTIONS", req, options);
2938
}
3039

31-
std::future<response> client::head(const request &req) {
40+
std::future<response> client::do_request(string_type method, request req, request_options options) {
3241
return std::future<response>();
3342
}
3443
} // namespace v2

http/src/http/v2/constants.cpp

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,50 @@
66
#include <network/http/v2/constants.hpp>
77
#include <unordered_map>
88

9-
static std::unordered_map<int, std::string> status_message_map() {
10-
std::unordered_map<int, std::string> status_messages;
11-
status_messages[100] = "Continue";
12-
status_messages[101] = "Switching Protocols";
13-
status_messages[200] = "OK";
14-
status_messages[201] = "Created";
15-
status_messages[202] = "Accepted";
16-
status_messages[203] = "Non-Authoritative Information";
17-
status_messages[204] = "No Content";
18-
status_messages[205] = "Reset Content";
19-
status_messages[206] = "Partial Content";
20-
status_messages[300] = "Multiple Choices";
21-
status_messages[301] = "Moved Permanently";
22-
status_messages[302] = "Found";
23-
status_messages[303] = "See Other";
24-
status_messages[304] = "Not Modified";
25-
status_messages[305] = "Use Proxy";
26-
status_messages[307] = "Temporary Redirect";
27-
status_messages[400] = "Bad Request";
28-
status_messages[401] = "Unauthorized";
29-
status_messages[402] = "Payment Required";
30-
status_messages[403] = "Forbidden";
31-
status_messages[404] = "Not Found";
32-
status_messages[405] = "Method Not Allowed";
33-
status_messages[406] = "Not Acceptable";
34-
status_messages[407] = "Proxy Authentication Required";
35-
status_messages[408] = "Request Timeout";
36-
status_messages[409] = "Conflict";
37-
status_messages[410] = "Gone";
38-
status_messages[411] = "Length Required";
39-
status_messages[412] = "Precondition Failed";
40-
status_messages[413] = "Request Entity Too Large";
41-
status_messages[414] = "Request Uri Too Long";
42-
status_messages[415] = "Unsupported Media Type";
43-
status_messages[416] = "Request Range Not Satisfiable";
44-
status_messages[417] = "Expectation Failed";
45-
status_messages[500] = "Internal Error";
46-
status_messages[501] = "Not Implemented";
47-
status_messages[502] = "Bad Gateway";
48-
status_messages[503] = "Service Unavailable";
49-
status_messages[504] = "Gateway Timeout";
50-
status_messages[505] = "HTTP Version Not Supported";
51-
}
52-
9+
std::string message(int status_code) {
10+
static std::unordered_map<int, std::string> status_messages{
11+
std::make_pair(100, "Continue"),
12+
std::make_pair(101, "Switching Protocols"),
13+
std::make_pair(200, "OK"),
14+
std::make_pair(201, "Created"),
15+
std::make_pair(202, "Accepted"),
16+
std::make_pair(203, "Non-Authoritative Information"),
17+
std::make_pair(204, "No Content"),
18+
std::make_pair(205, "Reset Content"),
19+
std::make_pair(206, "Partial Content"),
20+
std::make_pair(300, "Multiple Choices"),
21+
std::make_pair(301, "Moved Permanently"),
22+
std::make_pair(302, "Found"),
23+
std::make_pair(303, "See Other"),
24+
std::make_pair(304, "Not Modified"),
25+
std::make_pair(305, "Use Proxy"),
26+
std::make_pair(307, "Temporary Redirect"),
27+
std::make_pair(400, "Bad Request"),
28+
std::make_pair(401, "Unauthorized"),
29+
std::make_pair(402, "Payment Required"),
30+
std::make_pair(403, "Forbidden"),
31+
std::make_pair(404, "Not Found"),
32+
std::make_pair(405, "Method Not Allowed"),
33+
std::make_pair(406, "Not Acceptable"),
34+
std::make_pair(407, "Proxy Authentication Required"),
35+
std::make_pair(408, "Request Timeout"),
36+
std::make_pair(409, "Conflict"),
37+
std::make_pair(410, "Gone"),
38+
std::make_pair(411, "Length Required"),
39+
std::make_pair(412, "Precondition Failed"),
40+
std::make_pair(413, "Request Entity Too Large"),
41+
std::make_pair(414, "Request Uri Too Long"),
42+
std::make_pair(415, "Unsupported Media Type"),
43+
std::make_pair(416, "Request Range Not Satisfiable"),
44+
std::make_pair(417, "Expectation Failed"),
45+
std::make_pair(500, "Internal Error"),
46+
std::make_pair(501, "Not Implemented"),
47+
std::make_pair(502, "Bad Gateway"),
48+
std::make_pair(503, "Service Unavailable"),
49+
std::make_pair(504, "Gateway Timeout"),
50+
std::make_pair(505, "HTTP Version Not Supported"),
51+
};
5352

54-
std::string status_message(int status_code) {
55-
static const auto status_messages = status_message_map();
5653
auto it = status_messages.find(status_code);
5754
if (it != status_messages.end()) {
5855
return it->second;

http/src/network/http/v2/client/client.hpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
#ifndef __NETWORK_HTTP_V2_CLIENT_CLIENT_INC__
77
#define __NETWORK_HTTP_V2_CLIENT_CLIENT_INC__
88

9+
#include <network/http/v2/client/client_options.hpp>
910
#include <network/http/v2/client/request.hpp>
11+
#include <network/http/v2/client/request_options.hpp>
1012
#include <network/http/v2/client/response.hpp>
1113
#include <future>
1214

@@ -17,19 +19,31 @@ namespace network {
1719

1820
public:
1921

20-
client();
22+
typedef request::string_type string_type;
23+
24+
enum class method { GET, PUT, POST, DELETE, HEAD, OPTIONS, };
25+
26+
explicit client(client_options options = client_options());
2127
client(client const &) = delete;
2228
client(client &&) = delete;
2329

24-
std::future<response> get(const request &req);
30+
std::future<response> get(request req, request_options options = request_options());
31+
32+
std::future<response> post(request req, request_options options = request_options());
33+
34+
std::future<response> put(request req, request_options options = request_options());
35+
36+
std::future<response> delete_(request req, request_options options = request_options());
37+
38+
std::future<response> head(request req, request_options options = request_options());
2539

26-
std::future<response> post(const request &req);
40+
std::future<response> options(request req, request_options options = request_options());
2741

28-
std::future<response> put(const request &req);
42+
private:
2943

30-
std::future<response> delete_(const request &req);
44+
std::future<response> do_request(string_type method, request req, request_options options);
3145

32-
std::future<response> head(const request &req);
46+
client_options options_;
3347

3448
};
3549
} // namespace v2

http/src/network/http/v2/client/request.hpp

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cstdint>
1010
#include <memory>
1111
#include <string>
12+
#include <network/http/v2/constants.hpp>
1213
#include <network/http/v2/message_base.hpp>
1314
#include <network/uri.hpp>
1415

@@ -46,46 +47,69 @@ namespace network {
4647

4748
class request {
4849

49-
typedef byte_source::string_type string_type;
50-
5150
public:
5251

53-
request(uri locator, std::shared_ptr<byte_source> source = nullptr)
54-
: locator_(locator), source_(source) { }
52+
typedef byte_source::string_type string_type;
53+
54+
request(uri destination, std::shared_ptr<byte_source> source = nullptr)
55+
: destination_(destination), byte_source_(source) { }
5556

5657
request(const request &other)
57-
: locator_(other.locator_), source_(other.source_) { }
58+
: destination_(other.destination_), byte_source_(other.byte_source_) { }
5859

5960
request(request &&other) noexcept
60-
: locator_(std::move(other.locator_)), source_(std::move(other.source_)) { }
61+
: destination_(std::move(other.destination_))
62+
, byte_source_(std::move(other.byte_source_)) { }
6163

6264
request &operator = (request other) {
6365
other.swap(*this);
6466
return *this;
6567
}
6668

6769
void swap(request &other) noexcept {
68-
locator_.swap(other.locator_);
69-
source_.swap(other.source_);
70+
std::swap(destination_, other.destination_);
71+
std::swap(byte_source_, other.byte_source_);
72+
std::swap(headers_, other.headers_);
73+
std::swap(method_, other.method_);
74+
std::swap(version_, other.version_);
75+
}
76+
77+
void set_body(std::shared_ptr<byte_source> byte_source) {
78+
byte_source_ = byte_source;
79+
}
80+
81+
void add_header(string_type key, string_type value) {
82+
headers_.emplace(key, value);
83+
}
84+
85+
void remove_header(string_type key) {
86+
headers_.erase(key);
87+
}
88+
89+
void clear_headers() {
90+
headers_.clear();
91+
}
92+
93+
void set_method(string_type method) {
94+
method_ = std::move(method);
95+
}
96+
97+
string_type method() const {
98+
return method_;
99+
}
100+
101+
void set_version(string_type version) {
102+
version_ = std::move(version);
103+
}
104+
105+
string_type version() const {
106+
return version_;
70107
}
71108

72-
void set_destination(string_type destination);
73-
// source
74-
// add_header
75-
// remove_header
76-
// clear_headers
77-
// set_body
78-
// append_body
79-
// get_body
80-
81-
// method
82-
// status
83-
// status_message
84-
// uri
85-
// version
86-
87-
uri locator_;
88-
std::shared_ptr<byte_source> source_;
109+
uri destination_;
110+
std::shared_ptr<byte_source> byte_source_;
111+
std::map<string_type, string_type> headers_;
112+
string_type method_, version_;
89113

90114
};
91115
} // namespace v2

0 commit comments

Comments
 (0)