@@ -840,15 +840,22 @@ extension TaskHandler: ChannelDuplexHandler {
840
840
self . writeBody ( request: request, context: context)
841
841
} . flatMap {
842
842
context. eventLoop. assertInEventLoop ( )
843
+ if case . endOrError = self . state {
844
+ return context. eventLoop. makeSucceededFuture ( ( ) )
845
+ }
846
+
843
847
self . state = . bodySent
844
848
if let expectedBodyLength = self . expectedBodyLength, expectedBodyLength != self . actualBodyLength {
845
849
let error = HTTPClientError . bodyLengthMismatch
846
- self . errorCaught ( context: context, error: error)
847
850
return context. eventLoop. makeFailedFuture ( error)
848
851
}
849
852
return context. writeAndFlush ( self . wrapOutboundOut ( . end( nil ) ) )
850
853
} . map {
851
854
context. eventLoop. assertInEventLoop ( )
855
+ if case . endOrError = self . state {
856
+ return
857
+ }
858
+
852
859
self . state = . sent
853
860
self . callOutToDelegateFireAndForget ( self . delegate. didSendRequest)
854
861
} . flatMapErrorThrowing { error in
@@ -924,31 +931,28 @@ extension TaskHandler: ChannelDuplexHandler {
924
931
let response = self . unwrapInboundIn ( data)
925
932
switch response {
926
933
case . head( let head) :
927
- switch self . state {
928
- case . endOrError:
929
- preconditionFailure ( " unexpected state on .head " )
930
- default :
931
- if !head. isKeepAlive {
932
- self . closing = true
933
- }
934
+ if case . endOrError = self . state {
935
+ return
936
+ }
934
937
935
- if let redirectURL = self . redirectHandler? . redirectTarget ( status: head. status, headers: head. headers) {
936
- self . state = . redirected( head, redirectURL)
937
- } else {
938
- self . state = . head
939
- self . mayRead = false
940
- self . callOutToDelegate ( value: head, channelEventLoop: context. eventLoop, self . delegate. didReceiveHead)
941
- . whenComplete { result in
942
- self . handleBackpressureResult ( context: context, result: result)
943
- }
944
- }
938
+ if !head. isKeepAlive {
939
+ self . closing = true
940
+ }
941
+
942
+ if let redirectURL = self . redirectHandler? . redirectTarget ( status: head. status, headers: head. headers) {
943
+ self . state = . redirected( head, redirectURL)
944
+ } else {
945
+ self . state = . head
946
+ self . mayRead = false
947
+ self . callOutToDelegate ( value: head, channelEventLoop: context. eventLoop, self . delegate. didReceiveHead)
948
+ . whenComplete { result in
949
+ self . handleBackpressureResult ( context: context, result: result)
950
+ }
945
951
}
946
952
case . body( let body) :
947
953
switch self . state {
948
- case . redirected:
954
+ case . redirected, . endOrError :
949
955
break
950
- case . endOrError:
951
- preconditionFailure ( " unexpected state on .body " )
952
956
default :
953
957
self . state = . body
954
958
self . mayRead = false
@@ -960,7 +964,7 @@ extension TaskHandler: ChannelDuplexHandler {
960
964
case . end:
961
965
switch self . state {
962
966
case . endOrError:
963
- preconditionFailure ( " unexpected state on .end " )
967
+ break
964
968
case . redirected( let head, let redirectURL) :
965
969
self . state = . endOrError
966
970
self . task. releaseAssociatedConnection ( delegateType: Delegate . self, closing: self . closing) . whenSuccess {
0 commit comments