support closures

APPLEMAIL-24
Volker Birk 2020-06-02 19:13:44 +02:00
parent ab1dfc5ba4
commit a5e2c45653
8 changed files with 22 additions and 22 deletions

View File

@ -45,7 +45,7 @@ namespace pEp {
HTTPSDevice::HTTPSDevice(string url, string user_agent, int version)
: _url(url), _user_agent(user_agent), _version(version),
_stream(nullptr)
_stream(nullptr), notifyRead(nullptr)
{
if (version != 11)
throw logic_error("only HTTP version 1.1 implemented");
@ -54,7 +54,8 @@ namespace pEp {
HTTPSDevice::HTTPSDevice(const HTTPSDevice& second)
: _url(second._url), _user_agent(second._user_agent),
_version(second._version), _stream(nullptr)
_version(second._version), _stream(nullptr),
notifyRead(second.notifyRead)
{
_parser.body_limit(1000000000);
}
@ -64,7 +65,7 @@ namespace pEp {
close();
}
void HTTPSDevice::open(string url, notifyRead_t notifyRead)
void HTTPSDevice::open(string url, notifyRead_t *notifyRead)
{
if (url == "") {
if (_url != "")
@ -125,7 +126,7 @@ namespace pEp {
get(u.path, notifyRead);
}
void HTTPSDevice::get(string path, notifyRead_t notifyRead)
void HTTPSDevice::get(string path, notifyRead_t *notifyRead)
{
if (path == "")
throw invalid_argument("path needed for GET");
@ -151,7 +152,7 @@ namespace pEp {
http::read_header(*_stream, b, _parser);
if (header()["Content-Length"] != "0" && notifyRead)
notifyRead();
(*notifyRead)();
http::read(*_stream, b, _parser);

View File

@ -5,6 +5,7 @@
#include <iostream>
#include <fstream>
#include <functional>
#include <boost/asio.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
@ -20,7 +21,7 @@ namespace pEp {
namespace ssl = boost::asio::ssl;
namespace http = boost::beast::http;
typedef void (*notifyRead_t)(void);
typedef function< void() > notifyRead_t;
struct url_split {
string protocol;
@ -47,15 +48,15 @@ namespace pEp {
string _filename;
public:
notifyRead_t notifyRead;
notifyRead_t *notifyRead;
HTTPSDevice(string url = "", string user_agent="pEp/1.0", int
version=11);
HTTPSDevice(const HTTPSDevice& second);
~HTTPSDevice();
void open(string url = "", notifyRead_t notifyRead = nullptr);
void get(string path, notifyRead_t notifyRead = nullptr);
void open(string url = "", notifyRead_t *notifyRead = nullptr);
void get(string path, notifyRead_t *notifyRead = nullptr);
void close();
const http::fields& header() const { return _parser.get(); }

View File

@ -32,7 +32,7 @@ namespace pEp {
delete _str;
}
void UpdateDevice::open(CryptoPP::RSA::PublicKey update_key, notifyRead_t notifyRead)
void UpdateDevice::open(CryptoPP::RSA::PublicKey update_key, notifyRead_t *notifyRead)
{
CryptoPP::RSAES< CryptoPP::OAEP<CryptoPP::SHA256> >::Encryptor update_encryptor(update_key);

View File

@ -41,7 +41,7 @@ namespace pEp {
streamsize read(char* s, streamsize n);
void open(PublicKey update_key, notifyRead_t notifyRead = nullptr);
void open(PublicKey update_key, notifyRead_t *notifyRead = nullptr);
void close();
string filename() { return _https->filename(); }

View File

@ -26,7 +26,7 @@ namespace pEp {
return key;
}
string update(product p, PublicKey update_key, notifyRead_t notifyRead)
string update(product p, PublicKey update_key, notifyRead_t *notifyRead)
{
UpdateStream us { UpdateDevice(p) };
@ -41,7 +41,7 @@ namespace pEp {
return us->filename();
}
string update(product p, string keyfile, notifyRead_t notifyRead)
string update(product p, string keyfile, notifyRead_t *notifyRead)
{
PublicKey update_key = load_key(keyfile);
return update(p, update_key, notifyRead);

View File

@ -56,11 +56,11 @@ namespace pEp {
//
// see sample in test_updater.cc
string update(product p, PublicKey update_key, notifyRead_t notifyRead = nullptr);
string update(product p, PublicKey update_key, notifyRead_t *notifyRead = nullptr);
// update() - convenience function doing load_key() and update()
string update(product p, string keyfile, notifyRead_t notifyRead = nullptr);
string update(product p, string keyfile, notifyRead_t *notifyRead = nullptr);
}
}

View File

@ -23,7 +23,8 @@ int main()
product p { "pEp for Something", "https://fdik.org/cgidownload?hash=66232323234242" };
try {
string filename = update(p, "public.der", notifyRead);
pEp::notifyRead_t notifyRead = [=]()->void{cout << "notify: read\n";exit(1);};
string filename = update(p, "public.der", &notifyRead);
cout << "downloaded " << filename << "\n";
return 1;
}

View File

@ -2,17 +2,13 @@
// see LICENSE.txt
#include <iostream>
#include <functional>
#include <assert.h>
#include "downloadclient.cc"
using namespace std;
using namespace pEp::UpdateClient;
void notifyRead()
{
cout << "notify: read\n";
}
int main()
{
cout << "downloading...\n";
@ -20,7 +16,8 @@ int main()
product p { "pEp for Something", "https://fdik.org/cgidownload?hash=42232323234242" };
try {
string filename = update(p, "public.der", notifyRead);
pEp::notifyRead_t notifyRead = [=]()->void{cout << "notify: read\n";};
string filename = update(p, "public.der", &notifyRead);
cout << "downloaded " << filename << "\n";
return 0;
}