diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2012-02-28 04:57:59 +0100 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2012-02-28 04:57:59 +0100 |
commit | 30c37eb549c17b29a8131598cc06aeaf374896f5 (patch) | |
tree | 0105edcf5c8643c83a73f205577061686283a18e /includes | |
parent | 3998f46d06ec5235ade776ec8615e081dd909920 (diff) |
Having a slightly better approach for writing Http responses.
Diffstat (limited to 'includes')
-rw-r--r-- | includes/Http.h | 4 | ||||
-rw-r--r-- | includes/HttpServer.h | 24 |
2 files changed, 26 insertions, 2 deletions
diff --git a/includes/Http.h b/includes/Http.h index 8b0e313..62f8786 100644 --- a/includes/Http.h +++ b/includes/Http.h @@ -13,8 +13,7 @@ const char * getStatusMsg(int httpStatus); typedef std::map<String, String> StringMap; typedef std::map<String, IO<Handle> > FileList; -class Request { - public: +struct Request { int method; String host; String uri; @@ -22,6 +21,7 @@ class Request { StringMap headers; FileList files; bool persistent; + String version; }; enum { 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 <Exceptions.h> #include <Threads.h> #include <Handle.h> +#include <Buffer.h> #include <Http.h> namespace Balau { @@ -18,6 +19,28 @@ class HttpWorker; class HttpServer { public: + class Response { + public: + Response(HttpServer * server, Http::Request req, IO<Handle> 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<Buffer> get() { return m_buffer; } + IO<Buffer> 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<Handle> m_out; + + IO<Buffer> m_buffer; + int m_responseCode; + String m_type; + std::list<String> 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; |