1

Topic: https, connect, handshake

Greetings!  here some robot for REST-API, and in quality  on usage boost.beast took this:////Copyright (c) 2016-2017 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////-- … //Example: HTTP SSL client, asynchronous////------------------------------------------------------------------------------ #include "example/common/root_certificates.hpp" #include <boost/beast/core.hpp> #include <boost/beast/http.hpp> #include <boost/beast/version.hpp> #include <boost/asio/connect.hpp> #include <boost/asio/ip/tcp.hpp> #include <boost/asio/ssl/error.hpp> #include <boost/asio/ssl/stream.hpp> #include <cstdlib> #include <functional> #include <iostream> #include <memory> #include <string> using tcp = boost:: asio:: ip:: tcp;//from <boost/asio/ip/tcp.hpp> namespace ssl = boost:: asio:: ssl;//from <boost/asio/ssl.hpp> namespace http = boost:: beast:: http;//from <boost/beast/http.hpp>//------------------------------------------------------------------------------//Report a failure void fail (boost:: system:: error_code ec, char const* what) {std:: cerr <<what <<":" <<ec.message () <<"\n";} //Performs an HTTP GET and prints the response class session: public std:: enable_shared_from_this <session> {tcp:: resolver resolver _; ssl:: stream <tcp:: socket> stream _; boost:: beast:: flat_buffer buffer _;//(Must persist between reads) http:: request <http:: empty_body> req _; http:: response <http:: string_body> res _; public://Resolver and stream require an io_context explicit session (boost::asio::io_context& ioc, ssl::context& ctx): resolver _ (ioc), stream _ (ioc, ctx) {}//Start the asynchronous operation void run (char const* host, char const* port, char const* target, int version) {//Set SNI Hostname (many hosts need this to handshake successfully) if (! SSL_set_tlsext_host_name (stream_. native_handle (), host)) {boost:: system:: error_code ec {static_cast <int> (:: ERR_get_error ()), boost:: asio:: error:: get_ssl_category ()}; std:: cerr <<ec.message () <<"\n"; return;} //Set up an HTTP GET request message req_. version (version); req_. method (http::verb::get); req_. target (target); req_. set (http::field::host, host); req_. set (http::field::user_agent, BOOST_BEAST_VERSION_STRING);//Look up the domain name resolver_. async_resolve (host, port, std:: bind (&session::on_resolve, shared_from_this (), std:: placeholders:: _1, std:: placeholders:: _2));} void on_resolve (boost:: system:: error_code ec, tcp:: resolver:: results_type results) {if (ec) return fail (ec, "resolve");//Make the connection on the IP address we get from a lookup boost:: asio:: async_connect (stream_. next_layer (), results.begin (), results.end (), std:: bind (&session::on_connect, shared_from_this (), std:: placeholders:: _1));} void on_connect (boost:: system:: error_code ec) {if (ec) return fail (ec, "connect");//Perform the SSL handshake stream_. async_handshake (ssl:: stream_base:: client, std:: bind (&session::on_handshake, shared_from_this (), std:: placeholders:: _1));} void on_handshake (boost:: system:: error_code ec) {if (ec) return fail (ec, "handshake");//Send the HTTP request to the remote host http::async_write (stream _, req _, std:: bind (&session::on_write, shared_from_this (), std:: placeholders:: _1, std:: placeholders:: _2));} void on_write (boost:: system:: error_code ec, std:: size_t bytes_transferred) {boost:: ignore_unused (bytes_transferred); if (ec) return fail (ec, "write");//Receive the HTTP response http::async_read (stream _, buffer _, res _, std:: bind (&session::on_read, shared_from_this (), std:: placeholders:: _1, std:: placeholders:: _2));} void on_read (boost:: system:: error_code ec, std:: size_t bytes_transferred) {boost:: ignore_unused (bytes_transferred); if (ec) return fail (ec, "read");//Write the message to standard out std:: cout <<res_ <<std:: endl;//Gracefully close the stream stream_. async_shutdown (std:: bind (&session::on_shutdown, shared_from_this (), std:: placeholders:: _1));} void on_shutdown (boost:: system:: error_code ec) {if (ec == boost:: asio:: error:: eof) {//Rationale://http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error ec.assign (0, ec.category ());} if (ec) return fail (ec, "shutdown");//If we get here then the connection is closed gracefully}};//------------------------------------------------------------------------------ int main (int argc, char ** argv) {//Check command line arguments. if (argc! = 4 && argc! = 5) {std:: cerr <<"Usage: http-client-async-ssl <host> <port> <target> [<HTTP version: 1.0 or 1.1 (default)>] \n" <<"Example:\n" <<"http-client-async-ssl www.example.com 443/\n" <<"http-client-async-ssl www.example.com 443 / 1.0\n"; return EXIT_FAILURE;} auto const host = argv [1]; auto const port = argv [2]; auto const target = argv [3]; int version = argc == 5 &&! std:: strcmp ("1.0", argv [4])? 10: 11;//The io_context is required for all I/O boost:: asio:: io_context ioc;//The SSL context is required, and holds certificates ssl:: context ctx {ssl:: context:: sslv23_client};//This holds the root certificate used for verification load_root_certificates (ctx);//Launch the asynchronous operation std:: make_shared <session> (ioc, ctx)-> run (host, port, target, version);//Run the I/O service. The call will return when//the get operation is complete. ioc.run (); return EXIT_SUCCESS;} Entry point here a method session:: run (). My question consists in, whether I should for  request fulfill resolve+connect+handshake, or it needs to be made once and further simply to call write+read? I.e. I for example once create object , in the designer I fulfill resolve+connect+handshake, and further simply I call write+read?

2

Re: https, connect, handshake

What means - each request?  a connection  on each created session + further after channel creation, only rid/vrajt if from server side that session is fulfilled after everyone

3

Re: https, connect, handshake

Hello, niXman, you wrote: X> my question consists in, whether I should for  request fulfill resolve+connect+handshake, or it needs to be made once and further simply to call write+read? I am sorry, it is, of course, very informative, to write the HTTP-client using primitives level hardly above sockets, and what, for a C ++ there is no ready HTTP-client who hides all these details of implementation at itself under a cowl? Here in particular, there is such piece, as connection keep-alive (it when we do not close a socket, receiving the answer, and we use it, at possibility, for following request). It is necessary to track connections which like live, but only already half an hour as there leaves nothing or therefrom comes nothing. There is a compression of the data sent through HTTP, it not bad presses JSON. Happens that request answer redirect', and not bad to descend with this request in other place (or not to walk if to other place of trust is not present). At TLS/SSL there is a session restart when it is possible to avoid at repeated establishment of connection expensive RSA/DHE, and at once to pass to business. In , much that happens. You it are going to write all the hands? X> i.e. I for example once create object , in the designer I fulfill resolve+connect+handshake, and further simply I call write+read? Connections are sometimes torn, and them it is necessary . And so, while TCP-connection is live, at repeated requests in DNS to climb there is no special sense. All the same, the answer is not useful.

4

Re: https, connect, handshake

Hello, reversecode, you wrote: R> that means - each request? The client. I created object , resolve+connect+handshake. write+read I can repeatedly call further, or before everyone write+read I should do this all anew?

5

Re: https, connect, handshake

Judging by the code if to remove//Gracefully close the stream stream_. async_shutdown (that can be called repeatedly the channel will not be closed yet

6

Re: https, connect, handshake

Hello, reversecode, you wrote: R> that can be called repeatedly the channel my question will not be closed yet was not about the code, and generally about http/https. Whether it is necessary before everyone http-GET request to fulfill resolve+connect+handshake? Or it is enough to make of it once and further it is possible repeatedly write+read?

7

Re: https, connect, handshake

If everyone https GET on a different host yes, it is necessary to do if everyone https GET becomes on one host it is twisted from designing of a software the specification does not forbid to do repeatedly , all depends on everyones http keep-alive but if all write it https GET probably I would do each time handsnake  at me the a figurative allergy on keep-alive, after  deliveries

8

Re: https, connect, handshake

R> the specification does not forbid to do repeatedly , R> but if all write it https GET probably I would do each time handsnake excuse, handshake,  - hand shake

9

Re: https, connect, handshake

Hello, Pzz, you wrote: Pzz> I am sorry, it is, of course, very informative, to write the HTTP-client using primitives level hardly above sockets, and what, for a C ++ there is no ready HTTP-client who hides all these details of implementation at itself under a cowl? Same sect of witnesses of a spherical horse in vacuum With ++ community - all should be floppy, provisionablly and if on any iron it does not work, it means it is not necessary at all. Through it forces suffice only on learning of a dial-up of spare parts for a bicycle, on the decision of the practical task of forces does not remain. And still With ++ the programmer is assured that to it to what of 10-20 years of another's experience in a type ready  - he clever, now sits down and  one more mega-frejmvork deprived of a fatal lack

10

Re: https, connect, handshake

Hello, MTD, you wrote: MTD> Same sect of witnesses of a spherical horse in vacuum With ++ community - all should be floppy, provisionablly and if on any iron it does not work, it means it is not necessary at all. A C ++ - the useful piece. On it people learn to write good compilers for the bad languages.

11

Re: https, connect, handshake

Hello, Pzz, you wrote: Pzz> and what, for a C ++ there is no ready HTTP-client who hides all these details of implementation at itself under a cowl? For example, in POCO there was a simple and convenient enough implementation of the HTTP-client. But generally in a C ++ remarkably works libcurl, especially if it beforehand to envelop a convenient C ++  the interface.

12

Re: https, connect, handshake

Hello, so5team, you wrote: S> For example, in POCO there was a simple and convenient enough implementation of the HTTP-client. S> but generally in a C ++ remarkably works libcurl, especially if it beforehand to envelop a convenient C ++  the interface. About CURL I, certainly know. But I am surprised that it till now to  did not screw. Well or the did not rivet.

13

Re: https, connect, handshake

Hello, Pzz, you wrote: Pzz> But I am surprised that it till now to  did not screw. Well or the did not rivet. To Boost fastened Beast. But not so much to use it there where application-oriented developers need to work with HTTP, how many that Beast became basis on which somebody constructs convenient tools in usage. Like as the author Beast something such does, but this superstructure over Beast th for the present did not grow to public release. And what sense to rivet the, it is pure  when libcurl without problems it is used from pluses?

14

Re: https, connect, handshake

Hello, so5team, you wrote: S> And what sense to rivet the, it is pure  when libcurl without problems it is used from pluses? Without problems, if it not , and asynchronous operation with requests and support https will be the order of 1000 lines and to steam of indirect libraries, for example openssl and libevent - couple of weeks of operation of the developer at office. Without problems

15

Re: https, connect, handshake

Hello, std.denis, you wrote: SD> excuse, handshake,  - hand shake of Rukotrjasenie

16

Re: https, connect, handshake

Hello, MTD, you wrote: S>> And what sense to rivet the, it is pure  when libcurl without problems it is used from pluses? MTD> without problems, if it not , and asynchronous operation with requests and support https will be the order of 1000 lines and to steam of indirect libraries, for example openssl and libevent - couple of weeks of operation of the developer at office. Without problems Couple of days, you wanted to tell? With  it is on friendly terms, and that to ,  it as that, is not necessary, but if at you and so it is meant any event loop  it is rather simple to build in it and if your own cycle becomes around  and  it is possible to solder there.

17

Re: https, connect, handshake

Hello, Pzz, you wrote: Pzz> Couple of days, you wanted to tell? Couple of days is if already wrote and accurately know where to take and where to put. Here I for  wrote as how to do well I represent. Well and estimations of programmers, such estimations. Worked in one office, there all such vigorous were - estimations on the task gave from 15 minutes

18

Re: https, connect, handshake

Hello, MTD, you wrote: Pzz>> Couple of days, you wanted to tell? MTD> couple of days is if already wrote and accurately know where to take and where to put. Here I for  wrote as how to do well I represent. Well and estimations of programmers, such estimations. Worked in one office, there all such vigorous were - estimations on the task gave from 15 minutes Well, I with  pottered, complexity level I represent. Anything especially magic in it is not present, but the documentation occasionally the muffled. P.S. In the presence of Go it is necessary to have well very serious reasons to do such features on Si that with  that without.

19

Re: https, connect, handshake

Hello, Pzz, you wrote: Pzz> Well, I with  pottered, complexity level I represent. Anything especially magic in it is not present, but the documentation occasionally the muffled. Indeed, but everywhere there is a nuance. While only you will collect libopenssl (and this already in itself is uneasy as it is necessary to understand accurately what flags to include, and it also pulls dependences) day easily transits. And still to collect libcurl, zlib, libevent, yes under different platforms (I all about  speak it, instead of about on downloaded  from the Internet not clearly as well as whom collected or delivered from a repository on Linux - all works for me). Pluses - not Java, are not present Mavena. Pzz> P.S. In the presence of Go it is necessary to have well very serious reasons to do such features on Si that with  that without. Here generally objections are not present. For operation with a network to saw Something now on pluses it it is very strange.