Make the HTTP response size const

This commit is contained in:
Joseph Busch
2019-10-29 10:43:49 -05:00
committed by Manoj doshi
parent e26dd7bdfe
commit 7be7343e05

View File

@@ -52,13 +52,13 @@ class HTTPClientImp
public: public:
HTTPClientImp (boost::asio::io_service& io_service, HTTPClientImp (boost::asio::io_service& io_service,
const unsigned short port, const unsigned short port,
std::size_t responseSize, std::size_t maxResponseSize,
beast::Journal& j) beast::Journal& j)
: mSocket (io_service, httpClientSSLContext->context ()) : mSocket (io_service, httpClientSSLContext->context ())
, mResolver (io_service) , mResolver (io_service)
, mHeader (maxClientHeaderBytes) , mHeader (maxClientHeaderBytes)
, mPort (port) , mPort (port)
, mResponseSize (responseSize) , maxResponseSize_ (maxResponseSize)
, mDeadline (io_service) , mDeadline (io_service)
, j_ (j) , j_ (j)
{ {
@@ -386,16 +386,29 @@ public:
if (boost::regex_match (strHeader, smMatch, reBody)) // we got some body if (boost::regex_match (strHeader, smMatch, reBody)) // we got some body
mBody = smMatch[1]; mBody = smMatch[1];
if (boost::regex_match (strHeader, smMatch, reSize)) std::size_t const responseSize = [&] {
mResponseSize = if (boost::regex_match(strHeader, smMatch, reSize))
beast::lexicalCastThrow <int> (std::string(smMatch[1])); return beast::lexicalCast <std::size_t>(
std::string(smMatch[1]), maxResponseSize_);
return maxResponseSize_;
}();
if (mResponseSize == 0) if (responseSize > maxResponseSize_)
{
JLOG (j_.trace()) << "Response field too large";
invokeComplete (
boost::system::error_code {
boost::system::errc::value_too_large,
boost::system::system_category ()});
return;
}
if (responseSize == 0)
{ {
// no body wanted or available // no body wanted or available
invokeComplete (ecResult, mStatus); invokeComplete (ecResult, mStatus);
} }
else if (mBody.size () >= mResponseSize) else if (mBody.size () >= responseSize)
{ {
// we got the whole thing // we got the whole thing
invokeComplete (ecResult, mStatus, mBody); invokeComplete (ecResult, mStatus, mBody);
@@ -403,7 +416,7 @@ public:
else else
{ {
mSocket.async_read ( mSocket.async_read (
mResponse.prepare (mResponseSize - mBody.size ()), mResponse.prepare (responseSize - mBody.size ()),
boost::asio::transfer_all (), boost::asio::transfer_all (),
std::bind (&HTTPClientImp::handleData, std::bind (&HTTPClientImp::handleData,
shared_from_this (), shared_from_this (),
@@ -495,7 +508,7 @@ private:
boost::asio::streambuf mResponse; boost::asio::streambuf mResponse;
std::string mBody; std::string mBody;
const unsigned short mPort; const unsigned short mPort;
int mResponseSize; std::size_t const maxResponseSize_;
int mStatus; int mStatus;
std::function<void (boost::asio::streambuf& sb, std::string const& strHost)> std::function<void (boost::asio::streambuf& sb, std::string const& strHost)>
mBuild; mBuild;