Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HttpDownloader::HttpDownloader(boost::asio::io_context& io_context)
- : resolver_(io_context),
- ssl_context_(boost::asio::ssl::context::tlsv12_client),
- stream_(io_context, ssl_context_)
- {
- // Nothing
- }
- HttpDownloader::~HttpDownloader() = default;
- // static
- std::shared_ptr<HttpDownloader> HttpDownloader::create(boost::asio::io_context& io_context)
- {
- return std::shared_ptr<HttpDownloader>(new HttpDownloader(io_context));
- }
- void HttpDownloader::start(const std::string& file_url,
- const std::filesystem::path& file_path,
- Delegate* delegate)
- {
- DCHECK(delegate);
- file_path_ = file_path;
- delegate_ = delegate;
- //network::uri url(file_url);
- const char host[] = "update.aspia.org";
- const char path[] = "/version.xml";
- const char port[] = "443";
- // Set SNI Hostname (many hosts need this to handshake successfully).
- if (!SSL_set_tlsext_host_name(stream_.native_handle(), host))
- {
- LOG(LS_WARNING) << "SSL_set_tlsext_host_name failed";
- return;
- }
- stream_.set_verify_mode(boost::asio::ssl::verify_none);
- // Set up an HTTP GET request message.
- request_.version(11); // Using HTTP/1.1.
- request_.method(boost::beast::http::verb::get);
- request_.target(path);
- request_.set(boost::beast::http::field::host, host);
- request_.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);
- // Look up the domain name.
- resolver_.async_resolve(
- host,
- port,
- std::bind(&HttpDownloader::onResolve,
- shared_from_this(),
- std::placeholders::_1,
- std::placeholders::_2));
- }
- void HttpDownloader::onResolve(const boost::system::error_code& error_code,
- boost::asio::ip::tcp::resolver::results_type results)
- {
- if (error_code)
- {
- delegate_->onErrorOccurred(ErrorCode::URL_OPEN_ERROR);
- return;
- }
- // Make the connection on the IP address we get from a lookup.
- boost::asio::async_connect(
- stream_.next_layer(),
- results.begin(),
- results.end(),
- std::bind(&HttpDownloader::onConnect,
- shared_from_this(),
- std::placeholders::_1));
- }
- void HttpDownloader::onConnect(const boost::system::error_code& error_code)
- {
- if (error_code)
- {
- delegate_->onErrorOccurred(ErrorCode::URL_OPEN_ERROR);
- return;
- }
- // Perform the SSL handshake.
- stream_.async_handshake(
- boost::asio::ssl::stream_base::client,
- std::bind(&HttpDownloader::onHandshake,
- shared_from_this(),
- std::placeholders::_1));
- }
- void HttpDownloader::onHandshake(const boost::system::error_code& error_code)
- {
- if (error_code)
- {
- delegate_->onErrorOccurred(ErrorCode::URL_OPEN_ERROR);
- return;
- }
- // Send the HTTP request to the remote host.
- boost::beast::http::async_write(
- stream_,
- request_,
- std::bind(&HttpDownloader::onWrite,
- shared_from_this(),
- std::placeholders::_1,
- std::placeholders::_2));
- }
- void HttpDownloader::onWrite(const boost::system::error_code& error_code,
- size_t /* bytes_transferred */)
- {
- if (error_code)
- {
- delegate_->onErrorOccurred(ErrorCode::URL_READ_ERROR);
- return;
- }
- boost::beast::http::file_body::value_type file;
- boost::beast::error_code ec;
- file.open(file_path_.string().c_str(), boost::beast::file_mode::write, ec);
- if (!file.is_open())
- {
- delegate_->onErrorOccurred(ErrorCode::FILE_OPEN_ERROR);
- return;
- }
- response_.body() = std::move(file);
- // Receive the HTTP response
- boost::beast::http::async_read(
- stream_,
- buffer_,
- response_,
- std::bind(&HttpDownloader::onRead,
- shared_from_this(),
- std::placeholders::_1,
- std::placeholders::_2));
- }
- void HttpDownloader::onRead(const boost::system::error_code& error_code,
- size_t /* bytes_transferred */)
- {
- if (error_code)
- {
- delegate_->onErrorOccurred(ErrorCode::URL_READ_ERROR);
- return;
- }
- // Gracefully close the stream.
- stream_.async_shutdown(std::bind(&HttpDownloader::onShutdown,
- shared_from_this(),
- std::placeholders::_1));
- }
- void HttpDownloader::onShutdown(const boost::system::error_code& error_code)
- {
- if (error_code && (error_code != boost::asio::ssl::error::stream_truncated &&
- error_code != boost::asio::error::eof))
- {
- delegate_->onErrorOccurred(ErrorCode::URL_READ_ERROR);
- return;
- }
- delegate_->onFinished();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement