diff options
| -rw-r--r-- | Swiften/JID/JID.cpp | 71 | ||||
| -rw-r--r-- | Swiften/JID/UnitTest/JIDTest.cpp | 7 |
2 files changed, 15 insertions, 63 deletions
diff --git a/Swiften/JID/JID.cpp b/Swiften/JID/JID.cpp index e8a4700..a31c19f 100644 --- a/Swiften/JID/JID.cpp +++ b/Swiften/JID/JID.cpp | |||
| @@ -1,20 +1,13 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2010-2016 Isode Limited. | 2 | * Copyright (c) 2010-2018 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | #define SWIFTEN_CACHE_JID_PREP | ||
| 8 | |||
| 9 | #include <sstream> | 7 | #include <sstream> |
| 10 | #include <string> | 8 | #include <string> |
| 11 | #include <vector> | 9 | #include <vector> |
| 12 | 10 | ||
| 13 | #ifdef SWIFTEN_CACHE_JID_PREP | ||
| 14 | #include <mutex> | ||
| 15 | #include <unordered_map> | ||
| 16 | #endif | ||
| 17 | |||
| 18 | #include <boost/optional.hpp> | 11 | #include <boost/optional.hpp> |
| 19 | 12 | ||
| 20 | #include <Swiften/Base/String.h> | 13 | #include <Swiften/Base/String.h> |
| @@ -28,15 +21,6 @@ | |||
| 28 | 21 | ||
| 29 | using namespace Swift; | 22 | using namespace Swift; |
| 30 | 23 | ||
| 31 | #ifdef SWIFTEN_CACHE_JID_PREP | ||
| 32 | typedef std::unordered_map<std::string, std::string> PrepCache; | ||
| 33 | |||
| 34 | static std::mutex namePrepCacheMutex; | ||
| 35 | static PrepCache nodePrepCache; | ||
| 36 | static PrepCache domainPrepCache; | ||
| 37 | static PrepCache resourcePrepCache; | ||
| 38 | #endif | ||
| 39 | |||
| 40 | static const std::vector<char> escapedChars = {' ', '"', '&', '\'', '/', '<', '>', '@', ':'}; | 24 | static const std::vector<char> escapedChars = {' ', '"', '&', '\'', '/', '<', '>', '@', ':'}; |
| 41 | 25 | ||
| 42 | static IDNConverter* idnConverter = nullptr; | 26 | static IDNConverter* idnConverter = nullptr; |
| @@ -124,54 +108,15 @@ void JID::nameprepAndSetComponents(const std::string& node, const std::string& d | |||
| 124 | valid_ = false; | 108 | valid_ = false; |
| 125 | return; | 109 | return; |
| 126 | } | 110 | } |
| 127 | #ifndef SWIFTEN_CACHE_JID_PREP | ||
| 128 | node_ = idnConverter->getStringPrepared(node, IDNConverter::XMPPNodePrep); | ||
| 129 | domain_ = idnConverter->getStringPrepared(domain, IDNConverter::NamePrep); | ||
| 130 | resource_ = idnConverter->getStringPrepared(resource, IDNConverter::XMPPResourcePrep); | ||
| 131 | #else | ||
| 132 | std::unique_lock<std::mutex> lock(namePrepCacheMutex); | ||
| 133 | |||
| 134 | std::pair<PrepCache::iterator, bool> r; | ||
| 135 | |||
| 136 | r = nodePrepCache.insert(std::make_pair(node, std::string())); | ||
| 137 | if (r.second) { | ||
| 138 | try { | ||
| 139 | r.first->second = idnConverter->getStringPrepared(node, IDNConverter::XMPPNodePrep); | ||
| 140 | } | ||
| 141 | catch (...) { | ||
| 142 | nodePrepCache.erase(r.first); | ||
| 143 | valid_ = false; | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | node_ = r.first->second; | ||
| 148 | |||
| 149 | r = domainPrepCache.insert(std::make_pair(domain, std::string())); | ||
| 150 | if (r.second) { | ||
| 151 | try { | ||
| 152 | r.first->second = idnConverter->getStringPrepared(domain, IDNConverter::NamePrep); | ||
| 153 | } | ||
| 154 | catch (...) { | ||
| 155 | domainPrepCache.erase(r.first); | ||
| 156 | valid_ = false; | ||
| 157 | return; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | domain_ = r.first->second; | ||
| 161 | 111 | ||
| 162 | r = resourcePrepCache.insert(std::make_pair(resource, std::string())); | 112 | try { |
| 163 | if (r.second) { | 113 | node_ = idnConverter->getStringPrepared(node, IDNConverter::XMPPNodePrep); |
| 164 | try { | 114 | domain_ = idnConverter->getStringPrepared(domain, IDNConverter::NamePrep); |
| 165 | r.first->second = idnConverter->getStringPrepared(resource, IDNConverter::XMPPResourcePrep); | 115 | resource_ = idnConverter->getStringPrepared(resource, IDNConverter::XMPPResourcePrep); |
| 166 | } | 116 | } catch (...) { |
| 167 | catch (...) { | 117 | valid_ = false; |
| 168 | resourcePrepCache.erase(r.first); | 118 | return; |
| 169 | valid_ = false; | ||
| 170 | return; | ||
| 171 | } | ||
| 172 | } | 119 | } |
| 173 | resource_ = r.first->second; | ||
| 174 | #endif | ||
| 175 | 120 | ||
| 176 | if (domain_.empty()) { | 121 | if (domain_.empty()) { |
| 177 | valid_ = false; | 122 | valid_ = false; |
diff --git a/Swiften/JID/UnitTest/JIDTest.cpp b/Swiften/JID/UnitTest/JIDTest.cpp index 0101a4f..aefda33 100644 --- a/Swiften/JID/UnitTest/JIDTest.cpp +++ b/Swiften/JID/UnitTest/JIDTest.cpp | |||
| @@ -64,6 +64,7 @@ class JIDTest : public CppUnit::TestFixture | |||
| 64 | CPPUNIT_TEST(testGetEscapedNode_BackslashAtEnd); | 64 | CPPUNIT_TEST(testGetEscapedNode_BackslashAtEnd); |
| 65 | CPPUNIT_TEST(testGetUnescapedNode); | 65 | CPPUNIT_TEST(testGetUnescapedNode); |
| 66 | CPPUNIT_TEST(testGetUnescapedNode_XEP106Examples); | 66 | CPPUNIT_TEST(testGetUnescapedNode_XEP106Examples); |
| 67 | CPPUNIT_TEST(testStringPrepFailures); | ||
| 67 | CPPUNIT_TEST_SUITE_END(); | 68 | CPPUNIT_TEST_SUITE_END(); |
| 68 | 69 | ||
| 69 | public: | 70 | public: |
| @@ -159,6 +160,12 @@ class JIDTest : public CppUnit::TestFixture | |||
| 159 | CPPUNIT_ASSERT(!testling.isValid()); | 160 | CPPUNIT_ASSERT(!testling.isValid()); |
| 160 | } | 161 | } |
| 161 | 162 | ||
| 163 | void testStringPrepFailures() { | ||
| 164 | CPPUNIT_ASSERT_EQUAL(false, JID("foo@bar", "example.com").isValid()); | ||
| 165 | CPPUNIT_ASSERT_EQUAL(false, JID("foo^", "example*com").isValid()); | ||
| 166 | CPPUNIT_ASSERT_EQUAL(false, JID("foobar", "example^com").isValid()); | ||
| 167 | } | ||
| 168 | |||
| 162 | void testConstructorWithString_EmptyDomainWithResource() { | 169 | void testConstructorWithString_EmptyDomainWithResource() { |
| 163 | JID testling("bar@/resource"); | 170 | JID testling("bar@/resource"); |
| 164 | 171 | ||
Swift