@@ -274,10 +274,15 @@ private final class UnaryHandler: ChannelDuplexHandler {
274
274
preconditionFailure ( " invalid state, no pending request " )
275
275
}
276
276
277
- // As defined in RFC 2616 Section 8.1.2:
278
- // [...] unless otherwise indicated, the client SHOULD assume
279
- // that the server will maintain a persistent connection, even
280
- // after error responses from the server.
277
+ // As defined in RFC 7230 Section 6.3:
278
+ // HTTP/1.1 defaults to the use of "persistent connections", allowing
279
+ // multiple requests and responses to be carried over a single
280
+ // connection. The "close" connection option is used to signal that a
281
+ // connection will not persist after the current request/response. HTTP
282
+ // implementations SHOULD support persistent connections.
283
+ //
284
+ // That's why we only assume the connection shall be closed if we receive
285
+ // a "connection = close" header.
281
286
let serverCloseConnection = response. headers. first ( name: " connection " ) ? . lowercased ( ) == " close "
282
287
283
288
if !self . keepAlive || serverCloseConnection || response. version != . init( major: 1 , minor: 1 ) {
@@ -308,6 +313,9 @@ private final class UnaryHandler: ChannelDuplexHandler {
308
313
case is RequestCancelEvent :
309
314
if self . pending != nil {
310
315
self . completeWith ( . failure( HTTPClient . Errors. cancelled) )
316
+ // after the cancel error has been send, we want to close the connection so
317
+ // that no more packets can be read on this connection.
318
+ _ = context. channel. close ( )
311
319
}
312
320
default :
313
321
context. triggerUserOutboundEvent ( event, promise: promise)
0 commit comments