2017-07-20 08:01:46 -07:00
|
|
|
//
|
|
|
|
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
|
2016-04-29 06:04:40 -04:00
|
|
|
#ifndef BEAST_HTTP_DETAIL_CHUNK_ENCODE_HPP
|
|
|
|
#define BEAST_HTTP_DETAIL_CHUNK_ENCODE_HPP
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
#include <beast/core/buffer_cat.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <boost/asio/buffer.hpp>
|
2016-09-25 12:17:32 -04:00
|
|
|
#include <boost/assert.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <iterator>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace beast {
|
|
|
|
namespace http {
|
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
class chunk_encode_text
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
boost::asio::const_buffer cb_;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
// Storage for the longest hex string we might need, plus delimiters.
|
2016-05-07 17:06:46 -04:00
|
|
|
std::array<char, 2 * sizeof(std::size_t) + 2> buf_;
|
|
|
|
|
2016-11-07 16:57:41 -05:00
|
|
|
template<class = void>
|
|
|
|
void
|
|
|
|
copy(chunk_encode_text const& other);
|
|
|
|
|
|
|
|
template<class = void>
|
|
|
|
void
|
|
|
|
setup(std::size_t n);
|
|
|
|
|
2016-08-26 08:01:44 -04:00
|
|
|
template<class OutIter>
|
2016-05-07 17:06:46 -04:00
|
|
|
static
|
|
|
|
OutIter
|
|
|
|
to_hex(OutIter last, std::size_t n)
|
|
|
|
{
|
|
|
|
if(n == 0)
|
|
|
|
{
|
|
|
|
*--last = '0';
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
while(n)
|
|
|
|
{
|
|
|
|
*--last = "0123456789abcdef"[n&0xf];
|
|
|
|
n>>=4;
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
using value_type = boost::asio::const_buffer;
|
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
using const_iterator = value_type const*;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
chunk_encode_text(chunk_encode_text const& other)
|
|
|
|
{
|
2016-11-07 16:57:41 -05:00
|
|
|
copy(other);
|
2016-05-07 17:06:46 -04:00
|
|
|
}
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
explicit
|
|
|
|
chunk_encode_text(std::size_t n)
|
|
|
|
{
|
2016-11-07 16:57:41 -05:00
|
|
|
setup(n);
|
2016-05-07 17:06:46 -04:00
|
|
|
}
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
const_iterator
|
|
|
|
begin() const
|
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
return &cb_;
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator
|
|
|
|
end() const
|
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
return begin() + 1;
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
};
|
2016-11-07 16:57:41 -05:00
|
|
|
template<class>
|
|
|
|
void
|
|
|
|
chunk_encode_text::
|
|
|
|
copy(chunk_encode_text const& other)
|
|
|
|
{
|
|
|
|
auto const n =
|
|
|
|
boost::asio::buffer_size(other.cb_);
|
|
|
|
buf_ = other.buf_;
|
|
|
|
cb_ = boost::asio::const_buffer(
|
|
|
|
&buf_[buf_.size() - n], n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class>
|
|
|
|
void
|
|
|
|
chunk_encode_text::
|
|
|
|
setup(std::size_t n)
|
|
|
|
{
|
|
|
|
buf_[buf_.size() - 2] = '\r';
|
|
|
|
buf_[buf_.size() - 1] = '\n';
|
|
|
|
auto it = to_hex(buf_.end() - 2, n);
|
|
|
|
cb_ = boost::asio::const_buffer{&*it,
|
|
|
|
static_cast<std::size_t>(
|
|
|
|
std::distance(it, buf_.end()))};
|
|
|
|
}
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
/** Returns a chunk-encoded ConstBufferSequence.
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
This returns a buffer sequence representing the
|
|
|
|
first chunk of a chunked transfer coded body.
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2016-11-07 16:57:41 -05:00
|
|
|
@param fin `true` if this is the last chunk.
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
@param buffers The input buffer sequence.
|
|
|
|
|
|
|
|
@return A chunk-encoded ConstBufferSequence representing the input.
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
@see <a href=https://tools.ietf.org/html/rfc7230#section-4.1.3>rfc7230 section 4.1.3</a>
|
2017-07-20 08:01:46 -07:00
|
|
|
*/
|
|
|
|
template<class ConstBufferSequence>
|
|
|
|
#if GENERATING_DOCS
|
|
|
|
implementation_defined
|
|
|
|
#else
|
2016-05-07 17:06:46 -04:00
|
|
|
beast::detail::buffer_cat_helper<boost::asio::const_buffer,
|
|
|
|
chunk_encode_text, ConstBufferSequence, boost::asio::const_buffers_1>
|
2017-07-20 08:01:46 -07:00
|
|
|
#endif
|
2016-11-07 16:57:41 -05:00
|
|
|
chunk_encode(bool fin, ConstBufferSequence const& buffers)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
using boost::asio::buffer_size;
|
|
|
|
return buffer_cat(
|
|
|
|
chunk_encode_text{buffer_size(buffers)},
|
|
|
|
buffers,
|
2016-11-07 16:57:41 -05:00
|
|
|
fin ? boost::asio::const_buffers_1{"\r\n0\r\n\r\n", 7}
|
|
|
|
: boost::asio::const_buffers_1{"\r\n", 2});
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
2016-11-07 16:57:41 -05:00
|
|
|
/** Returns a chunked encoding final chunk.
|
|
|
|
|
|
|
|
@see <a href=https://tools.ietf.org/html/rfc7230#section-4.1.3>rfc7230 section 4.1.3</a>
|
|
|
|
*/
|
2017-07-20 08:01:46 -07:00
|
|
|
inline
|
|
|
|
#if GENERATING_DOCS
|
|
|
|
implementation_defined
|
|
|
|
#else
|
|
|
|
boost::asio::const_buffers_1
|
|
|
|
#endif
|
|
|
|
chunk_encode_final()
|
|
|
|
{
|
2016-11-07 16:57:41 -05:00
|
|
|
return boost::asio::const_buffers_1{"0\r\n\r\n", 5};
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // http
|
|
|
|
} // beast
|
|
|
|
|
|
|
|
#endif
|