From 3827b9871217ce4029e893eb178650cd71a03e78 Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Wed, 17 Aug 2016 13:16:27 +0300 Subject: [PATCH 1/2] website validation corrections --- indexer/editable_map_object.cpp | 37 +++++++++++++++---- .../editable_map_object_test.cpp | 7 +++- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/indexer/editable_map_object.cpp b/indexer/editable_map_object.cpp index 56a26b7194..40af906d33 100644 --- a/indexer/editable_map_object.cpp +++ b/indexer/editable_map_object.cpp @@ -52,6 +52,19 @@ size_t PushMwmLanguages(StringUtf8Multilang const & names, vector const return count; } + +string const kWebSiteProtocols[] = {"http://", "https://"}; +string const kWebSiteProtocolByDefault = "http://"; + +bool IsProtocolSpecified(string const & website) +{ + for (auto const & protocol : kWebSiteProtocols) + { + if (strings::StartsWith(website, protocol.c_str())) + return true; + } + return false; +} } // namespace namespace osm @@ -267,12 +280,9 @@ void EditableMapObject::SetEmail(string const & email) void EditableMapObject::SetWebsite(string website) { - if (!website.empty() && - !strings::StartsWith(website, "http://") && - !strings::StartsWith(website, "https://")) - { - website = "http://" + website; - } + if (!website.empty() && !IsProtocolSpecified(website)) + website = kWebSiteProtocolByDefault + website; + m_metadata.Set(feature::Metadata::FMD_WEBSITE, website); m_metadata.Drop(feature::Metadata::FMD_URL); } @@ -443,8 +453,21 @@ bool EditableMapObject::ValidateWebsite(string const & site) if (site.empty()) return true; + size_t startPos = 0; + for (auto const & protocol : kWebSiteProtocols) + { + if (strings::StartsWith(site, protocol.c_str())) + { + startPos = protocol.size(); + break; + } + } + + if (startPos >= site.size()) + return false; + // Site should contain at least one dot but not at the begining/end. - if ('.' == site.front() || '.' == site.back()) + if ('.' == site[startPos] || '.' == site.back()) return false; if (string::npos == site.find(".")) diff --git a/indexer/indexer_tests/editable_map_object_test.cpp b/indexer/indexer_tests/editable_map_object_test.cpp index c9db74a865..7df4730871 100644 --- a/indexer/indexer_tests/editable_map_object_test.cpp +++ b/indexer/indexer_tests/editable_map_object_test.cpp @@ -100,13 +100,18 @@ UNIT_TEST(EditableMapObject_ValidateWebsite) { TEST(EditableMapObject::ValidateWebsite(""), ()); TEST(EditableMapObject::ValidateWebsite("qwe.rty"), ()); + TEST(EditableMapObject::ValidateWebsite("http://websit.e"), ()); + TEST(EditableMapObject::ValidateWebsite("https://websit.e"), ()); TEST(!EditableMapObject::ValidateWebsite("qwerty"), ()); TEST(!EditableMapObject::ValidateWebsite(".qwerty"), ()); TEST(!EditableMapObject::ValidateWebsite("qwerty."), ()); TEST(!EditableMapObject::ValidateWebsite(".qwerty."), ()); - TEST(!EditableMapObject::ValidateWebsite(".qwerty."), ()); TEST(!EditableMapObject::ValidateWebsite("w..com"), ()); + TEST(!EditableMapObject::ValidateWebsite("http://.websit.e"), ()); + TEST(!EditableMapObject::ValidateWebsite("https://.websit.e"), ()); + TEST(!EditableMapObject::ValidateWebsite("http://"), ()); + TEST(!EditableMapObject::ValidateWebsite("https://"), ()); } UNIT_TEST(EditableMapObject_ValidateEmail) From 5e1ba34be3a62f388eaa03195fe442d8cb63a516 Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Wed, 17 Aug 2016 13:59:19 +0300 Subject: [PATCH 2/2] review fixes --- indexer/editable_map_object.cpp | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/indexer/editable_map_object.cpp b/indexer/editable_map_object.cpp index 40af906d33..e9f819de2e 100644 --- a/indexer/editable_map_object.cpp +++ b/indexer/editable_map_object.cpp @@ -53,17 +53,22 @@ size_t PushMwmLanguages(StringUtf8Multilang const & names, vector const return count; } -string const kWebSiteProtocols[] = {"http://", "https://"}; -string const kWebSiteProtocolByDefault = "http://"; +char const * const kWebsiteProtocols[] = {"http://", "https://"}; +size_t const kWebsiteProtocolDefaultIndex = 0; + +size_t GetProtocolNameLength(string const & website) +{ + for (auto const & protocol : kWebsiteProtocols) + { + if (strings::StartsWith(website, protocol)) + return strlen(protocol); + } + return 0; +} bool IsProtocolSpecified(string const & website) { - for (auto const & protocol : kWebSiteProtocols) - { - if (strings::StartsWith(website, protocol.c_str())) - return true; - } - return false; + return GetProtocolNameLength(website) > 0; } } // namespace @@ -281,7 +286,7 @@ void EditableMapObject::SetEmail(string const & email) void EditableMapObject::SetWebsite(string website) { if (!website.empty() && !IsProtocolSpecified(website)) - website = kWebSiteProtocolByDefault + website; + website = kWebsiteProtocols[kWebsiteProtocolDefaultIndex] + website; m_metadata.Set(feature::Metadata::FMD_WEBSITE, website); m_metadata.Drop(feature::Metadata::FMD_URL); @@ -453,15 +458,7 @@ bool EditableMapObject::ValidateWebsite(string const & site) if (site.empty()) return true; - size_t startPos = 0; - for (auto const & protocol : kWebSiteProtocols) - { - if (strings::StartsWith(site, protocol.c_str())) - { - startPos = protocol.size(); - break; - } - } + auto const startPos = GetProtocolNameLength(site); if (startPos >= site.size()) return false;