@@ -92,22 +92,13 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
92
92
self . failWithError ( . noResult( ) , context: context)
93
93
94
94
case . completed:
95
- let hadMark = self . bufferedWrittenMessages. hasMark
96
- while self . bufferedWrittenMessages. hasMark {
97
- // write until mark
98
- let bufferedPart = self . bufferedWrittenMessages. removeFirst ( )
99
- context. write ( bufferedPart. data, promise: bufferedPart. promise)
100
- }
101
-
102
- // flush any messages up to the mark
103
- if hadMark {
104
- context. flush ( )
105
- }
106
-
107
- // write remainder
108
- while let bufferedPart = self . bufferedWrittenMessages. popFirst ( ) {
95
+ while let ( bufferedPart, isMarked) = self . bufferedWrittenMessages. popFirstCheckMarked ( ) {
109
96
context. write ( bufferedPart. data, promise: bufferedPart. promise)
97
+ if isMarked {
98
+ context. flush ( )
99
+ }
110
100
}
101
+
111
102
}
112
103
113
104
context. leavePipeline ( removalToken: removalToken)
@@ -122,9 +113,12 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
122
113
public func handlerRemoved( context: ChannelHandlerContext ) {
123
114
switch self . state {
124
115
case . failed, . completed:
125
- // we don't expect there to be any buffered messages in these states
126
- assert ( self . bufferedWrittenMessages. isEmpty)
116
+ guard self . bufferedWrittenMessages. isEmpty else {
117
+ self . failWithError ( Error . droppedWrites ( ) , context: context)
118
+ return
119
+ }
127
120
break
121
+
128
122
case . initialized, . connectSent, . headReceived:
129
123
self . failWithError ( Error . noResult ( ) , context: context)
130
124
}
@@ -276,6 +270,7 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
276
270
case httpProxyHandshakeTimeout
277
271
case noResult
278
272
case channelUnexpectedlyInactive
273
+ case droppedWrites
279
274
}
280
275
281
276
final class Storage : Sendable {
@@ -331,6 +326,10 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
331
326
Error ( error: . channelUnexpectedlyInactive, file: file, line: line)
332
327
}
333
328
329
+ public static func droppedWrites( file: String = #file, line: UInt = #line) -> Error {
330
+ Error ( error: . droppedWrites, file: file, line: line)
331
+ }
332
+
334
333
fileprivate var errorCode : Int {
335
334
switch self . store. details {
336
335
case . proxyAuthenticationRequired:
@@ -347,6 +346,8 @@ public final class NIOHTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableC
347
346
return 5
348
347
case . channelUnexpectedlyInactive:
349
348
return 6
349
+ case . droppedWrites:
350
+ return 7
350
351
}
351
352
}
352
353
}
@@ -388,6 +389,8 @@ extension NIOHTTP1ProxyConnectHandler.Error.Details: CustomStringConvertible {
388
389
return " No Result "
389
390
case . channelUnexpectedlyInactive:
390
391
return " Channel Unexpectedly Inactive "
392
+ case . droppedWrites:
393
+ return " Handler Was Removed with Writes Left in the Buffer "
391
394
}
392
395
}
393
396
}
0 commit comments