decode percent encoded URIs

This commit is contained in:
badaix 2016-02-08 21:49:02 +01:00
parent cf628bfcd6
commit 538352fbf2
4 changed files with 31 additions and 8 deletions

View file

@ -33,12 +33,12 @@ Log::Log(std::string ident, int facility)
std::string Log::Timestamp()
{
struct tm * dt;
char buffer [30];
struct tm * dt;
char buffer [30];
std::time_t t = std::time(nullptr);
dt = localtime(&t);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H-%M-%S", dt);
return std::string(buffer);
dt = localtime(&t);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H-%M-%S", dt);
return std::string(buffer);
}

View file

@ -81,6 +81,29 @@ static inline std::string trim_copy(const std::string &s)
return trim(str);
}
// decode %xx to char
static std::string uriDecode(const std::string& src) {
std::string ret;
char ch;
for (size_t i=0; i<src.length(); i++)
{
if (int(src[i]) == 37)
{
unsigned int ii;
sscanf(src.substr(i+1, 2).c_str(), "%x", &ii);
ch = static_cast<char>(ii);
ret += ch;
i += 2;
}
else
{
ret += src[i];
}
}
return (ret);
}
static std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{

View file

@ -33,8 +33,9 @@ ReaderUri::ReaderUri(const std::string& uri)
// would be more elegant with regex. Not yet supported on my dev machine's gcc 4.8 :(
size_t pos;
this->uri = uri;
string decodedUri = uriDecode(uri);
id_ = uri;
id_ = decodedUri;
pos = id_.find('?');
if (pos != string::npos)
id_ = id_.substr(0, pos);
@ -43,7 +44,7 @@ ReaderUri::ReaderUri(const std::string& uri)
id_ = id_.substr(0, pos);
logD << "id: '" << id_ << "'\n";
string tmp(uri);
string tmp(decodedUri);
pos = tmp.find(':');
if (pos == string::npos)

View file

@ -50,7 +50,6 @@ struct ReaderUri
private:
std::string id_;
};