add boost on mac

This commit is contained in:
Bassem Girgis
2019-08-10 16:38:17 -05:00
parent 861b918727
commit be945cb63b
14105 changed files with 2714968 additions and 0 deletions

View File

@@ -0,0 +1,319 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_HTTP_ICY_STREAM_HPP
#define BOOST_BEAST_HTTP_ICY_STREAM_HPP
#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/core/error.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/logic/tribool.hpp>
#include <type_traits>
namespace boost {
namespace beast {
namespace http {
/** Stream wrapper to process Shoutcast HTTP responses
This wrapper replaces the word "ICY" in the first
HTTP response received on the connection, with "HTTP/1.1".
This allows the Beast parser to be used with Shoutcast
servers, which send a non-standard HTTP message as the
response.
For asynchronous operations, the application must ensure
that they are are all performed within the same implicit
or explicit strand.
@par Thread Safety
@e Distinct @e objects: Safe.@n
@e Shared @e objects: Unsafe.
The application must also ensure that all asynchronous
operations are performed within the same implicit or explicit strand.
@par Example
To use the @ref icy_stream template with an @ref tcp_stream
you would write:
@code
http::icy_stream<tcp_stream> is(ioc);
@endcode
@tparam NextLayer The type representing the next layer, to which
data will be read and written during operations. For synchronous
operations, the type must support the <em>SyncStream</em> concept.
For asynchronous operations, the type must support the
<em>AsyncStream</em> concept.
@note A stream object must not be moved or destroyed while there
are pending asynchronous operations associated with it.
@par Concepts
<em>AsyncStream</em>, <em>SyncStream</em>
*/
template<class NextLayer>
class icy_stream
{
NextLayer stream_;
char buf_[8];
unsigned char n_ = 0;
bool detect_ = true;
struct ops;
static
net::const_buffer
version()
{
return {"HTTP/1.1", 8};
}
public:
/// The type of the next layer.
using next_layer_type =
typename std::remove_reference<NextLayer>::type;
/// The type of the executor associated with the object.
using executor_type = typename next_layer_type::executor_type;
icy_stream(icy_stream&&) = default;
icy_stream(icy_stream const&) = default;
icy_stream& operator=(icy_stream&&) = default;
icy_stream& operator=(icy_stream const&) = default;
/** Destructor
The treatment of pending operations will be the same as that
of the next layer.
*/
~icy_stream() = default;
/** Constructor
Arguments, if any, are forwarded to the next layer's constructor.
*/
template<class... Args>
explicit
icy_stream(Args&&... args);
//--------------------------------------------------------------------------
/** Get the executor associated with the object.
This function may be used to obtain the executor object that the
stream uses to dispatch handlers for asynchronous operations.
@return A copy of the executor that stream will use to dispatch handlers.
*/
executor_type
get_executor() noexcept
{
return stream_.get_executor();
}
/** Get a reference to the next layer
This function returns a reference to the next layer
in a stack of stream layers.
@return A reference to the next layer in the stack of
stream layers.
*/
next_layer_type&
next_layer()
{
return stream_;
}
/** Get a reference to the next layer
This function returns a reference to the next layer in a
stack of stream layers.
@return A reference to the next layer in the stack of
stream layers.
*/
next_layer_type const&
next_layer() const
{
return stream_;
}
//--------------------------------------------------------------------------
/** Read some data from the stream.
This function is used to read data from the stream. The function call will
block until one or more bytes of data has been read successfully, or until
an error occurs.
@param buffers The buffers into which the data will be read.
@returns The number of bytes read.
@throws system_error Thrown on failure.
@note The `read_some` operation may not read all of the requested number of
bytes. Consider using the function `net::read` if you need to ensure
that the requested amount of data is read before the blocking operation
completes.
*/
template<class MutableBufferSequence>
std::size_t
read_some(MutableBufferSequence const& buffers);
/** Read some data from the stream.
This function is used to read data from the stream. The function call will
block until one or more bytes of data has been read successfully, or until
an error occurs.
@param buffers The buffers into which the data will be read.
@param ec Set to indicate what error occurred, if any.
@returns The number of bytes read.
@note The `read_some` operation may not read all of the requested number of
bytes. Consider using the function `net::read` if you need to ensure
that the requested amount of data is read before the blocking operation
completes.
*/
template<class MutableBufferSequence>
std::size_t
read_some(
MutableBufferSequence const& buffers,
error_code& ec);
/** Start an asynchronous read.
This function is used to asynchronously read one or more bytes of data from
the stream. The function call always returns immediately.
@param buffers The buffers into which the data will be read. Although the
buffers object may be copied as necessary, ownership of the underlying
buffers is retained by the caller, which must guarantee that they remain
valid until the handler is called.
@param handler The completion handler to invoke when the operation
completes. The implementation takes ownership of the handler by
performing a decay-copy. The equivalent function signature of
the handler must be:
@code
void handler(
const boost::system::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes read.
);
@endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `net::post`.
@note The `async_read_some` operation may not read all of the requested number of
bytes. Consider using the function `net::async_read` if you need
to ensure that the requested amount of data is read before the asynchronous
operation completes.
*/
template<
class MutableBufferSequence,
class ReadHandler>
BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
async_read_some(
MutableBufferSequence const& buffers,
ReadHandler&& handler);
/** Write some data to the stream.
This function is used to write data on the stream. The function call will
block until one or more bytes of data has been written successfully, or
until an error occurs.
@param buffers The data to be written.
@returns The number of bytes written.
@throws system_error Thrown on failure.
@note The `write_some` operation may not transmit all of the data to the
peer. Consider using the function `net::write` if you need to
ensure that all data is written before the blocking operation completes.
*/
template<class ConstBufferSequence>
std::size_t
write_some(ConstBufferSequence const& buffers);
/** Write some data to the stream.
This function is used to write data on the stream. The function call will
block until one or more bytes of data has been written successfully, or
until an error occurs.
@param buffers The data to be written.
@param ec Set to indicate what error occurred, if any.
@returns The number of bytes written.
@note The `write_some` operation may not transmit all of the data to the
peer. Consider using the function `net::write` if you need to
ensure that all data is written before the blocking operation completes.
*/
template<class ConstBufferSequence>
std::size_t
write_some(
ConstBufferSequence const& buffers,
error_code& ec);
/** Start an asynchronous write.
This function is used to asynchronously write one or more bytes of data to
the stream. The function call always returns immediately.
@param buffers The data to be written to the stream. Although the buffers
object may be copied as necessary, ownership of the underlying buffers is
retained by the caller, which must guarantee that they remain valid until
the handler is called.
@param handler The completion handler to invoke when the operation
completes. The implementation takes ownership of the handler by
performing a decay-copy. The equivalent function signature of
the handler must be:
@code
void handler(
error_code const& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes written.
);
@endcode
Regardless of whether the asynchronous operation completes
immediately or not, the handler will not be invoked from within
this function. Invocation of the handler will be performed in a
manner equivalent to using `net::post`.
@note The `async_write_some` operation may not transmit all of the data to
the peer. Consider using the function `net::async_write` if you need
to ensure that all data is written before the asynchronous operation completes.
*/
template<
class ConstBufferSequence,
class WriteHandler>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
async_write_some(
ConstBufferSequence const& buffers,
WriteHandler&& handler);
};
} // http
} // beast
} // boost
#include <boost/beast/_experimental/http/impl/icy_stream.hpp>
#endif

View File

@@ -0,0 +1,334 @@
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_CORE_IMPL_ICY_STREAM_HPP
#define BOOST_BEAST_CORE_IMPL_ICY_STREAM_HPP
#include <boost/beast/core/async_base.hpp>
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/core/error.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/beast/core/detail/is_invocable.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/assert.hpp>
#include <boost/throw_exception.hpp>
#include <cstring>
#include <memory>
#include <utility>
namespace boost {
namespace beast {
namespace http {
namespace detail {
template<class ConstBufferSequence>
boost::tribool
is_icy(ConstBufferSequence const& buffers)
{
char buf[3];
auto const n = net::buffer_copy(
net::mutable_buffer(buf, 3),
buffers);
if(n >= 1 && buf[0] != 'I')
return false;
if(n >= 2 && buf[1] != 'C')
return false;
if(n >= 3 && buf[2] != 'Y')
return false;
if(n < 3)
return boost::indeterminate;
return true;
}
} // detail
template<class NextLayer>
struct icy_stream<NextLayer>::ops
{
template<class Buffers, class Handler>
class read_op
: public beast::async_base<Handler,
beast::executor_type<icy_stream>>
, public net::coroutine
{
icy_stream& s_;
Buffers b_;
std::size_t n_ = 0;
error_code ec_;
bool match_ = false;
public:
template<class Handler_>
read_op(
Handler_&& h,
icy_stream& s,
Buffers const& b)
: async_base<Handler,
beast::executor_type<icy_stream>>(
std::forward<Handler_>(h), s.get_executor())
, s_(s)
, b_(b)
{
(*this)({}, 0, false);
}
void
operator()(
error_code ec,
std::size_t bytes_transferred,
bool cont = true)
{
BOOST_ASIO_CORO_REENTER(*this)
{
if(s_.detect_)
{
BOOST_ASSERT(s_.n_ == 0);
for(;;)
{
// Try to read the first three characters
BOOST_ASIO_CORO_YIELD
s_.next_layer().async_read_some(
net::mutable_buffer(
s_.buf_ + s_.n_, 3 - s_.n_),
std::move(*this));
s_.n_ += static_cast<char>(bytes_transferred);
if(ec)
goto upcall;
auto result = detail::is_icy(
net::const_buffer(s_.buf_, s_.n_));
if(boost::indeterminate(result))
continue;
if(result)
s_.n_ = static_cast<char>(net::buffer_copy(
net::buffer(s_.buf_, sizeof(s_.buf_)),
icy_stream::version()));
break;
}
s_.detect_ = false;
}
if(s_.n_ > 0)
{
bytes_transferred = net::buffer_copy(
b_, net::const_buffer(s_.buf_, s_.n_));
s_.n_ -= static_cast<char>(bytes_transferred);
std::memmove(
s_.buf_,
s_.buf_ + bytes_transferred,
sizeof(s_.buf_) - bytes_transferred);
}
else
{
BOOST_ASIO_CORO_YIELD
s_.next_layer().async_read_some(
b_, std::move(*this));
}
upcall:
if(! cont)
{
ec_ = ec;
n_ = bytes_transferred;
BOOST_ASIO_CORO_YIELD
s_.next_layer().async_read_some(
net::mutable_buffer{},
std::move(*this));
ec = ec_;
bytes_transferred = n_;
}
this->complete_now(ec, bytes_transferred);
}
}
};
struct run_read_op
{
template<class ReadHandler, class Buffers>
void
operator()(
ReadHandler&& h,
icy_stream* s,
Buffers const& b)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<ReadHandler,
void(error_code, std::size_t)>::value,
"ReadHandler type requirements not met");
read_op<
Buffers,
typename std::decay<ReadHandler>::type>(
std::forward<ReadHandler>(h), *s, b);
}
};
};
//------------------------------------------------------------------------------
template<class NextLayer>
template<class... Args>
icy_stream<NextLayer>::
icy_stream(Args&&... args)
: stream_(std::forward<Args>(args)...)
{
std::memset(buf_, 0, sizeof(buf_));
}
template<class NextLayer>
template<class MutableBufferSequence>
std::size_t
icy_stream<NextLayer>::
read_some(MutableBufferSequence const& buffers)
{
static_assert(is_sync_read_stream<next_layer_type>::value,
"SyncReadStream type requirements not met");
static_assert(net::is_mutable_buffer_sequence<
MutableBufferSequence>::value,
"MutableBufferSequence type requirements not met");
error_code ec;
auto n = read_some(buffers, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
return n;
}
template<class NextLayer>
template<class MutableBufferSequence>
std::size_t
icy_stream<NextLayer>::
read_some(MutableBufferSequence const& buffers, error_code& ec)
{
static_assert(is_sync_read_stream<next_layer_type>::value,
"SyncReadStream type requirements not met");
static_assert(net::is_mutable_buffer_sequence<
MutableBufferSequence>::value,
"MutableBufferSequence type requirements not met");
std::size_t bytes_transferred;
if(detect_)
{
BOOST_ASSERT(n_ == 0);
for(;;)
{
// Try to read the first three characters
bytes_transferred = next_layer().read_some(
net::mutable_buffer(buf_ + n_, 3 - n_), ec);
n_ += static_cast<char>(bytes_transferred);
if(ec)
return 0;
auto result = detail::is_icy(
net::const_buffer(buf_, n_));
if(boost::indeterminate(result))
continue;
if(result)
n_ = static_cast<char>(net::buffer_copy(
net::buffer(buf_, sizeof(buf_)),
icy_stream::version()));
break;
}
detect_ = false;
}
if(n_ > 0)
{
bytes_transferred = net::buffer_copy(
buffers, net::const_buffer(buf_, n_));
n_ -= static_cast<char>(bytes_transferred);
std::memmove(
buf_,
buf_ + bytes_transferred,
sizeof(buf_) - bytes_transferred);
}
else
{
bytes_transferred =
next_layer().read_some(buffers, ec);
}
return bytes_transferred;
}
template<class NextLayer>
template<
class MutableBufferSequence,
class ReadHandler>
BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
icy_stream<NextLayer>::
async_read_some(
MutableBufferSequence const& buffers,
ReadHandler&& handler)
{
static_assert(is_async_read_stream<next_layer_type>::value,
"AsyncReadStream type requirements not met");
static_assert(net::is_mutable_buffer_sequence<
MutableBufferSequence >::value,
"MutableBufferSequence type requirements not met");
return net::async_initiate<
ReadHandler,
void(error_code, std::size_t)>(
typename ops::run_read_op{},
handler,
this,
buffers);
}
template<class NextLayer>
template<class MutableBufferSequence>
std::size_t
icy_stream<NextLayer>::
write_some(MutableBufferSequence const& buffers)
{
static_assert(is_sync_write_stream<next_layer_type>::value,
"SyncWriteStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
MutableBufferSequence>::value,
"MutableBufferSequence type requirements not met");
return stream_.write_some(buffers);
}
template<class NextLayer>
template<class MutableBufferSequence>
std::size_t
icy_stream<NextLayer>::
write_some(MutableBufferSequence const& buffers, error_code& ec)
{
static_assert(is_sync_write_stream<next_layer_type>::value,
"SyncWriteStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
MutableBufferSequence>::value,
"MutableBufferSequence type requirements not met");
return stream_.write_some(buffers, ec);
}
template<class NextLayer>
template<
class MutableBufferSequence,
class WriteHandler>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
icy_stream<NextLayer>::
async_write_some(
MutableBufferSequence const& buffers,
WriteHandler&& handler)
{
static_assert(is_async_write_stream<next_layer_type>::value,
"AsyncWriteStream type requirements not met");
static_assert(net::is_const_buffer_sequence<
MutableBufferSequence>::value,
"MutableBufferSequence type requirements not met");
return stream_.async_write_some(
buffers, std::forward<WriteHandler>(handler));
}
} // http
} // beast
} // boost
#endif