rippled
HTTPDownloader.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_HTTPDOWNLOADER_H_INCLUDED
21 #define RIPPLE_NET_HTTPDOWNLOADER_H_INCLUDED
22 
23 #include <ripple/basics/Log.h>
24 #include <ripple/core/Config.h>
25 #include <ripple/net/HTTPStream.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  bool ssl = true);
63 
64  void
65  onStop();
66 
67  virtual ~HTTPDownloader() = default;
68 
69 protected:
70  using parser = boost::beast::http::basic_parser<false>;
71 
73 
74  void
75  fail(
76  boost::filesystem::path dstPath,
77  std::function<void(boost::filesystem::path)> const& complete,
78  boost::system::error_code const& ec,
79  std::string const& errMsg,
81 
82 private:
83  Config const& config_;
84  boost::asio::io_service::strand strand_;
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  bool ssl,
103  boost::asio::yield_context yield);
104 
106  getParser(
107  boost::filesystem::path dstPath,
108  std::function<void(boost::filesystem::path)> complete,
109  boost::system::error_code& ec) = 0;
110 
111  virtual bool
112  checkPath(boost::filesystem::path const& dstPath) = 0;
113 
114  virtual void
116 
117  virtual uint64_t
119 };
120 
121 } // namespace ripple
122 
123 #endif
ripple::HTTPDownloader::getParser
virtual std::shared_ptr< parser > getParser(boost::filesystem::path dstPath, std::function< void(boost::filesystem::path)> complete, boost::system::error_code &ec)=0
ripple::HTTPDownloader::checkPath
virtual bool checkPath(boost::filesystem::path const &dstPath)=0
ripple::HTTPDownloader::c_
std::condition_variable c_
Definition: HTTPDownloader.h:92
std::string
STL class.
std::shared_ptr
STL class.
ripple::HTTPDownloader::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: HTTPDownloader.cpp:296
ripple::HTTPDownloader::read_buf_
boost::beast::flat_buffer read_buf_
Definition: HTTPDownloader.h:86
ripple::HTTPDownloader::closeBody
virtual void closeBody(std::shared_ptr< parser > p)=0
std::function
ripple::HTTPDownloader::m_
std::mutex m_
Definition: HTTPDownloader.h:90
ripple::HTTPDownloader::stream_
std::unique_ptr< HTTPStream > stream_
Definition: HTTPDownloader.h:85
ripple::HTTPDownloader::HTTPDownloader
HTTPDownloader(boost::asio::io_service &io_service, beast::Journal j, Config const &config)
Definition: HTTPDownloader.cpp:25
ripple::Config
Definition: Config.h:67
ripple::HTTPDownloader::config_
Config const & config_
Definition: HTTPDownloader.h:83
ripple::HTTPDownloader::cancelDownloads_
std::atomic< bool > cancelDownloads_
Definition: HTTPDownloader.h:87
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::atomic< bool >
memory
ripple::HTTPDownloader::error_code
boost::system::error_code error_code
Definition: HTTPDownloader.h:47
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::HTTPDownloader::onStop
void onStop()
Definition: HTTPDownloader.cpp:88
ripple::HTTPDownloader::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, bool ssl=true)
Definition: HTTPDownloader.cpp:38
std::condition_variable
ripple::HTTPDownloader::j_
const beast::Journal j_
Definition: HTTPDownloader.h:72
std::mutex
STL class.
ripple::HTTPDownloader::~HTTPDownloader
virtual ~HTTPDownloader()=default
ripple::HTTPDownloader
Provides an asynchronous HTTP[S] file downloader.
Definition: HTTPDownloader.h:44
ripple::HTTPDownloader::parser
boost::beast::http::basic_parser< false > parser
Definition: HTTPDownloader.h:70
ripple::HTTPDownloader::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, bool ssl, boost::asio::yield_context yield)
Definition: HTTPDownloader.cpp:102
std::unique_ptr
STL class.
ripple::HTTPDownloader::size
virtual uint64_t size(std::shared_ptr< parser > p)=0
ripple::HTTPDownloader::sessionActive_
bool sessionActive_
Definition: HTTPDownloader.h:91
ripple::HTTPDownloader::strand_
boost::asio::io_service::strand strand_
Definition: HTTPDownloader.h:84