| 902 | |
| 903 | |
| 904 | Try<string> decode(const string& s) |
| 905 | { |
| 906 | ostringstream out; |
| 907 | |
| 908 | for (size_t i = 0; i < s.length(); ++i) { |
| 909 | if (s[i] != '%') { |
| 910 | out << (s[i] == '+' ? ' ' : s[i]); |
| 911 | continue; |
| 912 | } |
| 913 | |
| 914 | // We now expect two more characters: "% HEXDIG HEXDIG" |
| 915 | if (i + 2 >= s.length() || !isxdigit(s[i+1]) || !isxdigit(s[i+2])) { |
| 916 | return Error( |
| 917 | "Malformed % escape in '" + s + "': '" + s.substr(i, 3) + "'"); |
| 918 | } |
| 919 | |
| 920 | // Convert from HEXDIG HEXDIG to char value. |
| 921 | istringstream in(s.substr(i + 1, 2)); |
| 922 | unsigned long l; |
| 923 | in >> std::hex >> l; |
| 924 | if (l > UCHAR_MAX) { |
| 925 | ABORT("Unexpected conversion from hex string: " + s.substr(i + 1, 2) + |
| 926 | " to unsigned long: " + stringify(l)); |
| 927 | } |
| 928 | out << static_cast<unsigned char>(l); |
| 929 | |
| 930 | i += 2; |
| 931 | } |
| 932 | |
| 933 | return out.str(); |
| 934 | } |
| 935 | |
| 936 | |
| 937 | Try<vector<Response>> decodeResponses(const string& s) |