| 1 | // |
| 2 | // download.cpp |
| 3 | // |
| 4 | // This sample demonstrates the URIStreamOpener class. |
| 5 | // |
| 6 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 7 | // and Contributors. |
| 8 | // |
| 9 | // SPDX-License-Identifier: BSL-1.0 |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #include "Poco/URIStreamOpener.h" |
| 14 | #include "Poco/StreamCopier.h" |
| 15 | #include "Poco/Path.h" |
| 16 | #include "Poco/URI.h" |
| 17 | #include "Poco/SharedPtr.h" |
| 18 | #include "Poco/Exception.h" |
| 19 | #include "Poco/Net/HTTPStreamFactory.h" |
| 20 | #include "Poco/Net/HTTPSStreamFactory.h" |
| 21 | #include "Poco/Net/FTPStreamFactory.h" |
| 22 | #include "Poco/Net/SSLManager.h" |
| 23 | #include "Poco/Net/KeyConsoleHandler.h" |
| 24 | #include "Poco/Net/ConsoleCertificateHandler.h" |
| 25 | #include <memory> |
| 26 | #include <iostream> |
| 27 | |
| 28 | |
| 29 | using Poco::URIStreamOpener; |
| 30 | using Poco::StreamCopier; |
| 31 | using Poco::Path; |
| 32 | using Poco::URI; |
| 33 | using Poco::SharedPtr; |
| 34 | using Poco::Exception; |
| 35 | using Poco::Net::HTTPStreamFactory; |
| 36 | using Poco::Net::HTTPSStreamFactory; |
| 37 | using Poco::Net::FTPStreamFactory; |
| 38 | using Poco::Net::SSLManager; |
| 39 | using Poco::Net::Context; |
| 40 | using Poco::Net::KeyConsoleHandler; |
| 41 | using Poco::Net::PrivateKeyPassphraseHandler; |
| 42 | using Poco::Net::InvalidCertificateHandler; |
| 43 | using Poco::Net::ConsoleCertificateHandler; |
| 44 | |
| 45 | |
| 46 | class SSLInitializer |
| 47 | { |
| 48 | public: |
| 49 | SSLInitializer() |
| 50 | { |
| 51 | Poco::Net::initializeSSL(); |
| 52 | } |
| 53 | |
| 54 | ~SSLInitializer() |
| 55 | { |
| 56 | Poco::Net::uninitializeSSL(); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | int main(int argc, char** argv) |
| 62 | { |
| 63 | SSLInitializer sslInitializer; |
| 64 | HTTPStreamFactory::registerFactory(); |
| 65 | HTTPSStreamFactory::registerFactory(); |
| 66 | FTPStreamFactory::registerFactory(); |
| 67 | |
| 68 | if (argc != 2) |
| 69 | { |
| 70 | Path p(argv[0]); |
| 71 | std::cerr << "usage: " << p.getBaseName() << " <uri>" << std::endl; |
| 72 | std::cerr << " Download <uri> to standard output." << std::endl; |
| 73 | std::cerr << " Works with http, https, ftp and file URIs." << std::endl; |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | // Note: we must create the passphrase handler prior Context |
| 78 | SharedPtr<InvalidCertificateHandler> ptrCert = new ConsoleCertificateHandler(false); // ask the user via console |
| 79 | Context::Ptr ptrContext = new Context(Context::CLIENT_USE, "" , "" , "rootcert.pem" , Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH" ); |
| 80 | SSLManager::instance().initializeClient(0, ptrCert, ptrContext); |
| 81 | |
| 82 | try |
| 83 | { |
| 84 | URI uri(argv[1]); |
| 85 | std::unique_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(uri)); |
| 86 | StreamCopier::copyStream(*pStr.get(), std::cout); |
| 87 | } |
| 88 | catch (Exception& exc) |
| 89 | { |
| 90 | std::cerr << exc.displayText() << std::endl; |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |