From 6f213b0a8abba8d3ca85e688714c91755741f82c Mon Sep 17 00:00:00 2001 From: Pixel Date: Sun, 13 Nov 2011 15:56:31 -0700 Subject: Adding the skeleton of an HTTP server; still work in progress though: it still needs coding, and won't work as it. And it needs a unit test. --- src/HttpServer.cc | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/HttpServer.cc (limited to 'src') diff --git a/src/HttpServer.cc b/src/HttpServer.cc new file mode 100644 index 0000000..acfc9d2 --- /dev/null +++ b/src/HttpServer.cc @@ -0,0 +1,76 @@ +#include "HttpServer.h" +#include "Socket.h" +#include "BStream.h" + +static const ev_tstamp s_httpTimeout = 5; + +namespace Balau { + +class HttpWorker : public Task { + public: + HttpWorker(IO & io, void * server) : m_socket(io), m_parent((HttpServer *) server), m_evtTimeout(s_httpTimeout) { m_name.set("HttpWorker(%s)", m_socket->getName()); } + private: + virtual void Do(); + virtual const char * getName(); + IO m_socket; + String m_name; + HttpServer * m_parent; + Events::Timeout m_evtTimeout; +}; + +}; + +void Balau::HttpWorker::Do() { + waitFor(&m_evtTimeout); + setOkayToEAgain(true); + + String line; + IO strm(m_socket); + bool gotFirst = false; + + do { + try { + line = strm->readString(); + } + catch (EAgain) { + if (m_evtTimeout.gotSignal()) { + // do some log + return; + } + yield(); + continue; + } + if (line == "") + break; + + if (!gotFirst) { + gotFirst = true; + // parse request method, URL and HTTP version. + } else { + // parse HTTP header. + } + } while(true); + + if (!gotFirst) { + // do some log + return; + } +} + +const char * Balau::HttpWorker::getName() { + return m_name.to_charp(); +} + +typedef Balau::Listener HttpListener; + +void Balau::HttpServer::start() { + Assert(!m_started); + m_listenerPtr = new HttpListener(m_port, m_local.to_charp()); + m_started = true; +} + +void Balau::HttpServer::stop() { + Assert(m_started); + reinterpret_cast(m_listenerPtr)->stop(); + m_started = false; +} -- cgit v1.2.3