You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2022/02/21 18:21:50 UTC

[thrift] branch master updated: THRIFT-5467 Python: fix CannotSendHeader exception

This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new 103a11c  THRIFT-5467 Python: fix CannotSendHeader exception
103a11c is described below

commit 103a11c9c28ac963a3b2591ecac641db3cbaa113
Author: Márton Csordás <cs...@gmail.com>
AuthorDate: Thu Dec 16 10:08:11 2021 +0100

    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.
---
 lib/py/src/transport/THttpClient.py | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/lib/py/src/transport/THttpClient.py b/lib/py/src/transport/THttpClient.py
index b131690..82ca3d1 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'])