@@ -941,23 +941,26 @@ def _tunnel(self):
941
941
del headers
942
942
943
943
response = self .response_class (self .sock , method = self ._method )
944
- (version , code , message ) = response ._read_status ()
944
+ try :
945
+ (version , code , message ) = response ._read_status ()
945
946
946
- if code != http .HTTPStatus .OK :
947
- self .close ()
948
- raise OSError (f"Tunnel connection failed: { code } { message .strip ()} " )
949
- while True :
950
- line = response .fp .readline (_MAXLINE + 1 )
951
- if len (line ) > _MAXLINE :
952
- raise LineTooLong ("header line" )
953
- if not line :
954
- # for sites which EOF without sending a trailer
955
- break
956
- if line in (b'\r \n ' , b'\n ' , b'' ):
957
- break
947
+ if code != http .HTTPStatus .OK :
948
+ self .close ()
949
+ raise OSError (f"Tunnel connection failed: { code } { message .strip ()} " )
950
+ while True :
951
+ line = response .fp .readline (_MAXLINE + 1 )
952
+ if len (line ) > _MAXLINE :
953
+ raise LineTooLong ("header line" )
954
+ if not line :
955
+ # for sites which EOF without sending a trailer
956
+ break
957
+ if line in (b'\r \n ' , b'\n ' , b'' ):
958
+ break
958
959
959
- if self .debuglevel > 0 :
960
- print ('header:' , line .decode ())
960
+ if self .debuglevel > 0 :
961
+ print ('header:' , line .decode ())
962
+ finally :
963
+ response .close ()
961
964
962
965
def connect (self ):
963
966
"""Connect to the host and port specified in __init__."""
0 commit comments