From df79f29fa806268940ed7cfe6aa9b642cc545f11 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Mon, 1 Jun 2020 17:00:12 +0200 Subject: [PATCH] adding notifyRead --- HTTPSStream.cc | 11 ++++++++--- HTTPSStream.hh | 8 ++++++-- UpdateStream.cc | 4 ++-- UpdateStream.hh | 4 ++-- downloadclient.cc | 8 ++++---- downloadclient.hh | 5 +++-- test_noupdate.cc | 9 ++++++++- test_updater.cc | 7 ++++++- 8 files changed, 39 insertions(+), 17 deletions(-) diff --git a/HTTPSStream.cc b/HTTPSStream.cc index b0f8823..5780f0c 100644 --- a/HTTPSStream.cc +++ b/HTTPSStream.cc @@ -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"]; diff --git a/HTTPSStream.hh b/HTTPSStream.hh index 6329368..da580a5 100644 --- a/HTTPSStream.hh +++ b/HTTPSStream.hh @@ -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(); } diff --git a/UpdateStream.cc b/UpdateStream.cc index e30b334..d413422 100644 --- a/UpdateStream.cc +++ b/UpdateStream.cc @@ -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 >::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)); diff --git a/UpdateStream.hh b/UpdateStream.hh index 5399912..e9c7d96 100644 --- a/UpdateStream.hh +++ b/UpdateStream.hh @@ -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(); } diff --git a/downloadclient.cc b/downloadclient.cc index 36faf57..83bcb71 100644 --- a/downloadclient.cc +++ b/downloadclient.cc @@ -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); } } } diff --git a/downloadclient.hh b/downloadclient.hh index 8b5bfd1..d19a66c 100644 --- a/downloadclient.hh +++ b/downloadclient.hh @@ -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); } } diff --git a/test_noupdate.cc b/test_noupdate.cc index 39320f8..e1f2fcd 100644 --- a/test_noupdate.cc +++ b/test_noupdate.cc @@ -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; } diff --git a/test_updater.cc b/test_updater.cc index 48f1e7b..d383053 100644 --- a/test_updater.cc +++ b/test_updater.cc @@ -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; }