Commit bd7cdd7c authored by Thomas Bailleux's avatar Thomas Bailleux
Browse files

add doc

parent 245084b2
CC = g++
CXXFLAGS = -Iinclude/ -fPIC -Wall -Wextra -Werror -W -O2 -g -lboost_system -lcurl
LDFLAGS = -Iinclude/ -shared
CXX = /usr/bin/clang++
CXXFLAGS = -std=c++17 -Iinclude/ -fPIC -Wall -Wextra -Werror -W -O2 -g
LDFLAGS = -Iinclude/ -shared -lboost_system -lcurl
TARGET_LIB = libsms.so
SRCS = src/http_parser.cc src/server.cc src/sms.cc
......@@ -10,15 +10,15 @@ OBJS = $(SRCS:.cc=.o)
all: ${TARGET_LIB}
$(TARGET_LIB): $(OBJS)
$(CC) ${LDFLAGS} -o $@ $^
$(CXX) ${LDFLAGS} -o $@ $^
$(SRCS:.cc=.d):%.d:%.cc
$(CC) $(CXXFLAGS) -MM $< >$@
$(CXX) -MM $(CXXFLAGS) $< > $@
include $(SRCS:.cc=.d)
test: all
$(CC) -o simple_echo_port_9000 tests/simple_echo.cc -lsms -lboost_system
$(CXX) -o simple_echo_port_9000 tests/simple_echo.cc -lsms -lboost_system
install: all
mv ${TARGET_LIB} /usr/lib/ && cp -r include/sms/ /usr/include/
......
/** @headerfile */
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
......@@ -31,34 +32,70 @@
#define BAD_METHOD -0x1
#define BAD_REQUEST -0x2
/**
* @brief main libsms namespace.
*/
namespace sms {
/**
* @brief An HTTP header.
*/
class Header {
public:
/**
* Name of the HTTP header
*/
std::string m_name;
/**
* Value of the HTTP header
*/
std::string m_value;
};
/**
* @brief Parser for HTTP requests
*/
class HttpParser {
public:
/**
* @brief Construct a HTTP parser.
*
* @param buffer - Buffer which contains the HTTP request
* @param buffer_length - Length of the buffer
*/
HttpParser(char *buffer, size_t buffer_length): m_buffer(buffer),
m_buffer_length(buffer_length),
m_curl_easy_handle(curl_easy_init()){};
/**
* @brief Parse the buffer which contains the HTTP request.
*
* @return An error code
*/
int parse();
/**
* @brief Get a list of the header from the HTTP request.
*
* @return Vector of Headers
*/
std::vector<Header>& get_headers();
~HttpParser();
private:
std::string m_method;
std::string m_path;
std::vector<Header> m_headers;
char *m_buffer;
size_t m_buffer_length;
[[maybe_unused]] size_t m_buffer_length;
CURL* m_curl_easy_handle;
int parse_first_line(std::string& line);
int parse_header(std::string& line);
void uri_decode(std::string& data);
public:
HttpParser(char *buffer, size_t buffer_length): m_buffer(buffer),
m_buffer_length(buffer_length),
m_curl_easy_handle(curl_easy_init()){};
int parse();
std::vector<Header>& get_headers();
~HttpParser();
};
} /* namespace sms */
......
/** @headerfile */
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
......@@ -35,30 +36,102 @@
namespace sms {
/**
* @brief Events for the HTTP server.
*/
enum Event {
/**
* A SMS arrived
*/
MESSAGE
};
/**
* @brief Exception thrown when user provided a bad configuration for the server
*/
class ServerConfigurationException {
private:
const char *m_errstr;
public:
/**
* @brief Construct an ServerConfigurationException with the string-representation of the error.
*
* @param errstr String-representation of the error
*/
ServerConfigurationException(const char *errstr): m_errstr(errstr) {};
/**
* @brief Get the string-representation of the error.
*
* @return string-representation of the error
*/
const char* what();
private:
/**
* String-representation of the error
*/
const char *m_errstr;
};
/**
* @brief An Handler for a certain type of event
*/
class Handler {
public:
/**
* Event which is handle by the handler.
*/
Event m_event;
/**
* Callback which will be called when the event is fired.
*/
std::function<void(std::unique_ptr<Sms> sms)> m_callback;
};
class Connection;
/**
* @brief A HTTP-Server for receiving the SMS.
*/
class Server {
friend class Connection;
public:
/**
* @brief Construct a HTTP-Server for receiving the SMS.
*
* @param host - Host for binding the server
* @param port - Port for the server
*/
Server(std::string& host, unsigned short port);
/**
* @brief Get the ASIO context instance.
*
* @return ASIO context instance
*/
boost::asio::io_service& get_io_service();
/**
* @brief Assign an Handler for a specific event.
*
* @param handler - Handler to be assigned
* @return true if the handler has been assigned successfully, otherwise else
*/
bool set_event_handler(std::unique_ptr<Handler> handler);
/**
* @brief Prepare the server for receiving incoming connections.
*/
void start_accept();
/**
* @brief Launch the server.
*/
void run();
private:
boost::asio::io_service m_io_service;
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
......@@ -71,18 +144,24 @@ class Server {
void remove_connection(Connection* connection);
void notify_on_sms_received(std::unique_ptr<Sms> sms);
public:
Server(std::string& host, unsigned short port);
boost::asio::io_service& get_io_service();
bool set_event_handler(std::unique_ptr<Handler> handler);
void start_accept();
void run();
};
/**
* @brief A connection to the HTTP-server
*/
class Connection: std::enable_shared_from_this<Connection> {
friend class Server;
public:
/**
* @brief Construct a connection.
*
* @param server - Server which handle this connection
*/
explicit Connection(Server& server): m_socket(server.get_io_service()),
m_server(server),
m_data{0},
m_uuid(boost::uuids::random_generator()()) {}
private:
boost::asio::ip::tcp::socket m_socket;
Server& m_server;
......@@ -93,11 +172,6 @@ class Connection: std::enable_shared_from_this<Connection> {
boost::asio::ip::tcp::socket& get_socket();
void receive_http_request(const boost::system::error_code& error, size_t bytes_transferred);
void parse_http_request(size_t length);
public:
explicit Connection(Server& server): m_socket(server.get_io_service()),
m_server(server),
m_data{0},
m_uuid(boost::uuids::random_generator()()) {}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment