Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
void ParseHeader(const uint8_t* header,
size_t headerSize,
std::string& verb,
std::string& url,
urlParameters_t& params)
{
using std::begin;
using std::end;
using std::find;
using std::find_first_of;
using std::string;
const uint8_t urlDelims[] = {' ', '?'};
const uint8_t newlineDelims[] = {'\n', '\r'};
const uint8_t* pos = header;
const uint8_t* endOfLine = find_first_of(pos, pos + headerSize, begin(newlineDelims), end(newlineDelims));
if (endOfLine != header+headerSize)
{
verb = string(pos, find(pos, endOfLine, ' '));
pos += verb.length();
if (*pos++ == ' ')
{
url = string(pos, find_first_of(pos, endOfLine, begin(urlDelims), end(urlDelims)));
pos += url.length();
if (*pos++ == '?')
{
const uint8_t* endOfParams = find_first_of (pos, endOfLine, begin(newlineDelims), end(newlineDelims));
if (endOfParams != pos)
{
const uint8_t* delim = nullptr;
do
{
delim = find(pos, endOfParams, '&');
const uint8_t* equalSign = find(pos, delim, '=');
string paramName(pos, equalSign);
string paramValue;
if (equalSign != delim)
{
paramValue = string (equalSign+1, delim);
}
params.push_back( urlParameter_t(paramName, paramValue) );
pos = delim+1;
} while (delim != endOfParams);
}
}
}
url.append("blah");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment