|
1 | | -import Foundation |
2 | | -import Logging |
3 | | -import NIOCore |
4 | | -import NIOHTTP1 |
5 | | -import NIOConcurrencyHelpers |
| 1 | + import Foundation |
| 2 | + import Logging |
| 3 | + import NIOCore |
| 4 | + import NIOHTTP1 |
| 5 | + import NIOConcurrencyHelpers |
6 | 6 |
|
7 | | -/// A channel handler that logs both incoming and outgoing HTTP messages |
8 | | -final class HTTPLogger: ChannelDuplexHandler { |
9 | | - typealias InboundIn = HTTPServerRequestPart |
10 | | - typealias InboundOut = HTTPServerRequestPart |
11 | | - typealias OutboundIn = HTTPServerResponsePart |
12 | | - typealias OutboundOut = HTTPServerResponsePart |
| 7 | + /// A channel handler that logs both incoming and outgoing HTTP messages |
| 8 | + final class HTTPLogger: ChannelDuplexHandler { |
| 9 | + typealias InboundIn = HTTPServerRequestPart |
| 10 | + typealias InboundOut = HTTPServerRequestPart |
| 11 | + typealias OutboundIn = HTTPServerResponsePart |
| 12 | + typealias OutboundOut = HTTPServerResponsePart |
13 | 13 |
|
14 | | - private let httpLogger = Logger(label: "com.cocoanetics.SwiftMCP.HTTP") |
15 | | - private let lock = NIOLock() |
16 | | - |
17 | | - // Track current request/response state |
18 | | - private var currentRequestHead: HTTPRequestHead? |
19 | | - private var currentRequestBody = "" |
20 | | - private var currentResponseHead: HTTPResponseHead? |
21 | | - private var currentResponseBody = "" |
22 | | - |
23 | | - /// Log incoming requests and forward them to the next handler |
24 | | - func channelRead(context: ChannelHandlerContext, data: NIOAny) { |
25 | | - let reqPart = unwrapInboundIn(data) |
| 14 | + private let httpLogger = Logger(label: "com.cocoanetics.SwiftMCP.HTTP") |
| 15 | + private let sseLogger = Logger(label: "com.cocoanetics.SwiftMCP.SSE") |
| 16 | + private let lock = NIOLock() |
26 | 17 |
|
27 | | - lock.withLock { |
28 | | - switch reqPart { |
29 | | - case .head(let head): |
30 | | - // Log previous request if exists |
31 | | - logCurrentRequest() |
32 | | - |
33 | | - currentRequestHead = head |
34 | | - currentRequestBody = "" |
35 | | - |
36 | | - case .body(let buffer): |
37 | | - if let str = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) { |
38 | | - currentRequestBody += str |
| 18 | + // Track current request/response state |
| 19 | + private var currentRequestHead: HTTPRequestHead? |
| 20 | + private var currentRequestBody = "" |
| 21 | + private var currentResponseHead: HTTPResponseHead? |
| 22 | + private var currentResponseBody = "" |
| 23 | + private var isSSEConnection = false |
| 24 | + |
| 25 | + /// Log incoming requests and forward them to the next handler |
| 26 | + func channelRead(context: ChannelHandlerContext, data: NIOAny) { |
| 27 | + let reqPart = unwrapInboundIn(data) |
| 28 | + |
| 29 | + lock.withLock { |
| 30 | + switch reqPart { |
| 31 | + case .head(let head): |
| 32 | + // Check if this is an SSE connection |
| 33 | + if head.uri.hasPrefix("/sse") { |
| 34 | + isSSEConnection = true |
| 35 | + } |
| 36 | + |
| 37 | + // Log previous request if exists |
| 38 | + logCurrentRequest() |
| 39 | + |
| 40 | + currentRequestHead = head |
| 41 | + currentRequestBody = "" |
| 42 | + |
| 43 | + case .body(let buffer): |
| 44 | + if let str = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) { |
| 45 | + currentRequestBody += str |
| 46 | + } |
| 47 | + |
| 48 | + case .end: |
| 49 | + logCurrentRequest() |
| 50 | + currentRequestHead = nil |
| 51 | + currentRequestBody = "" |
39 | 52 | } |
40 | | - |
41 | | - case .end: |
42 | | - logCurrentRequest() |
43 | | - currentRequestHead = nil |
44 | | - currentRequestBody = "" |
45 | 53 | } |
| 54 | + |
| 55 | + context.fireChannelRead(data) |
46 | 56 | } |
47 | 57 |
|
48 | | - context.fireChannelRead(data) |
49 | | - } |
50 | | - |
51 | | - /// Log outgoing responses and forward them to the next handler |
52 | | - func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { |
53 | | - let resPart = unwrapOutboundIn(data) |
54 | | - |
55 | | - lock.withLock { |
56 | | - switch resPart { |
57 | | - case .head(let head): |
58 | | - // Log previous response if exists |
59 | | - logCurrentResponse() |
60 | | - |
61 | | - currentResponseHead = head |
62 | | - currentResponseBody = "" |
63 | | - |
64 | | - case .body(let body): |
65 | | - if case .byteBuffer(let buffer) = body { |
66 | | - if let str = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) { |
67 | | - currentResponseBody += str |
| 58 | + /// Log outgoing responses and forward them to the next handler |
| 59 | + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { |
| 60 | + let resPart = unwrapOutboundIn(data) |
| 61 | + |
| 62 | + lock.withLock { |
| 63 | + switch resPart { |
| 64 | + case .head(let head): |
| 65 | + // For SSE connections, only log the initial response headers |
| 66 | + if isSSEConnection { |
| 67 | + var log = "HTTP/\(head.version.major).\(head.version.minor) \(head.status.code) \(head.status.reasonPhrase)\n" |
| 68 | + head.headers.forEach { log += "\($0.name): \($0.value)\n" } |
| 69 | + log += "\n" |
| 70 | + sseLogger.info("Connection Established:\n\(log)") |
| 71 | + } else { |
| 72 | + // Log previous response if exists |
| 73 | + logCurrentResponse() |
| 74 | + currentResponseHead = head |
| 75 | + currentResponseBody = "" |
| 76 | + } |
| 77 | + |
| 78 | + case .body(let body): |
| 79 | + if case .byteBuffer(let buffer) = body { |
| 80 | + if let str = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) { |
| 81 | + if isSSEConnection { |
| 82 | + // For SSE, log each message immediately |
| 83 | + sseLogger.trace("Message: \(str)") |
| 84 | + } else { |
| 85 | + currentResponseBody += str |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + case .end: |
| 91 | + if !isSSEConnection { |
| 92 | + logCurrentResponse() |
| 93 | + currentResponseHead = nil |
| 94 | + currentResponseBody = "" |
68 | 95 | } |
69 | 96 | } |
70 | | - |
71 | | - case .end: |
72 | | - logCurrentResponse() |
73 | | - currentResponseHead = nil |
74 | | - currentResponseBody = "" |
75 | 97 | } |
| 98 | + |
| 99 | + context.write(data, promise: promise) |
76 | 100 | } |
77 | 101 |
|
78 | | - context.write(data, promise: promise) |
79 | | - } |
80 | | - |
81 | | - func flush(context: ChannelHandlerContext) { |
82 | | - context.flush() |
83 | | - } |
84 | | - |
85 | | - private func logCurrentRequest() { |
86 | | - guard let head = currentRequestHead else { return } |
87 | | - |
88 | | - var log = "\(head.method) \(head.uri) HTTP/\(head.version.major).\(head.version.minor)\n" |
89 | | - head.headers.forEach { log += "\($0.name): \($0.value)\n" } |
90 | | - log += "\n" // Empty line after headers |
91 | | - |
92 | | - if !currentRequestBody.isEmpty { |
93 | | - log += currentRequestBody + "\n" |
| 102 | + func flush(context: ChannelHandlerContext) { |
| 103 | + context.flush() |
94 | 104 | } |
95 | 105 |
|
96 | | - httpLogger.trace("\(log)") |
97 | | - } |
98 | | - |
99 | | - private func logCurrentResponse() { |
100 | | - guard let head = currentResponseHead else { return } |
101 | | - |
102 | | - var log = "HTTP/\(head.version.major).\(head.version.minor) \(head.status.code) \(head.status.reasonPhrase)\n" |
103 | | - head.headers.forEach { log += "\($0.name): \($0.value)\n" } |
104 | | - log += "\n" // Empty line after headers |
| 106 | + private func logCurrentRequest() { |
| 107 | + guard let head = currentRequestHead else { return } |
| 108 | + |
| 109 | + var log = "\(head.method) \(head.uri) HTTP/\(head.version.major).\(head.version.minor)\n" |
| 110 | + head.headers.forEach { log += "\($0.name): \($0.value)\n" } |
| 111 | + log += "\n" // Empty line after headers |
| 112 | + |
| 113 | + if !currentRequestBody.isEmpty { |
| 114 | + log += currentRequestBody + "\n" |
| 115 | + } |
| 116 | + |
| 117 | + httpLogger.info("\(log)") |
| 118 | + } |
105 | 119 |
|
106 | | - if !currentResponseBody.isEmpty { |
107 | | - log += currentResponseBody + "\n" |
| 120 | + private func logCurrentResponse() { |
| 121 | + guard let head = currentResponseHead else { return } |
| 122 | + |
| 123 | + var log = "HTTP/\(head.version.major).\(head.version.minor) \(head.status.code) \(head.status.reasonPhrase)\n" |
| 124 | + head.headers.forEach { log += "\($0.name): \($0.value)\n" } |
| 125 | + log += "\n" // Empty line after headers |
| 126 | + |
| 127 | + if !currentResponseBody.isEmpty { |
| 128 | + log += currentResponseBody + "\n" |
| 129 | + } |
| 130 | + |
| 131 | + httpLogger.info("\(log)") |
108 | 132 | } |
109 | 133 |
|
110 | | - httpLogger.trace("\(log)") |
111 | | - } |
112 | | - |
113 | | - func handlerAdded(context: ChannelHandlerContext) { |
114 | | - lock.withLock { |
115 | | - currentRequestHead = nil |
116 | | - currentRequestBody = "" |
117 | | - currentResponseHead = nil |
118 | | - currentResponseBody = "" |
| 134 | + func handlerAdded(context: ChannelHandlerContext) { |
| 135 | + lock.withLock { |
| 136 | + currentRequestHead = nil |
| 137 | + currentRequestBody = "" |
| 138 | + currentResponseHead = nil |
| 139 | + currentResponseBody = "" |
| 140 | + isSSEConnection = false |
| 141 | + } |
119 | 142 | } |
120 | | - } |
121 | | - |
122 | | - func handlerRemoved(context: ChannelHandlerContext) { |
123 | | - lock.withLock { |
124 | | - // Log any pending messages |
125 | | - logCurrentRequest() |
126 | | - logCurrentResponse() |
127 | | - |
128 | | - // Clear state |
129 | | - currentRequestHead = nil |
130 | | - currentRequestBody = "" |
131 | | - currentResponseHead = nil |
132 | | - currentResponseBody = "" |
| 143 | + |
| 144 | + func handlerRemoved(context: ChannelHandlerContext) { |
| 145 | + lock.withLock { |
| 146 | + // Log any pending messages |
| 147 | + logCurrentRequest() |
| 148 | + if !isSSEConnection { |
| 149 | + logCurrentResponse() |
| 150 | + } |
| 151 | + |
| 152 | + // Clear state |
| 153 | + currentRequestHead = nil |
| 154 | + currentRequestBody = "" |
| 155 | + currentResponseHead = nil |
| 156 | + currentResponseBody = "" |
| 157 | + isSSEConnection = false |
| 158 | + } |
133 | 159 | } |
134 | 160 | } |
135 | | -} |
|
0 commit comments