rippled
DatabaseDownloader.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 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 #include <ripple/net/DatabaseDownloader.h>
21 
22 namespace ripple {
23 
26  boost::asio::io_service& io_service,
27  Config const& config,
29 {
31  new DatabaseDownloader(io_service, config, j));
32 }
33 
35  boost::asio::io_service& io_service,
36  Config const& config,
38  : HTTPDownloader(io_service, config, j)
39  , config_(config)
40  , io_service_(io_service)
41 {
42 }
43 
44 auto
46  boost::filesystem::path dstPath,
47  std::function<void(boost::filesystem::path)> complete,
48  boost::system::error_code& ec) -> std::shared_ptr<parser>
49 {
50  using namespace boost::beast;
51 
52  auto p = std::make_shared<http::response_parser<DatabaseBody>>();
54  p->get().body().open(dstPath, config_, io_service_, ec);
55 
56  if (ec)
57  p->get().body().close();
58 
59  return p;
60 }
61 
62 bool
63 DatabaseDownloader::checkPath(boost::filesystem::path const& dstPath)
64 {
65  return dstPath.string().size() <= MAX_PATH_LEN;
66 }
67 
68 void
70 {
71  using namespace boost::beast;
72 
73  auto databaseBodyParser =
74  std::dynamic_pointer_cast<http::response_parser<DatabaseBody>>(p);
75  assert(databaseBodyParser);
76 
77  databaseBodyParser->get().body().close();
78 }
79 
82 {
83  using namespace boost::beast;
84 
85  auto databaseBodyParser =
86  std::dynamic_pointer_cast<http::response_parser<DatabaseBody>>(p);
87  assert(databaseBodyParser);
88 
89  return databaseBodyParser->get().body().size();
90 }
91 
92 } // namespace ripple
std::shared_ptr
STL class.
ripple::DatabaseDownloader::checkPath
bool checkPath(boost::filesystem::path const &dstPath) override
Definition: DatabaseDownloader.cpp:63
std::shared_ptr::get
T get(T... args)
std::function
ripple::DatabaseDownloader::closeBody
void closeBody(std::shared_ptr< parser > p) override
Definition: DatabaseDownloader.cpp:69
ripple::Config
Definition: Config.h:89
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint64_t
ripple::DatabaseDownloader::DatabaseDownloader
DatabaseDownloader(boost::asio::io_service &io_service, Config const &config, beast::Journal j)
Definition: DatabaseDownloader.cpp:34
ripple::DatabaseDownloader::MAX_PATH_LEN
static const std::uint8_t MAX_PATH_LEN
Definition: DatabaseDownloader.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::DatabaseDownloader::getParser
std::shared_ptr< parser > getParser(boost::filesystem::path dstPath, std::function< void(boost::filesystem::path)> complete, boost::system::error_code &ec) override
Definition: DatabaseDownloader.cpp:45
ripple::DatabaseDownloader::size
std::uint64_t size(std::shared_ptr< parser > p) override
Definition: DatabaseDownloader.cpp:81
ripple::DatabaseDownloader
Definition: DatabaseDownloader.h:28
ripple::HTTPDownloader
Provides an asynchronous HTTP[S] file downloader.
Definition: HTTPDownloader.h:44
ripple::make_DatabaseDownloader
std::shared_ptr< DatabaseDownloader > make_DatabaseDownloader(boost::asio::io_service &io_service, Config const &config, beast::Journal j)
Definition: DatabaseDownloader.cpp:25
std::numeric_limits