diff options
Diffstat (limited to 'Swiften/Parser/PayloadParsers')
7 files changed, 167 insertions, 59 deletions
diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index a40e8f6..0a0fc39 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -59,6 +59,7 @@ #include <Swiften/Parser/PayloadParsers/JingleS5BTransportMethodPayloadParser.h> #include <Swiften/Parser/PayloadParsers/JingleFileTransferDescriptionParserFactory.h> #include <Swiften/Parser/PayloadParsers/StreamInitiationFileInfoParser.h> +#include <Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.h> #include <Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h> #include <Swiften/Parser/PayloadParsers/JingleFileTransferHashParser.h> #include <Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h> @@ -122,6 +123,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(boost::make_shared<GenericPayloadParserFactory<JingleS5BTransportMethodPayloadParser> >("transport", "urn:xmpp:jingle:transports:s5b:1")); factories_.push_back(boost::make_shared<JingleFileTransferDescriptionParserFactory>(this)); factories_.push_back(boost::make_shared<GenericPayloadParserFactory<StreamInitiationFileInfoParser> >("file", "http://jabber.org/protocol/si/profile/file-transfer")); + factories_.push_back(boost::make_shared<GenericPayloadParserFactory<JingleFileTransferFileInfoParser> >("file", "urn:xmpp:jingle:apps:file-transfer:3")); factories_.push_back(boost::make_shared<GenericPayloadParserFactory<JingleFileTransferReceivedParser> >("received", "urn:xmpp:jingle:apps:file-transfer:3")); factories_.push_back(boost::make_shared<GenericPayloadParserFactory<JingleFileTransferHashParser> >("checksum")); factories_.push_back(boost::make_shared<GenericPayloadParserFactory<S5BProxyRequestParser> >("query", "http://jabber.org/protocol/bytestreams")); diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferDescriptionParser.cpp b/Swiften/Parser/PayloadParsers/JingleFileTransferDescriptionParser.cpp index b394115..fa6c6ae 100644 --- a/Swiften/Parser/PayloadParsers/JingleFileTransferDescriptionParser.cpp +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferDescriptionParser.cpp @@ -54,7 +54,7 @@ void JingleFileTransferDescriptionParser::handleEndElement(const std::string& el } if (level == 2) { - boost::shared_ptr<StreamInitiationFileInfo> info = boost::dynamic_pointer_cast<StreamInitiationFileInfo>(currentPayloadParser->getPayload()); + boost::shared_ptr<JingleFileTransferFileInfo> info = boost::dynamic_pointer_cast<JingleFileTransferFileInfo>(currentPayloadParser->getPayload()); if (info) { if (currentElement == OfferElement) { getPayloadInternal()->addOffer(*info); diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.cpp b/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.cpp new file mode 100644 index 0000000..769fc84 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2012 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include "JingleFileTransferFileInfoParser.h" + +#include <boost/optional.hpp> +#include <boost/lexical_cast.hpp> + +#include <Swiften/Base/DateTime.h> +#include <Swiften/Base/Log.h> + +#include <cstdio> + +namespace Swift { + +JingleFileTransferFileInfoParser::JingleFileTransferFileInfoParser() : level(0) { + +} + +void JingleFileTransferFileInfoParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) { + if (level == 0) { + + } else if (level == 1) { + if (element == "date" || element == "desc" || element == "name" || element == "size") { + cdataBuffer.clear(); + } else if (element == "hash") { + cdataBuffer.clear(); + getPayloadInternal()->setAlgo(attributes.getAttributeValue("algo").get_value_or("md5")); + } else { + if (element == "range") { + int offset = 0; + try { + offset = boost::lexical_cast<boost::uintmax_t>(attributes.getAttributeValue("offset").get_value_or("0")); + } catch (boost::bad_lexical_cast &) { + offset = 0; + } + if (offset == 0) { + getPayloadInternal()->setSupportsRangeRequests(true); + } else { + getPayloadInternal()->setRangeOffset(offset); + } + } + } + } + ++level; +} + +void JingleFileTransferFileInfoParser::handleEndElement(const std::string& element, const std::string&) { + --level; + if (element == "date") { + getPayloadInternal()->setDate(stringToDateTime(cdataBuffer)); + } else if (element == "desc") { + getPayloadInternal()->setDescription(cdataBuffer); + } else if (element == "name") { + getPayloadInternal()->setName(cdataBuffer); + } else if (element == "size") { + try { + getPayloadInternal()->setSize(boost::lexical_cast<boost::uintmax_t>(cdataBuffer)); + } catch (boost::bad_lexical_cast &) { + getPayloadInternal()->setSize(0); + } + } else if (element == "hash") { + getPayloadInternal()->setHash(cdataBuffer); + } +} + +void JingleFileTransferFileInfoParser::handleCharacterData(const std::string& data) { + cdataBuffer += data; +} + +} diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.h b/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.h new file mode 100644 index 0000000..5b44f9a --- /dev/null +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2012 Tobias Markmann + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <Swiften/Elements/JingleFileTransferFileInfo.h> +#include <Swiften/Parser/GenericPayloadParser.h> + +namespace Swift { + +class JingleFileTransferFileInfoParser : public GenericPayloadParser<JingleFileTransferFileInfo> { + public: + JingleFileTransferFileInfoParser(); + + virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes); + virtual void handleEndElement(const std::string& element, const std::string&); + virtual void handleCharacterData(const std::string& data); + + private: + int level; + std::string cdataBuffer; +}; + +} diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.cpp b/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.cpp index ae56981..afe1da1 100644 --- a/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.cpp +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.cpp @@ -7,8 +7,7 @@ #include <Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h> #include <boost/shared_ptr.hpp> -#include <Swiften/Parser/PayloadParsers/StreamInitiationFileInfoParser.h> -#include <Swiften/Parser/PayloadParsers/StreamInitiationFileInfoParser.h> +#include <Swiften/Parser/PayloadParsers/JingleFileTransferFileInfoParser.h> #include <Swiften/Parser/GenericPayloadParserFactory.h> #include <Swiften/Parser/PayloadParserFactory.h> @@ -19,7 +18,7 @@ JingleFileTransferReceivedParser::JingleFileTransferReceivedParser() : level(0) void JingleFileTransferReceivedParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) { if (level == 1 && element == "file") { - PayloadParserFactory* payloadParserFactory = new GenericPayloadParserFactory<StreamInitiationFileInfoParser>("file", "http://jabber.org/protocol/si/profile/file-transfer"); + PayloadParserFactory* payloadParserFactory = new GenericPayloadParserFactory<JingleFileTransferFileInfoParser>("file"); if (payloadParserFactory) { currentPayloadParser.reset(payloadParserFactory->createPayloadParser()); } @@ -32,18 +31,22 @@ void JingleFileTransferReceivedParser::handleStartElement(const std::string& ele ++level; } -void JingleFileTransferReceivedParser::handleEndElement(const std::string& element, const std::string& ) { +void JingleFileTransferReceivedParser::handleEndElement(const std::string& element, const std::string& ns) { --level; if (element == "file") { - boost::shared_ptr<StreamInitiationFileInfo> fileInfo = boost::dynamic_pointer_cast<StreamInitiationFileInfo>(currentPayloadParser->getPayload()); + boost::shared_ptr<JingleFileTransferFileInfo> fileInfo = boost::dynamic_pointer_cast<JingleFileTransferFileInfo>(currentPayloadParser->getPayload()); if (fileInfo) { getPayloadInternal()->setFileInfo(*fileInfo); } + } else { + currentPayloadParser->handleEndElement(element, ns); } } -void JingleFileTransferReceivedParser::handleCharacterData(const std::string& ) { - +void JingleFileTransferReceivedParser::handleCharacterData(const std::string &data) { + if (currentPayloadParser && level >= 1) { + currentPayloadParser->handleCharacterData(data); + } } } diff --git a/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h b/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h index 824b06d..d5ed138 100644 --- a/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h +++ b/Swiften/Parser/PayloadParsers/JingleFileTransferReceivedParser.h @@ -16,7 +16,7 @@ public: JingleFileTransferReceivedParser(); virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes); - virtual void handleEndElement(const std::string& element, const std::string&); + virtual void handleEndElement(const std::string& element, const std::string& ns); virtual void handleCharacterData(const std::string& data); private: @@ -24,4 +24,4 @@ private: int level; }; -}
\ No newline at end of file +} diff --git a/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp index 8719a5d..95423c1 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp +++ b/Swiften/Parser/PayloadParsers/UnitTest/JingleParserTest.cpp @@ -35,9 +35,9 @@ class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_TEST(testParse_Xep0234_Example3); CPPUNIT_TEST(testParse_Xep0234_Example5); CPPUNIT_TEST(testParse_Xep0234_Example8); - CPPUNIT_TEST(testParse_Xep0234_Example10); - CPPUNIT_TEST(testParse_Xep0234_Example11); CPPUNIT_TEST(testParse_Xep0234_Example12); + CPPUNIT_TEST(testParse_Xep0234_Example13); + CPPUNIT_TEST(testParse_Xep0234_Example14); CPPUNIT_TEST(testParse_Xep0260_Example1); CPPUNIT_TEST(testParse_Xep0260_Example3); @@ -229,13 +229,13 @@ class JingleParserTest : public CppUnit::TestFixture { " <content creator='initiator' name='a-file-offer'>\n" " <description xmlns='urn:xmpp:jingle:apps:file-transfer:3'>\n" " <offer>\n" - " <file xmlns='http://jabber.org/protocol/si/profile/file-transfer'\n" - " date='1969-07-21T02:56:15Z'\n" - " hash='552da749930852c69ae5d2141d3766b1'\n" - " name='test.txt'\n" - " size='1022'>\n" + " <file>\n" + " <date>1969-07-21T02:56:15Z</date>\n" " <desc>This is a test. If this were a real file...</desc>\n" + "............<name>test.txt</name>\n" " <range/>\n" + " <size>1022</size>\n" + " <hash xmlns='urn:xmpp:hashes:1' algo='sha-1'>552da749930852c69ae5d2141d3766b1</hash>\n" " </file>\n" " </offer>\n" " </description>\n" @@ -271,7 +271,7 @@ class JingleParserTest : public CppUnit::TestFixture { JingleFileTransferDescription::ref description = contents[0]->getDescription<JingleFileTransferDescription>(); - std::vector<StreamInitiationFileInfo> offers = description->getOffers(); + std::vector<JingleFileTransferFileInfo> offers = description->getOffers(); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), offers.size()); CPPUNIT_ASSERT_EQUAL(std::string("test.txt"), offers[0].getName()); CPPUNIT_ASSERT_EQUAL(std::string("552da749930852c69ae5d2141d3766b1"), offers[0].getHash()); @@ -279,7 +279,7 @@ class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("This is a test. If this were a real file..."), offers[0].getDescription()); CPPUNIT_ASSERT_EQUAL(true, offers[0].getSupportsRangeRequests()); CPPUNIT_ASSERT(stringToDateTime("1969-07-21T02:56:15Z") == offers[0].getDate()); - CPPUNIT_ASSERT_EQUAL(std::string("md5"), offers[0].getAlgo()); + CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), offers[0].getAlgo()); } // http://xmpp.org/extensions/xep-0234.html#example-3 @@ -293,14 +293,14 @@ class JingleParserTest : public CppUnit::TestFixture { " <content creator='initiator' name='a-file-offer'>\n" " <description xmlns='urn:xmpp:jingle:apps:file-transfer:3'>\n" " <offer>\n" - " <file xmlns='http://jabber.org/protocol/si/profile/file-transfer'\n" - " name='test.txt'\n" - " size='1022'\n" - " hash='552da749930852c69ae5d2141d3766b1'\n" - " date='1969-07-21T02:56:15Z'>\n" - " <desc>This is a test. If this were a real file...</desc>\n" - " <range/>\n" - " </file>\n" + " <file>\n" + " <date>1969-07-21T02:56:15Z</date>\n" + " <desc>This is a test. If this were a real file...</desc>\n" + "............<name>test.txt</name>\n" + " <range/>\n" + " <size>1022</size>\n" + " <hash xmlns='urn:xmpp:hashes:1' algo='sha-1'>552da749930852c69ae5d2141d3766b1</hash>\n" + " </file>\n" " </offer>\n" " </description>\n" " <transport xmlns='urn:xmpp:jingle:transports:s5b:1'\n" @@ -341,7 +341,7 @@ class JingleParserTest : public CppUnit::TestFixture { JingleFileTransferDescription::ref description = contents[0]->getDescription<JingleFileTransferDescription>(); - std::vector<StreamInitiationFileInfo> offers = description->getOffers(); + std::vector<JingleFileTransferFileInfo> offers = description->getOffers(); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), offers.size()); CPPUNIT_ASSERT_EQUAL(std::string("test.txt"), offers[0].getName()); CPPUNIT_ASSERT_EQUAL(std::string("552da749930852c69ae5d2141d3766b1"), offers[0].getHash()); @@ -349,7 +349,7 @@ class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("This is a test. If this were a real file..."), offers[0].getDescription()); CPPUNIT_ASSERT_EQUAL(true, offers[0].getSupportsRangeRequests()); CPPUNIT_ASSERT(stringToDateTime("1969-07-21T02:56:15Z") == offers[0].getDate()); - CPPUNIT_ASSERT_EQUAL(std::string("md5"), offers[0].getAlgo()); + CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), offers[0].getAlgo()); } // http://xmpp.org/extensions/xep-0234.html#example-5 @@ -395,9 +395,7 @@ class JingleParserTest : public CppUnit::TestFixture { " sid='a73sjjvkla37jfea'>\n" " <checksum xmlns='urn:xmpp:jingle:apps:file-transfer:3'>\n" " <file>\n" - " <hashes xmlns='urn:xmpp:hashes:0'>\n" - " <hash algo='sha-1'>552da749930852c69ae5d2141d3766b1</hash>\n" - " </hashes>\n" + " <hash xmlns='urn:xmpp:hashes:1' algo='sha-1'>552da749930852c69ae5d2141d3766b1</hash>\n" " </file>\n" " </checksum>\n" "</jingle>\n" @@ -414,8 +412,8 @@ class JingleParserTest : public CppUnit::TestFixture { } - // http://xmpp.org/extensions/xep-0234.html#example-10 - void testParse_Xep0234_Example10() { + // http://xmpp.org/extensions/xep-0234.html#example-12 + void testParse_Xep0234_Example12() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "<jingle xmlns='urn:xmpp:jingle:1'\n" @@ -425,9 +423,9 @@ class JingleParserTest : public CppUnit::TestFixture { " <content creator='initiator' name='a-file-request'>\n" " <description xmlns='urn:xmpp:jingle:apps:file-transfer:3'>\n" " <request>\n" - " <file xmlns='http://jabber.org/protocol/si/profile/file-transfer'\n" - " hash='552da749930852c69ae5d2141d3766b1'>\n" + " <file>\n" " <range offset='270336'/>\n" + " <hash xmlns='urn:xmpp:hashes:1' algo='sha-1'>552da749930852c69ae5d2141d3766b1</hash>\n" " </file>\n" " </request>\n" " </description>\n" @@ -466,14 +464,14 @@ class JingleParserTest : public CppUnit::TestFixture { JingleContentPayload::ref content = jingle->getPayload<JingleContentPayload>(); CPPUNIT_ASSERT(content); - StreamInitiationFileInfo file = content->getDescription<JingleFileTransferDescription>()->getRequests()[0]; + JingleFileTransferFileInfo file = content->getDescription<JingleFileTransferDescription>()->getRequests()[0]; CPPUNIT_ASSERT_EQUAL(std::string("552da749930852c69ae5d2141d3766b1"), file.getHash()); CPPUNIT_ASSERT_EQUAL(static_cast<boost::uintmax_t>(270336), file.getRangeOffset()); CPPUNIT_ASSERT_EQUAL(true, file.getSupportsRangeRequests()); } - // http://xmpp.org/extensions/xep-0234.html#example-11 - void testParse_Xep0234_Example11() { + // http://xmpp.org/extensions/xep-0234.html#example-13 + void testParse_Xep0234_Example13() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "<jingle xmlns='urn:xmpp:jingle:1'\n" @@ -483,21 +481,24 @@ class JingleParserTest : public CppUnit::TestFixture { " <content creator='initiator' name='a-file-offer'>\n" " <description xmlns='urn:xmpp:jingle:apps:file-transfer:3'>\n" " <offer>\n" - " <file xmlns='http://jabber.org/protocol/si/profile/file-transfer'\n" - " date='2011-06-01T15:58:15Z'\n" - " hash='a749930852c69ae5d2141d3766b1552d'\n" - " name='somefile.txt'\n" - " size='1234'/>\n" - " <file xmlns='http://jabber.org/protocol/si/profile/file-transfer'\n" - " date='2011-06-01T15:58:15Z'\n" - " hash='930852c69ae5d2141d3766b1552da749'\n" - " name='anotherfile.txt'\n" - " size='2345'/>\n" - " <file xmlns='http://jabber.org/protocol/si/profile/file-transfer'\n" - " date='2011-06-01T15:58:15Z'\n" - " hash='52c69ae5d2141d3766b1552da7499308'\n" - " name='yetanotherfile.txt'\n" - " size='3456'/>\n" + " <file>\n" + " <date>2011-06-01T15:58:15Z</date>\n" + " <name>somefile.txt</name>\n" + " <size>1234</size>\n" + " <hash xmlns='urn:xmpp:hashes:1' algo='sha-1'>a749930852c69ae5d2141d3766b1552d</hash>\n" + " </file>\n" + " <file>\n" + " <date>2011-06-01T15:58:15Z</date>\n" + " <name>somefile.txt</name>\n" + " <size>2345</size>\n" + " <hash xmlns='urn:xmpp:hashes:1' algo='sha-1'>a749930852c69ae5d2141d3766b1552d</hash>\n" + " </file>\n" + " <file>\n" + " <date>2011-06-01T15:58:15Z</date>\n" + " <name>yetanotherfile.txt</name>\n" + " <size>3456</size>\n" + " <hash xmlns='urn:xmpp:hashes:1' algo='sha-1'>52c69ae5d2141d3766b1552da7499308</hash>\n" + " </file>\n" " </offer>\n" " </description>\n" " <transport xmlns='urn:xmpp:jingle:transports:s5b:1'\n" @@ -531,12 +532,12 @@ class JingleParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(JingleContentPayload::InitiatorCreator, content->getCreator()); CPPUNIT_ASSERT_EQUAL(std::string("a-file-offer"), content->getName()); - std::vector<StreamInitiationFileInfo> offers = content->getDescription<JingleFileTransferDescription>()->getOffers(); + std::vector<JingleFileTransferFileInfo> offers = content->getDescription<JingleFileTransferDescription>()->getOffers(); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), offers.size()); } - // http://xmpp.org/extensions/xep-0234.html#example-12 - void testParse_Xep0234_Example12() { + // http://xmpp.org/extensions/xep-0234.html#example-14 + void testParse_Xep0234_Example14() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "<jingle xmlns='urn:xmpp:jingle:1'\n" @@ -544,8 +545,9 @@ class JingleParserTest : public CppUnit::TestFixture { " initiator='romeo@montague.lit/orchard'\n" " sid='a73sjjvkla37jfea'>\n" " <received xmlns='urn:xmpp:jingle:apps:file-transfer:3'>\n" - " <file xmlns='http://jabber.org/protocol/si/profile/file-transfer'\n" - " hash='a749930852c69ae5d2141d3766b1552d'/>\n" + " <file>\n" + " <hash xmlns='urn:xmpp:hashes:1' algo='sha-1'>a749930852c69ae5d2141d3766b1552d</hash>\n" + "....</file>" " </received>\n" "</jingle>\n" )); |