@@ -14,21 +14,25 @@ public final class LogTapURLProtocol: URLProtocol, URLSessionDataDelegate {
1414 private var resp : URLResponse ?
1515 private var data = Data ( )
1616 private var startedAt : CFAbsoluteTime = 0
17-
17+
1818 public override class func canInit( with request: URLRequest ) -> Bool {
1919 // Avoid loops: respect our marker
20- if URLProtocol . property ( forKey: " LogTapHandled " , in: request) as? Bool == true { return false }
20+ if URLProtocol . property ( forKey: " LogTapHandled " , in: request) as? Bool == true {
21+ return false
22+ }
2123 // Only HTTP/HTTPS
2224 return ( request. url? . scheme == " http " || request. url? . scheme == " https " )
2325 }
24-
25- public override class func canonicalRequest( for request: URLRequest ) -> URLRequest { request }
26-
26+
27+ public override class func canonicalRequest( for request: URLRequest ) -> URLRequest {
28+ request
29+ }
30+
2731 public override func startLoading( ) {
2832 startedAt = CFAbsoluteTimeGetCurrent ( )
2933 var r = ( self . request as NSURLRequest ) . mutableCopy ( ) as! NSMutableURLRequest
3034 URLProtocol . setProperty ( true , forKey: " LogTapHandled " , in: r)
31-
35+
3236 // Log request
3337 let bodyPreview : String ?
3438 if let body = r. httpBody, !body. isEmpty {
@@ -40,21 +44,25 @@ public final class LogTapURLProtocol: URLProtocol, URLSessionDataDelegate {
4044 } else {
4145 bodyPreview = nil
4246 }
43-
47+
4448 LogTap . shared. emit (
4549 LogEvent (
4650 kind: . http,
4751 direction: . request,
4852 summary: " → \( r. httpMethod ?? " GET " ) \( r. url? . absoluteString ?? " " ) " ,
4953 url: r. url? . absoluteString,
5054 method: r. httpMethod,
51- headers: r. allHTTPHeaderFields. map { d in d. reduce ( into: [ : ] ) { $0 [ $1. key] = [ $1. value] } } ,
55+ headers: r. allHTTPHeaderFields. map { d in
56+ d. reduce ( into: [ : ] ) {
57+ $0 [ $1. key] = [ $1. value]
58+ }
59+ } ,
5260 bodyPreview: bodyPreview,
5361 bodyIsTruncated: false ,
5462 tag: " HTTP "
5563 )
5664 )
57-
65+
5866 // Create a “passthrough” task
5967 let cfg = URLSessionConfiguration . default
6068 cfg. protocolClasses = [ ]
@@ -63,29 +71,31 @@ public final class LogTapURLProtocol: URLProtocol, URLSessionDataDelegate {
6371 relayTask = session. dataTask ( with: r as URLRequest )
6472 relayTask? . resume ( )
6573 }
66-
74+
6775 public override func stopLoading( ) {
6876 relayTask? . cancel ( )
6977 relaySession? . invalidateAndCancel ( )
7078 }
71-
79+
7280 // MARK: URLSessionDataDelegate
73-
81+
7482 public func urlSession( _ session: URLSession , dataTask: URLSessionDataTask , didReceive response: URLResponse , completionHandler: @escaping ( URLSession . ResponseDisposition ) -> Void ) {
7583 resp = response
7684 client? . urlProtocol ( self , didReceive: response, cacheStoragePolicy: . notAllowed)
7785 completionHandler ( . allow)
7886 }
79-
87+
8088 public func urlSession( _ session: URLSession , dataTask: URLSessionDataTask , didReceive data: Data ) {
8189 self . data. append ( data)
8290 client? . urlProtocol ( self , didLoad: data)
8391 }
84-
92+
8593 public func urlSession( _ session: URLSession , task: URLSessionTask , didCompleteWithError error: Error ? ) {
8694 let tookMs = Int64 ( ( CFAbsoluteTimeGetCurrent ( ) - startedAt) * 1000 )
87- defer { client? . urlProtocolDidFinishLoading ( self ) }
88-
95+ defer {
96+ client? . urlProtocolDidFinishLoading ( self )
97+ }
98+
8999 if let error = error {
90100 LogTap . shared. emit (
91101 LogEvent (
@@ -94,14 +104,14 @@ public final class LogTapURLProtocol: URLProtocol, URLSessionDataDelegate {
94104 summary: " HTTP ERROR \( self . request. httpMethod ?? " " ) \( self . request. url? . absoluteString ?? " " ) — \( error. localizedDescription) " ,
95105 url: self . request. url? . absoluteString,
96106 method: self . request. httpMethod,
97- reason: error. localizedDescription,
107+ reason: error. localizedDescription,
98108 tag: " HTTP "
99109 )
100110 )
101111 client? . urlProtocol ( self , didFailWithError: error)
102112 return
103113 }
104-
114+
105115 let httpResp = resp as? HTTPURLResponse
106116 let bodyPreview = LogTap . makeBodyPreview ( data: data, contentType: httpResp? . value ( forHTTPHeaderField: " Content-Type " ) )
107117 LogTap . shared. emit (
@@ -117,11 +127,11 @@ public final class LogTapURLProtocol: URLProtocol, URLSessionDataDelegate {
117127 acc [ k] = [ v]
118128 }
119129 }
120- ) ,
130+ ) ,
121131 bodyPreview: bodyPreview,
122132 bodyBytes: data. count,
123133 tookMs: tookMs,
124- tag: " HTTP " ,
134+ tag: " HTTP "
125135 )
126136 )
127137 }
0 commit comments