From 84382cad0b11f7608e8a3145416852b6711f45db Mon Sep 17 00:00:00 2001 From: Fred Nicolson Date: Thu, 20 Jul 2017 15:02:43 +0100 Subject: [PATCH] Made some changes recommended by Clang-Tidy. --- .../tcpsocket_client.cpp | 2 ++ include/frnetlib/Http.h | 4 ++-- include/frnetlib/HttpRequest.h | 3 ++- include/frnetlib/HttpResponse.h | 8 ++++--- include/frnetlib/NetworkEncoding.h | 24 +++++++------------ include/frnetlib/Socket.h | 20 ++++++++++++++-- include/frnetlib/TcpListener.h | 11 +++++---- include/frnetlib/TcpSocket.h | 4 ++-- include/frnetlib/URL.h | 2 +- main.cpp | 5 ---- src/Http.cpp | 19 ++++++++------- src/HttpRequest.cpp | 18 +++++++------- src/HttpResponse.cpp | 12 ++++------ src/Packet.cpp | 2 -- src/Socket.cpp | 4 ++-- src/SocketSelector.cpp | 7 +++--- src/TcpListener.cpp | 10 ++++---- src/TcpSocket.cpp | 4 ++-- src/URL.cpp | 20 ++++++++-------- tests/URLTest.cpp | 1 - 20 files changed, 93 insertions(+), 87 deletions(-) diff --git a/examples/simple_server_and_client/tcpsocket_client.cpp b/examples/simple_server_and_client/tcpsocket_client.cpp index 39de75a..4a463ca 100644 --- a/examples/simple_server_and_client/tcpsocket_client.cpp +++ b/examples/simple_server_and_client/tcpsocket_client.cpp @@ -74,6 +74,8 @@ int main() break; case 'q': break; + default: + std::cout << "Invalid input!" << std::endl; } //Exit/error check diff --git a/include/frnetlib/Http.h b/include/frnetlib/Http.h index 0cb3350..8838677 100644 --- a/include/frnetlib/Http.h +++ b/include/frnetlib/Http.h @@ -83,7 +83,7 @@ namespace fr }; Http(); - Http(Http &&); + Http(Http &&) noexcept; Http(const Http &); virtual ~Http() = default; @@ -269,7 +269,7 @@ namespace fr */ static inline int dectohex(const std::string &hex) { - return (int)strtol(&hex[0], 0, 16); + return (int)strtol(&hex[0], nullptr, 16); } /*! diff --git a/include/frnetlib/HttpRequest.h b/include/frnetlib/HttpRequest.h index ff30fc9..9568e80 100644 --- a/include/frnetlib/HttpRequest.h +++ b/include/frnetlib/HttpRequest.h @@ -18,11 +18,12 @@ namespace fr //Constructors HttpRequest(); HttpRequest(HttpRequest &&other)=default; - void operator=(const HttpRequest &other) + HttpRequest &operator=(const HttpRequest &other) { header_ended = other.header_ended; last_parsed_character = other.last_parsed_character; content_length = other.content_length; + return *this; } virtual ~HttpRequest() = default; diff --git a/include/frnetlib/HttpResponse.h b/include/frnetlib/HttpResponse.h index b64c1bc..fe2dc1e 100644 --- a/include/frnetlib/HttpResponse.h +++ b/include/frnetlib/HttpResponse.h @@ -16,8 +16,10 @@ namespace fr { public: //Constructors - HttpResponse(){}; - virtual ~HttpResponse(){} + HttpResponse() + : header_ended(false), + content_length(0){} + virtual ~HttpResponse() = default; /*! * Parse a HTTP response. @@ -41,7 +43,7 @@ namespace fr * * @param header_end_pos The position in 'body' of the end of the header */ - bool parse_header(int32_t header_end_pos); + bool parse_header(size_t header_end_pos); //State bool header_ended; diff --git a/include/frnetlib/NetworkEncoding.h b/include/frnetlib/NetworkEncoding.h index 8f49a2e..f471146 100644 --- a/include/frnetlib/NetworkEncoding.h +++ b/include/frnetlib/NetworkEncoding.h @@ -19,7 +19,8 @@ #else #define closesocket(x) close(x) #define INVALID_SOCKET 0 -#define SOCKET_ERROR -1 +#define SOCKET_ERROR (-1) + #include #include #include @@ -73,13 +74,6 @@ inline double ntohd(double val) return val; } -inline void *get_sin_addr(struct sockaddr *sa) -{ - if(sa->sa_family == AF_INET) - return &(((sockaddr_in*)sa)->sin_addr); - return &(((sockaddr_in6*)sa)->sin6_addr); -} - inline void set_unix_socket_blocking(int32_t socket_descriptor, bool is_blocking_already, bool should_block) { //Don't update it if we're already in that mode @@ -87,13 +81,13 @@ inline void set_unix_socket_blocking(int32_t socket_descriptor, bool is_blocking return; //Different API calls needed for both windows and unix - #ifdef WIN32 - u_long non_blocking = should_block ? 0 : 1; - ioctlsocket(socket_descriptor, FIONBIO, &non_blocking); - #else - int flags = fcntl(socket_descriptor, F_GETFL, 0); - fcntl(socket_descriptor, F_SETFL, is_blocking_already ? flags ^ O_NONBLOCK : flags ^= O_NONBLOCK); - #endif +#ifdef WIN32 + u_long non_blocking = should_block ? 0 : 1; + ioctlsocket(socket_descriptor, FIONBIO, &non_blocking); +#else + int flags = fcntl(socket_descriptor, F_GETFL, 0); + fcntl(socket_descriptor, F_SETFL, is_blocking_already ? flags ^ O_NONBLOCK : flags ^= O_NONBLOCK); +#endif } diff --git a/include/frnetlib/Socket.h b/include/frnetlib/Socket.h index 1369e3a..413f95b 100644 --- a/include/frnetlib/Socket.h +++ b/include/frnetlib/Socket.h @@ -41,7 +41,23 @@ namespace fr Socket() noexcept; virtual ~Socket() noexcept = default; - Socket(Socket &&) noexcept = default; + Socket(Socket &&o) noexcept + { + outbound_mutex.lock(); + inbound_mutex.lock(); + o.inbound_mutex.lock(); + o.outbound_mutex.lock(); + + remote_address = std::move(o.remote_address); + is_blocking = o.is_blocking; + ai_family = o.ai_family; + max_receive_size = o.max_receive_size; + + outbound_mutex.unlock(); + inbound_mutex.unlock(); + o.inbound_mutex.unlock(); + o.outbound_mutex.unlock(); + } /*! * Close the connection. @@ -72,7 +88,7 @@ namespace fr * * @param should_block True for blocking (default argument), false otherwise. */ - virtual void set_blocking(bool should_block = true) = 0; + virtual void set_blocking(bool should_block) = 0; /*! * Attempts to send raw data down the socket, without diff --git a/include/frnetlib/TcpListener.h b/include/frnetlib/TcpListener.h index 15b1fdd..082d346 100644 --- a/include/frnetlib/TcpListener.h +++ b/include/frnetlib/TcpListener.h @@ -17,7 +17,8 @@ class TcpListener : public Listener { public: TcpListener(); - virtual ~TcpListener() override; + + ~TcpListener() override; TcpListener(TcpListener &&o) = default; /*! @@ -44,26 +45,26 @@ public: * it to immediately return (you might want to do this if * you're exiting and need the blocking socket to return). */ - virtual void shutdown() override; + void shutdown() override; /*! * Gets the socket descriptor. * * @return The listen socket descriptor */ - virtual int32_t get_socket_descriptor() const override; + int32_t get_socket_descriptor() const override; /*! * Sets the socket descriptor. * * @param descriptor The listen descriptor to use */ - virtual void set_socket_descriptor(int32_t descriptor) override; + void set_socket_descriptor(int32_t descriptor) override; /*! * Closes the socket */ - virtual void close_socket() override; + void close_socket() override; private: diff --git a/include/frnetlib/TcpSocket.h b/include/frnetlib/TcpSocket.h index f7b6bc2..a4dc83b 100644 --- a/include/frnetlib/TcpSocket.h +++ b/include/frnetlib/TcpSocket.h @@ -82,7 +82,7 @@ public: * * @param should_block True to block, false otherwise. */ - virtual void set_blocking(bool should_block) override; + void set_blocking(bool should_block) override; /*! * Gets the unerlying socket descriptor @@ -96,7 +96,7 @@ public: * * @return True if it's connected. False otherwise. */ - inline virtual bool connected() const override final + inline bool connected() const final { return socket_descriptor > -1; } diff --git a/include/frnetlib/URL.h b/include/frnetlib/URL.h index 298f9a1..71ab64c 100644 --- a/include/frnetlib/URL.h +++ b/include/frnetlib/URL.h @@ -29,7 +29,7 @@ namespace fr */ URL() = default; - URL(const std::string &url); + explicit URL(const std::string &url); /*! * Parses a given URL, extracting its various components diff --git a/main.cpp b/main.cpp index fa476b1..3286dc3 100644 --- a/main.cpp +++ b/main.cpp @@ -1,18 +1,13 @@ #include -#include #include #include #include -#include #include "frnetlib/Packet.h" #include "frnetlib/TcpSocket.h" #include "frnetlib/TcpListener.h" #include "frnetlib/SocketSelector.h" #include "frnetlib/HttpRequest.h" #include "frnetlib/HttpResponse.h" -#include "frnetlib/SSLSocket.h" -#include "frnetlib/SSLContext.h" -#include "frnetlib/SSLListener.h" enum Enum : uint32_t { diff --git a/src/Http.cpp b/src/Http.cpp index 650fc97..d5c7e85 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -6,7 +6,6 @@ #include #include #include "frnetlib/Http.h" -#include "frnetlib/Socket.h" namespace fr { @@ -20,7 +19,7 @@ namespace fr } - Http::Http(Http &&o) + Http::Http(Http &&o) noexcept : header_data(std::move(o.header_data)), post_data(std::move(o.post_data)), get_data(std::move(o.get_data)), @@ -203,20 +202,21 @@ namespace fr while(true) { - auto equal_pos = str.find("=", read_index); + unsigned long equal_pos; + equal_pos = str.find('=', read_index); if(equal_pos != std::string::npos) { - auto and_pos = str.find("&", read_index); + unsigned long and_pos; + and_pos = str.find('&', read_index); if(and_pos == std::string::npos) { list.emplace_back(str.substr(read_index, equal_pos - read_index), str.substr(equal_pos + 1, str.size() - equal_pos - 1)); break; } - else - { - list.emplace_back(str.substr(read_index, equal_pos - read_index), str.substr(equal_pos + 1, and_pos - equal_pos - 1)); + + list.emplace_back(str.substr(read_index, equal_pos - read_index), str.substr(equal_pos + 1, and_pos - equal_pos - 1)); read_index = and_pos + 1; - } + } else { @@ -233,7 +233,8 @@ namespace fr if(colon_pos == std::string::npos) return; - auto data_begin = str.find_first_not_of(" ", colon_pos + 1); + unsigned long data_begin; + data_begin = str.find_first_not_of(' ', colon_pos + 1); if(data_begin == std::string::npos) return; diff --git a/src/HttpRequest.cpp b/src/HttpRequest.cpp index 928a8fd..cba2ce2 100644 --- a/src/HttpRequest.cpp +++ b/src/HttpRequest.cpp @@ -34,15 +34,13 @@ namespace fr //If the header end has not been found, return true, indicating that we need more data. if(!header_ended) - { return true; - } - else - { - if(!parse_header(header_end)) + + //Else parse it + if(!parse_header(header_end)) return false; body.clear(); - } + body += std::string(request + header_end + header_end_size, requestsz - header_end - header_end_size); } @@ -148,7 +146,7 @@ namespace fr //Find beginning of post data auto post_begin = body.find_first_not_of("\r\n"); if(post_begin == std::string::npos) - post_begin = body.find_first_not_of("\n"); + post_begin = body.find_first_not_of('\n'); //Find end of post data auto post_end = body.rfind("\r\n\r\n"); @@ -168,7 +166,7 @@ namespace fr void HttpRequest::parse_header_type(const std::string &str) { //Find the request type - auto type_end = str.find(" "); + auto type_end = str.find(' '); if(type_end != std::string::npos) { //Check what it is @@ -192,7 +190,7 @@ namespace fr void HttpRequest::parse_header_uri(const std::string &str) { - auto uri_begin = str.find("/"); + auto uri_begin = str.find('/'); auto uri_end = str.find("HTTP") - 1; if(uri_begin != std::string::npos) { @@ -200,7 +198,7 @@ namespace fr std::string uri = str.substr(uri_begin, uri_end - uri_begin); //Parse GET variables - auto get_begin = str.find("?"); + auto get_begin = str.find('?'); if(get_begin != std::string::npos) { auto get_vars = parse_argument_list(str.substr(get_begin, uri_end - get_begin)); diff --git a/src/HttpResponse.cpp b/src/HttpResponse.cpp index 29451fc..2661a36 100644 --- a/src/HttpResponse.cpp +++ b/src/HttpResponse.cpp @@ -26,14 +26,12 @@ namespace fr //If the header end has not been found, return true, indicating that we need more data. if(!header_ended) - { return true; - } - else - { - parse_header(header_end); + + //Else parse it + parse_header(header_end); body.clear(); - } + body += std::string(response_data + header_end + header_end_size, datasz - header_end - header_end_size); } @@ -72,7 +70,7 @@ namespace fr return response; } - bool HttpResponse::parse_header(int32_t header_end_pos) + bool HttpResponse::parse_header(size_t header_end_pos) { try { diff --git a/src/Packet.cpp b/src/Packet.cpp index 5b480c6..3463e5a 100644 --- a/src/Packet.cpp +++ b/src/Packet.cpp @@ -2,8 +2,6 @@ // Created by fred on 06/12/16. // -#include "frnetlib/Packet.h" - namespace fr { diff --git a/src/Socket.cpp b/src/Socket.cpp index 5b63bb0..457324c 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -67,12 +67,12 @@ namespace fr if(!connected()) return Socket::Disconnected; - int32_t bytes_remaining = (int32_t) buffer_size; + auto bytes_remaining = (int32_t) buffer_size; size_t bytes_read = 0; while(bytes_remaining > 0) { size_t received = 0; - char *arr = (char*)dest; + auto *arr = (char*)dest; Status status = receive_raw(&arr[bytes_read], (size_t)bytes_remaining, received); if(status != fr::Socket::Success) return status; diff --git a/src/SocketSelector.cpp b/src/SocketSelector.cpp index aea5b24..1f4be08 100644 --- a/src/SocketSelector.cpp +++ b/src/SocketSelector.cpp @@ -30,16 +30,17 @@ namespace fr } #endif - timeval wait_time; + timeval wait_time{}; wait_time.tv_sec = 0; wait_time.tv_usec = std::chrono::duration_cast(timeout).count(); listen_read = listen_set; - int select_result = select(max_descriptor + 1, &listen_read, NULL, NULL, timeout == std::chrono::milliseconds(0) ? NULL : &wait_time); + int select_result = select(max_descriptor + 1, &listen_read, nullptr, nullptr, timeout == std::chrono::milliseconds(0) ? nullptr + : &wait_time); if(select_result == 0) //If it's timed out return false; - else if(select_result == SOCKET_ERROR) //Else if error + if(select_result == SOCKET_ERROR) //Else if error throw std::logic_error("select() returned -1. Errno: " + std::to_string(errno)); return true; diff --git a/src/TcpListener.cpp b/src/TcpListener.cpp index 8ce4b62..9cc0522 100644 --- a/src/TcpListener.cpp +++ b/src/TcpListener.cpp @@ -24,7 +24,7 @@ namespace fr Socket::Status TcpListener::listen(const std::string &port) { addrinfo *info; - addrinfo hints; + addrinfo hints{}; memset(&hints, 0, sizeof(addrinfo)); @@ -32,7 +32,7 @@ namespace fr hints.ai_socktype = SOCK_STREAM; //TCP hints.ai_flags = AI_PASSIVE; //Have the IP filled in for us - if(getaddrinfo(NULL, port.c_str(), &hints, &info) != 0) + if(getaddrinfo(nullptr, port.c_str(), &hints, &info) != 0) { return Socket::Status::Unknown; } @@ -86,10 +86,10 @@ namespace fr Socket::Status TcpListener::accept(Socket &client_) { //Cast to TcpSocket. Will throw bad cast on failure. - TcpSocket &client = dynamic_cast(client_); + auto &client = dynamic_cast(client_); //Prepare to wait for the client - sockaddr_storage client_addr; + sockaddr_storage client_addr{}; int client_descriptor; char client_printable_addr[INET6_ADDRSTRLEN]; @@ -100,7 +100,7 @@ namespace fr return Socket::Unknown; //Get printable address. If we failed then set it as just 'unknown' - int err = getnameinfo((sockaddr*)&client_addr, client_addr_len, client_printable_addr, sizeof(client_printable_addr), 0,0,NI_NUMERICHOST); + int err = getnameinfo((sockaddr*)&client_addr, client_addr_len, client_printable_addr, sizeof(client_printable_addr), nullptr,0,NI_NUMERICHOST); if(err != 0) strcpy(client_printable_addr, "unknown"); diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp index df10c3f..035b6c1 100644 --- a/src/TcpSocket.cpp +++ b/src/TcpSocket.cpp @@ -25,7 +25,7 @@ namespace fr size_t sent = 0; while(sent < size) { - int32_t status = ::send(socket_descriptor, data + sent, size - sent, 0); + int64_t status = ::send(socket_descriptor, data + sent, size - sent, 0); if(status > 0) { sent += status; @@ -86,7 +86,7 @@ namespace fr Socket::Status TcpSocket::connect(const std::string &address, const std::string &port) { addrinfo *info; - addrinfo hints; + addrinfo hints{}; memset(&hints, 0, sizeof(addrinfo)); diff --git a/src/URL.cpp b/src/URL.cpp index ef0c859..de92767 100644 --- a/src/URL.cpp +++ b/src/URL.cpp @@ -21,6 +21,7 @@ namespace fr }; URL::URL(const std::string &url) + : scheme(Scheme::Unknown) { parse(url); } @@ -41,7 +42,7 @@ namespace fr } //Check to see if there's a port - pos = url.find(":", parse_offset); + pos = url.find(':', parse_offset); if(pos != std::string::npos) { //Store host @@ -49,7 +50,7 @@ namespace fr parse_offset += host.size(); //Find end of port - size_t port_end = url.find("/", parse_offset); + size_t port_end = url.find('/', parse_offset); port_end = (port_end == std::string::npos) ? url.size() : port_end; port = url.substr(pos + 1, port_end - pos - 1); parse_offset = port_end + 1; @@ -57,9 +58,9 @@ namespace fr else { //Store host - pos = url.find("/", parse_offset); - pos = (pos != std::string::npos) ? pos : url.find("?", parse_offset); - pos = (pos != std::string::npos) ? pos : url.find("#", parse_offset); + pos = url.find('/', parse_offset); + pos = (pos != std::string::npos) ? pos : url.find('?', parse_offset); + pos = (pos != std::string::npos) ? pos : url.find('#', parse_offset); pos = (pos != std::string::npos) ? pos : url.size(); host = url.substr(parse_offset, pos - parse_offset); parse_offset = pos + 1; @@ -92,7 +93,7 @@ namespace fr return; //Extract the path - pos = url.find("?", parse_offset); + pos = url.find('?', parse_offset); if(pos != std::string::npos) { path = url.substr(parse_offset, pos - parse_offset); @@ -100,15 +101,15 @@ namespace fr } else { - pos = url.find("#", parse_offset); - pos = (pos != std::string::npos) ? pos : url.find("?", parse_offset); + pos = url.find('#', parse_offset); + pos = (pos != std::string::npos) ? pos : url.find('?', parse_offset); pos = (pos != std::string::npos) ? pos : url.size(); path = url.substr(parse_offset, pos - parse_offset); parse_offset = pos + 1; } //Extract the query - pos = url.find("#", parse_offset - 1); + pos = url.find('#', parse_offset - 1); if(pos != std::string::npos) { if(pos + 1 != parse_offset) @@ -122,7 +123,6 @@ namespace fr query = url.substr(parse_offset, url.size() - parse_offset); } - return; } URL::Scheme URL::string_to_scheme(const std::string &scheme) diff --git a/tests/URLTest.cpp b/tests/URLTest.cpp index e865608..1f45890 100644 --- a/tests/URLTest.cpp +++ b/tests/URLTest.cpp @@ -3,7 +3,6 @@ // #include -#include #include TEST(URLTest, full_parse)