Made some changes recommended by Clang-Tidy.

This commit is contained in:
Fred Nicolson 2017-07-20 15:02:43 +01:00
parent a0ca16c891
commit 84382cad0b
20 changed files with 93 additions and 87 deletions

View File

@ -74,6 +74,8 @@ int main()
break; break;
case 'q': case 'q':
break; break;
default:
std::cout << "Invalid input!" << std::endl;
} }
//Exit/error check //Exit/error check

View File

@ -83,7 +83,7 @@ namespace fr
}; };
Http(); Http();
Http(Http &&); Http(Http &&) noexcept;
Http(const Http &); Http(const Http &);
virtual ~Http() = default; virtual ~Http() = default;
@ -269,7 +269,7 @@ namespace fr
*/ */
static inline int dectohex(const std::string &hex) static inline int dectohex(const std::string &hex)
{ {
return (int)strtol(&hex[0], 0, 16); return (int)strtol(&hex[0], nullptr, 16);
} }
/*! /*!

View File

@ -18,11 +18,12 @@ namespace fr
//Constructors //Constructors
HttpRequest(); HttpRequest();
HttpRequest(HttpRequest &&other)=default; HttpRequest(HttpRequest &&other)=default;
void operator=(const HttpRequest &other) HttpRequest &operator=(const HttpRequest &other)
{ {
header_ended = other.header_ended; header_ended = other.header_ended;
last_parsed_character = other.last_parsed_character; last_parsed_character = other.last_parsed_character;
content_length = other.content_length; content_length = other.content_length;
return *this;
} }
virtual ~HttpRequest() = default; virtual ~HttpRequest() = default;

View File

@ -16,8 +16,10 @@ namespace fr
{ {
public: public:
//Constructors //Constructors
HttpResponse(){}; HttpResponse()
virtual ~HttpResponse(){} : header_ended(false),
content_length(0){}
virtual ~HttpResponse() = default;
/*! /*!
* Parse a HTTP response. * Parse a HTTP response.
@ -41,7 +43,7 @@ namespace fr
* *
* @param header_end_pos The position in 'body' of the end of the header * @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 //State
bool header_ended; bool header_ended;

View File

@ -19,7 +19,8 @@
#else #else
#define closesocket(x) close(x) #define closesocket(x) close(x)
#define INVALID_SOCKET 0 #define INVALID_SOCKET 0
#define SOCKET_ERROR -1 #define SOCKET_ERROR (-1)
#include <netdb.h> #include <netdb.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -73,13 +74,6 @@ inline double ntohd(double val)
return 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) 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 //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; return;
//Different API calls needed for both windows and unix //Different API calls needed for both windows and unix
#ifdef WIN32 #ifdef WIN32
u_long non_blocking = should_block ? 0 : 1; u_long non_blocking = should_block ? 0 : 1;
ioctlsocket(socket_descriptor, FIONBIO, &non_blocking); ioctlsocket(socket_descriptor, FIONBIO, &non_blocking);
#else #else
int flags = fcntl(socket_descriptor, F_GETFL, 0); int flags = fcntl(socket_descriptor, F_GETFL, 0);
fcntl(socket_descriptor, F_SETFL, is_blocking_already ? flags ^ O_NONBLOCK : flags ^= O_NONBLOCK); fcntl(socket_descriptor, F_SETFL, is_blocking_already ? flags ^ O_NONBLOCK : flags ^= O_NONBLOCK);
#endif #endif
} }

View File

@ -41,7 +41,23 @@ namespace fr
Socket() noexcept; Socket() noexcept;
virtual ~Socket() noexcept = default; 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. * Close the connection.
@ -72,7 +88,7 @@ namespace fr
* *
* @param should_block True for blocking (default argument), false otherwise. * @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 * Attempts to send raw data down the socket, without

View File

@ -17,7 +17,8 @@ class TcpListener : public Listener
{ {
public: public:
TcpListener(); TcpListener();
virtual ~TcpListener() override;
~TcpListener() override;
TcpListener(TcpListener &&o) = default; TcpListener(TcpListener &&o) = default;
/*! /*!
@ -44,26 +45,26 @@ public:
* it to immediately return (you might want to do this if * it to immediately return (you might want to do this if
* you're exiting and need the blocking socket to return). * you're exiting and need the blocking socket to return).
*/ */
virtual void shutdown() override; void shutdown() override;
/*! /*!
* Gets the socket descriptor. * Gets the socket descriptor.
* *
* @return The listen 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. * Sets the socket descriptor.
* *
* @param descriptor The listen descriptor to use * @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 * Closes the socket
*/ */
virtual void close_socket() override; void close_socket() override;
private: private:

View File

@ -82,7 +82,7 @@ public:
* *
* @param should_block True to block, false otherwise. * @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 * Gets the unerlying socket descriptor
@ -96,7 +96,7 @@ public:
* *
* @return True if it's connected. False otherwise. * @return True if it's connected. False otherwise.
*/ */
inline virtual bool connected() const override final inline bool connected() const final
{ {
return socket_descriptor > -1; return socket_descriptor > -1;
} }

View File

@ -29,7 +29,7 @@ namespace fr
*/ */
URL() = default; URL() = default;
URL(const std::string &url); explicit URL(const std::string &url);
/*! /*!
* Parses a given URL, extracting its various components * Parses a given URL, extracting its various components

View File

@ -1,18 +1,13 @@
#include <iostream> #include <iostream>
#include <frnetlib/SSLListener.h>
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
#include <chrono>
#include "frnetlib/Packet.h" #include "frnetlib/Packet.h"
#include "frnetlib/TcpSocket.h" #include "frnetlib/TcpSocket.h"
#include "frnetlib/TcpListener.h" #include "frnetlib/TcpListener.h"
#include "frnetlib/SocketSelector.h" #include "frnetlib/SocketSelector.h"
#include "frnetlib/HttpRequest.h" #include "frnetlib/HttpRequest.h"
#include "frnetlib/HttpResponse.h" #include "frnetlib/HttpResponse.h"
#include "frnetlib/SSLSocket.h"
#include "frnetlib/SSLContext.h"
#include "frnetlib/SSLListener.h"
enum Enum : uint32_t enum Enum : uint32_t
{ {

View File

@ -6,7 +6,6 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include "frnetlib/Http.h" #include "frnetlib/Http.h"
#include "frnetlib/Socket.h"
namespace fr namespace fr
{ {
@ -20,7 +19,7 @@ namespace fr
} }
Http::Http(Http &&o) Http::Http(Http &&o) noexcept
: header_data(std::move(o.header_data)), : header_data(std::move(o.header_data)),
post_data(std::move(o.post_data)), post_data(std::move(o.post_data)),
get_data(std::move(o.get_data)), get_data(std::move(o.get_data)),
@ -203,20 +202,21 @@ namespace fr
while(true) 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) 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) 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)); list.emplace_back(str.substr(read_index, equal_pos - read_index), str.substr(equal_pos + 1, str.size() - equal_pos - 1));
break; 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; read_index = and_pos + 1;
}
} }
else else
{ {
@ -233,7 +233,8 @@ namespace fr
if(colon_pos == std::string::npos) if(colon_pos == std::string::npos)
return; 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) if(data_begin == std::string::npos)
return; return;

View File

@ -34,15 +34,13 @@ namespace fr
//If the header end has not been found, return true, indicating that we need more data. //If the header end has not been found, return true, indicating that we need more data.
if(!header_ended) if(!header_ended)
{
return true; return true;
}
else //Else parse it
{
if(!parse_header(header_end)) if(!parse_header(header_end))
return false; return false;
body.clear(); body.clear();
}
body += std::string(request + header_end + header_end_size, requestsz - header_end - header_end_size); 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 //Find beginning of post data
auto post_begin = body.find_first_not_of("\r\n"); auto post_begin = body.find_first_not_of("\r\n");
if(post_begin == std::string::npos) 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 //Find end of post data
auto post_end = body.rfind("\r\n\r\n"); auto post_end = body.rfind("\r\n\r\n");
@ -168,7 +166,7 @@ namespace fr
void HttpRequest::parse_header_type(const std::string &str) void HttpRequest::parse_header_type(const std::string &str)
{ {
//Find the request type //Find the request type
auto type_end = str.find(" "); auto type_end = str.find(' ');
if(type_end != std::string::npos) if(type_end != std::string::npos)
{ {
//Check what it is //Check what it is
@ -192,7 +190,7 @@ namespace fr
void HttpRequest::parse_header_uri(const std::string &str) 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; auto uri_end = str.find("HTTP") - 1;
if(uri_begin != std::string::npos) if(uri_begin != std::string::npos)
{ {
@ -200,7 +198,7 @@ namespace fr
std::string uri = str.substr(uri_begin, uri_end - uri_begin); std::string uri = str.substr(uri_begin, uri_end - uri_begin);
//Parse GET variables //Parse GET variables
auto get_begin = str.find("?"); auto get_begin = str.find('?');
if(get_begin != std::string::npos) if(get_begin != std::string::npos)
{ {
auto get_vars = parse_argument_list(str.substr(get_begin, uri_end - get_begin)); auto get_vars = parse_argument_list(str.substr(get_begin, uri_end - get_begin));

View File

@ -26,14 +26,12 @@ namespace fr
//If the header end has not been found, return true, indicating that we need more data. //If the header end has not been found, return true, indicating that we need more data.
if(!header_ended) if(!header_ended)
{
return true; return true;
}
else //Else parse it
{
parse_header(header_end); parse_header(header_end);
body.clear(); body.clear();
}
body += std::string(response_data + header_end + header_end_size, datasz - header_end - header_end_size); body += std::string(response_data + header_end + header_end_size, datasz - header_end - header_end_size);
} }
@ -72,7 +70,7 @@ namespace fr
return response; return response;
} }
bool HttpResponse::parse_header(int32_t header_end_pos) bool HttpResponse::parse_header(size_t header_end_pos)
{ {
try try
{ {

View File

@ -2,8 +2,6 @@
// Created by fred on 06/12/16. // Created by fred on 06/12/16.
// //
#include "frnetlib/Packet.h"
namespace fr namespace fr
{ {

View File

@ -67,12 +67,12 @@ namespace fr
if(!connected()) if(!connected())
return Socket::Disconnected; return Socket::Disconnected;
int32_t bytes_remaining = (int32_t) buffer_size; auto bytes_remaining = (int32_t) buffer_size;
size_t bytes_read = 0; size_t bytes_read = 0;
while(bytes_remaining > 0) while(bytes_remaining > 0)
{ {
size_t received = 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); Status status = receive_raw(&arr[bytes_read], (size_t)bytes_remaining, received);
if(status != fr::Socket::Success) if(status != fr::Socket::Success)
return status; return status;

View File

@ -30,16 +30,17 @@ namespace fr
} }
#endif #endif
timeval wait_time; timeval wait_time{};
wait_time.tv_sec = 0; wait_time.tv_sec = 0;
wait_time.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(timeout).count(); wait_time.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(timeout).count();
listen_read = listen_set; 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 if(select_result == 0) //If it's timed out
return false; 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)); throw std::logic_error("select() returned -1. Errno: " + std::to_string(errno));
return true; return true;

View File

@ -24,7 +24,7 @@ namespace fr
Socket::Status TcpListener::listen(const std::string &port) Socket::Status TcpListener::listen(const std::string &port)
{ {
addrinfo *info; addrinfo *info;
addrinfo hints; addrinfo hints{};
memset(&hints, 0, sizeof(addrinfo)); memset(&hints, 0, sizeof(addrinfo));
@ -32,7 +32,7 @@ namespace fr
hints.ai_socktype = SOCK_STREAM; //TCP hints.ai_socktype = SOCK_STREAM; //TCP
hints.ai_flags = AI_PASSIVE; //Have the IP filled in for us 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; return Socket::Status::Unknown;
} }
@ -86,10 +86,10 @@ namespace fr
Socket::Status TcpListener::accept(Socket &client_) Socket::Status TcpListener::accept(Socket &client_)
{ {
//Cast to TcpSocket. Will throw bad cast on failure. //Cast to TcpSocket. Will throw bad cast on failure.
TcpSocket &client = dynamic_cast<TcpSocket&>(client_); auto &client = dynamic_cast<TcpSocket&>(client_);
//Prepare to wait for the client //Prepare to wait for the client
sockaddr_storage client_addr; sockaddr_storage client_addr{};
int client_descriptor; int client_descriptor;
char client_printable_addr[INET6_ADDRSTRLEN]; char client_printable_addr[INET6_ADDRSTRLEN];
@ -100,7 +100,7 @@ namespace fr
return Socket::Unknown; return Socket::Unknown;
//Get printable address. If we failed then set it as just '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) if(err != 0)
strcpy(client_printable_addr, "unknown"); strcpy(client_printable_addr, "unknown");

View File

@ -25,7 +25,7 @@ namespace fr
size_t sent = 0; size_t sent = 0;
while(sent < size) 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) if(status > 0)
{ {
sent += status; sent += status;
@ -86,7 +86,7 @@ namespace fr
Socket::Status TcpSocket::connect(const std::string &address, const std::string &port) Socket::Status TcpSocket::connect(const std::string &address, const std::string &port)
{ {
addrinfo *info; addrinfo *info;
addrinfo hints; addrinfo hints{};
memset(&hints, 0, sizeof(addrinfo)); memset(&hints, 0, sizeof(addrinfo));

View File

@ -21,6 +21,7 @@ namespace fr
}; };
URL::URL(const std::string &url) URL::URL(const std::string &url)
: scheme(Scheme::Unknown)
{ {
parse(url); parse(url);
} }
@ -41,7 +42,7 @@ namespace fr
} }
//Check to see if there's a port //Check to see if there's a port
pos = url.find(":", parse_offset); pos = url.find(':', parse_offset);
if(pos != std::string::npos) if(pos != std::string::npos)
{ {
//Store host //Store host
@ -49,7 +50,7 @@ namespace fr
parse_offset += host.size(); parse_offset += host.size();
//Find end of port //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_end = (port_end == std::string::npos) ? url.size() : port_end;
port = url.substr(pos + 1, port_end - pos - 1); port = url.substr(pos + 1, port_end - pos - 1);
parse_offset = port_end + 1; parse_offset = port_end + 1;
@ -57,9 +58,9 @@ namespace fr
else else
{ {
//Store host //Store host
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.find("#", parse_offset); pos = (pos != std::string::npos) ? pos : url.find('#', parse_offset);
pos = (pos != std::string::npos) ? pos : url.size(); pos = (pos != std::string::npos) ? pos : url.size();
host = url.substr(parse_offset, pos - parse_offset); host = url.substr(parse_offset, pos - parse_offset);
parse_offset = pos + 1; parse_offset = pos + 1;
@ -92,7 +93,7 @@ namespace fr
return; return;
//Extract the path //Extract the path
pos = url.find("?", parse_offset); pos = url.find('?', parse_offset);
if(pos != std::string::npos) if(pos != std::string::npos)
{ {
path = url.substr(parse_offset, pos - parse_offset); path = url.substr(parse_offset, pos - parse_offset);
@ -100,15 +101,15 @@ namespace fr
} }
else else
{ {
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(); pos = (pos != std::string::npos) ? pos : url.size();
path = url.substr(parse_offset, pos - parse_offset); path = url.substr(parse_offset, pos - parse_offset);
parse_offset = pos + 1; parse_offset = pos + 1;
} }
//Extract the query //Extract the query
pos = url.find("#", parse_offset - 1); pos = url.find('#', parse_offset - 1);
if(pos != std::string::npos) if(pos != std::string::npos)
{ {
if(pos + 1 != parse_offset) if(pos + 1 != parse_offset)
@ -122,7 +123,6 @@ namespace fr
query = url.substr(parse_offset, url.size() - parse_offset); query = url.substr(parse_offset, url.size() - parse_offset);
} }
return;
} }
URL::Scheme URL::string_to_scheme(const std::string &scheme) URL::Scheme URL::string_to_scheme(const std::string &scheme)

View File

@ -3,7 +3,6 @@
// //
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <string>
#include <frnetlib/URL.h> #include <frnetlib/URL.h>
TEST(URLTest, full_parse) TEST(URLTest, full_parse)