@@ -839,8 +839,8 @@ extension TaskHandler: ChannelDuplexHandler {
839
839
} . flatMap {
840
840
self . writeBody ( request: request, context: context)
841
841
} . flatMap {
842
- self . state = . bodySent
843
842
context. eventLoop. assertInEventLoop ( )
843
+ self . state = . bodySent
844
844
if let expectedBodyLength = self . expectedBodyLength, expectedBodyLength != self . actualBodyLength {
845
845
let error = HTTPClientError . bodyLengthMismatch
846
846
self . errorCaught ( context: context, error: error)
@@ -924,24 +924,31 @@ extension TaskHandler: ChannelDuplexHandler {
924
924
let response = self . unwrapInboundIn ( data)
925
925
switch response {
926
926
case . head( let head) :
927
- if !head. isKeepAlive {
928
- self . closing = true
929
- }
927
+ switch self . state {
928
+ case . endOrError:
929
+ preconditionFailure ( " unexpected state on .head " )
930
+ default :
931
+ if !head. isKeepAlive {
932
+ self . closing = true
933
+ }
930
934
931
- if let redirectURL = self . redirectHandler? . redirectTarget ( status: head. status, headers: head. headers) {
932
- self . state = . redirected( head, redirectURL)
933
- } else {
934
- self . state = . head
935
- self . mayRead = false
936
- self . callOutToDelegate ( value: head, channelEventLoop: context. eventLoop, self . delegate. didReceiveHead)
937
- . whenComplete { result in
938
- self . handleBackpressureResult ( context: context, result: result)
939
- }
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
+ }
940
945
}
941
946
case . body( let body) :
942
947
switch self . state {
943
948
case . redirected:
944
949
break
950
+ case . endOrError:
951
+ preconditionFailure ( " unexpected state on .body " )
945
952
default :
946
953
self . state = . body
947
954
self . mayRead = false
@@ -952,6 +959,8 @@ extension TaskHandler: ChannelDuplexHandler {
952
959
}
953
960
case . end:
954
961
switch self . state {
962
+ case . endOrError:
963
+ preconditionFailure ( " unexpected state on .end " )
955
964
case . redirected( let head, let redirectURL) :
956
965
self . state = . endOrError
957
966
self . task. releaseAssociatedConnection ( delegateType: Delegate . self, closing: self . closing) . whenSuccess {
0 commit comments