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  onStop();
61 
62  virtual ~HTTPDownloader() = default;
63 
64 protected:
65  // must be accessed through a shared_ptr
66  // use make_XXX functions to create
68  boost::asio::io_service& io_service,
69  Config const& config,
70  beast::Journal j);
71 
72  using parser = boost::beast::http::basic_parser<false>;
73 
75 
76  void
77  fail(
78  boost::filesystem::path dstPath,
79  boost::system::error_code const& ec,
80  std::string const& errMsg,
82 
83 private:
84  Config const& config_;
85  boost::asio::io_service::strand strand_;
87  boost::beast::flat_buffer read_buf_;
89 
90  // Used to protect sessionActive_
94 
95  void
96  do_session(
97  std::string host,
98  std::string port,
99  std::string target,
100  int version,
101  boost::filesystem::path dstPath,
102  std::function<void(boost::filesystem::path)> complete,
103  bool ssl,
104  boost::asio::yield_context yield);
105 
107  getParser(
108  boost::filesystem::path dstPath,
109  std::function<void(boost::filesystem::path)> complete,
110  boost::system::error_code& ec) = 0;
111 
112  virtual bool
113  checkPath(boost::filesystem::path const& dstPath) = 0;
114 
115  virtual void
117 
118  virtual uint64_t
120 };
121 
122 } // namespace ripple
123 
124 #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:93
std::string
STL class.
std::shared_ptr
STL class.
ripple::HTTPDownloader::read_buf_
boost::beast::flat_buffer read_buf_
Definition: HTTPDownloader.h:87
ripple::HTTPDownloader::closeBody
virtual void closeBody(std::shared_ptr< parser > p)=0
std::function
ripple::HTTPDownloader::stop_
std::atomic< bool > stop_
Definition: HTTPDownloader.h:88
ripple::HTTPDownloader::m_
std::mutex m_
Definition: HTTPDownloader.h:91
ripple::HTTPDownloader::stream_
std::unique_ptr< HTTPStream > stream_
Definition: HTTPDownloader.h:86
ripple::Config
Definition: Config.h:69
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:296
ripple::HTTPDownloader::config_
Config const & config_
Definition: HTTPDownloader.h:84
std::enable_shared_from_this
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:283
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:74
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:72
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:92
ripple::HTTPDownloader::strand_
boost::asio::io_service::strand strand_
Definition: HTTPDownloader.h:85