@@ -78,13 +78,35 @@ public struct RoutedHTTPHandler: HTTPHandler, Sendable {
7878 insert ( ( route, ClosureHTTPHandler ( handler) ) , at: index)
7979 }
8080
81+ #if compiler(>=6.2)
82+ nonisolated ( nonsending) public func handleRequest( _ request: HTTPRequest ) async throws -> HTTPResponse {
83+ for entry in handlers {
84+ do {
85+ if let response = try await Self . handleMatchedRequest (
86+ request,
87+ to: entry. route,
88+ handler: entry. handler. handleRequest
89+ ) {
90+ return response
91+ }
92+ } catch is HTTPUnhandledError {
93+ continue
94+ } catch {
95+ throw error
96+ }
97+ }
98+ throw HTTPUnhandledError( )
99+ }
100+ #else
81101 public func handleRequest( _ request: HTTPRequest) async throws -> HTTPResponse {
82102 for entry in handlers {
83103 do {
84- if await entry. route ~= request {
85- return try await HTTPRequest . $matchedRoute. withValue ( entry. route) {
86- return try await entry. handler. handleRequest ( request)
87- }
104+ if let response = try await Self . handleMatchedRequest (
105+ request,
106+ to: entry. route,
107+ handler: entry. handler. handleRequest
108+ ) {
109+ return response
88110 }
89111 } catch is HTTPUnhandledError {
90112 continue
@@ -94,6 +116,7 @@ public struct RoutedHTTPHandler: HTTPHandler, Sendable {
94116 }
95117 throw HTTPUnhandledError( )
96118 }
119+ #endif
97120}
98121
99122public extension RoutedHTTPHandler {
@@ -136,9 +159,21 @@ public extension RoutedHTTPHandler {
136159 }
137160}
138161
139- #if compiler(>=6.0)
140162public extension RoutedHTTPHandler {
141-
163+ #if compiler(>=6.2)
164+ nonisolated ( nonsending) static func handleMatchedRequest(
165+ _ request: HTTPRequest ,
166+ to route: HTTPRoute ,
167+ handler: ( HTTPRequest ) async throws -> HTTPResponse
168+ ) async throws -> HTTPResponse ? {
169+ if await route ~= request {
170+ return try await HTTPRequest . $matchedRoute. withValue ( route) {
171+ return try await handler ( request)
172+ }
173+ }
174+ return nil
175+ }
176+ #elseif compiler(>=6.0)
142177 static func handleMatchedRequest(
143178 isolation: isolated ( any Actor) ? = #isolation,
144179 _ request: HTTPRequest,
@@ -153,8 +188,21 @@ public extension RoutedHTTPHandler {
153188 }
154189 return nil
155190 }
156- }
191+ #else
192+ static func handleMatchedRequest(
193+ _ request: HTTPRequest,
194+ to route: HTTPRoute,
195+ handler: ( HTTPRequest) async throws -> HTTPResponse
196+ ) async throws -> HTTPResponse? {
197+ if await route ~= request {
198+ return try await HTTPRequest . $matchedRoute. withValue ( route) {
199+ return try await handler ( request)
200+ }
201+ }
202+ return nil
203+ }
157204#endif
205+ }
158206
159207extension RoutedHTTPHandler : RangeReplaceableCollection {
160208 public typealias Index = Array < Element > . Index
0 commit comments