48 lines
1.0 KiB
C++
48 lines
1.0 KiB
C++
#include "request.hpp"
|
|
#include <sstream>
|
|
|
|
namespace Http
|
|
{
|
|
template<class T>
|
|
T ToEnum(std::string const & str, std::vector<std::string> const & enumStrings)
|
|
{
|
|
for(unsigned i = 0; i < enumStrings.size(); ++i)
|
|
{
|
|
if (str.compare(enumStrings[i]) == 0)
|
|
{
|
|
return static_cast<T>(i);
|
|
}
|
|
}
|
|
|
|
return static_cast<T>(-1);
|
|
}
|
|
|
|
Http::Request Request::Deserialize(std::vector<char> const & bytes)
|
|
{
|
|
// TODO serialize more than just the start
|
|
Http::Request request;
|
|
|
|
std::stringstream ss(std::string(bytes.begin(), bytes.end()));
|
|
|
|
std::string requestTypeString;
|
|
ss >> requestTypeString;
|
|
request.type = ToEnum<HttpRequest::Type>(requestTypeString, HttpRequest::typeStrings);
|
|
if (request.type == HttpRequest::Type::UNKNOWN)
|
|
{
|
|
throw std::runtime_error("Bad request type");
|
|
}
|
|
|
|
std::string rawUrl;
|
|
ss >> rawUrl;
|
|
if(!request.url.TryParseFromUrlString(rawUrl))
|
|
{
|
|
throw std::runtime_error("Bad url in request");
|
|
}
|
|
|
|
std::string httpProtocolString;
|
|
ss >> httpProtocolString;
|
|
|
|
return request;
|
|
}
|
|
}
|