@@ -11,31 +11,15 @@ import Foundation
1111/// This is required by some Navigators to access a local publication's
1212/// resources.
1313public protocol HTTPServer {
14- typealias FailureHandler = ( _ request: HTTPServerRequest , _ error: ResourceError ) -> Void
15-
16- /// Serves resources at the given `endpoint`.
17- ///
18- /// Subsequent calls with the same `endpoint` overwrite each other.
19- ///
20- /// - Returns the base URL for this endpoint.
21- @discardableResult
22- func serve(
23- at endpoint: HTTPServerEndpoint ,
24- handler: @escaping ( HTTPServerRequest ) -> Resource
25- ) throws -> HTTPURL
26-
2714 /// Serves resources at the given `endpoint`.
2815 ///
2916 /// Subsequent calls with the same `endpoint` overwrite each other.
3017 ///
31- /// If the resource cannot be served, the `failureHandler` is called.
32- ///
3318 /// - Returns the base URL for this endpoint.
3419 @discardableResult
3520 func serve(
3621 at endpoint: HTTPServerEndpoint ,
37- handler: @escaping ( HTTPServerRequest ) -> Resource ,
38- failureHandler: FailureHandler ?
22+ handler: HTTPRequestHandler
3923 ) throws -> HTTPURL
4024
4125 /// Registers a `Resource` transformer that will be run on all responses
@@ -48,15 +32,6 @@ public protocol HTTPServer {
4832}
4933
5034public extension HTTPServer {
51- @discardableResult
52- func serve(
53- at endpoint: HTTPServerEndpoint ,
54- handler: @escaping ( HTTPServerRequest ) -> Resource ,
55- failureHandler: FailureHandler ?
56- ) throws -> HTTPURL {
57- try serve ( at: endpoint, handler: handler)
58- }
59-
6035 /// Serves the local file `url` at the given `endpoint`.
6136 ///
6237 /// If the provided `url` is a directory, then all the files in the
@@ -70,9 +45,9 @@ public extension HTTPServer {
7045 func serve(
7146 at endpoint: HTTPServerEndpoint ,
7247 contentsOf url: FileURL ,
73- failureHandler : FailureHandler ? = nil
48+ onFailure : HTTPRequestHandler . OnFailure ? = nil
7449 ) throws -> HTTPURL {
75- func handler ( request: HTTPServerRequest ) -> Resource {
50+ func onRequest ( request: HTTPServerRequest ) -> Resource {
7651 let file = request. href. flatMap { url. resolve ( $0) }
7752 ?? url
7853
@@ -87,8 +62,10 @@ public extension HTTPServer {
8762
8863 return try serve (
8964 at: endpoint,
90- handler: handler ( request: ) ,
91- failureHandler: failureHandler
65+ handler: HTTPRequestHandler (
66+ onRequest: onRequest,
67+ onFailure: onFailure
68+ )
9269 )
9370 }
9471
@@ -102,11 +79,11 @@ public extension HTTPServer {
10279 func serve(
10380 at endpoint: HTTPServerEndpoint ,
10481 publication: Publication ,
105- failureHandler : FailureHandler ? = nil
82+ onFailure : HTTPRequestHandler . OnFailure ? = nil
10683 ) throws -> HTTPURL {
107- func handler ( request: HTTPServerRequest ) -> Resource {
84+ func onRequest ( request: HTTPServerRequest ) -> Resource {
10885 guard let href = request. href else {
109- failureHandler ? ( request, . notFound( nil ) )
86+ onFailure ? ( request, . notFound( nil ) )
11087
11188 return FailureResource (
11289 link: Link ( href: request. url. string) ,
@@ -119,8 +96,10 @@ public extension HTTPServer {
11996
12097 return try serve (
12198 at: endpoint,
122- handler: handler ( request: ) ,
123- failureHandler: failureHandler
99+ handler: HTTPRequestHandler (
100+ onRequest: onRequest,
101+ onFailure: onFailure
102+ )
124103 )
125104 }
126105}
@@ -141,3 +120,19 @@ public struct HTTPServerRequest {
141120 self . href = href
142121 }
143122}
123+
124+ /// Callbacks handling a request.
125+ ///
126+ /// If the resource cannot be served, the `onFailure` callback is called.
127+ public struct HTTPRequestHandler {
128+ public typealias OnRequest = ( _ request: HTTPServerRequest ) -> Resource
129+ public typealias OnFailure = ( _ request: HTTPServerRequest , _ error: ResourceError ) -> Void
130+
131+ public let onRequest : OnRequest
132+ public let onFailure : OnFailure ?
133+
134+ public init ( onRequest: @escaping OnRequest , onFailure: OnFailure ? = nil ) {
135+ self . onRequest = onRequest
136+ self . onFailure = onFailure
137+ }
138+ }
0 commit comments