Introducing Beast: HTTP and WebSockets C++ library Vinnie Falco Ripple CppCon 2016 Sep/18/2016

What is it? ●

HTTP and WebSockets using Boost.Asio



Header-only, C++11 or later, Open source



Emulates Boost.Asio style



In the Boost incubator for review



Full documentation, tests, and samples



Running on production Ripple servers!



Git repo https://github.com/vinniefalco/Beast

Why Do We Need This? HTTP Request in JavaScript var xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", theUrl, false ); xmlHttp.send( null ); return xmlHttp.responseText;

Why Do We Need This? HTTP Request in JavaScript var xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", theUrl, false ); xmlHttp.send( null ); return xmlHttp.responseText;

HTTP Request in C++ // ???

WebSocket Scope ●

Establish WebSocket sessions



Send and receive WebSocket messages



Build clients or servers, sync or async



Production-level performance



Autobahn|Testsuite:

WebSocket Echo Example ●

Connect to remote WebSocket echo server



Handshake and send a message



Receive and print echoed message

Connect to Remote Host #include #include using namespace boost::asio; int main() { auto host = "echo.websocket.org"; io_service ios; ip::tcp::resolver r{ios}; ip::tcp::socket sock{ios}; connect(sock, r.resolve( ip::tcp::resolver::query{host, "80"}));

Handshake and Send a Message // websocket::stream wraps your socket, // SSL stream, or user defined type! // beast::websocket::stream< ip::tcp::socket&> ws{sock}; ws.handshake(host, "/"); ws.write(buffer("Hello, world!"));

Handshake and Send a Message // websocket::stream wraps your socket, // SSL stream, or user defined type! // beast::websocket::stream< ip::tcp::socket&> ws{sock}; ws.handshake(host, "/"); ws.write(buffer("Hello, world!"));

Handshake and Send a Message // websocket::stream wraps your socket, // SSL stream, or user defined type! // beast::websocket::stream< ip::tcp::socket&> ws{sock}; ws.handshake(host, "/"); ws.write(buffer("Hello, world!"));

Receive and print echoed message boost::asio::streambuf sb; beast::websocket::opcode op; ws.read(op, sb); std::cout << to_string(sb.data()); // Send WebSocket close frame ws.close( beast::websocket::close_code::normal);

Receive and print echoed message boost::asio::streambuf sb; beast::websocket::opcode op; ws.read(op, sb); std::cout << to_string(sb.data()); // Send WebSocket close frame ws.close( beast::websocket::close_code::normal);

Receive and print echoed message boost::asio::streambuf sb; beast::websocket::opcode op; ws.read(op, sb); std::cout << to_string(sb.data()); // Send WebSocket close frame ws.close( beast::websocket::close_code::normal);

Asynchronous Interfaces boost::asio::streambuf sb; beast::websocket::opcode op; // Or use coroutines, std::future, or // user defined types using Asio's // `async_result` customization ws.async_read(op, sb, [&](beast::error_code const& ec) { std::cout << to_string(sb.data()); });

But Wait, There's More! WebSocket uses HTTP to perform the handshake

HTTP Scope ●

A universal HTTP message container



Send and receive HTTP/1 messages



Build clients or servers, sync or async



Works with SSL or any Stream concept



Production-level performance



For library developers, not end users



Use Beast to build higher level abstractions: (e.g. build a better curl)

HTTP GET Example ●

Connect to remote host



Assemble and send HTTP GET request



Receive and print HTTP Response

Connect to Remote Host #include #include using namespace boost::asio; int main() { auto host = "boost.org"; io_service ios; ip::tcp::resolver r{ios}; ip::tcp::socket sock{ios}; connect(sock, r.resolve( ip::tcp::resolver::query{host,"http"}));

Send HTTP GET Request beast::http::request_v1< beast::http::empty_body> req; req.method = "GET"; req.url = "/"; req.version = 11; req.headers.insert("User-Agent", "Me"); // (`sock` could be an SSL stream) beast::http::prepare(req); beast::http::write(sock, req);

Send HTTP GET Request beast::http::request_v1< beast::http::empty_body> req; req.method = "GET"; req.url = "/"; req.version = 11; req.headers.insert("User-Agent", "Me"); // (`sock` could be an SSL stream) beast::http::prepare(req); beast::http::write(sock, req);

Send HTTP GET Request beast::http::request_v1< beast::http::empty_body> req; req.method = "GET"; req.url = "/"; req.version = 11; req.headers.insert("User-Agent", "Me"); // (`sock` could be an SSL stream) beast::http::prepare(req); beast::http::write(sock, req);

Receive HTTP Response beast::http::response_v1< beast::http::string_body> res; // (`sock` could be an SSL stream) boost::asio::streambuf sb; beast::http::read(sock, sb, res); std::cout << res;

Receive HTTP Response beast::http::response_v1< beast::http::string_body> res; // (`sock` could be an SSL stream) boost::asio::streambuf sb; beast::http::read(sock, sb, res); std::cout << res;

Asynchronous Interfaces beast::http::response_v1< beast::http::string_body> res; // Or use coroutines, std::future, or // user defined types using Asio's // “async_result” customization boost::asio::streambuf sb; beast::http::async_read(sock, sb, res, [&](beast::error_code const& ec) { std::cout << res; });

Advanced HTTP Features ●

● ●



Customize the message body –

User defined type in message



Custom algorithm for serializing and deserializing

Send incremental body data from coroutine Read-only message bodies (e.g. A body that streams from a file) HTTP/1 parser is zero alloc and self contained

Summary ●





https://github.com/vinniefalco/Beast If Christopher Kohloff (Boost.Asio author) wrote an HTTP and WebSockets library, it would look like this! Any questions? HTTP

WebSockets

request_v1 req;

stream ws{sock};

req.method = "GET"; req.url = "/"; req.version = 11;

ws.handshake(host, "/");

prepare(req); write(sock, req);

ws.write(asio::buffer( "Hello, world!"));

HTTP Parser Performance beast.http.parser_bench Parser speed test, 34377KB in 200000 messages sizeof(request parser) == 48 sizeof(response parser) == 48 nodejs_parser Trial 1: 4111 ms Trial 2: 4096 ms Trial 3: 4091 ms http::basic_parser_v1 Trial 1: 4510 ms Trial 2: 4520 ms Trial 3: 4527 ms Longest suite times: 26.2s beast.http.parser_bench

HTTP Message Container template< bool isRequest, class Body, Class Headers = beast::http::headers > struct message { Headers headers; // Trait controls this member's type! typename Body::value_type body; };

HTTP Body Concept struct string_body { // The message::body data member type using value_type = std::string; // Algorithm for reading a string_body class reader; // Algorithm for writing a string_body class writer; };

CppCon 2016 - GitHub

Sep 18, 2016 - Send and receive WebSocket messages. ○ Build clients or servers, sync or async. ○ Production-level performance. ○ Autobahn|Testsuite: ...

465KB Sizes 25 Downloads 221 Views

Recommend Documents

CppCon 2016 - GitHub
Sep 18, 2016 - using namespace boost::asio; int main(). { auto host = "echo.websocket.org"; io_service ios; ip::tcp::resolver r{ios}; ip::tcp::socket sock{ios};.

CppCon 2016 (eng): C++14 Reflections Without Macros, Markup nor ...
template static void print (Stream& out, const T& value) { if (!!FieldIndex) out

Vowpal Wabbit 2016 - GitHub
Community. 1. BSD license. 2. Mailing list >500, Github >1K forks, >1K,. >1K issues, >100 contributors. 3. The official strawman for large scale logistic regression @ NIPS :-) ...

FALL even (2016) - GitHub
Equations. MATH 665 Topics in. Graduate Mathematics. MATH 665 Topics in. Graduate Mathematics. MATH 611 Mathemati- cal Physics. MATH 612 Mathemati-.

Playable Experiences at AIIDE 2016 - GitHub
ebrates these efforts and emphasizes the development of polished experiences that ..... Conclusion. AIIDE is a meeting ground between entertainment software.

Eric Evenchick 2016-05-14 - GitHub
May 14, 2016 - April 2018: all cars sold in EU must have eCall. Page 8. CAN Bus. • Controller Area Network. • Low cost, integrated controllers. • Types: • High speed (differential). • Low speed (single ended). • Fault Tolerant. • CAN FD

Sumner Evans September 22, 2016 - GitHub
https://www.git-tower.com/blog/8-reasons-for-switching-to-git. Sumner Evans. Git ... remote, a version of the repository hosted externally from your local machine. ... Play around with a bunch of them and see which one you like best. Here are a few t

Ordinary Differential Equations Autumn 2016 - GitHub
Mar 29, 2017 - A useful table of Laplace transforms: http://tutorial.math.lamar.edu/pdf/Laplace Table.pdf. Comment. Here you finally get the opportunity to practise solving ODE's using the powerful method of Laplace transformations. Please takes note

Ahmet Emre Alada˘g April, 2016 - GitHub
Personal Site: http://www.emrealadag.com. Web Pages. Projects: ... Woramo is a social network for sharing product/service reviews online. Friends ... Server Faces, installed/configured a server with RHEL5 and Oracle 10g En- terprise Server ...

Lecture 11 — November 22, 2016 Volkswagen Emissions ... - GitHub
Emissions Workshop, an academic conference, in May 2014. Regulators and ... “This VW Diesel Scandal is Much Worse Than a Re- call.” 21 September 2015.

Bitcoin Wallet Privacy Rating Report 2nd Edition, March 2016 - GitHub
PaGE 1 table of contents introduction 2. Overall Wallet Privacy Rankings 3 individual .... information directly from nodes in the Bitcoin network By accessing the Bitcoin .... average user prone to snooping from their Internet Service Provider or.

Tomasz Dąbrowski / Rockhard GIC 2016 Poznań - GitHub
Page 3 ... so I can hot-swap any code at any moment. • will prefer dynamic data structures over performance. • also important on platforms where you want to hotpatch code over the network (mobile, consoles) ... are just huge because they can. •

GitHub
domain = meq.domain(10,20,0,10); cells = meq.cells(domain,num_freq=200, num_time=100); ...... This is now contaminator-free. – Observe the ghosts. Optional ...

GitHub
data can only be “corrected” for a single point on the sky. ... sufficient to predict it at the phase center (shifting ... errors (well this is actually good news, isn't it?)

MATH 241 Fall 2016 (3 credits) Course Syllabus - GitHub
Dec 5, 2016 - Thus, I will need the email address that you reliably check. The ... scanning them in as a PDF (sub 2MB) and sending them to me. .... good boost to your grade; I once had a student go from a B to and A- based on these.

MATH 390.03-02 Soring 2016 & MATH 650.03-01 Spring 2016 - GitHub
May 3, 2016 - of frequentist methods followed by a survey of statistical modeling using the Bayesian ... Lectures. I have a no computer / tablet / phone policy during lectures. Only pen / pencil and paper. Classes are 75 minutes and run from Monday,

Torsten - GitHub
Metrum Research Group has developed a prototype Pharmacokinetic/Pharmacodynamic (PKPD) model library for use in Stan 2.12. ... Torsten uses a development version of Stan, that follows the 2.12 release, in order to implement the matrix exponential fun

Untitled - GitHub
The next section reviews some approaches adopted for this problem, in astronomy and in computer vision gener- ... cussed below), we would question the sensitivity of a. Delaunay triangulation alone for capturing the .... computation to be improved fr

ECf000172411 - GitHub
Robert. Spec Sr Trading Supt. ENA West Power Fundamental Analysis. Timothy A Heizenrader. 1400 Smith St, Houston, Tx. Yes. Yes. Arnold. John. VP Trading.