void connection::send(const reply& r) { // send! boost::asio::async_write(socket_, r.to_buffers(), strand_.wrap( boost::bind(&connection::handle_write, shared_from_this(), boost::asio::placeholders::error))); }
void handle_jquery(server_ptr serv, connection_ptr conn, const request& req, const std::string& path, const std::string&query, reply&rep) { #include "jquery/jquery.i" static const std::string jqst(JQUERY_STR, sizeof(JQUERY_STR)); //this redirect the browser so it thinks the stream url is unique rep.status = reply::ok; rep.headers.clear(); rep.content = jqst; rep.headers.push_back(header("Content-Length", boost::lexical_cast<std::string>(sizeof(JQUERY_STR)))); rep.headers.push_back(header("Content-Type", mime_types::extension_to_type("js"))); conn->async_write(rep.to_buffers()); }
inline void connection::handle_read(const boost::system::error_code& e, std::size_t bytes_transferred) { if (!e) { boost::tribool result; boost::tie(result, boost::tuples::ignore) = request_parser_.parse( *request_, buffer_.data(), buffer_.data() + bytes_transferred); if (result) { try { request_handler_.handle_request(*request_, reply_); } catch (webserver::reply rep) { reply_ = rep; } std::cerr << reply_.content << std::endl; boost::asio::async_write(socket_, reply_.to_buffers(), strand_.wrap( boost::bind(&connection::handle_write, shared_from_this(), boost::asio::placeholders::error))); } else if (!result) { reply_ = reply::stock_reply(reply::bad_request); boost::asio::async_write(socket_, reply_.to_buffers(), strand_.wrap( boost::bind(&connection::handle_write, shared_from_this(), boost::asio::placeholders::error))); } else { socket_.async_read_some(boost::asio::buffer(buffer_), strand_.wrap( boost::bind(&connection::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred))); } } // If an error occurs then no new asynchronous operations are started. This // means that all shared_ptr references to the connection object will // disappear and the object will be destroyed automatically after this // handler returns. The connection class's destructor closes the socket. }