Skip to content

Instantly share code, notes, and snippets.

@garethlewin
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garethlewin/526c62faba37b37c4670 to your computer and use it in GitHub Desktop.
Save garethlewin/526c62faba37b37c4670 to your computer and use it in GitHub Desktop.
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