Use url parsing
This commit is contained in:
@@ -27,8 +27,17 @@ namespace Http
|
||||
std::string requestTypeString;
|
||||
ss >> requestTypeString;
|
||||
request.requestType = ToEnum<HttpRequest::Type>(requestTypeString, HttpRequest::typeStrings);
|
||||
if (request.requestType == HttpRequest::Type::UNKNOWN)
|
||||
{
|
||||
throw std::runtime_error("Bad request type");
|
||||
}
|
||||
|
||||
ss >> request.path;
|
||||
std::string rawUrl;
|
||||
ss >> rawUrl;
|
||||
if(!request.url.TryParseFromUrlString(rawUrl))
|
||||
{
|
||||
throw std::runtime_error("Bad url in request");
|
||||
}
|
||||
|
||||
std::string httpProtocolString;
|
||||
ss >> httpProtocolString;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#pragma once
|
||||
#include "../constants/httprequest.hpp"
|
||||
#include "url.hpp"
|
||||
|
||||
namespace Http
|
||||
{
|
||||
struct Request
|
||||
{
|
||||
HttpRequest::Type requestType;
|
||||
std::string path;
|
||||
Url url;
|
||||
|
||||
static Request Deserialize(std::vector<char> const & bytes);
|
||||
};
|
||||
|
||||
53
src/http/url.cpp
Normal file
53
src/http/url.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "url.hpp"
|
||||
|
||||
namespace Http
|
||||
{
|
||||
bool Url::HasPath() const
|
||||
{
|
||||
return path.size() > 1;
|
||||
}
|
||||
|
||||
bool Url::HasQuery() const
|
||||
{
|
||||
return query.size() > 1;
|
||||
}
|
||||
|
||||
std::string const & Url::GetPath() const
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
std::string const & Url::GetQuery() const
|
||||
{
|
||||
return query;
|
||||
}
|
||||
|
||||
bool Url::TryParseFromUrlString(std::string urlstring)
|
||||
{
|
||||
unsigned queryPos = 0;
|
||||
static std::string const validSpecialCharacters = "-._~:/?#[]@!$&'()*+,;=";
|
||||
for(unsigned i = 0; i < urlstring.size(); ++i)
|
||||
{
|
||||
if (!std::isalnum(urlstring[i]) && validSpecialCharacters.find(urlstring[i]) == std::string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (urlstring[i] == '?' && queryPos == 0)
|
||||
{
|
||||
queryPos = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (queryPos == 0)
|
||||
{
|
||||
path = urlstring;
|
||||
}
|
||||
else
|
||||
{
|
||||
path = urlstring.substr(0, queryPos);
|
||||
query = urlstring.substr(queryPos, urlstring.size());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
21
src/http/url.hpp
Normal file
21
src/http/url.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace Http
|
||||
{
|
||||
class Url
|
||||
{
|
||||
private:
|
||||
std::string path;
|
||||
std::string query;
|
||||
|
||||
public:
|
||||
bool HasPath() const;
|
||||
bool HasQuery() const;
|
||||
|
||||
std::string const & GetPath() const;
|
||||
std::string const & GetQuery() const;
|
||||
|
||||
bool TryParseFromUrlString(std::string urlstring);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user