#pragma once #include #include #include #include #include #include struct HTTPHeader { BAN::StringView name; BAN::StringView value; }; struct HTTPRequest { BAN::StringView method; BAN::StringView path; BAN::StringView version; BAN::Vector headers; BAN::ConstByteSpan body; }; class HTTPServer { public: HTTPServer(); ~HTTPServer(); BAN::ErrorOr initialize(BAN::StringView root, BAN::IPv4Address ip, int port); void start(); BAN::StringView web_root() const { return m_web_root.sv(); } private: BAN::ErrorOr get_http_request(BAN::Vector& data); BAN::ErrorOr send_http_response(int fd, unsigned status, BAN::ConstByteSpan, BAN::StringView mime); BAN::ErrorOr handle_request(int fd, BAN::Vector& data); // Returns false if the connection should be closed bool handle_all_requests(int fd, BAN::Vector& data); private: BAN::String m_web_root; int m_listen_socket { -1 }; BAN::HashMap> m_client_data; };