Skip to content

Commit

Permalink
http/client: allow setting body size limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tectu committed Jul 8, 2022
1 parent 0d6843d commit 1df756b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
12 changes: 10 additions & 2 deletions lib/malloy/client/controller.hpp
Expand Up @@ -76,6 +76,11 @@ namespace malloy::client
* @details Set as the User-Agent in http headers
*/
std::string user_agent{"malloy"};

/**
* The maximum allowed response body size in bytes.
*/
std::uint64_t body_limit = 100'000'000;
};

controller(config cfg);
Expand Down Expand Up @@ -235,7 +240,8 @@ namespace malloy::client
auto conn = std::make_shared<http::connection_tls<Body, Filter, std::decay_t<Callback>>>(
m_cfg.logger->clone(m_cfg.logger->name() + " | HTTPS connection"),
*m_ioc,
*m_tls_ctx
*m_tls_ctx,
m_cfg.body_limit
);

// Set SNI hostname (many hosts need this to handshake successfully)
Expand All @@ -256,7 +262,9 @@ namespace malloy::client
#endif
cb(std::make_shared<http::connection_plain<Body, Filter, std::decay_t<Callback>>>(
m_cfg.logger->clone(m_cfg.logger->name() + " | HTTP connection"),
*m_ioc));
*m_ioc,
m_cfg.body_limit)
);
}([this, prom = std::move(prom), req = std::move(req), filter = std::forward<Filter>(filter), cb = std::forward<Callback>(cb)](auto&& conn) mutable {
if (!malloy::http::has_field(req, malloy::http::field::user_agent)) {
req.set(malloy::http::field::user_agent, m_cfg.user_agent);
Expand Down
5 changes: 4 additions & 1 deletion lib/malloy/client/http/connection.hpp
Expand Up @@ -28,13 +28,16 @@ namespace malloy::client::http
using resp_t = typename Filter::response_type;
using callback_t = Callback;

connection(std::shared_ptr<spdlog::logger> logger, boost::asio::io_context& io_ctx) :
connection(std::shared_ptr<spdlog::logger> logger, boost::asio::io_context& io_ctx, const std::uint64_t body_limit) :
m_logger(std::move(logger)),
m_resolver(boost::asio::make_strand(io_ctx))
{
// Sanity check
if (!m_logger)
throw std::invalid_argument("no valid logger provided.");

// Set body limit
m_parser.body_limit(body_limit);
}

// Start the asynchronous operation
Expand Down
4 changes: 2 additions & 2 deletions lib/malloy/client/http/connection_plain.hpp
Expand Up @@ -16,8 +16,8 @@ namespace malloy::client::http
using parent_t = connection<connection_plain<ConnArgs...>, ConnArgs...>;

public:
connection_plain(std::shared_ptr<spdlog::logger> logger, boost::asio::io_context& io_ctx) :
parent_t(std::move(logger), io_ctx),
connection_plain(std::shared_ptr<spdlog::logger> logger, boost::asio::io_context& io_ctx, const std::uint64_t body_limit) :
parent_t(std::move(logger), io_ctx, body_limit),
m_stream(boost::asio::make_strand(io_ctx))
{
}
Expand Down
5 changes: 3 additions & 2 deletions lib/malloy/client/http/connection_tls.hpp
Expand Up @@ -21,9 +21,10 @@ namespace malloy::client::http
connection_tls(
std::shared_ptr<spdlog::logger> logger,
boost::asio::io_context& io_ctx,
boost::asio::ssl::context& tls_ctx
boost::asio::ssl::context& tls_ctx,
const std::uint64_t body_limit
) :
parent_t(std::move(logger), io_ctx),
parent_t(std::move(logger), io_ctx, body_limit),
m_stream(boost::asio::make_strand(io_ctx), tls_ctx)
{
}
Expand Down

0 comments on commit 1df756b

Please sign in to comment.