Skip to content

Commit c906de4

Browse files
author
Hussein Habibi Juybari
committed
Refactor WebSocket handling to use LogTapWebSocket for improved logging and error handling; add URLSessionWebSocketInterceptor for enhanced message interception
1 parent 52c3287 commit c906de4

File tree

9 files changed

+377
-140
lines changed

9 files changed

+377
-140
lines changed

LogTapFramework/LogTapFramework.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
E631EDBD2E917A3600A7563E /* Resources+HTML.swift in Sources */ = {isa = PBXBuildFile; fileRef = E631EDBB2E917A3600A7563E /* Resources+HTML.swift */; };
3636
E631EDBE2E917A3600A7563E /* Resources+JS.swift in Sources */ = {isa = PBXBuildFile; fileRef = E631EDBC2E917A3600A7563E /* Resources+JS.swift */; };
3737
E631EDBF2E917A3600A7563E /* Resources+CSS.swift in Sources */ = {isa = PBXBuildFile; fileRef = E631EDBA2E917A3600A7563E /* Resources+CSS.swift */; };
38+
E6B517572E924E0400C2BC83 /* URLSessionWebSocketInterceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6B517562E924E0400C2BC83 /* URLSessionWebSocketInterceptor.swift */; };
3839
E96D0476EA6E919968A1F3C7 /* LogTapServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7589C7CD61455B415F260418 /* LogTapServer.swift */; };
3940
EAA0A093AFE43B4B27883B36 /* LogTapPrintBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A3DEA0291AAE499650252EE /* LogTapPrintBridge.swift */; };
4041
/* End PBXBuildFile section */
@@ -54,6 +55,7 @@
5455
E631EDBA2E917A3600A7563E /* Resources+CSS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Resources+CSS.swift"; sourceTree = "<group>"; };
5556
E631EDBB2E917A3600A7563E /* Resources+HTML.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Resources+HTML.swift"; sourceTree = "<group>"; };
5657
E631EDBC2E917A3600A7563E /* Resources+JS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Resources+JS.swift"; sourceTree = "<group>"; };
58+
E6B517562E924E0400C2BC83 /* URLSessionWebSocketInterceptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionWebSocketInterceptor.swift; sourceTree = "<group>"; };
5759
F0479E30BB2B2746ECB70926 /* WebSocketLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebSocketLogger.swift; sourceTree = "<group>"; };
5860
F376AE3EADFC5B9BDC157B84 /* LogTapStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogTapStore.swift; sourceTree = "<group>"; };
5961
FFAA137FDC5351D62DF23711 /* LogTapSinkAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogTapSinkAdapter.swift; sourceTree = "<group>"; };
@@ -120,6 +122,7 @@
120122
656C6F4A0FDB95A25563A442 /* Interceptor */ = {
121123
isa = PBXGroup;
122124
children = (
125+
E6B517562E924E0400C2BC83 /* URLSessionWebSocketInterceptor.swift */,
123126
530D0CC9FF4454CA8BAB149D /* URLProtocol+Interceptor.swift */,
124127
);
125128
path = Interceptor;
@@ -281,6 +284,7 @@
281284
E96D0476EA6E919968A1F3C7 /* LogTapServer.swift in Sources */,
282285
0E984B67744E25C42B47278E /* LogTapSinkAdapter.swift in Sources */,
283286
B603DE71406978978792B930 /* LogTapStore.swift in Sources */,
287+
E6B517572E924E0400C2BC83 /* URLSessionWebSocketInterceptor.swift in Sources */,
284288
B0EFC6368A3014296ABAE170 /* Resources.swift in Sources */,
285289
80738B9D53394333931FA06D /* URLProtocol+Interceptor.swift in Sources */,
286290
D1D8267FFA7613F0678F2981 /* WebSocketLogger.swift in Sources */,

LogTapFramework/Sources/LogTapFramework/Interceptor/URLProtocol+Interceptor.swift

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)