Skip to content

Commit 676e781

Browse files
committed
ProxyConnectHandler unexpected chann inactive errors
HTTP1ProxyConnectHandler now errors rather than fails a precondition if it becomes inactive unexpectedly when in initializing state
1 parent 3b7bd3d commit 676e781

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Sources/NIOExtras/HTTP1ProxyConnectHandler.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
113113
context.leavePipeline(removalToken: removalToken)
114114
}
115115

116-
117116
public func handlerAdded(context: ChannelHandlerContext) {
118117
if context.channel.isActive {
119118
self.sendConnect(context: context)
@@ -139,7 +138,7 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
139138
public func channelInactive(context: ChannelHandlerContext) {
140139
switch self.state {
141140
case .initialized:
142-
preconditionFailure("How can we receive a channelInactive before a channelActive?")
141+
self.failWithError(Error.channelUnexpectedlyInactive(), context: context, closeConnection: false)
143142
case .connectSent(let timeout), .headReceived(let timeout):
144143
timeout.cancel()
145144
self.failWithError(Error.remoteConnectionClosed(), context: context, closeConnection: false)
@@ -276,6 +275,7 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
276275
case remoteConnectionClosed
277276
case httpProxyHandshakeTimeout
278277
case noResult
278+
case channelUnexpectedlyInactive
279279
}
280280

281281
final class Storage: Sendable {
@@ -326,6 +326,11 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
326326
Error(error: .noResult, file: file, line: line)
327327
}
328328

329+
/// Handler became unexpectedly inactive before a connection was made
330+
public static func channelUnexpectedlyInactive(file: String = #file, line: UInt = #line) -> Error {
331+
Error(error: .channelUnexpectedlyInactive, file: file, line: line)
332+
}
333+
329334
fileprivate var errorCode: Int {
330335
switch self.store.details {
331336
case .proxyAuthenticationRequired:
@@ -340,6 +345,8 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
340345
return 4
341346
case .noResult:
342347
return 5
348+
case .channelUnexpectedlyInactive:
349+
return 6
343350
}
344351
}
345352
}
@@ -379,6 +386,8 @@ extension NIOHTTP1ProxyConnectHandler.Error.Details: CustomStringConvertible {
379386
return "HTTP Proxy Handshake Timeout"
380387
case .noResult:
381388
return "No Result"
389+
case .channelUnexpectedlyInactive:
390+
return "Channel Unexpectedly Inactive"
382391
}
383392
}
384393
}

0 commit comments

Comments
 (0)