zaphoyd/websocketpp — Issue #51: Working with data on the stack during async_write
Author: megabyte1024
Created: 2012-01-19 16:54:43+00:00
State: open

There is a couple of places where the async_write method is called using a local string variable as the buffer parameter. The places are

void client::connection::write_request() {
.....
boost::asio::async_write(
m_connection.get_socket(),
boost::asio::buffer(raw),
.....

and

void server::connection::write_response() {
.....
boost::asio::async_write(
m_connection.get_socket(),
boost::asio::buffer(raw),

I fixed the issue by using a reference counted buffer from an asio example. The class code is

class shared_const_buffer {
public:
explicit shared_const_buffer(const std::string &data) : m_data(new std::vector(data.begin(), data.end())),
m_buffer(boost::asio::buffer(*m_data)) {}
public:
typedef boost::asio::const_buffer value_type;
typedef const boost::asio::const_buffer *const_iterator;
const boost::asio::const_buffer *begin() const { return &m_buffer; }
const boost::asio::const_buffer *end() const { return &m_buffer + 1; }
private:
boost::shared_ptr< std::vector > m_data;
boost::asio::const_buffer m_buffer;
};

and replacing "boost::asio::buffer(" by "shared_const_buffer"

Plus a mistyping:

diff --git a/src/roles/server.hpp b/src/roles/server.hpp

- throw http::exception("Recieved invalid HTTP Request",http::status_code::BAD_REQUEST);
+ throw http::exception("Received invalid HTTP Request",http::status_code::BAD_REQUEST);