X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Server%2FHTTPRequest.cc;fp=Server%2FHTTPRequest.cc;h=4fb444e1e583fc2fd44b80d4f435b2f8cfa10bf8;hb=c05982737183fe02022b3e5e7948d58bc13bf953;hp=0000000000000000000000000000000000000000;hpb=3c4b5e4e5f828331621e3c2d816a245cf731aa0f;p=mediaserv.git diff --git a/Server/HTTPRequest.cc b/Server/HTTPRequest.cc new file mode 100644 index 0000000..4fb444e --- /dev/null +++ b/Server/HTTPRequest.cc @@ -0,0 +1,105 @@ +// $Id$ +// +// Copyright (C) 2006 + +// Definition of non-inline non-template functions + +#include "HTTPRequest.hh" +//#include "HTTPRequest.ih" + +// Custom includes +#include +#include +#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(handle).peer().host(); + } + catch (std::bad_cast const *) { + host_ = "(unidentified address)"; + } + boost::algorithm::split_iterator + line (request, boost::algorithm::first_finder("\n")); + boost::algorithm::split_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 line) +{ + boost::algorithm::split_iterator + token (line, boost::algorithm::first_finder(" ")); + boost::algorithm::split_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 line) +{ + boost::iterator_range 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" + + +// Local Variables: +// mode: c++ +// End: