Demonstrates how to use sockets and the TLS layer.This example is a most-trivial HTTPS client that requests "/" on the passed host and outputs what the server sends verbatim.
#include <iostream>
#include <type_traits>
namespace {
{
logger()
{
}
virtual void do_log(fz::logmsg::type t, std::wstring && msg) {
std::cerr <<
"Log: " << int(t) <<
" " <<
fz::to_string(msg) <<
"\n";
}
};
}
{
public:
, trust_store_(pool_)
{
s_ = std::make_unique<fz::socket>(pool_, this);
if (res) {
event_loop_.stop();
return;
}
tls_ = std::make_unique<fz::tls_layer>(event_loop_, this, *s_, &trust_store_, log_);
event_loop_.stop();
return;
}
snd_.append("GET / HTTP/1.1\r\nConnection: close\r\nUser-Agent: lfz (socket demo)\r\nHost: ");
snd_.append(host);
snd_.append("\r\n\r\n");
}
virtual ~handler()
{
}
bool success_{};
private:
{
fz::dispatch<fz::socket_event>(ev, this, &handler::on_socket_event);
}
if (error) {
switch (type) {
break;
break;
break;
default:
break;
}
event_loop_.stop();
return;
}
while (!snd_.empty()) {
int w = tls_->write(snd_.get(), snd_.size(),
error);
if (w > 0) {
snd_.consume(w);
}
else {
if (w < 0) {
if (error == EAGAIN) {
return;
}
}
event_loop_.stop();
return;
}
}
log_.log(fz::logmsg::status, "Sent request");
}
char buf[1024];
while (true) {
int r = tls_->read(buf, 1024, error);
if (r > 0) {
std::cout << std::string_view(buf, r);
continue;
}
if (!r) {
log_.log(fz::logmsg::status, "Got eof");
success_ = true;
}
else {
if (error == EAGAIN) {
return;
}
}
event_loop_.stop();
return;
}
}
}
logger log_;
std::unique_ptr<fz::socket> s_;
std::unique_ptr<fz::tls_layer> tls_;
};
int main(int argc , char * argv[])
{
if (argc <= 1) {
std::cerr << "Need to pass hostname\n";
return 1;
}
std::string host = argv[1];
handler h(l, host);
return h.success_ ? 0 : 1;
}