From b96d1545a8d2a173dc5911ed0bca3e04dbb02176 Mon Sep 17 00:00:00 2001 From: Michele Papalini Date: Wed, 1 Feb 2023 17:25:21 +0100 Subject: fix(apps): fix issues reported by sonarqube Ref: HICN-836 Signed-off-by: Michele Papalini Change-Id: Ie76771ca75bd224084ce3c893aba661b97064419 --- .../includes/hicn/http-proxy/forwarder_interface.h | 23 ++++------ .../includes/hicn/http-proxy/http_proxy.h | 13 +++--- .../includes/hicn/http-proxy/http_session.h | 24 +++++----- .../includes/hicn/http-proxy/icn_receiver.h | 10 ++--- apps/http-proxy/includes/hicn/http-proxy/utils.h | 4 +- apps/http-proxy/main.cc | 5 +-- apps/http-proxy/src/forwarder_interface.cc | 10 ++--- apps/http-proxy/src/http_1x_message_fast_parser.cc | 5 ++- apps/http-proxy/src/http_proxy.cc | 45 +++++++++---------- apps/http-proxy/src/http_session.cc | 52 +++++++++------------- apps/http-proxy/src/icn_receiver.cc | 12 +++-- 11 files changed, 91 insertions(+), 112 deletions(-) (limited to 'apps/http-proxy') diff --git a/apps/http-proxy/includes/hicn/http-proxy/forwarder_interface.h b/apps/http-proxy/includes/hicn/http-proxy/forwarder_interface.h index c60e26c63..ae9562a12 100644 --- a/apps/http-proxy/includes/hicn/http-proxy/forwarder_interface.h +++ b/apps/http-proxy/includes/hicn/http-proxy/forwarder_interface.h @@ -52,16 +52,11 @@ using RouteInfoPtr = std::shared_ptr; class ForwarderInterface { public: - ForwarderInterface(asio::io_service &io_service) + explicit ForwarderInterface(asio::io_service &io_service) : external_ioservice_(io_service), work_(std::make_unique(internal_ioservice_)), - sock_(nullptr), thread_(std::make_unique( - [this]() { internal_ioservice_.run(); })), - check_routes_timer_(nullptr), - pending_add_route_counter_(0), - route_id_(0), - closed_(false) {} + [this]() { internal_ioservice_.run(); })) {} ~ForwarderInterface(); @@ -95,20 +90,20 @@ class ForwarderInterface { void internalCreateFaceAndRoute(RouteInfoPtr route_info, uint8_t max_try, asio::steady_timer *timer, - SetRouteCallback callback); + const SetRouteCallback &callback); - int tryToCreateFaceAndRoute(route_info_t *route_info); + int tryToCreateFaceAndRoute(const route_info_t *route_info); asio::io_service &external_ioservice_; asio::io_service internal_ioservice_; std::unique_ptr work_; - hc_sock_t *sock_; + hc_sock_t *sock_ = nullptr; std::unique_ptr thread_; std::unordered_map route_status_; - std::unique_ptr check_routes_timer_; - uint32_t pending_add_route_counter_; - uint32_t route_id_; - bool closed_; + std::unique_ptr check_routes_timer_ = nullptr; + uint32_t pending_add_route_counter_ = 0; + uint32_t route_id_ = 0; + bool closed_ = false; }; } // namespace transport diff --git a/apps/http-proxy/includes/hicn/http-proxy/http_proxy.h b/apps/http-proxy/includes/hicn/http-proxy/http_proxy.h index 1fa96956a..567ab4540 100644 --- a/apps/http-proxy/includes/hicn/http-proxy/http_proxy.h +++ b/apps/http-proxy/includes/hicn/http-proxy/http_proxy.h @@ -34,7 +34,8 @@ class TcpListener { public: using AcceptCallback = std::function; - TcpListener(asio::io_service& io_service, short port, AcceptCallback callback) + TcpListener(asio::io_service& io_service, short port, + const AcceptCallback& callback) : acceptor_(io_service), #if ((ASIO_VERSION / 100 % 1000) < 12) socket_(io_service), @@ -49,7 +50,6 @@ class TcpListener { acceptor_.listen(); } - public: void doAccept() { #if ((ASIO_VERSION / 100 % 1000) >= 12) acceptor_.async_accept( @@ -80,7 +80,7 @@ class HTTPClientConnectionCallback; class Receiver { public: - Receiver() : thread_() {} + Receiver() {} virtual ~Receiver() = default; void stopAndJoinThread() { thread_.stop(); } virtual void stop() = 0; @@ -115,13 +115,13 @@ class TcpReceiver : public Receiver { std::deque http_clients_; std::unordered_set used_http_clients_; ForwarderConfig forwarder_config_; - bool stopped_; + bool stopped_ = false; }; class IcnReceiver : public Receiver { public: template - IcnReceiver(Args&&... args) + explicit IcnReceiver(Args&&... args) : Receiver(), icn_consum_producer_(thread_.getIoService(), std::forward(args)...) { @@ -148,6 +148,7 @@ class HTTPProxy { std::string prefix; std::string first_ipv6_word; + virtual ~CommonParams() {} virtual void printParams() { LoggerInfo() << "Parameters: "; }; }; @@ -206,4 +207,4 @@ class HTTPProxy { asio::signal_set signals_; }; -} // namespace transport \ No newline at end of file +} // namespace transport diff --git a/apps/http-proxy/includes/hicn/http-proxy/http_session.h b/apps/http-proxy/includes/hicn/http-proxy/http_session.h index 1563431dc..1b7df96a6 100644 --- a/apps/http-proxy/includes/hicn/http-proxy/http_session.h +++ b/apps/http-proxy/includes/hicn/http-proxy/http_session.h @@ -73,13 +73,16 @@ class HTTPSession { }; public: - HTTPSession(asio::io_service &io_service, std::string &ip_address, - std::string &port, ContentReceivedCallback receive_callback, - OnConnectionClosed on_reconnect_callback, bool client = false); + HTTPSession(asio::io_service &io_service, const std::string &ip_address, + const std::string &port, + const ContentReceivedCallback &receive_callback, + const OnConnectionClosed &on_reconnect_callback, + bool client = false); HTTPSession(asio::ip::tcp::socket socket, - ContentReceivedCallback receive_callback, - OnConnectionClosed on_reconnect_callback, bool client = true); + const ContentReceivedCallback &receive_callback, + const OnConnectionClosed &on_reconnect_callback, + bool client = true); ~HTTPSession(); @@ -103,7 +106,6 @@ class HTTPSession { bool checkConnected(); - private: void handleRead(const std::error_code &ec, std::size_t length); void tryReconnection(); void startConnectionTimer(); @@ -120,14 +122,14 @@ class HTTPSession { asio::streambuf input_buffer_; bool reverse_; - bool is_reconnection_; - bool data_available_; + bool is_reconnection_ = false; + bool data_available_ = false; - std::size_t content_length_; + std::size_t content_length_ = 0; // Chunked encoding - bool is_last_chunk_; - bool chunked_; + bool is_last_chunk_ = false; + bool chunked_ = false; ContentReceivedCallback receive_callback_; OnConnectionClosed on_connection_closed_callback_; diff --git a/apps/http-proxy/includes/hicn/http-proxy/icn_receiver.h b/apps/http-proxy/includes/hicn/http-proxy/icn_receiver.h index a402d44cf..e3ab97fdd 100644 --- a/apps/http-proxy/includes/hicn/http-proxy/icn_receiver.h +++ b/apps/http-proxy/includes/hicn/http-proxy/icn_receiver.h @@ -24,7 +24,6 @@ #include #include #include -//#include "http_session.h" namespace transport { @@ -48,9 +47,7 @@ class AsyncConsumerProducer { const std::string& content_lifetime, bool manifest) : AsyncConsumerProducer(internal_io_service_, prefix, first_ipv6_word, origin_address, origin_port, cache_size, mtu, - content_lifetime, manifest) { - external_io_service_ = false; - } + content_lifetime, manifest) {} void run(); @@ -72,7 +69,7 @@ class AsyncConsumerProducer { core::Prefix prefix_; asio::io_service& io_service_; asio::io_service internal_io_service_; - bool external_io_service_; + bool external_io_service_ = false; interface::ProducerSocket producer_socket_; std::string ip_address_; @@ -80,9 +77,8 @@ class AsyncConsumerProducer { uint32_t cache_size_; uint32_t mtu_; - uint64_t request_counter_; + uint64_t request_counter_ = 0; - // std::unordered_map> // connection_map_; HTTPSession connector_; diff --git a/apps/http-proxy/includes/hicn/http-proxy/utils.h b/apps/http-proxy/includes/hicn/http-proxy/utils.h index 0df24dfd9..9865d8e4c 100644 --- a/apps/http-proxy/includes/hicn/http-proxy/utils.h +++ b/apps/http-proxy/includes/hicn/http-proxy/utils.h @@ -35,7 +35,7 @@ TRANSPORT_ALWAYS_INLINE std::string generatePrefix( str += pos; uint32_t locator_hash = utils::hash::fnv32_buf(str, strlen(str)); - uint16_t* word = (uint16_t*)&locator_hash; + const uint16_t* word = (const uint16_t*)&locator_hash; std::stringstream stream; stream << first_ipv6_word << ":0"; @@ -47,4 +47,4 @@ TRANSPORT_ALWAYS_INLINE std::string generatePrefix( stream << "::"; return stream.str(); -} \ No newline at end of file +} diff --git a/apps/http-proxy/main.cc b/apps/http-proxy/main.cc index 32be35bdb..832ec23e6 100644 --- a/apps/http-proxy/main.cc +++ b/apps/http-proxy/main.cc @@ -18,7 +18,7 @@ using namespace transport; -int usage(char* program) { +int usage(const char* program) { LoggerInfo() << "USAGE: " << program << "[-C|-S] [options] \n" << "Server or Client: \n" << " -P [FIRST_IPv6_WORD_HEX]\n" @@ -132,7 +132,6 @@ int main(int argc, char** argv) { case 't': params.n_thread = std::stoul(optarg); break; - case 'h': default: return usage(argv[0]); } @@ -150,4 +149,4 @@ int main(int argc, char** argv) { delete proxy; return 0; -} \ No newline at end of file +} diff --git a/apps/http-proxy/src/forwarder_interface.cc b/apps/http-proxy/src/forwarder_interface.cc index 205b290d8..717679e09 100644 --- a/apps/http-proxy/src/forwarder_interface.cc +++ b/apps/http-proxy/src/forwarder_interface.cc @@ -154,10 +154,9 @@ void ForwarderInterface::internalRemoveConnectedUser(uint32_t route_id) { hc_data_free(data); } -void ForwarderInterface::internalCreateFaceAndRoute(RouteInfoPtr route_info, - uint8_t max_try, - asio::steady_timer *timer, - SetRouteCallback callback) { +void ForwarderInterface::internalCreateFaceAndRoute( + RouteInfoPtr route_info, uint8_t max_try, asio::steady_timer *timer, + const SetRouteCallback &callback) { int ret = tryToCreateFaceAndRoute(route_info.get()); if (ret < 0 && max_try > 0) { @@ -186,7 +185,8 @@ void ForwarderInterface::internalCreateFaceAndRoute(RouteInfoPtr route_info, delete timer; } -int ForwarderInterface::tryToCreateFaceAndRoute(route_info_t *route_info) { +int ForwarderInterface::tryToCreateFaceAndRoute( + const route_info_t *route_info) { if (!sock_) return -1; hc_data_t *data; diff --git a/apps/http-proxy/src/http_1x_message_fast_parser.cc b/apps/http-proxy/src/http_1x_message_fast_parser.cc index e97c33161..ffae2368b 100644 --- a/apps/http-proxy/src/http_1x_message_fast_parser.cc +++ b/apps/http-proxy/src/http_1x_message_fast_parser.cc @@ -99,7 +99,8 @@ bool HTTPMessageFastParser::isMpdRequest(const uint8_t *headers, return false; } -uint32_t HTTPMessageFastParser::parseCacheControl(const uint8_t *headers, - std::size_t length) { +uint32_t HTTPMessageFastParser::parseCacheControl( + [[maybe_unused]] const uint8_t *headers, + [[maybe_unused]] std::size_t length) { return 0; } diff --git a/apps/http-proxy/src/http_proxy.cc b/apps/http-proxy/src/http_proxy.cc index b517ae30f..7419c7f7f 100644 --- a/apps/http-proxy/src/http_proxy.cc +++ b/apps/http-proxy/src/http_proxy.cc @@ -29,16 +29,15 @@ using interface::ConsumerInterestCallback; using interface::ConsumerSocket; using interface::TransportProtocolAlgorithms; -class HTTPClientConnectionCallback : interface::ConsumerSocket::ReadCallback { +class HTTPClientConnectionCallback + : public interface::ConsumerSocket::ReadCallback { public: HTTPClientConnectionCallback(TcpReceiver& tcp_receiver, utils::EventThread& thread) : tcp_receiver_(tcp_receiver), thread_(thread), prefix_hash_(tcp_receiver_.prefix_hash_), - consumer_(TransportProtocolAlgorithms::RAAQM, thread_), - session_(nullptr), - current_size_(0) { + consumer_(TransportProtocolAlgorithms::RAAQM, thread_) { consumer_.setSocketOption(ConsumerCallbacksOptions::READ_CALLBACK, this); consumer_.setSocketOption( ConsumerCallbacksOptions::INTEREST_OUTPUT, @@ -62,7 +61,7 @@ class HTTPClientConnectionCallback : interface::ConsumerSocket::ReadCallback { std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5), - [this](asio::ip::tcp::socket& socket) -> bool { + [this](const asio::ip::tcp::socket& socket) { try { std::string remote_address = socket.remote_endpoint().address().to_string(); @@ -136,10 +135,6 @@ class HTTPClientConnectionCallback : interface::ConsumerSocket::ReadCallback { current_size_ += size; if (is_last) { - // TRANSPORT_LOGD("Request received: %s", - // std::string((const char*)tmp_buffer_.first->data(), - // tmp_buffer_.first->length()) - // .c_str()); if (current_size_ < 1400) { request_buffer_queue_.emplace_back(std::move(tmp_buffer_)); } else { @@ -162,15 +157,16 @@ class HTTPClientConnectionCallback : interface::ConsumerSocket::ReadCallback { // hicn callbacks - void processLeavingInterest(interface::ConsumerSocket& c, - const core::Interest& interest) { + void processLeavingInterest( + [[maybe_unused]] const interface::ConsumerSocket& c, + const core::Interest& interest) { if (interest.getName().getSuffix() == 0 && interest.payloadSize() == 0) { Interest& int2 = const_cast(interest); int2.appendPayload(request_buffer_queue_.front().first->clone()); } } - void processInterestRetx(interface::ConsumerSocket& c, + void processInterestRetx([[maybe_unused]] const interface::ConsumerSocket& c, const core::Interest& interest) { if (interest.payloadSize() == 0) { Interest& int2 = const_cast(interest); @@ -179,15 +175,18 @@ class HTTPClientConnectionCallback : interface::ConsumerSocket::ReadCallback { } bool isBufferMovable() noexcept { return true; } - void getReadBuffer(uint8_t** application_buffer, size_t* max_length) {} - void readDataAvailable(size_t length) noexcept {} + void getReadBuffer(uint8_t** application_buffer, + size_t* max_length) { /*nothing to do*/ + } + void readDataAvailable(size_t length) noexcept { /*nothing to do*/ + } size_t maxBufferSize() const { return 64 * 1024; } void readBufferAvailable(std::unique_ptr&& buffer) noexcept { // Response received. Send it back to client auto _buffer = buffer.release(); // TRANSPORT_LOGD("From hicn: %zu bytes.", _buffer->length()); - session_->send(_buffer, []() {}); + session_->send(_buffer, []() { /*nothing to do*/ }); } void readError(const std::error_code& ec) noexcept { @@ -218,7 +217,9 @@ class HTTPClientConnectionCallback : interface::ConsumerSocket::ReadCallback { } } else { tcp_receiver_.parseHicnHeader( - it->second, [this](bool result, std::string configured_prefix) { + it->second, + [this](bool result, + [[maybe_unused]] const std::string& configured_prefix) { const char* reply = nullptr; if (result) { reply = HTTPMessageFastParser::http_ok; @@ -238,16 +239,15 @@ class HTTPClientConnectionCallback : interface::ConsumerSocket::ReadCallback { } } - private: TcpReceiver& tcp_receiver_; utils::EventThread& thread_; std::string& prefix_hash_; ConsumerSocket consumer_; - std::unique_ptr session_; + std::unique_ptr session_ = nullptr; std::deque, std::string>> request_buffer_queue_; std::pair, std::string> tmp_buffer_; - std::size_t current_size_; + std::size_t current_size_ = 0; }; TcpReceiver::TcpReceiver(std::uint16_t port, const std::string& prefix, @@ -260,8 +260,7 @@ TcpReceiver::TcpReceiver(std::uint16_t port, const std::string& prefix, ipv6_first_word_(ipv6_first_word), prefix_hash_(generatePrefix(prefix_, ipv6_first_word_)), forwarder_config_( - thread_.getIoService(), - [this](const std::error_code& ec) { + thread_.getIoService(), [this](const std::error_code& ec) { if (!ec) { listener_.doAccept(); for (int i = 0; i < 10; i++) { @@ -269,8 +268,7 @@ TcpReceiver::TcpReceiver(std::uint16_t port, const std::string& prefix, new HTTPClientConnectionCallback(*this, thread_)); } } - }), - stopped_(false) { + }) { forwarder_config_.tryToConnectToForwarder(); } @@ -349,7 +347,6 @@ void HTTPProxy::stop() { HTTPProxy::HTTPProxy(ClientParams& params, std::size_t n_thread) : signals_(main_io_context_, SIGINT, SIGQUIT) { for (uint16_t i = 0; i < n_thread; i++) { - // icn_receivers_.emplace_back(std::make_unique(icn_params)); receivers_.emplace_back(std::make_unique( params.tcp_listen_port, params.prefix, params.first_ipv6_word)); } diff --git a/apps/http-proxy/src/http_session.cc b/apps/http-proxy/src/http_session.cc index def4c61cf..06a81dc27 100644 --- a/apps/http-proxy/src/http_session.cc +++ b/apps/http-proxy/src/http_session.cc @@ -21,22 +21,16 @@ namespace transport { -HTTPSession::HTTPSession(asio::io_service &io_service, std::string &ip_address, - std::string &port, - ContentReceivedCallback receive_callback, - OnConnectionClosed on_connection_closed_callback, - bool client) +HTTPSession::HTTPSession( + asio::io_service &io_service, const std::string &ip_address, + const std::string &port, const ContentReceivedCallback &receive_callback, + const OnConnectionClosed &on_connection_closed_callback, bool client) : io_service_(io_service), socket_(io_service_), resolver_(io_service_), endpoint_iterator_(resolver_.resolve({ip_address, port})), timer_(io_service), reverse_(client), - is_reconnection_(false), - data_available_(false), - content_length_(0), - is_last_chunk_(false), - chunked_(false), receive_callback_(receive_callback), on_connection_closed_callback_(on_connection_closed_callback) { input_buffer_.prepare(buffer_size + 2048); @@ -51,10 +45,10 @@ HTTPSession::HTTPSession(asio::io_service &io_service, std::string &ip_address, doConnect(); } -HTTPSession::HTTPSession(asio::ip::tcp::socket socket, - ContentReceivedCallback receive_callback, - OnConnectionClosed on_connection_closed_callback, - bool client) +HTTPSession::HTTPSession( + asio::ip::tcp::socket socket, + const ContentReceivedCallback &receive_callback, + const OnConnectionClosed &on_connection_closed_callback, bool client) : #if ((ASIO_VERSION / 100 % 1000) < 12) io_service_(socket.get_io_service()), @@ -120,9 +114,7 @@ void HTTPSession::close() { if (state_ != ConnectorState::CLOSED) { state_ = ConnectorState::CLOSED; if (socket_.is_open()) { - // socket_.shutdown(asio::ip::tcp::socket::shutdown_type::shutdown_both); socket_.close(); - // on_disconnect_callback_(); } } } @@ -130,16 +122,17 @@ void HTTPSession::close() { void HTTPSession::doWrite() { auto &buffer = write_msgs_.front().first; - asio::async_write(socket_, asio::buffer(buffer->data(), buffer->length()), - [this](const std::error_code &ec, std::size_t length) { - if (TRANSPORT_EXPECT_FALSE(!ec)) { - write_msgs_.front().second(); - write_msgs_.pop_front(); - if (!write_msgs_.empty()) { - doWrite(); - } - } - }); + asio::async_write( + socket_, asio::buffer(buffer->data(), buffer->length()), + [this](const std::error_code &ec, [[maybe_unused]] std::size_t length) { + if (TRANSPORT_EXPECT_FALSE(!ec)) { + write_msgs_.front().second(); + write_msgs_.pop_front(); + if (!write_msgs_.empty()) { + doWrite(); + } + } + }); } // namespace transport void HTTPSession::handleRead(const std::error_code &ec, std::size_t length) { @@ -147,8 +140,8 @@ void HTTPSession::handleRead(const std::error_code &ec, std::size_t length) { content_length_ -= length; const uint8_t *buffer = asio::buffer_cast(input_buffer_.data()); - bool is_last = chunked_ ? (is_last_chunk_ ? !content_length_ : false) - : !content_length_; + bool check = is_last_chunk_ ? !content_length_ : false; + bool is_last = chunked_ ? check : !content_length_; receive_callback_(buffer, input_buffer_.size(), is_last, false, nullptr); input_buffer_.consume(input_buffer_.size()); @@ -274,7 +267,6 @@ void HTTPSession::tryReconnection() { is_reconnection_ = true; io_service_.post([this]() { if (socket_.is_open()) { - // socket_.shutdown(asio::ip::tcp::socket::shutdown_type::shutdown_both); socket_.close(); } startConnectionTimer(); @@ -295,8 +287,6 @@ void HTTPSession::doConnect() { asio::ip::tcp::no_delay noDelayOption(true); socket_.set_option(noDelayOption); - // on_reconnect_callback_(); - doReadHeader(); if (data_available_ && !write_msgs_.empty()) { diff --git a/apps/http-proxy/src/icn_receiver.cc b/apps/http-proxy/src/icn_receiver.cc index c97524906..c8904aa95 100644 --- a/apps/http-proxy/src/icn_receiver.cc +++ b/apps/http-proxy/src/icn_receiver.cc @@ -33,18 +33,15 @@ AsyncConsumerProducer::AsyncConsumerProducer( const std::string& mtu, const std::string& content_lifetime, bool manifest) : prefix_(core::Prefix(generatePrefix(prefix, first_ipv6_word), 64)), io_service_(io_service), - external_io_service_(true), - producer_socket_(), ip_address_(origin_address), port_(origin_port), cache_size_((uint32_t)std::stoul(cache_size)), mtu_((uint32_t)std::stoul(mtu)), - request_counter_(0), connector_(io_service_, ip_address_, port_, std::bind(&AsyncConsumerProducer::publishContent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), - [this](asio::ip::tcp::socket& socket) -> bool { + [this]([[maybe_unused]] const asio::ip::tcp::socket& socket) { std::queue empty; std::swap(response_name_queue_, empty); @@ -100,7 +97,7 @@ void AsyncConsumerProducer::stop() { void AsyncConsumerProducer::doReceive() { producer_socket_.setSocketOption( interface::ProducerCallbacksOptions::CACHE_MISS, - [this](interface::ProducerSocket& producer, + [this]([[maybe_unused]] const interface::ProducerSocket& producer, interface::Interest& interest) { if (interest.payloadSize() > 0) { // Interest may contain http request @@ -149,7 +146,8 @@ void AsyncConsumerProducer::manageIncomingInterest( response_name_queue_.emplace(std::move(name), is_mpd ? 1000 : default_content_lifetime_); - connector_.send(payload, [packet = std::move(packet)]() {}); + connector_.send(payload, + [packet = std::move(packet)]() { /*nothing to do*/ }); } void AsyncConsumerProducer::publishContent(const uint8_t* data, @@ -162,7 +160,7 @@ void AsyncConsumerProducer::publishContent(const uint8_t* data, abort(); } - interface::PublicationOptions& options = response_name_queue_.front(); + const interface::PublicationOptions& options = response_name_queue_.front(); int ret = producer_socket_.setSocketOption( interface::GeneralTransportOptions::CONTENT_OBJECT_EXPIRY_TIME, -- cgit 1.2.3-korg