forked from georgesofianosgr/HTTP-Web-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httputility.h
45 lines (38 loc) · 826 Bytes
/
httputility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef HTTPUTILITY_H
#define HTTPUTILITY_H
#include <sstream>
#include <string>
#include <cctype>
char from_hex(char ch)
{
return std::isdigit(ch) ? ch - '0' : std::tolower(ch) - 'a' + 10;
}
std::string url_decode(std::string text)
{
char h;
std::ostringstream escaped;
escaped.fill('0');
for (auto i = text.begin(), n = text.end(); i != n; ++i)
{
std::string::value_type c = (*i);
if (c == '%')
{
if (i[1] && i[2])
{
h = from_hex(i[1]) << 4 | from_hex(i[2]);
escaped << h;
i += 2;
}
}
else if (c == '+')
{
escaped << ' ';
}
else
{
escaped << c;
}
}
return escaped.str();
}
#endif // HTTPUTILITY_H