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 
44 class HTTPDownloader : public std::enable_shared_from_this<HTTPDownloader>
45 {
46 public:
47  using error_code = boost::system::error_code;
48 
49  bool
50  download(
51  std::string const& host,
52  std::string const& port,
53  std::string const& target,
54  int version,
55  boost::filesystem::path const& dstPath,
56  std::function<void(boost::filesystem::path)> complete,
57  bool ssl = true);
58 
59  void
60  stop();
61 
62  virtual ~HTTPDownloader() = default;
63 
64  bool
65  sessionIsActive() const;
66 
67  bool
68  isStopping() const;
69 
70 protected:
71  // must be accessed through a shared_ptr
72  // use make_XXX functions to create
74  boost::asio::io_service& io_service,
75  Config const& config,
76  beast::Journal j);
77 
78  using parser = boost::beast::http::basic_parser<false>;
79 
81 
82  void
83  fail(
84  boost::filesystem::path dstPath,
85  boost::system::error_code const& ec,
86  std::string const& errMsg,
88 
89 private:
90  Config const& config_;
91  boost::asio::io_service::strand strand_;
93  boost::beast::flat_buffer read_buf_;
95 
96  // Used to protect sessionActive_
97  mutable std::mutex m_;
100 
101  void
102  do_session(
103  std::string host,
104  std::string port,
105  std::string target,
106  int version,
107  boost::filesystem::path dstPath,
108  std::function<void(boost::filesystem::path)> complete,
109  bool ssl,
110  boost::asio::yield_context yield);
111 
113  getParser(
114  boost::filesystem::path dstPath,
115  std::function<void(boost::filesystem::path)> complete,
116  boost::system::error_code& ec) = 0;
117 
118  virtual bool
119  checkPath(boost::filesystem::path const& dstPath) = 0;
120 
121  virtual void
123 
124  virtual uint64_t
126 };
127 
128 } // namespace ripple
129 
130 #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::HTTPDownloader
HTTPDownloader(boost::asio::io_service &io_service, Config const &config, beast::Journal j)
Definition: HTTPDownloader.cpp:25
ripple::HTTPDownloader::c_
std::condition_variable c_
Definition: HTTPDownloader.h:99
std::string
STL class.
std::shared_ptr
STL class.
ripple::HTTPDownloader::read_buf_
boost::beast::flat_buffer read_buf_
Definition: HTTPDownloader.h:93
ripple::HTTPDownloader::closeBody
virtual void closeBody(std::shared_ptr< parser > p)=0
std::function
ripple::HTTPDownloader::stop_
std::atomic< bool > stop_
Definition: HTTPDownloader.h:94
ripple::HTTPDownloader::stop
void stop()
Definition: HTTPDownloader.cpp:284
ripple::HTTPDownloader::m_
std::mutex m_
Definition: HTTPDownloader.h:97
ripple::HTTPDownloader::stream_
std::unique_ptr< HTTPStream > stream_
Definition: HTTPDownloader.h:92
ripple::Config
Definition: Config.h:89
ripple::HTTPDownloader::fail
void fail(boost::filesystem::path dstPath, boost::system::error_code const &ec, std::string const &errMsg, std::shared_ptr< parser > parser)
Definition: HTTPDownloader.cpp:311
ripple::HTTPDownloader::config_
Config const & config_
Definition: HTTPDownloader.h:90
std::enable_shared_from_this
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::atomic< bool >
memory
ripple::HTTPDownloader::sessionIsActive
bool sessionIsActive() const
Definition: HTTPDownloader.cpp:297
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::isStopping
bool isStopping() const
Definition: HTTPDownloader.cpp:304
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:80
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:78
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:87
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:98
ripple::HTTPDownloader::strand_
boost::asio::io_service::strand strand_
Definition: HTTPDownloader.h:91