Made some changes recommended by Clang-Tidy.
This commit is contained in:
parent
a0ca16c891
commit
84382cad0b
@ -74,6 +74,8 @@ int main()
|
||||
break;
|
||||
case 'q':
|
||||
break;
|
||||
default:
|
||||
std::cout << "Invalid input!" << std::endl;
|
||||
}
|
||||
|
||||
//Exit/error check
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -19,7 +19,8 @@
|
||||
#else
|
||||
#define closesocket(x) close(x)
|
||||
#define INVALID_SOCKET 0
|
||||
#define SOCKET_ERROR -1
|
||||
#define SOCKET_ERROR (-1)
|
||||
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
5
main.cpp
5
main.cpp
@ -1,18 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <frnetlib/SSLListener.h>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <chrono>
|
||||
#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
|
||||
{
|
||||
|
||||
17
src/Http.cpp
17
src/Http.cpp
@ -6,7 +6,6 @@
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#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));
|
||||
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;
|
||||
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
//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));
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
//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
|
||||
{
|
||||
|
||||
@ -2,8 +2,6 @@
|
||||
// Created by fred on 06/12/16.
|
||||
//
|
||||
|
||||
#include "frnetlib/Packet.h"
|
||||
|
||||
namespace fr
|
||||
{
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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<std::chrono::microseconds>(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;
|
||||
|
||||
@ -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<TcpSocket&>(client_);
|
||||
auto &client = dynamic_cast<TcpSocket&>(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");
|
||||
|
||||
|
||||
@ -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));
|
||||
|
||||
|
||||
20
src/URL.cpp
20
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)
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
//
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <string>
|
||||
#include <frnetlib/URL.h>
|
||||
|
||||
TEST(URLTest, full_parse)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user