72 lines
1.3 KiB
C++
72 lines
1.3 KiB
C++
#include "HttpRequest.h"
|
|
|
|
#include "StringUtils.h"
|
|
#include "HttpParser.h"
|
|
|
|
#include <sstream>
|
|
|
|
HttpRequest::HttpRequest(Verb verb, const String& path)
|
|
: mVerb(verb)
|
|
{
|
|
mPreamble.mPath = path;
|
|
}
|
|
|
|
HttpRequest::Verb HttpRequest::getVerb() const
|
|
{
|
|
return mVerb;
|
|
}
|
|
|
|
String HttpRequest::getPath() const
|
|
{
|
|
return mPreamble.mPath;
|
|
}
|
|
|
|
String HttpRequest::toString(const String& host) const
|
|
{
|
|
String out;
|
|
|
|
if (mVerb == Verb::GET)
|
|
{
|
|
out += "GET";
|
|
}
|
|
|
|
auto path = mPreamble.mPath;
|
|
out += " /" + path + " HTTP/" + mHeader.getHttpVersion() + "\n";
|
|
out += "Host: " + host + "\n";
|
|
out += "Accept - Encoding: \n";
|
|
return out;
|
|
}
|
|
|
|
void HttpRequest::fromString(const String& message)
|
|
{
|
|
Stringstream ss(message);
|
|
|
|
String buffer;
|
|
bool firstLine{ true };
|
|
|
|
Vector<String> headers;
|
|
while (std::getline(ss, buffer, '\n'))
|
|
{
|
|
if (firstLine)
|
|
{
|
|
HttpParser::parsePreamble(buffer, mPreamble);
|
|
firstLine = false;
|
|
}
|
|
else
|
|
{
|
|
headers.push_back(buffer);
|
|
}
|
|
}
|
|
if (mPreamble.mMethod == "GET")
|
|
{
|
|
mVerb = Verb::GET;
|
|
}
|
|
mHeader.parse(headers);
|
|
|
|
mRequiredBytes = 0;
|
|
}
|
|
|
|
size_t HttpRequest::requiredBytes() const
|
|
{
|
|
return mRequiredBytes;
|
|
}
|