rippled
SSLHTTPDownloader.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2018 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_NET_SSLHTTPDOWNLOADER_H_INCLUDED
21 #define RIPPLE_NET_SSLHTTPDOWNLOADER_H_INCLUDED
22 
23 #include <ripple/basics/Log.h>
24 #include <ripple/core/Config.h>
25 #include <ripple/net/HTTPClientSSLContext.h>
26 
27 #include <boost/asio/connect.hpp>
28 #include <boost/asio/io_service.hpp>
29 #include <boost/asio/ip/tcp.hpp>
30 #include <boost/asio/spawn.hpp>
31 #include <boost/asio/ssl/error.hpp>
32 #include <boost/asio/ssl/stream.hpp>
33 #include <boost/beast/core.hpp>
34 #include <boost/beast/http.hpp>
35 #include <boost/beast/version.hpp>
36 #include <boost/filesystem.hpp>
37 
38 #include <memory>
39 
40 namespace ripple {
41 
45 {
46 public:
47  using error_code = boost::system::error_code;
48 
50  boost::asio::io_service& io_service,
52  Config const& config);
53 
54  bool
55  download(
56  std::string const& host,
57  std::string const& port,
58  std::string const& target,
59  int version,
60  boost::filesystem::path const& dstPath,
61  std::function<void(boost::filesystem::path)> complete);
62 
63  void
64  onStop();
65 
66  virtual ~SSLHTTPDownloader() = default;
67 
68 protected:
69  using parser = boost::beast::http::basic_parser<false>;
70 
72 
73  void
74  fail(
75  boost::filesystem::path dstPath,
76  std::function<void(boost::filesystem::path)> const& complete,
77  boost::system::error_code const& ec,
78  std::string const& errMsg,
80 
81 private:
83  boost::asio::io_service::strand strand_;
84  boost::optional<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>
86  boost::beast::flat_buffer read_buf_;
88 
89  // Used to protect sessionActive_
93 
94  void
95  do_session(
96  std::string host,
97  std::string port,
98  std::string target,
99  int version,
100  boost::filesystem::path dstPath,
101  std::function<void(boost::filesystem::path)> complete,
102  boost::asio::yield_context yield);
103 
105  getParser(
106  boost::filesystem::path dstPath,
107  std::function<void(boost::filesystem::path)> complete,
108  boost::system::error_code& ec) = 0;
109 
110  virtual bool
111  checkPath(boost::filesystem::path const& dstPath) = 0;
112 
113  virtual void
115 
116  virtual uint64_t
118 };
119 
120 } // namespace ripple
121 
122 #endif
ripple::SSLHTTPDownloader::parser
boost::beast::http::basic_parser< false > parser
Definition: SSLHTTPDownloader.h:69
ripple::SSLHTTPDownloader::j_
const beast::Journal j_
Definition: SSLHTTPDownloader.h:71
ripple::SSLHTTPDownloader::size
virtual uint64_t size(std::shared_ptr< parser > p)=0
std::string
STL class.
std::shared_ptr
STL class.
ripple::SSLHTTPDownloader::~SSLHTTPDownloader
virtual ~SSLHTTPDownloader()=default
ripple::SSLHTTPDownloader::onStop
void onStop()
Definition: SSLHTTPDownloader.cpp:85
ripple::SSLHTTPDownloader::strand_
boost::asio::io_service::strand strand_
Definition: SSLHTTPDownloader.h:83
ripple::SSLHTTPDownloader::checkPath
virtual bool checkPath(boost::filesystem::path const &dstPath)=0
ripple::SSLHTTPDownloader::do_session
void do_session(std::string host, std::string port, std::string target, int version, boost::filesystem::path dstPath, std::function< void(boost::filesystem::path)> complete, boost::asio::yield_context yield)
Definition: SSLHTTPDownloader.cpp:99
std::function
ripple::SSLHTTPDownloader::closeBody
virtual void closeBody(std::shared_ptr< parser > p)=0
ripple::SSLHTTPDownloader::SSLHTTPDownloader
SSLHTTPDownloader(boost::asio::io_service &io_service, beast::Journal j, Config const &config)
Definition: SSLHTTPDownloader.cpp:25
ripple::SSLHTTPDownloader::read_buf_
boost::beast::flat_buffer read_buf_
Definition: SSLHTTPDownloader.h:86
ripple::Config
Definition: Config.h:67
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::atomic< bool >
ripple::SSLHTTPDownloader::getParser
virtual std::shared_ptr< parser > getParser(boost::filesystem::path dstPath, std::function< void(boost::filesystem::path)> complete, boost::system::error_code &ec)=0
memory
ripple::HTTPClientSSLContext
Definition: HTTPClientSSLContext.h:34
ripple::SSLHTTPDownloader::fail
void fail(boost::filesystem::path dstPath, std::function< void(boost::filesystem::path)> const &complete, boost::system::error_code const &ec, std::string const &errMsg, std::shared_ptr< parser > parser)
Definition: SSLHTTPDownloader.cpp:315
ripple::SSLHTTPDownloader::download
bool download(std::string const &host, std::string const &port, std::string const &target, int version, boost::filesystem::path const &dstPath, std::function< void(boost::filesystem::path)> complete)
Definition: SSLHTTPDownloader.cpp:38
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SSLHTTPDownloader::stream_
boost::optional< boost::asio::ssl::stream< boost::asio::ip::tcp::socket > > stream_
Definition: SSLHTTPDownloader.h:85
ripple::SSLHTTPDownloader::c_
std::condition_variable c_
Definition: SSLHTTPDownloader.h:92
ripple::SSLHTTPDownloader::ssl_ctx_
HTTPClientSSLContext ssl_ctx_
Definition: SSLHTTPDownloader.h:82
std::condition_variable
std::mutex
STL class.
ripple::SSLHTTPDownloader::sessionActive_
bool sessionActive_
Definition: SSLHTTPDownloader.h:91
ripple::SSLHTTPDownloader::error_code
boost::system::error_code error_code
Definition: SSLHTTPDownloader.h:47
ripple::SSLHTTPDownloader::cancelDownloads_
std::atomic< bool > cancelDownloads_
Definition: SSLHTTPDownloader.h:87
ripple::SSLHTTPDownloader
Provides an asynchronous HTTPS file downloader.
Definition: SSLHTTPDownloader.h:44
ripple::SSLHTTPDownloader::m_
std::mutex m_
Definition: SSLHTTPDownloader.h:90