From 30c37eb549c17b29a8131598cc06aeaf374896f5 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Tue, 28 Feb 2012 04:57:59 +0100 Subject: Having a slightly better approach for writing Http responses. --- includes/HttpServer.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'includes/HttpServer.h') diff --git a/includes/HttpServer.h b/includes/HttpServer.h index 37353ac..7dbf912 100644 --- a/includes/HttpServer.h +++ b/includes/HttpServer.h @@ -9,6 +9,7 @@ #include #include #include +#include #include namespace Balau { @@ -18,6 +19,28 @@ class HttpWorker; class HttpServer { public: + class Response { + public: + Response(HttpServer * server, Http::Request req, IO out) : m_server(server), m_req(req), m_out(out), m_buffer(new Buffer()), m_responseCode(200), m_type("text/html; charset=UTF-8"), m_flushed(false) { } + void SetResponseCode(int code) { m_responseCode = code; } + void SetContentType(const String & type) { m_type = type; } + IO get() { return m_buffer; } + IO operator->() { return m_buffer; } + void Flush(); + void AddHeader(const String & line) { m_extraHeaders.push_front(line); } + void AddHeader(const String & key, const String & val) { AddHeader(key + ": " + val); } + private: + HttpServer * m_server; + Http::Request m_req; + IO m_out; + + IO m_buffer; + int m_responseCode; + String m_type; + std::list m_extraHeaders; + bool m_flushed; + }; + class Action { public: Action(const Regex & regex, const Regex & host = Regexes::any) : m_regex(regex), m_host(host), m_refCount(0) { } @@ -48,6 +71,7 @@ class HttpServer { Action::ActionMatch matches; }; ActionFound findAction(const char * uri, const char * host); + String getServerName() { return "Balau/1.0"; } private: bool m_started; void * m_listenerPtr; -- cgit v1.2.3