diff options
Diffstat (limited to 'SwifTools/URIHandler')
-rw-r--r-- | SwifTools/URIHandler/MacOSXURIHandler.mm | 11 | ||||
-rw-r--r-- | SwifTools/URIHandler/UnitTest/XMPPURITest.cpp | 8 | ||||
-rw-r--r-- | SwifTools/URIHandler/XMPPURI.cpp | 90 | ||||
-rw-r--r-- | SwifTools/URIHandler/XMPPURI.h | 1 |
4 files changed, 20 insertions, 90 deletions
diff --git a/SwifTools/URIHandler/MacOSXURIHandler.mm b/SwifTools/URIHandler/MacOSXURIHandler.mm index 0575d47..cdfba33 100644 --- a/SwifTools/URIHandler/MacOSXURIHandler.mm +++ b/SwifTools/URIHandler/MacOSXURIHandler.mm @@ -1,4 +1,4 @@ /* - * Copyright (c) 2011 Remko Tronçon + * Copyright (c) 2011-2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. @@ -13,13 +13,18 @@ using namespace Swift; @interface MacOSXURIEventHandler : NSObject { - URIHandler* handler; } + - (id) initWithHandler: (URIHandler*) handler; - (void) getUrl: (NSAppleEventDescriptor*) event withReplyEvent: (NSAppleEventDescriptor*) replyEvent; @end + @implementation MacOSXURIEventHandler + { + URIHandler* handler; + } + - (id) initWithHandler: (URIHandler*) h { - if ([super init]) { + if ((self = [super init])) { handler = h; } diff --git a/SwifTools/URIHandler/UnitTest/XMPPURITest.cpp b/SwifTools/URIHandler/UnitTest/XMPPURITest.cpp index 8d03b60..35020da 100644 --- a/SwifTools/URIHandler/UnitTest/XMPPURITest.cpp +++ b/SwifTools/URIHandler/UnitTest/XMPPURITest.cpp @@ -66,7 +66,7 @@ class XMPPURITest : public CppUnit::TestFixture { void testFromString_AuthorityWithIntlChars() { - XMPPURI testling = XMPPURI::fromString("xmpp://nasty!%23$%25()*+,-.;=\%3F\%5B\%5C\%5D\%5E_\%60\%7B\%7C\%7D~node@example.com"); + XMPPURI testling = XMPPURI::fromString("xmpp://nasty!%23$%25()*+,-.;=%3F%5B%5C%5D%5E_%60%7B%7C%7D~node@example.com"); - CPPUNIT_ASSERT_EQUAL(JID("nasty!#$\%()*+,-.;=?[\\]^_`{|}~node@example.com"), testling.getAuthority()); + CPPUNIT_ASSERT_EQUAL(JID("nasty!#$%()*+,-.;=?[\\]^_`{|}~node@example.com"), testling.getAuthority()); } @@ -119,7 +119,7 @@ class XMPPURITest : public CppUnit::TestFixture { void testFromString_PathWithIntlChars() { - XMPPURI testling = XMPPURI::fromString("xmpp:nasty!%23$%25()*+,-.;=\%3F\%5B\%5C\%5D\%5E_\%60\%7B\%7C\%7D~node@example.com"); + XMPPURI testling = XMPPURI::fromString("xmpp:nasty!%23$%25()*+,-.;=%3F%5B%5C%5D%5E_%60%7B%7C%7D~node@example.com"); - CPPUNIT_ASSERT_EQUAL(JID("nasty!#$\%()*+,-.;=?[\\]^_`{|}~node@example.com"), testling.getPath()); + CPPUNIT_ASSERT_EQUAL(JID("nasty!#$%()*+,-.;=?[\\]^_`{|}~node@example.com"), testling.getPath()); } diff --git a/SwifTools/URIHandler/XMPPURI.cpp b/SwifTools/URIHandler/XMPPURI.cpp index cb81391..cf99ae6 100644 --- a/SwifTools/URIHandler/XMPPURI.cpp +++ b/SwifTools/URIHandler/XMPPURI.cpp @@ -7,4 +7,5 @@ #include <SwifTools/URIHandler/XMPPURI.h> +#include <Swiften/Base/URL.h> #include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/find_format.hpp> @@ -19,81 +20,4 @@ using namespace Swift; -// Disabling this code for now, since GCC4.5+boost1.42 (on ubuntu) seems to -// result in a bug. Replacing it with naive code. -#if 0 -// Should be in anonymous namespace, but older GCCs complain if we do that -struct PercentEncodedCharacterFinder { - template<typename Iterator> - boost::iterator_range<Iterator> operator()(Iterator begin, Iterator end) { - boost::iterator_range<Iterator> r = boost::first_finder("%")(begin, end); - if (r.end() == end) { - return r; - } - else { - if (r.end() + 1 == end || r.end() + 2 == end) { - throw std::runtime_error("Incomplete escape character"); - } - else { - r.advance_end(2); - return r; - } - } - } -}; - -struct PercentUnencodeFormatter { - template<typename FindResult> - std::string operator()(const FindResult& match) const { - std::stringstream s; - s << std::hex << std::string(match.begin() + 1, match.end()); - unsigned int value; - s >> value; - if (s.fail() || s.bad()) { - throw std::runtime_error("Invalid escape character"); - } - unsigned char charValue = static_cast<unsigned char>(value); - return std::string(reinterpret_cast<const char*>(&charValue), 1); - } -}; - -namespace { - std::string unescape(const std::string& s) { - try { - return boost::find_format_all_copy(s, PercentEncodedCharacterFinder(), PercentUnencodeFormatter()); - } - catch (const std::exception&) { - return ""; - } - } -} -#endif -namespace { - std::string unescape(const std::string& str) { - std::string result; - for (size_t i = 0; i < str.size(); ++i) { - if (str[i] == '%') { - if (i + 3 < str.size()) { - std::stringstream s; - s << std::hex << str.substr(i+1, 2); - unsigned int value; - s >> value; - if (s.fail() || s.bad()) { - return ""; - } - unsigned char charValue = static_cast<unsigned char>(value); - result += std::string(reinterpret_cast<const char*>(&charValue), 1); - i += 2; - } - else { - return ""; - } - } - else { - result += str[i]; - } - } - return result; - } -} XMPPURI::XMPPURI() { @@ -111,5 +35,5 @@ XMPPURI XMPPURI::fromString(const std::string& s) { if (boost::starts_with(uri, "//")) { size_t i = uri.find_first_of("/#?", 2); - result.setAuthority(JID(unescape(uri.substr(2, i - 2)))); + result.setAuthority(JID(URL::unescape(uri.substr(2, i - 2)))); if (i == uri.npos) { uri = ""; @@ -130,5 +54,5 @@ XMPPURI XMPPURI::fromString(const std::string& s) { if (parsePath) { size_t i = uri.find_first_of("#?"); - result.setPath(JID(unescape(uri.substr(0, i)))); + result.setPath(JID(URL::unescape(uri.substr(0, i)))); if (i == uri.npos) { uri = ""; @@ -154,12 +78,12 @@ XMPPURI XMPPURI::fromString(const std::string& s) { boost::split(keyValue, *it, boost::is_any_of("=")); if (keyValue.size() == 1) { - result.addQueryParameter(unescape(keyValue[0]), ""); + result.addQueryParameter(URL::unescape(keyValue[0]), ""); } else if (keyValue.size() >= 2) { - result.addQueryParameter(unescape(keyValue[0]), unescape(keyValue[1])); + result.addQueryParameter(URL::unescape(keyValue[0]), URL::unescape(keyValue[1])); } } else { - result.setQueryType(unescape(boost::copy_range<std::string>(*it))); + result.setQueryType(URL::unescape(boost::copy_range<std::string>(*it))); haveType = true; } @@ -170,5 +94,5 @@ XMPPURI XMPPURI::fromString(const std::string& s) { // Parse fragment if (parseFragment) { - result.setFragment(unescape(uri)); + result.setFragment(URL::unescape(uri)); } } diff --git a/SwifTools/URIHandler/XMPPURI.h b/SwifTools/URIHandler/XMPPURI.h index 266b79b..36bfc41 100644 --- a/SwifTools/URIHandler/XMPPURI.h +++ b/SwifTools/URIHandler/XMPPURI.h @@ -13,4 +13,5 @@ namespace Swift { + // TODO: Implement using Base/URI class XMPPURI { public: |