Skip to content

Commit 12a1fe2

Browse files
committed
Merge petertodd#153: Only set Authorization header when we have it
dd89943 Only set Authorization header when we have it (Alfie John) Pull request description: Tree-SHA512: 464ef6874d59bcd865a06bc230240e18cefa727f0f9865490ba2341b6ca25443b4bace4a117cd08cc4cb12021dfa8e02c7d4235702a9caf261d1cde9d7f79567
2 parents 98676f9 + dd89943 commit 12a1fe2

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

bitcoin/rpc.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def __init__(self,
129129
# __conn being created __del__() can detect the condition and handle it
130130
# correctly.
131131
self.__conn = None
132+
authpair = None
132133

133134
if service_url is None:
134135
# Figure out the path to the bitcoin.conf file
@@ -186,8 +187,12 @@ def __init__(self,
186187
else:
187188
port = self.__url.port
188189
self.__id_count = 0
189-
authpair = authpair.encode('utf8')
190-
self.__auth_header = b"Basic " + base64.b64encode(authpair)
190+
191+
if authpair is None:
192+
self.__auth_header = None
193+
else:
194+
authpair = authpair.encode('utf8')
195+
self.__auth_header = b"Basic " + base64.b64encode(authpair)
191196

192197
self.__conn = httplib.HTTPConnection(self.__url.hostname, port=port,
193198
timeout=timeout)
@@ -200,11 +205,17 @@ def _call(self, service_name, *args):
200205
'method': service_name,
201206
'params': args,
202207
'id': self.__id_count})
203-
self.__conn.request('POST', self.__url.path, postdata,
204-
{'Host': self.__url.hostname,
205-
'User-Agent': DEFAULT_USER_AGENT,
206-
'Authorization': self.__auth_header,
207-
'Content-type': 'application/json'})
208+
209+
headers = {
210+
'Host': self.__url.hostname,
211+
'User-Agent': DEFAULT_USER_AGENT,
212+
'Content-type': 'application/json',
213+
}
214+
215+
if self.__auth_header is not None:
216+
headers['Authorization'] = self.__auth_header
217+
218+
self.__conn.request('POST', self.__url.path, postdata, headers)
208219

209220
response = self._get_response()
210221
if response['error'] is not None:
@@ -218,12 +229,17 @@ def _call(self, service_name, *args):
218229

219230
def _batch(self, rpc_call_list):
220231
postdata = json.dumps(list(rpc_call_list))
221-
self.__conn.request('POST', self.__url.path, postdata,
222-
{'Host': self.__url.hostname,
223-
'User-Agent': DEFAULT_USER_AGENT,
224-
'Authorization': self.__auth_header,
225-
'Content-type': 'application/json'})
226232

233+
headers = {
234+
'Host': self.__url.hostname,
235+
'User-Agent': DEFAULT_USER_AGENT,
236+
'Content-type': 'application/json',
237+
}
238+
239+
if self.__auth_header is not None:
240+
headers['Authorization'] = self.__auth_header
241+
242+
self.__conn.request('POST', self.__url.path, postdata, headers)
227243
return self._get_response()
228244

229245
def _get_response(self):

0 commit comments

Comments
 (0)