Make Hint encoding safe for passing them as GET parameter in URLs

Thanks @TheMarex for flagging this!
This commit is contained in:
Daniel J. Hofmann 2016-03-15 11:12:12 +01:00 committed by Patrick Niklaus
parent a7aa27c87c
commit b6b59e5c08

View File

@ -4,6 +4,9 @@
#include <boost/assert.hpp>
#include <iterator>
#include <algorithm>
namespace osrm
{
namespace engine
@ -18,13 +21,29 @@ bool Hint::IsValid(const util::Coordinate new_input_coordinates,
facade.GetCheckSum() == data_checksum;
}
std::string Hint::ToBase64() const { return encodeBase64Bytewise(*this); }
std::string Hint::ToBase64() const
{
auto base64 = encodeBase64Bytewise(*this);
// Make safe for usage as GET parameter in URLs
std::replace(begin(base64), end(base64), '+', '-');
std::replace(begin(base64), end(base64), '/', '_');
return base64;
}
Hint Hint::FromBase64(const std::string &base64Hint)
{
BOOST_ASSERT_MSG(base64Hint.size() == ENCODED_HINT_SIZE, "Hint has invalid size");
return decodeBase64Bytewise<Hint>(base64Hint);
// We need mutability but don't want to change the API
auto encoded = base64Hint;
// Reverses above encoding we need for GET parameters in URL
std::replace(begin(encoded), end(encoded), '-', '+');
std::replace(begin(encoded), end(encoded), '_', '/');
return decodeBase64Bytewise<Hint>(encoded);
}
}
}