build: Move http code into its own lib

This commit is contained in:
John Colvin committed 2024-10-17 05:37:16 +00:00
1 parent 8b80c9821f
commit b8799622d5
37 files changed
+275 -252

No files matched your search

+1
View File
@@ -102,6 +102,7 @@ endif()
# subdirs
add_subdirectory(src/lib_system)
add_subdirectory(src/lib_domain)
add_subdirectory(src/lib_http)
if (BUILD_SPARKD)
add_subdirectory(src/lib_python_md)
add_subdirectory(src/app_sparkd)
@@ -2,9 +2,9 @@
#include "domain/protobufs/venue.pb.h"
#include "domain/security.h"
#include "domain/security_criteria.h"
#include "http/query_params.h"
#include "spark/api/md/BinanceMarketDataConfig.pb.h"
#include "spark/io/ProtoPrinter.h"
#include "spark/io/QueryParams.h"
#include "spark/logging/logging.h"
#include "spark/md/IMarketDataListener.h"
#include "spark/persistence/IDGenerator.h"
@@ -40,8 +40,8 @@ using bts::domain::SecurityCriteria;
using bts::domain::SecurityID;
using bts::domain::protobufs::Venue;
using bts::domain::protobufs::Venue_Name;
using bts::http::QueryParams;
using bts::io::ProtoPrinter;
using bts::io::QueryParams;
using bts::persistence::IDGenerator;
using std::pair;
using std::string;
@@ -2,9 +2,9 @@
#include "domain/protobufs/venue.pb.h"
#include "domain/security.h"
#include "domain/security_criteria.h"
#include "http/query_params.h"
#include "spark/api/md/BinanceUsMarketDataConfig.pb.h"
#include "spark/io/ProtoPrinter.h"
#include "spark/io/QueryParams.h"
#include "spark/logging/logging.h"
#include "spark/md/IMarketDataListener.h"
#include "spark/persistence/IDGenerator.h"
@@ -42,8 +42,8 @@ using bts::domain::SecurityCriteria;
using bts::domain::SecurityID;
using bts::domain::protobufs::Venue;
using bts::domain::protobufs::Venue_Name;
using bts::http::QueryParams;
using bts::io::ProtoPrinter;
using bts::io::QueryParams;
using bts::persistence::IDGenerator;
using std::pair;
using std::shared_ptr;
@@ -3,9 +3,9 @@
#include "domain/security.h"
#include "domain/security_criteria.h"
#include "domain/security_id.h"
#include "http/query_params.h"
#include "spark/api/md/BinanceUsdsFuturesMarketDataConfig.pb.h"
#include "spark/io/ProtoPrinter.h"
#include "spark/io/QueryParams.h"
#include "spark/logging/logging.h"
#include "spark/md/IMarketDataListener.h"
#include "spark/persistence/IDGenerator.h"
@@ -36,8 +36,8 @@ using bts::domain::SecurityCriteria;
using bts::domain::SecurityID;
using bts::domain::protobufs::Venue;
using bts::domain::protobufs::Venue_Name;
using bts::http::QueryParams;
using bts::io::ProtoPrinter;
using bts::io::QueryParams;
using bts::persistence::IDGenerator;
using std::shared_ptr;
using std::string;
@@ -6,8 +6,8 @@
namespace bts::md {
using bts::algorithm::StringCaseConverter;
using bts::io::http::HttpMethod;
using bts::io::http::HttpProtocol;
using bts::http::HttpMethod;
using bts::http::HttpProtocol;
using std::map;
using std::string;
@@ -1,8 +1,8 @@
#pragma once
#include "spark/io/QueryParams.h"
#include "spark/io/http/HttpMethod.h"
#include "spark/io/http/HttpProtocol.h"
#include "http/http_method.h"
#include "http/http_protocol.h"
#include "http/query_params.h"
#include <cstdint>
#include <map>
@@ -14,13 +14,13 @@ class CCApiHttpParams {
public:
std::map<std::string, std::string> to_map() const;
io::http::HttpProtocol protocol{io::http::HttpProtocol::HTTPS};
http::HttpProtocol protocol{http::HttpProtocol::HTTPS};
std::uint16_t port{0};
std::string path{"/"};
io::http::HttpMethod method{io::http::HttpMethod::GET};
http::HttpMethod method{http::HttpMethod::GET};
std::map<std::string, std::string> headers{
{"Content-Type", "application/json"}};
io::QueryParams query_params;
http::QueryParams query_params;
};
} // namespace bts::md
@@ -1,9 +1,9 @@
#include "CoinbaseSubscription.h"
#include "domain/security.h"
#include "domain/security_criteria.h"
#include "http/query_params.h"
#include "spark/api/md/CoinbaseMarketDataConfig.pb.h"
#include "spark/io/ProtoPrinter.h"
#include "spark/io/QueryParams.h"
#include "spark/md/IMarketDataListener.h"
#include "spark/md/common.h"
#include "spark/persistence/IDGenerator.h"
@@ -31,7 +31,7 @@ using bts::api::md::CoinbaseMarketDataConfig;
using bts::domain::Security;
using bts::domain::SecurityCriteria;
using bts::domain::SecurityID;
using bts::io::QueryParams;
using bts::http::QueryParams;
using bts::io::ProtoPrinter;
using bts::persistence::IDGenerator;
using std::shared_ptr;
+12
View File
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.23)
project(http)
include(${CMAKE_SOURCE_DIR}/cmake/InitLibrary.cmake)
init_library(STATIC)
target_link_libraries(
${PROJECT_NAME}
PUBLIC
system
curl
protobuf
)
+5
View File
@@ -0,0 +1,5 @@
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components(@PROJECT_NAME@)
@@ -1,4 +1,5 @@
#include "CurlHandle.h"
#include "http/curl_handle.h"
#include <curl/curl.h>
#include <stdexcept>
File renamed without changes.
@@ -1,12 +1,12 @@
#include "HTTPClient.h"
#include "HTTPRequest.h"
#include "HTTPResponse.h"
#include "spark/io/URL.h"
#include "spark/logging/logging.h"
#include "http_client.h"
#include "http/http_request.h"
#include "http/http_response.h"
#include "http/url.h"
#include <curl/curl.h>
#include <fmt/format.h>
namespace bts::io::http {
namespace bts::http {
static size_t write_data_(
void *ptr, size_t size, size_t nmemb, std::string *data) {
@@ -14,7 +14,7 @@ static size_t write_data_(
return size * nmemb;
}
HTTPResponse HTTPClient::get(const HTTPRequest &request) {
HttpResponse HttpClient::get(const HttpRequest &request) {
auto *curl = curl_easy_init();
auto url_str = request.url().to_string();
// LOG(md, bts::Info, "do GET: {}", url_str);
@@ -44,12 +44,12 @@ HTTPResponse HTTPClient::get(const HTTPRequest &request) {
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl = NULL;
HTTPResponse response;
response.headers() = HTTPHeaders(response_header_string);
HttpResponse response;
response.headers() = HttpHeaders(response_header_string);
response.body() = std::move(response_string);
response.code() = response_code;
// LOG(md, bts::Info, "do GET: {}. Received response:\n{}", url_str, response);
return response;
}
} // namespace bts::io::http
} // namespace bts::http
@@ -21,15 +21,15 @@
#include <unordered_map>
#include <vector>
namespace bts::io::http {
namespace bts::http {
class HTTPRequest;
class HTTPResponse;
class HttpRequest;
class HttpResponse;
class HTTPClient {
class HttpClient {
public:
// HTTPClient(const std::string &url);
HTTPResponse get(const HTTPRequest &request);
// HttpClient(const std::string &url);
HttpResponse get(const HttpRequest &request);
};
} // namespace bts::io::http
} // namespace bts::http
@@ -1,14 +1,16 @@
#include "HTTPHeaders.h"
#include "spark/utils/StringSplitter.h"
#include "http_headers.h"
#include "system/string_utils.h"
#include <string>
namespace bts::io::http {
namespace bts::http {
HTTPHeaders::HTTPHeaders(const std::string &str) {
auto item_strs = utils::StringSplitter::split(str, "\n");
using bts::system::string_utils::split;
HttpHeaders::HttpHeaders(const std::string &str) {
auto item_strs = split(str, "\n");
for (const auto &item_str : item_strs) {
auto kv = utils::StringSplitter::split(item_str, ":");
auto kv = split(item_str, ":");
if (kv.empty()) {
try_emplace("", "");
continue;
@@ -21,7 +23,7 @@ HTTPHeaders::HTTPHeaders(const std::string &str) {
}
}
std::string HTTPHeaders::to_string() const {
std::string HttpHeaders::to_string() const {
auto it = begin();
if (it == end()) {
return "";
@@ -34,9 +36,9 @@ std::string HTTPHeaders::to_string() const {
}
std::ostream &operator<<(
std::ostream &out, const HTTPHeaders &headers) noexcept {
std::ostream &out, const HttpHeaders &headers) noexcept {
out << headers.to_string();
return out;
}
} // namespace bts::io::http
} // namespace bts::http
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include "system/collections/flat_unordered_map.h"
#include <string>
namespace bts::http {
class HttpHeaders
: public collections::FlatUnorderedMap<std::string, std::string> {
public:
HttpHeaders() = default;
explicit HttpHeaders(const std::string &header_str);
HttpHeaders(const HttpHeaders &) noexcept = default;
HttpHeaders(HttpHeaders &&) noexcept = default;
HttpHeaders &operator=(const HttpHeaders &) noexcept = default;
HttpHeaders &operator=(HttpHeaders &&) noexcept = default;
std::string to_string() const;
};
std::ostream &operator<<(
std::ostream &out, const HttpHeaders &headers) noexcept;
} // namespace bts::http
@@ -1,5 +1,4 @@
#pragma once
#ifdef SPARK_ENABLE_CCAPI
#include "system/FMT_IMPL.h"
#include "system/enum_macros.h"
@@ -8,10 +7,10 @@
#include <nlohmann/json.hpp>
#include <ostream>
namespace bts::io::http {
namespace bts::http {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
#define FOR_ALL_VALUES(per_value) \
per_value(HttpMethod, NONE) \
per_value(HttpMethod, GET) \
per_value(HttpMethod, POST)
@@ -24,8 +23,6 @@ ENUM_CLASS_DEF(HttpMethod, FOR_ALL_VALUES)
#undef FOR_ALL_VALUES
} // namespace bts::io::http
} // namespace bts::http
FMT_IMPL(bts::io::http::HttpMethod, m, "{}", m.name());
#endif
FMT_IMPL(bts::http::HttpMethod, m, "{}", m.name());
@@ -1,9 +1,5 @@
#pragma once
#ifdef SPARK_ENABLE_CCAPI
#include "domain/protobufs/security_type.pb.h"
#include "spark/algorithm/StringCaseConverter.h"
#include "spark/domain_model/Security.pb.h"
#include "system/FMT_IMPL.h"
#include "system/enum_macros.h"
@@ -12,7 +8,7 @@
#include <ostream>
#include <string>
namespace bts::io::http {
namespace bts::http {
// clang-format off
#define FOR_ALL_VALUES(per_value) \
@@ -52,8 +48,6 @@ constexpr std::uint16_t HttpProtocol::default_port() const {
#undef FOR_ALL_VALUES
} // namespace bts::io::http
} // namespace bts::http
FMT_IMPL(bts::io::http::HttpProtocol, p, "{}", p.name());
#endif
FMT_IMPL(bts::http::HttpProtocol, p, "{}", p.name());
+17
View File
@@ -0,0 +1,17 @@
#include "http_request.h"
namespace bts::http {
Url &HttpRequest::url() { return url_; }
const Url &HttpRequest::url() const { return url_; }
HttpHeaders &HttpRequest::headers() { return headers_; }
const HttpHeaders &HttpRequest::headers() const { return headers_; }
std::string &HttpRequest::body() { return body_; }
const std::string &HttpRequest::body() const { return body_; }
} // namespace bts::http
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include "http/http_headers.h"
#include "http/url.h"
#include <string>
#include <vector>
namespace bts::http {
class HttpRequest {
public:
Url &url();
const Url &url() const;
HttpHeaders &headers();
const HttpHeaders &headers() const;
std::string &body();
const std::string &body() const;
private:
Url url_;
HttpHeaders headers_;
std::string body_;
};
} // namespace bts::http
+26
View File
@@ -0,0 +1,26 @@
#include "http_response.h"
#include <fmt/format.h>
namespace bts::http {
HttpHeaders &HttpResponse::headers() { return headers_; }
const HttpHeaders &HttpResponse::headers() const { return headers_; }
std::string &HttpResponse::body() { return body_; }
const std::string &HttpResponse::body() const { return body_; }
std::int64_t &HttpResponse::code() { return code_; }
std::int64_t HttpResponse::code() const { return code_; }
std::ostream &operator<<(
std::ostream &out, const HttpResponse &response) noexcept {
out << response.headers() << "\n"
<< response.body() << "\nCode: " << response.code();
return out;
}
} // namespace bts::http
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include "http/url.h"
#include "http_headers.h"
#include <string>
#include <vector>
namespace bts::http {
class HttpResponse {
public:
Url &url();
const Url &url() const;
HttpHeaders &headers();
const HttpHeaders &headers() const;
std::string &body();
const std::string &body() const;
std::int64_t &code();
std::int64_t code() const;
private:
Url url_;
HttpHeaders headers_;
std::string body_;
std::int64_t code_;
};
std::ostream &operator<<(
std::ostream &out, const HttpResponse &response) noexcept;
} // namespace bts::http
@@ -0,0 +1,7 @@
syntax = "proto3";
package bts.http.protobufs;
message QueryParam {
string key = 1;
string value = 2;
}
@@ -1,12 +1,9 @@
syntax = "proto3";
package bts.api.io;
package bts.http.protobufs;
message QueryParam {
string key = 1;
string value = 2;
}
import "http/protobufs/query_param.proto";
message URL {
message Url {
string protocol = 1;
string host = 2;
uint32 port = 3;
@@ -1,6 +1,6 @@
#include "QueryParams.h"
#include "http/query_params.h"
namespace bts::io {
namespace bts::http {
std::string QueryParams::to_string() const {
std::string rv;
@@ -15,4 +15,4 @@ std::string QueryParams::to_string() const {
return rv;
}
} // namespace bts::io
} // namespace bts::http
@@ -4,7 +4,7 @@
#include <string>
namespace bts::io {
namespace bts::http {
class QueryParams
: public collections::FlatUnorderedMap<std::string, std::string> {
@@ -12,4 +12,4 @@ class QueryParams
std::string to_string() const;
};
} // namespace bts::io
} // namespace bts::http
@@ -1,12 +1,12 @@
#include "URL.h"
#include "spark/api/io/URL.pb.h"
#include "url.h"
#include "http/protobufs/url.pb.h"
#include <iostream>
#include <string>
namespace bts::io {
namespace bts::http {
URL::URL(const bts::api::io::URL &url)
Url::Url(const protobufs::Url &url)
: protocol_(url.protocol()), host_(url.host()), port_(url.port()),
path_(url.path()) {
for (const auto &qp : url.query_params()) {
@@ -14,14 +14,14 @@ URL::URL(const bts::api::io::URL &url)
}
}
std::vector<std::pair<std::string, std::uint16_t>> URL::well_known_ports_() {
std::vector<std::pair<std::string, std::uint16_t>> Url::well_known_ports_() {
return std::vector<std::pair<std::string, std::uint16_t>>{
{"http", 80}, {"https", 443}, {"ws", 80},
{"wss", 443}, {"ssh", 22}, {"sftp", 22},
};
}
std::uint16_t URL::get_known_port_for_protocol(const std::string &protocol) {
std::uint16_t Url::get_known_port_for_protocol(const std::string &protocol) {
auto wkps = well_known_ports_();
for (const auto &p : wkps) {
if (p.first == protocol) {
@@ -31,7 +31,7 @@ std::uint16_t URL::get_known_port_for_protocol(const std::string &protocol) {
return 0;
}
std::string URL::get_known_protocol_for_port(std::uint16_t port) {
std::string Url::get_known_protocol_for_port(std::uint16_t port) {
auto wkps = well_known_ports_();
for (const auto &p : wkps) {
if (p.second == port) {
@@ -41,23 +41,23 @@ std::string URL::get_known_protocol_for_port(std::uint16_t port) {
return "";
}
std::string &URL::protocol() { return protocol_; }
const std::string &URL::protocol() const { return protocol_; }
std::string &Url::protocol() { return protocol_; }
const std::string &Url::protocol() const { return protocol_; }
std::string &URL::host() { return host_; }
const std::string &URL::host() const { return host_; }
std::string &Url::host() { return host_; }
const std::string &Url::host() const { return host_; }
std::uint16_t &URL::port() { return port_; }
std::uint16_t URL::port() const { return port_; }
std::uint16_t &Url::port() { return port_; }
std::uint16_t Url::port() const { return port_; }
std::string &URL::path() { return path_; }
const std::string &URL::path() const { return path_; }
std::string &Url::path() { return path_; }
const std::string &Url::path() const { return path_; }
QueryParams &URL::query_params() { return query_params_; }
QueryParams &Url::query_params() { return query_params_; }
const QueryParams &URL::query_params() const { return query_params_; }
const QueryParams &Url::query_params() const { return query_params_; }
std::string URL::to_string() const {
std::string Url::to_string() const {
auto rv = protocol() + "://";
if (host().empty()) {
rv.append("localhost");
@@ -77,36 +77,36 @@ std::string URL::to_string() const {
return rv;
}
std::ostream &operator<<(std::ostream &out, const URL &url) noexcept {
std::ostream &operator<<(std::ostream &out, const Url &url) noexcept {
out << url.to_string();
return out;
}
void URL::fill_protocol(const std::string &protocol) {
void Url::fill_protocol(const std::string &protocol) {
if (protocol_.empty()) {
protocol_ = protocol;
}
}
void URL::fill_host(const std::string &host) {
void Url::fill_host(const std::string &host) {
if (host_.empty()) {
host_ = host;
}
}
void URL::fill_port(std::uint16_t port) {
void Url::fill_port(std::uint16_t port) {
if (port_ == 0) {
port_ = port;
}
}
void URL::fill_path(const std::string &path) {
void Url::fill_path(const std::string &path) {
if (path_.empty()) {
path_ = path;
}
}
void URL::fill_query_params(
void Url::fill_query_params(
const QueryParams &query_params, bool add_missing_params) {
if (query_params_.empty()) {
query_params_ = query_params;
@@ -117,7 +117,7 @@ void URL::fill_query_params(
}
}
void URL::fill_with(const URL &url) {
void Url::fill_with(const Url &url) {
fill_path(url.path());
fill_host(url.host());
fill_protocol(url.protocol());
@@ -125,7 +125,7 @@ void URL::fill_with(const URL &url) {
fill_query_params(url.query_params());
}
bool operator==(const URL &lhs, const URL &rhs) noexcept {
bool operator==(const Url &lhs, const Url &rhs) noexcept {
bool rv = lhs.protocol() == rhs.protocol() && lhs.host() == rhs.host() &&
lhs.path() == rhs.path() && lhs.port() == rhs.port();
if (rv) {
@@ -136,8 +136,8 @@ bool operator==(const URL &lhs, const URL &rhs) noexcept {
return rv;
}
bool operator!=(const URL &lhs, const URL &rhs) noexcept {
bool operator!=(const Url &lhs, const Url &rhs) noexcept {
return !(lhs == rhs);
}
} // namespace bts::io
} // namespace bts::http
@@ -1,5 +1,6 @@
#pragma once
#include "QueryParams.h"
#include "http/query_params.h"
#include "system/hash_phaser.h"
#include <functional>
@@ -9,21 +10,21 @@
#include <variant>
#include <vector>
namespace bts::api::io {
class URL;
namespace bts::http::protobufs {
class Url;
}
namespace bts::io {
namespace bts::http {
class URL {
class Url {
public:
URL() = default;
explicit URL(const bts::api::io::URL &url);
Url() = default;
explicit Url(const protobufs::Url &url);
URL(const URL &) = default;
URL(URL &&) noexcept = default;
URL &operator=(const URL &) = default;
URL &operator=(URL &&) noexcept = default;
Url(const Url &) = default;
Url(Url &&) noexcept = default;
Url &operator=(const Url &) = default;
Url &operator=(Url &&) noexcept = default;
std::string &protocol();
const std::string &protocol() const;
@@ -48,7 +49,7 @@ class URL {
void fill_port(std::uint16_t port);
void fill_query_params(
const QueryParams &query_params, bool add_missing_params = false);
void fill_with(const URL &url);
void fill_with(const Url &url);
static std::uint16_t get_known_port_for_protocol(
const std::string &protocol);
@@ -65,18 +66,18 @@ class URL {
well_known_ports_();
};
bool operator==(const URL &lhs, const URL &rhs) noexcept;
bool operator!=(const URL &lhs, const URL &rhs) noexcept;
bool operator==(const Url &lhs, const Url &rhs) noexcept;
bool operator!=(const Url &lhs, const Url &rhs) noexcept;
std::ostream &operator<<(std::ostream &out, const URL &url) noexcept;
std::ostream &operator<<(std::ostream &out, const Url &url) noexcept;
} // namespace bts::io
} // namespace bts::http
namespace std {
template <>
struct hash<bts::io::URL> {
std::size_t operator()(const bts::io::URL &url) const;
struct hash<bts::http::Url> {
std::size_t operator()(const bts::http::Url &url) const;
};
} // namespace std
@@ -87,8 +88,8 @@ struct hash<bts::io::URL> {
namespace std {
inline std::size_t hash<bts::io::URL>::operator()(
const bts::io::URL &url) const {
inline std::size_t hash<bts::http::Url>::operator()(
const bts::http::Url &url) const {
return bts::core::HashPhaser::auto_phase(
url.protocol(), url.host(), url.port(), url.path(),
url.query_params().size());
@@ -3,7 +3,7 @@ function(get_link_libraries dest)
set(${dest}
system
domain
curl
http
tbb
boost_system
boost_thread
@@ -3,6 +3,7 @@ function(get_link_libraries dest)
set(${dest}
system
domain
http
protobuf
double-conversion
boost_system
@@ -14,7 +15,6 @@ function(get_link_libraries dest)
boost_atomic
SQLiteCpp
sqlite3
curl
uuid
ssl
crypto
-24
View File
@@ -1,24 +0,0 @@
#pragma once
#include "system/collections/flat_unordered_map.h"
namespace bts::io::http {
class HTTPHeaders
: public collections::FlatUnorderedMap<std::string, std::string> {
public:
HTTPHeaders() = default;
explicit HTTPHeaders(const std::string &header_str);
HTTPHeaders(const HTTPHeaders &) noexcept = default;
HTTPHeaders(HTTPHeaders &&) noexcept = default;
HTTPHeaders &operator=(const HTTPHeaders &) noexcept = default;
HTTPHeaders &operator=(HTTPHeaders &&) noexcept = default;
std::string to_string() const;
};
std::ostream &operator<<(
std::ostream &out, const HTTPHeaders &headers) noexcept;
} // namespace bts::io::http
@@ -1,17 +0,0 @@
#include "HTTPRequest.h"
namespace bts::io::http {
io::URL &HTTPRequest::url() { return url_; }
const io::URL &HTTPRequest::url() const { return url_; }
HTTPHeaders &HTTPRequest::headers() { return headers_; }
const HTTPHeaders &HTTPRequest::headers() const { return headers_; }
std::string &HTTPRequest::body() { return body_; }
const std::string &HTTPRequest::body() const { return body_; }
} // namespace bts::io::http
-26
View File
@@ -1,26 +0,0 @@
#pragma once
#include "HTTPHeaders.h"
#include "spark/io/URL.h"
#include <string>
#include <vector>
namespace bts::io::http {
class HTTPRequest {
public:
io::URL &url();
const io::URL &url() const;
HTTPHeaders &headers();
const HTTPHeaders &headers() const;
std::string &body();
const std::string &body() const;
private:
io::URL url_;
HTTPHeaders headers_;
std::string body_;
};
} // namespace bts::io::http
@@ -1,25 +0,0 @@
#include "HTTPResponse.h"
#include <fmt/format.h>
namespace bts::io::http {
HTTPHeaders &HTTPResponse::headers() { return headers_; }
const HTTPHeaders &HTTPResponse::headers() const { return headers_; }
std::string &HTTPResponse::body() { return body_; }
const std::string &HTTPResponse::body() const { return body_; }
std::int64_t &HTTPResponse::code() { return code_; }
std::int64_t HTTPResponse::code() const { return code_; }
std::ostream &operator<<(
std::ostream &out, const HTTPResponse &response) noexcept {
out << response.headers() << "\n"
<< response.body() << "\nCode: " << response.code();
return out;
}
} // namespace bts::io::http
@@ -1,33 +0,0 @@
#pragma once
#include "HTTPHeaders.h"
#include "spark/io/URL.h"
#include <string>
#include <vector>
namespace bts::io::http {
class HTTPResponse {
public:
io::URL &url();
const io::URL &url() const;
HTTPHeaders &headers();
const HTTPHeaders &headers() const;
std::string &body();
const std::string &body() const;
std::int64_t &code();
std::int64_t code() const;
private:
io::URL url_;
HTTPHeaders headers_;
std::string body_;
std::int64_t code_;
};
std::ostream &operator<<(
std::ostream &out, const HTTPResponse &response) noexcept;
} // namespace bts::io::http
@@ -1,10 +1,10 @@
#include "WebsocketClientConfiguration.h"
#include "spark/io/URL.h"
#include "http/url.h"
namespace bts::io::websocket {
WebsocketClientConfiguration WebsocketClientConfiguration::from_url(
const URL &url) {
const http::Url &url) {
return WebsocketClientConfiguration{
url.host(), std::to_string(url.port()), url.host(), 0, 0, 1000};
}
@@ -1,8 +1,8 @@
#pragma once
#include <string>
namespace bts::io {
class URL;
namespace bts::http {
class Url;
}
namespace bts::io::websocket {
@@ -17,7 +17,7 @@ class WebsocketClientConfiguration {
const size_t msecs_bw_connection_tries;
const size_t timer_ms;
static WebsocketClientConfiguration from_url(const URL &url);
static WebsocketClientConfiguration from_url(const http::Url &url);
};
} // namespace bts::io::websocket
@@ -42,7 +42,6 @@
#include <vector>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace websocket_core = beast::websocket; // from <boost/beast/websocket.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
@@ -112,7 +111,7 @@ class SyncWebsocketEchoServer {
ws.set_option(websocket_core::stream_base::decorator(
[](websocket_core::response_type &res) {
res.set(
http::field::server,
beast::http::field::server,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-server-sync");
}));