Skip to content

Commit

Permalink
decode all URIs, fixes #937, 386
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisOSRM committed Mar 4, 2014
1 parent 0baa821 commit 32bf99b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Server/RequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ RequestHandler::RequestHandler() : routing_machine(NULL) { }
void RequestHandler::handle_request(const http::Request& req, http::Reply& rep){
//parse command
try {
std::string request(req.uri);
std::string request;
URIDecode(req.uri, request);

time_t ltime;
struct tm *Tm;
Expand All @@ -61,7 +62,7 @@ void RequestHandler::handle_request(const http::Request& req, http::Reply& rep){
Tm->tm_min << ":" << (Tm->tm_sec < 10 ? "0" : "" ) <<
Tm->tm_sec << " " << req.endpoint.to_string() << " " <<
req.referrer << ( 0 == req.referrer.length() ? "- " :" ") <<
req.agent << ( 0 == req.agent.length() ? "- " :" ") << req.uri;
req.agent << ( 0 == req.agent.length() ? "- " :" ") << request;

RouteParameters route_parameters;
APIGrammarParser api_parser(&route_parameters);
Expand Down
31 changes: 31 additions & 0 deletions Util/StringUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/spirit/include/qi.hpp>

#include <cstdio>
#include <cctype>
#include <string>

// precision: position after decimal point
Expand Down Expand Up @@ -177,6 +178,36 @@ inline std::string HTMLDeEntitize( std::string & result) {
return result;
}

inline std::size_t URIDecode(const std::string & input, std::string & output) {
std::string::const_iterator src_iter = input.begin();
output.resize(input.size()+1);
std::size_t decoded_length = 0;
for( decoded_length = 0; src_iter != input.end(); ++decoded_length ) {
if(
src_iter[0] == '%' &&
src_iter[1] &&
src_iter[2] &&
isxdigit(src_iter[1]) &&
isxdigit(src_iter[2])
) {
std::string::value_type a = src_iter[1];
std::string::value_type b = src_iter[2];
a -= src_iter[1] < 58 ? 48 : src_iter[1] < 71 ? 55 : 87;
b -= src_iter[2] < 58 ? 48 : src_iter[2] < 71 ? 55 : 87;
output[decoded_length] = 16 * a + b;
src_iter += 3;
continue;
}
output[decoded_length] = *src_iter++;
}
output.resize(decoded_length);
return decoded_length;
}

inline std::size_t URIDecodeInPlace(std::string & URI) {
return URIDecode(URI, URI);
}

inline bool StringStartsWith(
const std::string & input,
const std::string & prefix
Expand Down

0 comments on commit 32bf99b

Please sign in to comment.