Skip to content

Commit

Permalink
Use ReadiumGCDWebServer* symbols
Browse files Browse the repository at this point in the history
This depends on the following PR: readium/GCDWebServer#12

Together these solve issue readium#402
  • Loading branch information
ettore authored and Ettore Pasquini committed Mar 25, 2024
1 parent 6365bb9 commit 1a7ab31
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
24 changes: 12 additions & 12 deletions Sources/Adapters/GCDWebServer/GCDHTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public enum GCDHTTPServerError: Error {
case nullServerURL
}

/// Implementation of `HTTPServer` using GCDWebServer under the hood.
/// Implementation of `HTTPServer` using ReadiumGCDWebServer under the hood.
public class GCDHTTPServer: HTTPServer, Loggable {
/// Shared instance of the HTTP server.
public static let shared = GCDHTTPServer()

/// The actual underlying HTTP server instance.
private let server = GCDWebServer()
private let server = ReadiumGCDWebServer()

/// Mapping between endpoints and their handlers.
private var handlers: [HTTPServerEndpoint: (HTTPServerRequest) -> Resource] = [:]
Expand All @@ -45,15 +45,15 @@ public class GCDHTTPServer: HTTPServer, Loggable {

/// Creates a new instance of the HTTP server.
///
/// - Parameter logLevel: See `GCDWebServer.setLogLevel`.
/// - Parameter logLevel: See `ReadiumGCDWebServer.setLogLevel`.
public init(logLevel: Int = 3) {
GCDWebServer.setLogLevel(Int32(logLevel))
ReadiumGCDWebServer.setLogLevel(Int32(logLevel))

NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

server.addDefaultHandler(
forMethod: "GET",
request: GCDWebServerRequest.self,
request: ReadiumGCDWebServerRequest.self,
asyncProcessBlock: { [weak self] request, completion in
self?.handle(request: request, completion: completion)
}
Expand Down Expand Up @@ -83,9 +83,9 @@ public class GCDHTTPServer: HTTPServer, Loggable {
}
}

private func handle(request: GCDWebServerRequest, completion: @escaping GCDWebServerCompletionBlock) {
private func handle(request: ReadiumGCDWebServerRequest, completion: @escaping ReadiumGCDWebServerCompletionBlock) {
responseResource(for: request) { resource in
let response: GCDWebServerResponse
let response: ReadiumGCDWebServerResponse
switch resource.length {
case let .success(length):
response = ResourceResponse(
Expand All @@ -95,14 +95,14 @@ public class GCDHTTPServer: HTTPServer, Loggable {
)
case let .failure(error):
self.log(.error, error)
response = GCDWebServerErrorResponse(statusCode: error.httpStatusCode)
response = ReadiumGCDWebServerErrorResponse(statusCode: error.httpStatusCode)
}

completion(response)
}
}

private func responseResource(for request: GCDWebServerRequest, completion: @escaping (Resource) -> Void) {
private func responseResource(for request: ReadiumGCDWebServerRequest, completion: @escaping (Resource) -> Void) {
let completion = { resource in
// Escape the queue to avoid deadlocks if something is using the
// server in the handler.
Expand Down Expand Up @@ -219,12 +219,12 @@ public class GCDHTTPServer: HTTPServer, Loggable {

do {
try server.start(options: [
GCDWebServerOption_Port: port,
GCDWebServerOption_BindToLocalhost: true,
ReadiumGCDWebServerOption_Port: port,
ReadiumGCDWebServerOption_BindToLocalhost: true,
// We disable automatically suspending the server in the
// background, to be able to play audiobooks even with the
// screen locked.
GCDWebServerOption_AutomaticallySuspendInBackground: false,
ReadiumGCDWebServerOption_AutomaticallySuspendInBackground: false,
])
} catch {
throw GCDHTTPServerError.failedToStartServer(cause: error)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adapters/GCDWebServer/ResourceResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum WebServerResponseError: Error {

/// The object containing the response's ressource data.
/// If the ressource to be served is too big, multiple responses will be created.
class ResourceResponse: GCDWebServerFileResponse, Loggable {
class ResourceResponse: ReadiumGCDWebServerFileResponse, Loggable {
private let bufferSize = 32 * 1024

private var resource: Resource
Expand Down
36 changes: 18 additions & 18 deletions Sources/Streamer/Server/PublicationServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum PublicationServerError: Error {
@available(*, deprecated, message: "See the 2.5.0 migration guide to migrate the HTTP server")
public class PublicationServer: ResourcesServer, Loggable {
/// The HTTP server.
var webServer: GCDWebServer
var webServer: ReadiumGCDWebServer

// Mapping between endpoint and the matching publication.
public private(set) var publications: [String: Publication] = [:]
Expand All @@ -46,11 +46,11 @@ public class PublicationServer: ResourcesServer, Loggable {

public init?() {
#if DEBUG
GCDWebServer.setLogLevel(2)
ReadiumGCDWebServer.setLogLevel(2)
#else
GCDWebServer.setLogLevel(3)
ReadiumGCDWebServer.setLogLevel(3)
#endif
webServer = GCDWebServer()
webServer = ReadiumGCDWebServer()
if startWebServer() == false {
return nil
}
Expand Down Expand Up @@ -100,11 +100,11 @@ public class PublicationServer: ResourcesServer, Loggable {
// Check if it's supported by WKWebView first.
webServer.stop()
try webServer.start(options: [
GCDWebServerOption_Port: port,
GCDWebServerOption_BindToLocalhost: true,
ReadiumGCDWebServerOption_Port: port,
ReadiumGCDWebServerOption_BindToLocalhost: true,
// We disable automatically suspending the server in the background, to be able to play
// audiobooks even with the screen locked.
GCDWebServerOption_AutomaticallySuspendInBackground: false,
ReadiumGCDWebServerOption_AutomaticallySuspendInBackground: false,
])
}

Expand Down Expand Up @@ -208,11 +208,11 @@ public class PublicationServer: ResourcesServer, Loggable {
webServer.addHandler(
forMethod: "GET",
pathRegex: "/\(endpoint)/.*",
request: GCDWebServerRequest.self,
request: ReadiumGCDWebServerRequest.self,
processBlock: { [weak self, weak publication] request in
guard let publication = publication else {
self?.log(.error, "The publication was deallocated.")
return GCDWebServerErrorResponse(statusCode: 500)
return ReadiumGCDWebServerErrorResponse(statusCode: 500)
}

// Remove the prefix from the URI.
Expand All @@ -237,17 +237,17 @@ public class PublicationServer: ResourcesServer, Loggable {
webServer.addHandler(
forMethod: "GET",
pathRegex: "/\(endpoint)/manifest.json",
request: GCDWebServerRequest.self,
request: ReadiumGCDWebServerRequest.self,
processBlock: { [weak self, weak publication] _ in
guard let publication = publication else {
self?.log(.error, "The publication was deallocated.")
return GCDWebServerErrorResponse(statusCode: 500)
return ReadiumGCDWebServerErrorResponse(statusCode: 500)
}
guard let manifestData = publication.jsonManifest?.data(using: .utf8) else {
return GCDWebServerResponse(statusCode: 404)
return ReadiumGCDWebServerResponse(statusCode: 404)
}
let type = "\(MediaType.readiumWebPubManifest.string); charset=utf-8"
return GCDWebServerDataResponse(data: manifestData, contentType: type)
return ReadiumGCDWebServerDataResponse(data: manifestData, contentType: type)
}
)
}
Expand Down Expand Up @@ -314,7 +314,7 @@ public class PublicationServer: ResourcesServer, Loggable {
webServer.addHandler(
forMethod: "GET",
pathRegex: "\(path)(/.*)?",
request: GCDWebServerRequest.self,
request: ReadiumGCDWebServerRequest.self,
processBlock: resourceHandler
)
}
Expand All @@ -324,7 +324,7 @@ public class PublicationServer: ResourcesServer, Loggable {
return baseURL.appendingPathComponent(String(path.dropFirst()))
}

private func resourceHandler(_ request: GCDWebServerRequest?) -> GCDWebServerResponse? {
private func resourceHandler(_ request: ReadiumGCDWebServerRequest?) -> ReadiumGCDWebServerResponse? {
guard let request = request else {
return nil
}
Expand All @@ -333,13 +333,13 @@ public class PublicationServer: ResourcesServer, Loggable {
guard let basePath = paths.first(where: { path.hasPrefix($0) }),
var file = resources[basePath]
else {
return GCDWebServerResponse(statusCode: 404)
return ReadiumGCDWebServerResponse(statusCode: 404)
}
path = String(path.dropFirst(basePath.count + 1))
file.appendPathComponent(path)

guard let data = try? Data(contentsOf: file) else {
return GCDWebServerResponse(statusCode: 404)
return ReadiumGCDWebServerResponse(statusCode: 404)
}

let contentType = MediaType.of(file)?.string
Expand All @@ -348,7 +348,7 @@ public class PublicationServer: ResourcesServer, Loggable {
// log(.debug, "Serve resource `\(path)` (\(contentType))")

assert(file.pathExtension.lowercased() != "css" || contentType == "text/css")
return GCDWebServerDataResponse(data: data, contentType: contentType)
return ReadiumGCDWebServerDataResponse(data: data, contentType: contentType)
}

@available(*, unavailable, message: "Passing a `Container` is not needed anymore")
Expand Down
4 changes: 2 additions & 2 deletions Sources/Streamer/Server/WebServerResourceResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation
import GCDWebServer
import R2Shared

extension GCDWebServerResponse: Loggable {}
extension ReadiumGCDWebServerResponse: Loggable {}

/// Errors thrown by the `WebServerResourceResponse`
///
Expand All @@ -22,7 +22,7 @@ public enum WebServerResponseError: Error {
/// The object containing the response's ressource data.
/// If the ressource to be served is too big, multiple responses will be created.
@available(*, deprecated, message: "See the 2.5.0 migration guide to migrate the HTTP server")
open class WebServerResourceResponse: GCDWebServerFileResponse {
open class WebServerResourceResponse: ReadiumGCDWebServerFileResponse {
private let bufferSize = 32 * 1024

private var resource: Resource
Expand Down

0 comments on commit 1a7ab31

Please sign in to comment.