From 51e9ce7c91fc834138cfcb4181f5f0d38b045729 Mon Sep 17 00:00:00 2001 From: Garth Snyder Date: Sun, 3 Jun 2018 15:22:53 -0700 Subject: [PATCH] Terminate XML parsing as soon as a complete feed is received --- Sources/FeedKit/Parser/XMLFeedParser.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/FeedKit/Parser/XMLFeedParser.swift b/Sources/FeedKit/Parser/XMLFeedParser.swift index e1adbdbf..6b5837e1 100644 --- a/Sources/FeedKit/Parser/XMLFeedParser.swift +++ b/Sources/FeedKit/Parser/XMLFeedParser.swift @@ -60,6 +60,7 @@ class XMLFeedParser: NSObject, XMLParserDelegate, FeedParserProtocol { /// A parsing error, if any. var parsingError: NSError? + var parseComplete = false /// Starts parsing the feed. func parse() -> Result { @@ -167,6 +168,10 @@ extension XMLFeedParser { { // Update the current path along the XML's DOM elements by deleting last component. self.currentXMLDOMPath = self.currentXMLDOMPath.deletingLastPathComponent() + if currentXMLDOMPath.absoluteString == "/" { + parseComplete = true + xmlParser.abortParsing() + } } func parser(_ parser: XMLParser, foundCDATA CDATABlock: Data) @@ -184,7 +189,12 @@ extension XMLFeedParser { } func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) { - self.parsingError = NSError(domain: parseError.localizedDescription, code: -1) + // Ignore errors that occur after a feed is successfully parsed. Some + // real-world feeds contain junk such as "[]" after the XML segment; + // just ignore this stuff. + guard !parseComplete, parsingError == nil else { return } + self.parsingError = NSError(domain: parseError.localizedDescription, code: -1, + userInfo: ["CurrentPath": currentXMLDOMPath.absoluteString]) } }