summaryrefslogtreecommitdiffhomepage
path: root/libcommon/url.cpp
blob: 5baf603c6c8bf2c4f740b048847f830f4fb1b1dd (plain)
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
#include "url.h"

std::string urlDecode(std::string s)
{
 std::string result;

 size_t pos = 0;
 while (pos < s.size()) {
  char c {s[pos]};
  if (c == '+') {
   result += ' ';
  } else if (c == '%' && pos + 2 < s.size()) {
   try {
    int i = stoi(s.substr(pos + 1, 2), 0, 16);
    if (i < 0 || i > 255)
     return result;

    result += static_cast<char>(i);
   } catch (...) {
    return result;
   }

   pos += 2;
  } else {
   result += c;
  }
  pos++;
 }

 return result;
}