/* MediaMtx (C++) - small HTTP/1.1 server (MediaMTX: internal/protocols/httpp) Used by the WebRTC (WHEP) server and the control API. One thread per connection, keep-alive, Content-Length bodies only (no chunked uploads), small requests (signalling / API). */ #pragma once #include #include #include #include #include #include #include #include #include "../../logger/logger.h" namespace mtx { struct HttpRequest { std::string method; std::string path; // decoded, without the query std::string query; std::map headers; // lower case names std::string body; std::string remoteAddr; std::string header(const std::string & lowerName) const { auto it = headers.find(lowerName); return it == headers.end() ? std::string() : it->second; } }; struct HttpResponse { int status = 200; std::vector> headers; std::string body; void setHeader(const std::string & name, const std::string & value) { headers.emplace_back(name, value); } }; class HttpServer { public: typedef std::function Handler; explicit HttpServer(const LogParent * parent) : m_pParent(parent) {} ~HttpServer() { stop(); } bool start(const std::string & address, Handler handler, std::string & err); void stop(); private: void acceptLoop(); void serveConnection(int fd, std::string remote); const LogParent * m_pParent; Handler m_handler; int m_nListenFd = -1; int m_aWake[2] = {-1, -1}; std::atomic m_bRun{false}; std::atomic m_nConnections{0}; std::thread m_thread; std::mutex m_connMutex; std::vector m_vConnFds; }; const char * httpStatusText(int status); } // namespace mtx