From 51db4d491a027ce151c176b0d5ce3a6217f710fa Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 29 Mar 2019 15:02:05 -0700 Subject: [PATCH] basic_parser::content_length is stable (API Change): fix #1375 * The value returned from `basic_parser::content_length` no longer changes as the body of the message is received. Actions Required: * Call `basic_parser::content_length_remaining` instead of `basic_parser::content_length`. --- CHANGELOG.md | 1 + doc/qbk/release_notes.qbk | 7 +++++++ include/boost/beast/http/basic_parser.hpp | 13 +++++++++++++ include/boost/beast/http/impl/basic_parser.ipp | 13 +++++++++++++ 4 files changed, 34 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d01545a1..f17b1f78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version 241: * Tidy up a doc code snippet +* basic_parser::content_length is stable (API Change) -------------------------------------------------------------------------------- diff --git a/doc/qbk/release_notes.qbk b/doc/qbk/release_notes.qbk index e737d8be..2e2599d9 100644 --- a/doc/qbk/release_notes.qbk +++ b/doc/qbk/release_notes.qbk @@ -239,6 +239,13 @@ - `async_accept`, `async_accept_ex` - `async_handshake`, `async_handshake_ex` +* ([issue 1375]) The value returned from `basic_parser::content_length` + no longer changes as the body of the message is received. + ['Actions Required]: Call `basic_parser::content_length_remaining` instead + of `basic_parser::content_length` in order to determine the remaining + number of bytes in the body. + + [*Examples] * All example programs are updated: diff --git a/include/boost/beast/http/basic_parser.hpp b/include/boost/beast/http/basic_parser.hpp index 25748f38..650e810a 100644 --- a/include/boost/beast/http/basic_parser.hpp +++ b/include/boost/beast/http/basic_parser.hpp @@ -72,6 +72,7 @@ class basic_parser std::uint64_t body_limit_ = default_body_limit(is_request{}); // max payload body std::uint64_t len_ = 0; // size of chunk or body + std::uint64_t len0_ = 0; // content length if known std::unique_ptr buf_; // temp storage std::size_t buf_len_ = 0; // size of buf_ std::size_t skip_ = 0; // resume search here @@ -231,6 +232,18 @@ public: boost::optional content_length() const; + /** Returns the remaining content length if known + + If the message header specifies a Content-Length, + the return value will be the number of bytes remaining + in the payload body have not yet been parsed. + + @note The return value is undefined unless + @ref is_header_done would return `true`. + */ + boost::optional + content_length_remaining() const; + /** Returns `true` if the message semantics require an end of file. Depending on the contents of the header, the parser may diff --git a/include/boost/beast/http/impl/basic_parser.ipp b/include/boost/beast/http/impl/basic_parser.ipp index c7088049..99947ae1 100644 --- a/include/boost/beast/http/impl/basic_parser.ipp +++ b/include/boost/beast/http/impl/basic_parser.ipp @@ -48,6 +48,17 @@ template boost::optional basic_parser:: content_length() const +{ + BOOST_ASSERT(is_header_done()); + if(! (f_ & flagContentLength)) + return boost::none; + return len0_; +} + +template +boost::optional +basic_parser:: +content_length_remaining() const { BOOST_ASSERT(is_header_done()); if(! (f_ & flagContentLength)) @@ -461,6 +472,7 @@ finish_header(error_code& ec, std::true_type) else { len_ = 0; + len0_ = 0; state_ = state::complete; } @@ -796,6 +808,7 @@ do_field(field f, ec = {}; len_ = v; + len0_ = v; f_ |= flagContentLength; return; }