Skip to content

Commit c1ba429

Browse files
authored
Fix data race in OPDS1Parser (#529)
1 parent 2192579 commit c1ba429

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ All notable changes to this project will be documented in this file. Take a look
1414

1515
* The default `ZIPArchiveOpener` is now using ZIPFoundation instead of Minizip, with improved performances when reading ranges of `stored` ZIP entries.
1616

17+
### Fixed
18+
19+
#### OPDS
20+
21+
* Fixed a data race in the OPDS 1 parser.
22+
1723

1824
## [3.0.0-beta.1]
1925

Sources/OPDS/OPDS1Parser.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,10 @@ struct MimeTypeParameters {
2828
}
2929

3030
public class OPDS1Parser: Loggable {
31-
static var feedURL: URL?
32-
3331
/// Parse an OPDS feed or publication.
3432
/// Feed can only be v1 (XML).
3533
/// - parameter url: The feed URL
3634
public static func parseURL(url: URL, completion: @escaping (ParseData?, Error?) -> Void) {
37-
feedURL = url
38-
3935
URLSession.shared.dataTask(with: url) { data, response, error in
4036
guard let data = data, let response = response else {
4137
completion(nil, error ?? OPDSParserError.documentNotFound)
@@ -58,19 +54,17 @@ public class OPDS1Parser: Loggable {
5854
/// - parameter response: The response payload
5955
/// - Returns: The intermediate structure of type ParseData
6056
public static func parse(xmlData: Data, url: URL, response: URLResponse) throws -> ParseData {
61-
feedURL = url
62-
6357
var parseData = ParseData(url: url, response: response, version: .OPDS1)
6458

6559
let xmlDocument = try XMLDocument(data: xmlData)
6660

6761
if xmlDocument.root?.tag == "feed" {
6862
// Feed
69-
parseData.feed = try? parse(document: xmlDocument)
63+
parseData.feed = try? parse(document: xmlDocument, feedURL: url)
7064
} else if xmlDocument.root?.tag == "entry" {
7165
// Publication only
7266
do {
73-
parseData.publication = try parseEntry(document: xmlDocument)
67+
parseData.publication = try parseEntry(document: xmlDocument, feedURL: url)
7468
} catch {
7569
log(.warning, "Failed to parse Publication at \(url)")
7670
}
@@ -85,7 +79,7 @@ public class OPDS1Parser: Loggable {
8579
/// Feed can only be v1 (XML).
8680
/// - parameter document: The XMLDocument data
8781
/// - Returns: The resulting Feed
88-
public static func parse(document: ReadiumFuzi.XMLDocument) throws -> Feed {
82+
private static func parse(document: ReadiumFuzi.XMLDocument, feedURL: URL) throws -> Feed {
8983
document.definePrefix("thr", forNamespace: "http://purl.org/syndication/thread/1.0")
9084
document.definePrefix("dcterms", forNamespace: "http://purl.org/dc/terms/")
9185
document.definePrefix("opds", forNamespace: "http://opds-spec.org/2010/catalog")
@@ -136,7 +130,7 @@ public class OPDS1Parser: Loggable {
136130
}
137131

138132
if !isNavigation {
139-
if let publication = parseEntry(entry: entry) {
133+
if let publication = parseEntry(entry: entry, feedURL: feedURL) {
140134
// Checking if this publication need to go into a group or in publications.
141135
if let collectionLink = collectionLink {
142136
addPublicationInGroup(feed, publication, collectionLink)
@@ -218,11 +212,11 @@ public class OPDS1Parser: Loggable {
218212
/// Publication can only be v1 (XML).
219213
/// - parameter document: The XMLDocument data
220214
/// - Returns: The resulting Publication
221-
public static func parseEntry(document: ReadiumFuzi.XMLDocument) throws -> Publication? {
215+
public static func parseEntry(document: ReadiumFuzi.XMLDocument, feedURL: URL) throws -> Publication? {
222216
guard let root = document.root else {
223217
throw OPDS1ParserError.rootNotFound
224218
}
225-
return parseEntry(entry: root)
219+
return parseEntry(entry: root, feedURL: feedURL)
226220
}
227221

228222
/// Fetch an Open Search template from an OPDS feed.
@@ -297,7 +291,7 @@ public class OPDS1Parser: Loggable {
297291
return MimeTypeParameters(type: type, parameters: params)
298292
}
299293

300-
static func parseEntry(entry: ReadiumFuzi.XMLElement) -> Publication? {
294+
static func parseEntry(entry: ReadiumFuzi.XMLElement, feedURL: URL) -> Publication? {
301295
// Shortcuts to get tag(s)' string value.
302296
func tag(_ name: String) -> String? {
303297
entry.firstChild(tag: name)?.stringValue

0 commit comments

Comments
 (0)