forked from cat/WebBase
64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
#include <asio.hpp>
|
|
#include <asio/buffer.hpp>
|
|
#include <asio/error_code.hpp>
|
|
#include <asio/impl/read.hpp>
|
|
#include <asio/impl/read_until.hpp>
|
|
#include <asio/impl/write.hpp>
|
|
#include <asio/io_context.hpp>
|
|
#include <asio/ip/address.hpp>
|
|
#include <asio/ip/tcp.hpp>
|
|
#include <asio/placeholders.hpp>
|
|
#include <asio/streambuf.hpp>
|
|
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
#define IP "127.0.0.1"
|
|
#define PORT 8000
|
|
using asocket = asio::ip::tcp::socket;
|
|
using asocket_ptr = std::shared_ptr<asocket>;
|
|
|
|
// We handle HTTP requests with processHTTPHeader, writeData, and processRequest
|
|
class HTTPrequest : public std::enable_shared_from_this<HTTPrequest> {
|
|
public:
|
|
typedef std::shared_ptr<HTTPrequest> HTTPrequest_ptr;
|
|
|
|
asio::ip::tcp::socket &socket();
|
|
static HTTPrequest_ptr create(asio::io_context &context);
|
|
|
|
void start();
|
|
|
|
private:
|
|
HTTPrequest(asio::io_context &context);
|
|
void processRequest();
|
|
void writeData(std::string data);
|
|
|
|
// Breaking Header to in lines
|
|
void processHTTPHeader();
|
|
void inline processArgs();
|
|
void inline processHeaderValues(std::basic_istream<char> &stream,
|
|
std::size_t &octetCount,
|
|
std::size_t headerSize);
|
|
void inline processBody();
|
|
|
|
// Request itself
|
|
std::string requestType, requestPath;
|
|
std::unordered_map<std::string, std::string> headers, args;
|
|
std::string bodyContent;
|
|
|
|
// Networking
|
|
asio::ip::tcp::socket sock;
|
|
asio::streambuf buffer;
|
|
};
|
|
|
|
// Server manages connections/requests
|
|
class Webserver : public std::enable_shared_from_this<Webserver> {
|
|
public:
|
|
Webserver(asio::io_context &context);
|
|
|
|
private:
|
|
void begin();
|
|
asio::io_context &io;
|
|
asio::ip::tcp::acceptor accept;
|
|
};
|