Skip to content

Commit cd7774b

Browse files
committed
Various URL API changes
1 parent d76938a commit cd7774b

32 files changed

+390
-235
lines changed

Sources/Adapters/GCDWebServer/GCDHTTPServer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public class GCDHTTPServer: HTTPServer, Loggable {
113113
}
114114

115115
queue.async { [self] in
116-
guard let url = HTTPURL(url: request.url) else {
116+
guard let url = request.url.httpURL else {
117117
completion(FailureResource(link: Link(href: request.url.absoluteString), error: .notFound(nil)))
118118
return
119119
}
@@ -130,7 +130,7 @@ public class GCDHTTPServer: HTTPServer, Loggable {
130130
}
131131

132132
for (endpoint, handler) in handlers {
133-
if endpoint == url.removingQuery()?.removingFragment() {
133+
if endpoint == url.removingQuery().removingFragment() {
134134
let resource = handler(HTTPServerRequest(url: url, href: nil))
135135
completion(transform(resource: resource, at: endpoint))
136136
return
@@ -243,7 +243,7 @@ public class GCDHTTPServer: HTTPServer, Loggable {
243243
throw GCDHTTPServerError.failedToStartServer(cause: error)
244244
}
245245

246-
guard let baseURL = server.serverURL.flatMap(HTTPURL.init(url:)) else {
246+
guard let baseURL = server.serverURL?.httpURL else {
247247
stop()
248248
throw GCDHTTPServerError.nullServerURL
249249
}

Sources/LCP/LCPRenewDelegate.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public protocol LCPRenewDelegate {
2121
///
2222
/// You should present the URL in a `SFSafariViewController` and call the `completion` callback when the browser
2323
/// is dismissed by the user.
24-
func presentWebPage(url: URL, completion: @escaping (CancellableResult<Void, Error>) -> Void)
24+
func presentWebPage(url: HTTPURL, completion: @escaping (CancellableResult<Void, Error>) -> Void)
2525
}
2626

2727
/// Default `LCPRenewDelegate` implementation using standard views.
@@ -41,8 +41,8 @@ public class LCPDefaultRenewDelegate: NSObject, LCPRenewDelegate {
4141
completion(.success(nil))
4242
}
4343

44-
public func presentWebPage(url: URL, completion: @escaping (CancellableResult<Void, Error>) -> Void) {
45-
let safariVC = SFSafariViewController(url: url)
44+
public func presentWebPage(url: HTTPURL, completion: @escaping (CancellableResult<Void, Error>) -> Void) {
45+
let safariVC = SFSafariViewController(url: url.url)
4646
safariVC.modalPresentationStyle = modalPresentationStyle
4747
safariVC.presentationController?.delegate = self
4848
safariVC.delegate = self

Sources/LCP/License/License.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ extension License: LCPLicense {
192192
func renewWithWebPage(_ link: Link) throws -> Deferred<Data, Error> {
193193
guard
194194
let statusURL = try? license.url(for: .status, preferredType: .lcpStatusDocument),
195-
let url = link.url
195+
let url = link.url()
196196
else {
197197
throw LCPError.licenseInteractionNotAvailable
198198
}
@@ -216,13 +216,13 @@ extension License: LCPLicense {
216216
: Deferred.success(nil)
217217
}
218218

219-
func makeRenewURL(from endDate: Date?) throws -> URL {
219+
func makeRenewURL(from endDate: Date?) throws -> HTTPURL {
220220
var params = device.asQueryParameters
221221
if let end = endDate {
222222
params["end"] = end.iso8601
223223
}
224224

225-
guard let url = link.url(with: params) else {
225+
guard let url = link.url(parameters: params) else {
226226
throw LCPError.licenseInteractionNotAvailable
227227
}
228228
return url
@@ -263,8 +263,13 @@ extension License: LCPLicense {
263263
}
264264

265265
func returnPublication(completion: @escaping (LCPError?) -> Void) {
266-
guard let status = documents.status,
267-
let url = try? status.url(for: .return, preferredType: .lcpStatusDocument, with: device.asQueryParameters)
266+
guard
267+
let status = documents.status,
268+
let url = try? status.url(
269+
for: .return,
270+
preferredType: .lcpStatusDocument,
271+
parameters: device.asQueryParameters
272+
)
268273
else {
269274
completion(LCPError.licenseInteractionNotAvailable)
270275
return
@@ -299,7 +304,7 @@ public extension LCPRenewDelegate {
299304
Deferred { preferredEndDate(maximum: maximum, completion: $0) }
300305
}
301306

302-
func presentWebPage(url: URL) -> Deferred<Void, Error> {
307+
func presentWebPage(url: HTTPURL) -> Deferred<Void, Error> {
303308
Deferred { presentWebPage(url: url, completion: $0) }
304309
}
305310
}

Sources/LCP/License/Model/Components/Link.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,19 @@ public struct Link {
5050

5151
/// Gets the valid URL if possible, applying the given template context as query parameters if the link is templated.
5252
/// eg. http://url{?id,name} + [id: x, name: y] -> http://url?id=x&name=y
53-
func url(with parameters: [String: LosslessStringConvertible]) -> URL? {
53+
func url(parameters: [String: LosslessStringConvertible] = [:]) -> HTTPURL? {
5454
var href = href
5555

5656
if templated {
5757
href = URITemplate(href).expand(with: parameters.mapValues { String(describing: $0) })
5858
}
5959

60-
return URL(string: href)
60+
return HTTPURL(string: href)
6161
}
6262

6363
/// Expands the href without any template context.
64-
var url: URL? {
65-
url(with: [:])
66-
}
64+
@available(*, unavailable, message: "Use url() instead")
65+
var url: URL? { fatalError() }
6766

6867
var mediaType: MediaType {
6968
type.flatMap { MediaType.of(mediaType: $0) } ?? .binary

Sources/LCP/License/Model/LicenseDocument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ public struct LicenseDocument {
9999
/// are found, the first link with the `rel` and an empty `type` will be returned.
100100
///
101101
/// - Throws: `LCPError.invalidLink` if the URL can't be built.
102-
func url(for rel: Rel, preferredType: MediaType? = nil, with parameters: [String: LosslessStringConvertible] = [:]) throws -> URL {
102+
func url(for rel: Rel, preferredType: MediaType? = nil, parameters: [String: LosslessStringConvertible] = [:]) throws -> HTTPURL {
103103
let link = link(for: rel, type: preferredType)
104104
?? links.firstWithRelAndNoType(rel.rawValue)
105105

106-
guard let url = link?.url(with: parameters) else {
106+
guard let url = link?.url(parameters: parameters) else {
107107
throw ParsingError.url(rel: rel.rawValue)
108108
}
109109

Sources/LCP/License/Model/StatusDocument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ public struct StatusDocument {
104104
/// are found, the first link with the `rel` and an empty `type` will be returned.
105105
///
106106
/// - Throws: `LCPError.invalidLink` if the URL can't be built.
107-
func url(for rel: Rel, preferredType: MediaType? = nil, with parameters: [String: LosslessStringConvertible] = [:]) throws -> URL {
107+
func url(for rel: Rel, preferredType: MediaType? = nil, parameters: [String: LosslessStringConvertible] = [:]) throws -> HTTPURL {
108108
let link = link(for: rel, type: preferredType)
109109
?? linkWithNoType(for: rel)
110110

111-
guard let url = link?.url(with: parameters) else {
111+
guard let url = link?.url(parameters: parameters) else {
112112
throw ParsingError.url(rel: rel.rawValue)
113113
}
114114

Sources/LCP/Services/CRLService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class CRLService {
4444

4545
/// Fetches the updated Certificate Revocation List from EDRLab.
4646
private func fetch(timeout: TimeInterval? = nil) -> Deferred<String, Error> {
47-
let url = URL(string: "http://crl.edrlab.telesec.de/rl/EDRLab_CA.crl")!
47+
let url = HTTPURL(string: "http://crl.edrlab.telesec.de/rl/EDRLab_CA.crl")!
4848

4949
return httpClient.fetch(HTTPRequest(url: url, timeoutInterval: timeout))
5050
.mapError { _ in LCPError.crlFetching }

Sources/LCP/Services/DeviceService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class DeviceService {
5050
guard !registered else {
5151
return .success(nil)
5252
}
53-
guard let url = link.url(with: self.asQueryParameters) else {
53+
guard let url = link.url(parameters: self.asQueryParameters) else {
5454
throw LCPError.licenseInteractionNotAvailable
5555
}
5656

Sources/Navigator/EPUB/CSS/ReadiumCSS.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ extension ReadiumCSS: HTMLInjectable {
114114
let hasStyles = hasStyles(html)
115115
var stylesheetsFolder = baseURL
116116
if let folder = layout.stylesheets.folder {
117-
stylesheetsFolder = stylesheetsFolder.appendingPath(folder, isDirectory: true)!
117+
stylesheetsFolder = stylesheetsFolder.appendingPath(folder, isDirectory: true)
118118
}
119119

120120
inj.append(.stylesheetLink(
121-
href: stylesheetsFolder.appendingPath("ReadiumCSS-before.css")!.string,
121+
href: stylesheetsFolder.appendingPath("ReadiumCSS-before.css", isDirectory: false).string,
122122
prepend: true
123123
))
124124
if !hasStyles {
125-
inj.append(.stylesheetLink(href: stylesheetsFolder.appendingPath("ReadiumCSS-default.css")!.string))
125+
inj.append(.stylesheetLink(href: stylesheetsFolder.appendingPath("ReadiumCSS-default.css", isDirectory: false).string))
126126
}
127-
inj.append(.stylesheetLink(href: stylesheetsFolder.appendingPath("ReadiumCSS-after.css")!.string))
127+
inj.append(.stylesheetLink(href: stylesheetsFolder.appendingPath("ReadiumCSS-after.css", isDirectory: false).string))
128128

129129
// Fix Readium CSS issue with the positioning of <audio> elements.
130130
// https://github.com/readium/readium-css/issues/94

Sources/Navigator/EPUB/EPUBNavigatorViewModel.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ final class EPUBNavigatorViewModel: Loggable {
5858
)
5959
}
6060

61+
let resourceURL = Bundle.module.resourceURL!.fileURL!
62+
6163
// FIXME: Remove in Readium 3.0
6264
// Serve the fonts under the /fonts endpoint as the Streamer's
6365
// EPUBHTMLInjector is expecting it there.
64-
if let fontsURL = (Bundle.module.resourceURL?.appendingPathComponent("Assets/Static/fonts")).flatMap(FileURL.init(url:)) {
65-
try httpServer.serve(at: "fonts", contentsOf: fontsURL)
66-
}
66+
try httpServer.serve(
67+
at: "fonts",
68+
contentsOf: resourceURL.appendingPath("Assets/Static/fonts", isDirectory: true)
69+
)
6770

6871
try self.init(
6972
publication: publication,
@@ -73,7 +76,7 @@ final class EPUBNavigatorViewModel: Loggable {
7376
publicationBaseURL: baseURL,
7477
assetsURL: httpServer.serve(
7578
at: "readium",
76-
contentsOf: FileURL(url: Bundle.module.resourceURL!.appendingPathComponent("Assets/Static"))!
79+
contentsOf: resourceURL.appendingPath("Assets/Static", isDirectory: true)
7780
),
7881
useLegacySettings: false
7982
)
@@ -105,25 +108,25 @@ final class EPUBNavigatorViewModel: Loggable {
105108
) {
106109
var config = config
107110

108-
if let fontsDir = Bundle.module.resourceURL.flatMap(FileURL.init(url:))?.appendingPath("Assets/Static/fonts") {
111+
if let fontsDir = Bundle.module.resourceURL?.fileURL?.appendingPath("Assets/Static/fonts", isDirectory: true) {
109112
config.fontFamilyDeclarations.append(
110113
CSSFontFamilyDeclaration(
111114
fontFamily: .openDyslexic,
112115
fontFaces: [
113116
CSSFontFace(
114-
file: fontsDir.appendingPath("OpenDyslexic-Regular.otf")!,
117+
file: fontsDir.appendingPath("OpenDyslexic-Regular.otf", isDirectory: false),
115118
style: .normal, weight: .standard(.normal)
116119
),
117120
CSSFontFace(
118-
file: fontsDir.appendingPath("OpenDyslexic-Italic.otf")!,
121+
file: fontsDir.appendingPath("OpenDyslexic-Italic.otf", isDirectory: false),
119122
style: .italic, weight: .standard(.normal)
120123
),
121124
CSSFontFace(
122-
file: fontsDir.appendingPath("OpenDyslexic-Bold.otf")!,
125+
file: fontsDir.appendingPath("OpenDyslexic-Bold.otf", isDirectory: false),
123126
style: .normal, weight: .standard(.bold)
124127
),
125128
CSSFontFace(
126-
file: fontsDir.appendingPath("OpenDyslexic-BoldItalic.otf")!,
129+
file: fontsDir.appendingPath("OpenDyslexic-BoldItalic.otf", isDirectory: false),
127130
style: .italic, weight: .standard(.bold)
128131
),
129132
]
@@ -150,7 +153,7 @@ final class EPUBNavigatorViewModel: Loggable {
150153
css = ReadiumCSS(
151154
layout: CSSLayout(),
152155
rsProperties: config.readiumCSSRSProperties,
153-
baseURL: assetsURL.appendingPath("readium-css/")!,
156+
baseURL: assetsURL.appendingPath("readium-css", isDirectory: true),
154157
fontFamilyDeclarations: config.fontFamilyDeclarations
155158
)
156159

0 commit comments

Comments
 (0)