adding notifyRead

APPLEMAIL-24
Volker Birk 3 years ago
parent a08c4b6eb1
commit df79f29fa8

@ -64,7 +64,7 @@ namespace pEp {
close();
}
void HTTPSDevice::open(string url)
void HTTPSDevice::open(string url, notifyRead_t notifyRead)
{
if (url == "") {
if (_url != "")
@ -122,10 +122,10 @@ namespace pEp {
}
if (u.path != "")
get(u.path);
get(u.path, notifyRead);
}
void HTTPSDevice::get(string path)
void HTTPSDevice::get(string path, notifyRead_t notifyRead)
{
if (path == "")
throw invalid_argument("path needed for GET");
@ -148,6 +148,11 @@ namespace pEp {
_parser.get().body().open(temp_file_path(), file_mode::write, ec);
boost::beast::flat_buffer b;
http::read_header(*_stream, b, _parser);
if (header()["Content-Length"] != "0" && notifyRead)
notifyRead();
http::read(*_stream, b, _parser);
auto cl = header()["Content-Disposition"];

@ -20,6 +20,8 @@ namespace pEp {
namespace ssl = boost::asio::ssl;
namespace http = boost::beast::http;
typedef void (*notifyRead_t)(void);
struct url_split {
string protocol;
string login;
@ -45,13 +47,15 @@ namespace pEp {
string _filename;
public:
notifyRead_t notifyRead;
HTTPSDevice(string url = "", string user_agent="pEp/1.0", int
version=11);
HTTPSDevice(const HTTPSDevice& second);
~HTTPSDevice();
void open(string url = "");
void get(string path);
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(); }

@ -32,7 +32,7 @@ namespace pEp {
delete _str;
}
void UpdateDevice::open(CryptoPP::RSA::PublicKey update_key)
void UpdateDevice::open(CryptoPP::RSA::PublicKey update_key, notifyRead_t notifyRead)
{
CryptoPP::RSAES< CryptoPP::OAEP<CryptoPP::SHA256> >::Encryptor update_encryptor(update_key);
@ -64,7 +64,7 @@ namespace pEp {
url += "&challenge=" + _delivery_key_enc;
_https->open(url);
_https->open(url, notifyRead);
byte iv[12];
_https->read((char *) iv, sizeof(iv));

@ -27,7 +27,6 @@ namespace pEp {
typedef CryptoPP::RSA::PublicKey PublicKey;
class UpdateDevice : public io::source {
HTTPSStream _https;
product _p;
CryptoPP::GCM< CryptoPP::AES >::Decryption _delivery_decryptor;
CryptoPP::SecByteBlock _key;
@ -35,13 +34,14 @@ namespace pEp {
iostream *_str;
public:
HTTPSStream _https;
UpdateDevice(const product p);
UpdateDevice(const UpdateDevice& second);
~UpdateDevice();
streamsize read(char* s, streamsize n);
void open(PublicKey update_key);
void open(PublicKey update_key, notifyRead_t notifyRead = nullptr);
void close();
string filename() { return _https->filename(); }

@ -26,12 +26,12 @@ namespace pEp {
return key;
}
string update(product p, PublicKey update_key)
string update(product p, PublicKey update_key, notifyRead_t notifyRead)
{
UpdateStream us { UpdateDevice(p) };
try {
us->open(update_key);
us->open(update_key, notifyRead);
}
catch (exception&) { }
@ -41,10 +41,10 @@ namespace pEp {
return us->filename();
}
string update(product p, string keyfile)
string update(product p, string keyfile, notifyRead_t notifyRead)
{
PublicKey update_key = load_key(keyfile);
return update(p, update_key);
return update(p, update_key, notifyRead);
}
}
}

@ -42,6 +42,7 @@ namespace pEp {
// url (in) download URL with GET param:
// hash serial number
// update_key (in) Update Key as CryptoPP::RSA::PublicKey
// notifyRead (in) notified on read
//
// returns:
// filename of downloaded file
@ -55,11 +56,11 @@ namespace pEp {
//
// see sample in test_updater.cc
string update(product p, PublicKey update_key);
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);
string update(product p, string keyfile, notifyRead_t notifyRead = nullptr);
}
}

@ -8,6 +8,13 @@
using namespace std;
using namespace pEp::UpdateClient;
void notifyRead()
{
cout << "notify: read\n";
// we expect no update
exit(1);
}
int main()
{
cout << "trying to download...\n";
@ -16,7 +23,7 @@ int main()
product p { "pEp for Something", "https://fdik.org/cgidownload?hash=42232323234242" };
try {
string filename = update(p, "public.der");
string filename = update(p, "public.der", notifyRead);
cout << "downloaded " << filename << "\n";
return 1;
}

@ -8,6 +8,11 @@
using namespace std;
using namespace pEp::UpdateClient;
void notifyRead()
{
cout << "notify: read\n";
}
int main()
{
cout << "downloading...\n";
@ -15,7 +20,7 @@ int main()
product p { "pEp for Something", "https://fdik.org/cgidownload?hash=23232323234242" };
try {
string filename = update(p, "public.der");
string filename = update(p, "public.der", notifyRead);
cout << "downloaded " << filename << "\n";
return 0;
}

Loading…
Cancel
Save