Finished version 0.1 of media server .. Jay !!!
[mediaserv.git] / Server / HTTPRequest.cc
diff --git a/Server/HTTPRequest.cc b/Server/HTTPRequest.cc
new file mode 100644 (file)
index 0000000..4fb444e
--- /dev/null
@@ -0,0 +1,105 @@
+// $Id$
+//
+// Copyright (C) 2006 
+
+// Definition of non-inline non-template functions
+
+#include "HTTPRequest.hh"
+//#include "HTTPRequest.ih"
+
+// Custom includes
+#include <boost/algorithm/string.hpp>
+#include <boost/range.hpp>
+#include "Socket/ClientSocketHandle.hh"
+#include "Socket/INetAddressing.hh"
+#include "Socket/CommunicationPolicy.hh"
+
+//#include "HTTPRequest.mpp"
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+prefix_ g0dil::mediaserv::HTTPRequest::HTTPRequest()
+{}
+
+prefix_ g0dil::mediaserv::HTTPRequest::HTTPRequest(satcom::lib::FileHandle handle,
+                                                   std::string const & request)
+{
+    parseRequest(handle,request);
+}
+
+prefix_ void g0dil::mediaserv::HTTPRequest::parseRequest(satcom::lib::FileHandle handle,
+                                                         std::string const & request)
+{
+    typedef satcom::lib::ClientSocketHandle< satcom::lib::MakeSocketPolicy< 
+       satcom::lib::INet4AddressingPolicy,
+       satcom::lib::ConnectedCommunicationPolicy >::policy> IPHandle;
+    try {
+       host_ = satcom::lib::dynamic_socket_cast<IPHandle>(handle).peer().host();
+    }
+    catch (std::bad_cast const *) {
+       host_ = "(unidentified address)";
+    }
+    boost::algorithm::split_iterator<std::string::const_iterator> 
+       line (request, boost::algorithm::first_finder("\n"));
+    boost::algorithm::split_iterator<std::string::const_iterator> line_end;
+    if (line == line_end) return;
+    parseRequestURL(*line);
+    for (++line; line != line_end; ++line)
+       parseRequestHeader(*line);
+}
+
+prefix_ void g0dil::mediaserv::HTTPRequest::
+parseRequestURL(boost::iterator_range<std::string::const_iterator> line)
+{
+    boost::algorithm::split_iterator<std::string::const_iterator> 
+       token (line, boost::algorithm::first_finder(" "));
+    boost::algorithm::split_iterator<std::string::const_iterator> token_end;
+
+    if (token == token_end)
+       throw InvalidHTTPRequestException();
+    method_ = std::string(token->begin(),token->end());
+    if (method_ != "GET")
+       throw InvalidHTTPRequestException();
+    if (++token == token_end)
+       throw InvalidHTTPRequestException();
+    url_ = std::string(token->begin(),token->end());
+    if (++token == token_end)
+       return;
+    version_ = std::string(token->begin(),token->end());
+    boost::trim(version_);
+    if (! boost::starts_with(version_,"HTTP/"))
+       throw InvalidHTTPRequestException();
+    if (++token != token_end)
+       throw InvalidHTTPRequestException();
+}
+
+prefix_ void g0dil::mediaserv::HTTPRequest::
+parseRequestHeader(boost::iterator_range<std::string::const_iterator> line)
+{
+    boost::iterator_range<std::string::const_iterator> i (boost::find_first(line,":"));
+    if (i.empty())
+       throw InvalidHTTPRequestException();
+    std::string key (line.begin(),i.begin());
+    std::string value (i.end(),line.end());
+    boost::to_lower(key);
+    boost::trim(value);
+    headers_.insert(std::make_pair(key,value));
+}
+
+prefix_ std::string const & g0dil::mediaserv::HTTPRequest::operator[](std::string const & key)
+    const
+{
+    static std::string empty;
+    Headers::const_iterator i (headers_.find(key));
+    if (i == headers_.end()) return empty;
+    return i->second;
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+//#include "HTTPRequest.mpp"
+
+\f
+// Local Variables:
+// mode: c++
+// End: