diff options
Diffstat (limited to 'apps/higet/higet.cc')
-rw-r--r-- | apps/higet/higet.cc | 116 |
1 files changed, 49 insertions, 67 deletions
diff --git a/apps/higet/higet.cc b/apps/higet/higet.cc index 7584148a9..77cbb52d2 100644 --- a/apps/higet/higet.cc +++ b/apps/higet/higet.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -13,25 +13,20 @@ * limitations under the License. */ +#include <hicn/apps/utils/logger.h> #include <hicn/transport/http/client_connection.h> -#include <fstream> -#include <map> +#include <hicn/transport/utils/chrono_typedefs.h> + #include <algorithm> +#include <asio.hpp> +#include <fstream> #include <functional> +#include <map> +#ifndef ASIO_STANDALONE #define ASIO_STANDALONE +#endif #include <asio.hpp> -#undef ASIO_STANDALONE - -#include <thread> - -typedef std::chrono::time_point<std::chrono::system_clock> Time; -typedef std::chrono::milliseconds TimeDuration; - -Time t1; - -#define DEFAULT_BETA 0.99 -#define DEFAULT_GAMMA 0.07 namespace http { @@ -47,16 +42,13 @@ class ReadBytesCallbackImplementation static std::string chunk_separator; public: - ReadBytesCallbackImplementation(std::string file_name, long yet_downloaded) + ReadBytesCallbackImplementation(const std::string &file_name, + long yet_downloaded) : file_name_(file_name), temp_file_name_(file_name_ + ".temp"), yet_downloaded_(yet_downloaded), byte_downloaded_(yet_downloaded), - chunked_(false), - chunk_size_(0), - work_(std::make_unique<asio::io_service::work>(io_service_)), - thread_( - std::make_unique<std::thread>([this]() { io_service_.run(); })) { + work_(std::make_unique<asio::io_service::work>(io_service_)) { std::streambuf *buf; if (file_name_ != "-") { of_.open(temp_file_name_, std::ofstream::binary | std::ofstream::app); @@ -68,12 +60,6 @@ class ReadBytesCallbackImplementation out_ = new std::ostream(buf); } - ~ReadBytesCallbackImplementation() { - if (thread_->joinable()) { - thread_->join(); - } - } - void onBytesReceived(std::unique_ptr<utils::MemBuf> &&buffer) { auto buffer_ptr = buffer.release(); io_service_.post([this, buffer_ptr]() { @@ -101,7 +87,7 @@ class ReadBytesCallbackImplementation if (chunked_) { if (chunk_size_ > 0) { - out_->write((char *)payload->data(), chunk_size_); + out_->write((char *)payload->writableData(), chunk_size_); payload->trimStart(chunk_size_); if (payload->length() >= chunk_separator.size()) { @@ -118,7 +104,7 @@ class ReadBytesCallbackImplementation auto it = std::search(begin, end, begincrlf2, endcrlf2); if (it != end) { chunk_size_ = std::stoul(begin, 0, 16); - content_size_ += chunk_size_; + content_size_ += (long)chunk_size_; payload->trimStart(it + chunk_separator.size() - begin); std::size_t to_write; @@ -129,8 +115,8 @@ class ReadBytesCallbackImplementation chunk_size_ -= payload->length(); } - out_->write((char *)payload->data(), to_write); - byte_downloaded_ += to_write; + out_->write((char *)payload->writableData(), to_write); + byte_downloaded_ += (long)to_write; payload->trimStart(to_write); if (payload->length() >= chunk_separator.size()) { @@ -139,8 +125,8 @@ class ReadBytesCallbackImplementation } } } else { - out_->write((char *)payload->data(), payload->length()); - byte_downloaded_ += payload->length(); + out_->write((char *)payload->writableData(), payload->length()); + byte_downloaded_ += (long)payload->length(); } if (file_name_ != "-") { @@ -174,13 +160,13 @@ class ReadBytesCallbackImplementation } print_bar(100, 100, true); - std::cout << "\nDownloaded " << bytes << " bytes" << std::endl; + LoggerInfo() << "\nDownloaded " << bytes << " bytes"; } work_.reset(); }); } - void onError(const std::error_code ec) { + void onError(const std::error_code &ec) { io_service_.post([this]() { of_.close(); delete out_; @@ -207,7 +193,7 @@ class ReadBytesCallbackImplementation #endif std::cout << "["; - int pos = barWidth * progress; + int pos = barWidth * (int)progress; for (int i = 0; i < barWidth; ++i) { if (i < pos) { std::cout << "="; @@ -218,15 +204,13 @@ class ReadBytesCallbackImplementation } } if (last) { - std::cout << "] " << int(progress * 100.0) << " %" << std::endl - << std::endl; + std::cout << "] " << int(progress * 100.0) << " %"; } else { std::cout << "] " << int(progress * 100.0) << " %\r"; std::cout.flush(); } } - private: std::string file_name_; std::string temp_file_name_; std::ostream *out_; @@ -235,41 +219,36 @@ class ReadBytesCallbackImplementation long content_size_; bool first_chunk_read_ = false; long byte_downloaded_ = 0; - bool chunked_; - std::size_t chunk_size_; + bool chunked_ = false; + std::size_t chunk_size_ = 0; asio::io_service io_service_; std::unique_ptr<asio::io_service::work> work_; - std::unique_ptr<std::thread> thread_; }; std::string ReadBytesCallbackImplementation::chunk_separator = "\r\n"; -long checkFileStatus(std::string file_name) { +long checkFileStatus(const std::string &file_name) { struct stat stat_buf; std::string temp_file_name_ = file_name + ".temp"; int rc = stat(temp_file_name_.c_str(), &stat_buf); return rc == 0 ? stat_buf.st_size : -1; } -void usage(char *program_name) { - std::cerr << "usage:" << std::endl; - std::cerr << program_name << " [option]... [url]..." << std::endl; - std::cerr << program_name << " options:" << std::endl; - std::cerr - << "-O <out_put_path> = write documents to <out_put_file>" - << std::endl; - std::cerr << "-S = print server response" - << std::endl; - std::cerr << "-P = first word of the ipv6 name of " - "the response" - << std::endl; - std::cerr << "example:" << std::endl; - std::cerr << "\t" << program_name << " -O - http://origin/index.html" - << std::endl; - exit(EXIT_FAILURE); +void usage(const char *program_name) { + LoggerInfo() << "usage:"; + LoggerInfo() << program_name << " [option]... [url]..."; + LoggerInfo() << program_name << " options:"; + LoggerInfo() + << "-O <out_put_path> = write documents to <out_put_file>"; + LoggerInfo() << "-S = print server response"; + LoggerInfo() + << "-P = first word of the ipv6 name of " + "the response"; + LoggerInfo() << "example:"; + LoggerInfo() << "\t" << program_name << " -O - http://origin/index.html"; } -int main(int argc, char **argv) { +int http_main(int argc, char **argv) { #ifdef _WIN32 WSADATA wsaData = {0}; WSAStartup(MAKEWORD(2, 2), &wsaData); @@ -298,20 +277,20 @@ int main(int argc, char **argv) { case 'P': conf.ipv6_first_word = optarg; break; - case 'h': default: usage(argv[0]); - break; + exit(EXIT_FAILURE); } } if (!argv[optind]) { usage(argv[0]); + exit(EXIT_FAILURE); } name = argv[optind]; - std::cerr << "Using name " << name << " and name first word " - << conf.ipv6_first_word << std::endl; + LoggerInfo() << "Using name " << name << " and name first word " + << conf.ipv6_first_word; if (conf.file_name.empty()) { conf.file_name = name.substr(1 + name.find_last_of("/")); @@ -334,13 +313,16 @@ int main(int argc, char **argv) { {"Connection", "Keep-Alive"}, {"Range", range}}; } + transport::http::HTTPClientConnection connection; + if (!conf.producer_certificate.empty()) { - connection.setCertificate(conf.producer_certificate); + std::shared_ptr<transport::auth::Verifier> verifier = + std::make_shared<transport::auth::AsymmetricVerifier>( + conf.producer_certificate); + connection.setVerifier(verifier); } - t1 = std::chrono::system_clock::now(); - http::ReadBytesCallbackImplementation readBytesCallback(conf.file_name, yetDownloaded); @@ -356,4 +338,4 @@ int main(int argc, char **argv) { } // end namespace http -int main(int argc, char **argv) { return http::main(argc, argv); } +int main(int argc, char **argv) { return http::http_main(argc, argv); } |