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  : public std::enable_shared_from_this <SSLHTTPDownloader>
46 {
47 public:
48  using error_code = boost::system::error_code;
49 
51  boost::asio::io_service& io_service,
53  Config const& config,
54  bool isPaused = false);
55 
56  bool
57  download(
58  std::string const& host,
59  std::string const& port,
60  std::string const& target,
61  int version,
62  boost::filesystem::path const& dstPath,
63  std::function<void(boost::filesystem::path)> complete);
64 
65  void
66  onStop();
67 
68  virtual
69  ~SSLHTTPDownloader() = default;
70 
71 protected:
72 
73  using parser = boost::beast::http::basic_parser<false>;
74 
76 
77  bool
78  fail(
79  boost::filesystem::path dstPath,
80  std::function<void(boost::filesystem::path)> const& complete,
81  boost::system::error_code const& ec,
82  std::string const& errMsg,
84 
85 private:
87  boost::asio::io_service::strand strand_;
88  boost::optional<
89  boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> stream_;
90  boost::beast::flat_buffer read_buf_;
95 
96  void
97  do_session(
98  std::string host,
99  std::string port,
100  std::string target,
101  int version,
102  boost::filesystem::path dstPath,
103  std::function<void(boost::filesystem::path)> complete,
104  boost::asio::yield_context yield);
105 
106  virtual
108  getParser(
109  boost::filesystem::path dstPath,
110  std::function<void(boost::filesystem::path)> complete,
111  boost::system::error_code & ec) = 0;
112 
113  virtual
114  bool
115  checkPath(
116  boost::filesystem::path const& dstPath) = 0;
117 
118  virtual
119  void
121 
122  virtual
123  uint64_t
125 };
126 
127 } // ripple
128 
129 #endif
ripple::SSLHTTPDownloader::parser
boost::beast::http::basic_parser< false > parser
Definition: SSLHTTPDownloader.h:73
ripple::SSLHTTPDownloader::j_
const beast::Journal j_
Definition: SSLHTTPDownloader.h:75
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:78
ripple::SSLHTTPDownloader::strand_
boost::asio::io_service::strand strand_
Definition: SSLHTTPDownloader.h:87
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:96
ripple::SSLHTTPDownloader::fail
bool 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=nullptr)
Definition: SSLHTTPDownloader.cpp:328
std::function
ripple::SSLHTTPDownloader::closeBody
virtual void closeBody(std::shared_ptr< parser > p)=0
ripple::SSLHTTPDownloader::read_buf_
boost::beast::flat_buffer read_buf_
Definition: SSLHTTPDownloader.h:90
ripple::SSLHTTPDownloader::isStopped_
std::atomic< bool > isStopped_
Definition: SSLHTTPDownloader.h:91
ripple::Config
Definition: Config.h:67
std::enable_shared_from_this
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
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::SSLHTTPDownloader::SSLHTTPDownloader
SSLHTTPDownloader(boost::asio::io_service &io_service, beast::Journal j, Config const &config, bool isPaused=false)
Definition: SSLHTTPDownloader.cpp:25
ripple::HTTPClientSSLContext
Definition: HTTPClientSSLContext.h:34
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:39
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:89
ripple::SSLHTTPDownloader::c_
std::condition_variable c_
Definition: SSLHTTPDownloader.h:94
ripple::SSLHTTPDownloader::ssl_ctx_
HTTPClientSSLContext ssl_ctx_
Definition: SSLHTTPDownloader.h:86
std::condition_variable
std::mutex
STL class.
ripple::SSLHTTPDownloader::sessionActive_
bool sessionActive_
Definition: SSLHTTPDownloader.h:92
ripple::SSLHTTPDownloader::error_code
boost::system::error_code error_code
Definition: SSLHTTPDownloader.h:48
ripple::SSLHTTPDownloader
Provides an asynchronous HTTPS file downloader.
Definition: SSLHTTPDownloader.h:44
ripple::SSLHTTPDownloader::m_
std::mutex m_
Definition: SSLHTTPDownloader.h:93