Return error_code from beast::http::basic_parser:

This changes the HTTP parser interface to return an error_code instead
of a bool. This eliminates the need for the error() member function and
simplifies calling code.
This commit is contained in:
Vinnie Falco
2014-10-12 19:10:33 -07:00
parent 88cb0a1f7a
commit 3cd391daa6
3 changed files with 41 additions and 43 deletions

View File

@@ -111,16 +111,12 @@ public:
return complete_;
}
/** Returns the error if write or write_eof returned false. */
error_code
error() const noexcept;
/** Write data to the parser.
@param data A buffer containing the data to write
@param bytes The size of the buffer pointed to by data.
@return A pair with bool success, and the number of bytes consumed.
*/
std::pair <bool, std::size_t>
std::pair <error_code, std::size_t>
write (void const* data, std::size_t bytes);
/** Write a set of buffer data to the parser.
@@ -131,24 +127,8 @@ public:
@return A pair with bool success, and the number of bytes consumed.
*/
template <class ConstBufferSequence>
std::pair <bool, std::size_t>
write (ConstBufferSequence const& buffers)
{
std::pair <bool, std::size_t> result (true, 0);
for (auto const& buffer : buffers)
{
std::size_t bytes_consumed;
std::tie (result.first, bytes_consumed) =
write (boost::asio::buffer_cast <void const*> (buffer),
boost::asio::buffer_size (buffer));
if (! result.first)
break;
result.second += bytes_consumed;
if (complete())
break;
}
return result;
}
std::pair <error_code, std::size_t>
write (ConstBufferSequence const& buffers);
/** Called to indicate the end of file.
HTTP needs to know where the end of the stream is. For example,
@@ -158,7 +138,7 @@ public:
@note This is typically called when a socket read returns eof.
@return `true` if the message is complete.
*/
bool
error_code
write_eof();
protected:
@@ -253,6 +233,27 @@ private:
static int cb_message_complete (joyent::http_parser*);
};
template <class ConstBufferSequence>
auto
basic_parser::write (ConstBufferSequence const& buffers) ->
std::pair <error_code, std::size_t>
{
std::pair <error_code, std::size_t> result ({}, 0);
for (auto const& buffer : buffers)
{
std::size_t bytes_consumed;
std::tie (result.first, bytes_consumed) =
write (boost::asio::buffer_cast <void const*> (buffer),
boost::asio::buffer_size (buffer));
if (result.first)
break;
result.second += bytes_consumed;
if (complete())
break;
}
return result;
}
} // http
} // beast