Maximum name length in "ge0" url is 256 now.

This commit is contained in:
vng 2013-05-17 22:27:19 +03:00 committed by Alex Zolotarev
parent 9d4f2c6bbe
commit 6c17666ef4
3 changed files with 19 additions and 14 deletions

View file

@ -11,8 +11,6 @@
#include "../base/string_utils.hpp"
static const int ZOOM_POSITION = 6;
url_api::Ge0Parser::Ge0Parser()
{
for (size_t i = 0; i < 256; ++i)
@ -34,6 +32,12 @@ bool url_api::Ge0Parser::Parse(string const & url, Request & request)
// || | | |
// ge0://ZCoordba64/Name
const int ZOOM_POSITION = 6;
const int LATLON_POSITION = ZOOM_POSITION + 1;
const int NAME_POSITON_IN_URL = 17;
const int LATLON_LENGTH = NAME_POSITON_IN_URL - LATLON_POSITION - 1;
const size_t MAX_NAME_LENGTH = 256;
request.Clear();
if (url.size() < 16 || !strings::StartsWith(url, "ge0://"))
return false;
@ -46,7 +50,7 @@ bool url_api::Ge0Parser::Parse(string const & url, Request & request)
request.m_points.push_back(url_api::Point());
url_api::Point & newPt = request.m_points.back();
DecodeLatLon(url.substr(7, 9), newPt.m_lat, newPt.m_lon);
DecodeLatLon(url.substr(LATLON_POSITION, LATLON_LENGTH), newPt.m_lat, newPt.m_lon);
ASSERT(MercatorBounds::ValidLon(newPt.m_lon), (newPt.m_lon));
ASSERT(MercatorBounds::ValidLat(newPt.m_lat), (newPt.m_lat));
@ -55,7 +59,8 @@ bool url_api::Ge0Parser::Parse(string const & url, Request & request)
request.m_viewportLon = newPt.m_lon;
if (url.size() >= NAME_POSITON_IN_URL)
newPt.m_name = DecodeName(url.substr(NAME_POSITON_IN_URL, url.size() - NAME_POSITON_IN_URL));
newPt.m_name = DecodeName(url.substr(NAME_POSITON_IN_URL,
min(url.size() - NAME_POSITON_IN_URL, MAX_NAME_LENGTH)));
return true;
}
@ -107,13 +112,12 @@ double url_api::Ge0Parser::DecodeLonFromInt(int const lon, int const maxValue)
return static_cast<double>(lon) / (maxValue + 1.0) * 360.0 - 180;
}
string url_api::Ge0Parser::DecodeName(string const & name)
string url_api::Ge0Parser::DecodeName(string name)
{
string resultName = name;
ValidateName(resultName);
resultName = UrlDecode(resultName);
SpacesToUnderscore(resultName);
return resultName;
ValidateName(name);
name = UrlDecode(name);
SpacesToUnderscore(name);
return name;
}
void url_api::Ge0Parser::SpacesToUnderscore(string & name)

View file

@ -2,7 +2,6 @@
#include "../base/base.hpp"
#include "../std/string.hpp"
static const int NAME_POSITON_IN_URL = 17;
namespace url_api
{
@ -23,7 +22,7 @@ protected:
void DecodeLatLonToInt(string const & url, int & lat, int & lon, int const bytes);
double DecodeLatFromInt(int const lat, int const maxValue);
double DecodeLonFromInt(int const lon, int const maxValue);
string DecodeName(string const & name);
string DecodeName(string name);
void SpacesToUnderscore(string & name);
void ValidateName(string & name);
static bool IsHexChar(char const a);

View file

@ -186,9 +186,11 @@ UNIT_TEST(NameDecoding)
TestSuccess("ge0://AwAAAAAAAA/%d0%9c%d0%b8%d0%bd%d1%81%d0%ba_%d1%83%d0%bb._%d0%9b%d0%b5%d0%bd%d0%b8%d0%bd%d0%b0_9", 0, 0, 4, "Минск ул. Ленина 9");
TestSuccess("ge0://AwAAAAAAAA/z%c3%bcrich_bahnhofstrasse", 0, 0, 4, "zürich bahnhofstrasse");
TestSuccess("ge0://AwAAAAAAAA/%e5%8c%97%e4%ba%ac_or_B%c4%9bij%c4%abng%3F", 0, 0, 4, "北京 or Běijīng?");
TestSuccess("ge0://AwAAAAAAAA/%d0%9a%d0%b0%d0%ba_%d0%b2%d1%8b_%d1%81%d1%87%d0%b8%d1%82%d0%b0%d0%b5%d1%82%d0%b5%2C_%d0%bd%d0%b0%d0%b4%d0%be_%d0%bb%d0%b8_%d0%bf%d0%b8%d1%81%d0%b0%d1%82%d1%8c_const_%d0%b4%d0%bb%d1%8f_%d0%bf%d0%b0%d1%80%d0%b0%d0%bc%d0%b5%d1%82%d1%80%d0%be%d0%b2%2C_%d0%ba%d0%be%d1%82%d0%be%d1%80%d1%8b%d0%b5_%d0%bf%d0%b5%d1%80%d0%b5%d0%b4%d0%b0%d1%8e%d1%82%d1%81%d1%8f_%d0%b2_%d1%84%d1%83%d0%bd%d0%ba%d1%86%d0%b8%d1%8e_%d0%bf%d0%be_%d0%b7%d0%bd%d0%b0%d1%87%d0%b5%d0%bd%d0%b8%d1%8e%3F",
0, 0, 4, "Как вы считаете, надо ли писать const для параметров, которые передаются в функцию по значению?");
TestSuccess("ge0://AwAAAAAAAA/\xd1\x81\xd1\x82\xd1\x80\xd0\xbe\xd0\xba\xd0\xb0_\xd0\xb2_\xd1\x8e\xd1\x82\xd1\x84-8", 0, 0, 4, "строка в ютф-8");
// name is valid, but too long
//TestSuccess("ge0://AwAAAAAAAA/%d0%9a%d0%b0%d0%ba_%d0%b2%d1%8b_%d1%81%d1%87%d0%b8%d1%82%d0%b0%d0%b5%d1%82%d0%b5%2C_%d0%bd%d0%b0%d0%b4%d0%be_%d0%bb%d0%b8_%d0%bf%d0%b8%d1%81%d0%b0%d1%82%d1%8c_const_%d0%b4%d0%bb%d1%8f_%d0%bf%d0%b0%d1%80%d0%b0%d0%bc%d0%b5%d1%82%d1%80%d0%be%d0%b2%2C_%d0%ba%d0%be%d1%82%d0%be%d1%80%d1%8b%d0%b5_%d0%bf%d0%b5%d1%80%d0%b5%d0%b4%d0%b0%d1%8e%d1%82%d1%81%d1%8f_%d0%b2_%d1%84%d1%83%d0%bd%d0%ba%d1%86%d0%b8%d1%8e_%d0%bf%d0%be_%d0%b7%d0%bd%d0%b0%d1%87%d0%b5%d0%bd%d0%b8%d1%8e%3F",
// 0, 0, 4, "Как вы считаете, надо ли писать const для параметров, которые передаются в функцию по значению?");
}
UNIT_TEST(LatLonFullAndClippedCoordinates)