#pragma once // HTTP message types: one Request, one Response hierarchy with a subclass per // format. Subclasses are constructor conveniences over the base value type so // handlers can return by value without slicing loss. Gap: real molo::http_msg // in 05-request-routing.md owns the final API; this skeleton mirrors it. #include #include #include #include namespace molo::example::fw { enum class Method { Get, Post, Put, Patch, Delete, Head, Options, Propfind, Proppatch, Mkcol, Copy, Move, Lock, Unlock, Unknown, }; Method parse_method(std::string_view name); std::string method_name(Method method); class Headers { public: void set(std::string name, std::string value); std::string get(std::string_view name) const; bool has(std::string_view name) const; const std::vector>& entries() const; private: std::vector> entries_; }; struct Request { Method method = Method::Get; std::string path = "/"; std::map query; Headers headers; std::map cookies; std::map path_params; std::string body; std::string correlation_id; std::string query_or(std::string_view key, std::string_view fallback) const; std::string param_or(std::string_view key, std::string_view fallback) const; std::string cookie_or(std::string_view key, std::string_view fallback) const; }; struct Response { int status = 200; Headers headers; std::string body; Response() = default; Response(int status, std::string body, std::string content_type); std::string content_type() const; }; struct TextResponse : Response { explicit TextResponse(std::string body, int status = 200); }; struct HtmlResponse : Response { explicit HtmlResponse(std::string body, int status = 200); }; struct JsonResponse : Response { explicit JsonResponse(std::string body, int status = 200); }; struct XmlResponse : Response { explicit XmlResponse(std::string body, int status = 200); }; struct RedirectResponse : Response { RedirectResponse(std::string location, bool permanent); }; struct FileResponse : Response { FileResponse(std::string body, std::string content_type, std::string filename); }; struct StreamingResponse : Response { explicit StreamingResponse(std::vector chunks); std::vector chunks; }; struct SseResponse : Response { explicit SseResponse(std::vector events); std::vector events; }; std::string url_decode(std::string_view in); std::map parse_query(std::string_view qs); std::map parse_cookies(std::string_view header); std::map parse_form(std::string_view body); std::string status_text(int status); std::string json_escape(std::string_view in); } // namespace molo::example::fw