URI parsing

This commit is contained in:
BENEDEK László 2023-12-05 18:58:39 +01:00
parent c62c4f4ff9
commit c567bb2447
16 changed files with 210 additions and 61 deletions

View File

@ -4,16 +4,18 @@ project(Rum VERSION 1.0)
# compiler setup
set(CMAKE_CXX_COMPILER "clang++")
#set(CMAKE_CXX_STANDARD 23)
#set(CMAKE_CXX_STANDARD_REQUIRED true)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror") # warnings
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2b") # std c++23
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror") # warnings and errors
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2b") # std c++23 for clang
if (STATIC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") # static
endif()
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") # static compile
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") # debug symbols
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2") # enable optimisations
if (DEBUG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") # debug symbols
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2") # optimisations
# IDE setup (clangd)
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
@ -29,3 +31,8 @@ add_custom_target(run
DEPENDS server.bin
COMMENT "Running server.bin"
)
add_custom_target(docker
COMMAND docker build -t dowerx/rum:${CMAKE_PROJECT_VERSION} ..
COMMENT "Building docker image"
)

View File

@ -4,6 +4,3 @@
#include "request.h"
#include "response.h"
#include "server.h"
#define WORKERS 10
#define BUFFER_LEN 8196

View File

@ -5,5 +5,5 @@ namespace Rum::HTTP {
enum Method { GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE };
std::string method_to_string(Method m);
std::string to_string(Method method);
} // namespace Rum::HTTP

View File

@ -3,15 +3,16 @@
#include <map>
#include <string>
#include "method.h"
#include "rum/http/uri.h"
namespace Rum::HTTP {
class Request {
public:
Request() : method(GET), path(""), body("") {}
Request(Method method, std::string path) : method(method), path(path), body("") {}
Request() : method(GET), uri(URI()), body("") {}
Request(Method method, const URI& uri) : method(method), uri(uri), body("") {}
Method get_method() const { return method; }
std::string get_path() const { return path; }
const URI& get_uri() const { return uri; }
const std::map<std::string, std::string>& get_headers() const { return headers; }
std::string get_header(std::string name) const { return headers.at(name); }
@ -26,7 +27,7 @@ class Request {
private:
Method method;
std::string path;
URI uri;
std::map<std::string, std::string> headers;
std::string body;
};

View File

@ -8,10 +8,11 @@ class Response {
const int client_sock;
bool sent_header;
bool sent_body;
bool closed;
unsigned int code;
public:
Response(int client_sock) : client_sock(client_sock), sent_header(false), sent_body(false), code(200) {}
Response(int client_sock) : client_sock(client_sock), sent_header(false), sent_body(false), closed(false), code(200) {}
~Response();
void send_header(const std::string& name, const std::string& value);

View File

@ -11,8 +11,8 @@
#include "request.h"
#include "response.h"
#define WORKERS 10
#define BUFFER_LEN 8196
#define DEFAULT_WORKER_COUNT 10
#define DEFAULT_BUFFER_SIZE 8196
namespace Rum::HTTP {
@ -25,6 +25,8 @@ class Server : public Rum::TCP::Server {
sockaddr_in sockaddr;
};
const size_t buffer_size;
std::vector<std::thread> workers;
std::queue<Task> tasks;
std::mutex mtx;
@ -36,8 +38,8 @@ class Server : public Rum::TCP::Server {
void handler(int client_sock, const sockaddr_in& client_address, char* buffer);
public:
Server(unsigned int port, size_t worker_count);
Server(unsigned int port) : Server(port, WORKERS) {}
Server(unsigned int port, size_t worker_count, size_t buffer_size);
Server(unsigned int port) : Server(port, DEFAULT_WORKER_COUNT, DEFAULT_BUFFER_SIZE) {}
~Server();
void listen();

34
inc/rum/http/uri.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include <format>
#include <map>
#include <string>
namespace Rum::HTTP {
class URI {
private:
std::string scheme;
std::string user;
std::string password;
std::string host;
std::string port;
std::string path;
std::map<std::string, std::string> parameters;
std::string fragment;
public:
URI(const std::string& uri);
URI() {}
const std::string& get_scheme() const { return scheme; }
const std::string& get_user() const { return user; }
const std::string& get_password() const { return password; }
const std::string& get_host() const { return host; }
const std::string& get_port() const { return port; }
const std::string& get_path() const { return path; }
const std::map<std::string, std::string>& get_parameters() const { return parameters; }
const std::string& get_fragment() const { return fragment; }
operator std::string() const;
};
} // namespace Rum::HTTP

View File

@ -4,16 +4,22 @@
#include <unistd.h>
#include <functional>
#define MAX_PENDING 10
namespace Rum::TCP {
class Server {
private:
const int sock;
unsigned short port;
bool stop;
public:
Server(unsigned short port);
virtual ~Server();
void listen(std::function<void(int, sockaddr_in)> handler) const;
unsigned short get_port() const { return port; }
void listen(const std::function<void(int, sockaddr_in)>& handler) const;
virtual void end() {
stop = true;
close(sock);

View File

@ -5,6 +5,6 @@
namespace Rum::TCP {
std::string address_to_string(sockaddr_in address);
std::string to_string(const sockaddr_in& address);
} // namespace Rum::TCP

View File

@ -1,8 +1,8 @@
#include <rum/http/method.h>
namespace Rum::HTTP {
std::string method_to_string(Method m) {
switch (m) {
std::string to_string(Method method) {
switch (method) {
case GET:
return "GET";
case HEAD:

View File

@ -1,18 +1,24 @@
#include <rum/http/request.h>
#include <format>
#include <iostream>
#include <string>
#include "rum/http/method.h"
namespace Rum::HTTP {
Request::operator std::string() const {
std::string result("Request{\n\tmethod: " + method_to_string(method) + "\n\tpath: \"" + path + "\"\n\theaders:\n");
for (auto header : get_headers()) {
result += "\t\t\"" + header.first + "\": \"" + header.second + "\"\n";
std::string headers_string;
for (auto header : headers) {
headers_string += "\t\t'" + header.first + "': '" + header.second + "'\n";
}
result += "\tbody: \"" + body + "\"\n}";
return result;
return std::vformat(
"Request{{\n"
"\tmethod: {}\n"
"\t{}\n"
"\theaders: \n{}"
"\tbody: \n'{}'"
"}}",
std::make_format_args(to_string(method), (std::string)uri, headers_string, body));
}
std::ostream& operator<<(std::ostream& stream, const Request req) {

View File

@ -5,8 +5,8 @@
#include <stdexcept>
namespace Rum::HTTP {
std::string status_code_to_string(int code) {
static std::map<int, std::string> codes{{100, "Continue"},
std::string to_string(int code) {
static const std::map<int, std::string> codes{{100, "Continue"},
{101, "Switching Protocols"},
{102, "Processing"},
{103, "Early Hints"},
@ -77,16 +77,20 @@ std::string status_code_to_string(int code) {
void Response::send_header(const std::string& name, const std::string& value) {
if (!sent_header) {
std::string resp("HTTP/1.1 " + std::to_string(code) + " " + status_code_to_string(code) + "\r\n");
if (-1 == send(client_sock, resp.c_str(), resp.size(), 0))
std::string resp("HTTP/1.1 " + std::to_string(code) + " " + to_string(code) + "\r\n");
if (-1 == send(client_sock, resp.c_str(), resp.size(), 0)) {
closed = true;
throw TCP::Error(TCP::Error::CLOSED);
}
sent_header = true;
}
std::string header = name + ": " + value + "\r\n";
if (-1 == send(client_sock, header.c_str(), header.size(), 0))
if (-1 == send(client_sock, header.c_str(), header.size(), 0)) {
closed = true;
throw TCP::Error(TCP::Error::CLOSED);
}
}
void Response::send_body(const std::string& value) {
if (!sent_header) {
@ -94,18 +98,22 @@ void Response::send_body(const std::string& value) {
}
if (!sent_body) {
if (-1 == send(client_sock, "\r\n", 2, 0))
if (-1 == send(client_sock, "\r\n", 2, 0)) {
closed = true;
throw TCP::Error(TCP::Error::CLOSED);
}
sent_body = true;
}
if (-1 == send(client_sock, value.c_str(), value.size(), 0))
if (-1 == send(client_sock, value.c_str(), value.size(), 0)) {
closed = true;
throw TCP::Error(TCP::Error::CLOSED);
}
}
Response::~Response() {
if (!sent_header) {
std::string resp("HTTP/1.1 " + std::to_string(code) + " " + status_code_to_string(code));
if (!sent_header && !closed) {
std::string resp("HTTP/1.1 " + std::to_string(code) + " " + to_string(code));
send(client_sock, resp.c_str(), resp.size(), 0);
}
}

View File

@ -1,14 +1,16 @@
#include "rum/tcp/server.h"
#include <rum/http/server.h>
#include <rum/tcp/error.h>
#include <rum/tcp/utility.h>
#include <iostream>
#include <regex>
#include <string>
namespace Rum::HTTP {
Server::Server(unsigned int port, size_t worker_count) : Rum::TCP::Server(port), stop(false) {
Server::Server(unsigned int port, size_t worker_count, size_t buffer_size) : Rum::TCP::Server(port), buffer_size(buffer_size), stop(false) {
for (size_t i = 0; i < worker_count; i++) {
std::thread worker([this, i]() {
char* buffer = new char[BUFFER_LEN]();
char* buffer = new char[this->buffer_size]();
while (true) {
Task task;
@ -30,7 +32,7 @@ Server::Server(unsigned int port, size_t worker_count) : Rum::TCP::Server(port),
handler(task.client_sock, task.sockaddr, buffer);
if (int status = close(task.client_sock); status == TCP::Error::UNKNOWN) {
std::cerr << TCP::address_to_string(task.sockaddr) << ": " << TCP::Error((TCP::Error::Type)status).what() << std::endl;
std::cerr << TCP::to_string(task.sockaddr) << ": " << TCP::Error((TCP::Error::Type)status).what() << std::endl;
}
}
});
@ -85,7 +87,7 @@ Method string_to_method(std::string text) {
}
void Server::handler(int client_sock, const sockaddr_in& client_address, char* buffer) {
std::string address = TCP::address_to_string(client_address);
std::string address = TCP::to_string(client_address);
std::cout << address << ": connected" << std::endl;
Request request;
@ -96,7 +98,7 @@ void Server::handler(int client_sock, const sockaddr_in& client_address, char* b
std::string message;
while (true) {
ssize_t recieved = recv(client_sock, buffer, BUFFER_LEN, 0);
ssize_t recieved = recv(client_sock, buffer, buffer_size, 0);
switch (recieved) {
case TCP::Error::CLOSED:
std::cout << address << ": connection closed" << std::endl;
@ -109,10 +111,11 @@ void Server::handler(int client_sock, const sockaddr_in& client_address, char* b
message += std::string(buffer, buffer + recieved);
if (stage == METHOD && message.contains("\r\n")) {
std::regex method_regex("(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE) (\\/.*) .*");
std::regex method_regex("(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE) (\\/.*) HTTP.*");
std::smatch match;
if (std::regex_match(message.cbegin(), message.cbegin() + message.find("\r\n"), match, method_regex)) {
request = Request(string_to_method(match.str(1)), match.str(2));
// create request object
request = Request(string_to_method(match.str(1)), URI("http://0.0.0.0:" + std::to_string(TCP::Server::get_port()) + match.str(2)));
message = message.substr(message.find("\r\n"));
stage = HEADER;
} else {
@ -148,9 +151,10 @@ void Server::handler(int client_sock, const sockaddr_in& client_address, char* b
bool found = false;
for (auto it = paths[request.get_method()].cbegin(); it != paths[request.get_method()].cend(); it++) {
if (std::regex_match(request.get_path(), it->first)) {
if (std::regex_match(request.get_uri().get_path(), it->first)) {
it->second(request, resp);
found = true;
break;
}
}

64
src/rum/http/uri.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <rum/http/uri.h>
#include <exception>
#include <iostream>
#include <regex>
#include <string>
#define MATCH(var, i) \
if (match[i].matched) \
var = match.str(i);
namespace Rum::HTTP {
URI::operator std::string() const {
std::string query;
for (auto param : parameters) {
query += "\t\t'" + param.first + "': '" + param.second + "'\n";
}
return std::vformat(
"URI{{"
"\n\tscheme: '{}'"
"\n\tuser: '{}'"
"\n\tpassword: '{}'"
"\n\thost: '{}'"
"\n\tport: '{}'"
"\n\tpath: '{}'"
"\n\tquery: \n{}"
"\n\tfragment: '{}'"
"\n}}",
std::make_format_args(scheme, user, password, host, port, path, query, fragment));
}
URI::URI(const std::string& uri) {
std::regex uri_regex(R"((([\w\d]+):\/\/)?(([\w\d]+)(:([\w\d]+)?)@)?([\w\d\.]+)(:(\d+))?(\/?[\w\d\.\/]+)?(\?([\w\d\=\&]+))?(\#([\w\d]*))?)");
std::smatch match;
std::cout << uri << std::endl;
if (std::regex_match(uri.cbegin(), uri.cend(), match, uri_regex)) {
MATCH(scheme, 2)
MATCH(user, 4)
MATCH(password, 6)
MATCH(host, 7)
MATCH(port, 9)
MATCH(path, 10)
std::string query;
MATCH(query, 12)
MATCH(fragment, 14)
std::stringstream parameters_string(query);
std::string parameter;
while (std::getline(parameters_string, parameter, '&')) {
if (size_t pos = parameter.find('='); pos != parameter.npos) {
parameters[parameter.substr(0, pos)] = parameter.substr(pos + 1);
} else {
parameters[parameter] = "";
}
}
} else {
throw std::exception();
}
}
} // namespace Rum::HTTP

View File

@ -3,21 +3,22 @@
#include <rum/tcp/server.h>
#include <sys/socket.h>
#include <unistd.h>
#include <csignal>
#include <functional>
#include <string>
#define MAX_PENDING 10
namespace Rum::TCP {
std::string address_to_string(sockaddr_in address) {
std::string to_string(const sockaddr_in& address) {
return std::string(inet_ntoa(address.sin_addr)) + ":" + std::to_string(ntohs(address.sin_port));
}
Server::Server(unsigned short port) : sock(socket(AF_INET, SOCK_STREAM, 0)), stop(false) {
Server::Server(unsigned short port) : sock(socket(AF_INET, SOCK_STREAM, 0)), port(port), stop(false) {
if (sock == -1)
throw Error(Error::UNKNOWN);
std::signal(SIGPIPE, SIG_IGN);
sockaddr_in address = {.sin_family = AF_INET, .sin_port = htons(port), .sin_addr = {.s_addr = INADDR_ANY}};
if (bind(sock, (struct sockaddr*)&address, sizeof(address)) == -1)
@ -28,10 +29,9 @@ Server::~Server() {
end();
}
void Server::listen(std::function<void(int, sockaddr_in)> handler) const {
void Server::listen(const std::function<void(int, sockaddr_in)>& handler) const {
if (::listen(sock, MAX_PENDING) == -1)
throw Error(Error::UNKNOWN);
;
while (!stop) {
sockaddr_in client_address;

View File

@ -8,24 +8,43 @@ Rum::HTTP::Server* server;
int main(int argc, char** argv) {
Flags::Parser parser;
int* port = parser.add<int>("port", "tcp port number", true, 8080);
parser.parse(argc, argv);
int* port = parser.add<int>("port", "tcp port number", false, 8080);
unsigned long int* workers = parser.add<unsigned long int>("workers", "number of worker threads", false, 10);
bool* help = parser.add<bool>("help", "print help", false, false);
if (!parser.parse(argc, argv) || *help) {
parser.help();
return -1;
}
std::cout << "Port: " << *port << std::endl;
std::cout << "Workers: " << *workers << std::endl;
try {
server = new Rum::HTTP::Server(*port);
server = new Rum::HTTP::Server(*port, *workers, DEFAULT_BUFFER_SIZE);
std::signal(SIGINT, [](int) {
std::cout << "\nStopping server..." << std::endl;
server->end();
});
server->add_path<Rum::HTTP::GET>("/", [](const Rum::HTTP::Request&, Rum::HTTP::Response& resp) {
server->add_path<Rum::HTTP::GET>("/asd", [](const Rum::HTTP::Request& req, Rum::HTTP::Response& resp) {
std::cout << "request accepted" << std::endl;
resp.send_body("<h1>Hello World</h1>");
resp.send_body("<h1>asd</h1><pre>" + (std::string)req + "</pre>");
});
server->add_path<Rum::HTTP::GET>("/.*", [](const Rum::HTTP::Request& req, Rum::HTTP::Response& resp) {
std::cout << "request accepted" << std::endl;
resp.send_body("<h1>Hello World</h1><pre>" + (std::string)req + "</pre>");
});
server->listen();
delete server;
} catch (Rum::TCP::Error&) {
std::cerr << "Failed to bind port " << *port << std::endl;
return -1;
}
}