summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMárton Csordás <csordasmarton92@gmail.com>2021-12-16 10:08:11 +0100
committerJens Geyer <Jens-G@users.noreply.github.com>2022-02-21 19:19:42 +0100
commit103a11c9c28ac963a3b2591ecac641db3cbaa113 (patch)
treee8b187fabc92540991a31ffc7848ee72290fb53d
parentff746966584816988a3babf25debc8a87c50581d (diff)
downloadthrift-103a11c9c28ac963a3b2591ecac641db3cbaa113.tar.gz
THRIFT-5467 Python: fix CannotSendHeader exception
Based on the python source for `http.client`, `HTTPConnection.putheader` can only be called after a request has been started, and before it's been sent. Otherwise it will throw a `http.client.CannotSendHeader` exception. If the server returns a `Set-Cookie` header, the client will always fail with the `CannotSendHeader` exception because `HTTPConnection.putheader` is called after reading the response. With this patch we will call this method before the request has been sent.
-rw-r--r--lib/py/src/transport/THttpClient.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/lib/py/src/transport/THttpClient.py b/lib/py/src/transport/THttpClient.py
index b13169011..82ca3d12d 100644
--- a/lib/py/src/transport/THttpClient.py
+++ b/lib/py/src/transport/THttpClient.py
@@ -91,6 +91,7 @@ class THttpClient(TTransportBase):
self.__http_response = None
self.__timeout = None
self.__custom_headers = None
+ self.headers = None
@staticmethod
def basic_proxy_auth_header(proxy):
@@ -175,6 +176,12 @@ class THttpClient(TTransportBase):
for key, val in six.iteritems(self.__custom_headers):
self.__http.putheader(key, val)
+ # Saves the cookie sent by the server in the previous response.
+ # HTTPConnection.putheader can only be called after a request has been
+ # started, and before it's been sent.
+ if self.headers and 'Set-Cookie' in self.headers:
+ self.__http.putheader('Cookie', self.headers['Set-Cookie'])
+
self.__http.endheaders()
# Write payload
@@ -185,7 +192,3 @@ class THttpClient(TTransportBase):
self.code = self.__http_response.status
self.message = self.__http_response.reason
self.headers = self.__http_response.msg
-
- # Saves the cookie sent by the server response
- if 'Set-Cookie' in self.headers:
- self.__http.putheader('Cookie', self.headers['Set-Cookie'])