51 """An XML-RPC transport handling authentication via cookies""" |
51 """An XML-RPC transport handling authentication via cookies""" |
52 |
52 |
53 _http_connection = httplib.HTTPConnection |
53 _http_connection = httplib.HTTPConnection |
54 _http_connection_compat = TimeoutHTTP |
54 _http_connection_compat = TimeoutHTTP |
55 |
55 |
56 def __init__(self, user_agent, credentials=(), cookies=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
56 def __init__(self, user_agent, credentials=(), cookies=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None): |
57 xmlrpclib.Transport.__init__(self) |
57 xmlrpclib.Transport.__init__(self) |
58 self.user_agent = user_agent |
58 self.user_agent = user_agent |
59 self.credentials = credentials |
59 self.credentials = credentials |
60 self.cookies = cookies |
60 self.cookies = cookies |
61 self.timeout = timeout |
61 self.timeout = timeout |
|
62 self.headers = headers |
62 if self._connection_required_compat(): |
63 if self._connection_required_compat(): |
63 self.make_connection = self._make_connection_compat |
64 self.make_connection = self._make_connection_compat |
64 self.get_response = self._get_response_compat |
65 self.get_response = self._get_response_compat |
65 |
66 |
66 def _connection_required_compat(self): |
67 def _connection_required_compat(self): |
101 connection.putheader('Cookie', '%s=%s' % (cookie.name, cookie.value)) |
102 connection.putheader('Cookie', '%s=%s' % (cookie.name, cookie.value)) |
102 elif self.credentials: |
103 elif self.credentials: |
103 auth = 'Basic %s' % base64.encodestring("%s:%s" % self.credentials).strip() |
104 auth = 'Basic %s' % base64.encodestring("%s:%s" % self.credentials).strip() |
104 connection.putheader('Authorization', auth) |
105 connection.putheader('Authorization', auth) |
105 |
106 |
|
107 # send custom headers |
|
108 def send_headers(self, connection): |
|
109 for k, v in (self.headers or {}).iteritems(): |
|
110 connection.putheader(k, v) |
|
111 |
106 # dummy request class for extracting cookies |
112 # dummy request class for extracting cookies |
107 class CookieRequest(urllib2.Request): |
113 class CookieRequest(urllib2.Request): |
108 pass |
114 pass |
109 |
115 |
110 # dummy response info headers helper |
116 # dummy response info headers helper |
135 if verbose: |
141 if verbose: |
136 connection.set_debuglevel(1) |
142 connection.set_debuglevel(1) |
137 self.send_request(connection, handler, request_body) |
143 self.send_request(connection, handler, request_body) |
138 self.send_host(connection, host) |
144 self.send_host(connection, host) |
139 self.send_user_agent(connection) |
145 self.send_user_agent(connection) |
|
146 self.send_headers(connection) |
140 self.send_content(connection, request_body) |
147 self.send_content(connection, request_body) |
141 # get response |
148 # get response |
142 return self.get_response(connection, host, handler) |
149 return self.get_response(connection, host, handler) |
143 |
150 |
144 def get_response(self, connection, host, handler): |
151 def get_response(self, connection, host, handler): |
173 |
180 |
174 _http_connection = httplib.HTTPSConnection |
181 _http_connection = httplib.HTTPSConnection |
175 _http_connection_compat = TimeoutHTTPS |
182 _http_connection_compat = TimeoutHTTPS |
176 |
183 |
177 |
184 |
178 def getClient(uri, credentials=(), verbose=False, allow_none=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
185 def getClient(uri, credentials=(), verbose=False, allow_none=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None): |
179 """Get an XML-RPC client which supports basic authentication""" |
186 """Get an XML-RPC client which supports basic authentication""" |
180 if uri.startswith('https:'): |
187 if uri.startswith('https:'): |
181 transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY secure transport)', credentials, timeout=timeout) |
188 transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY secure transport)', credentials, timeout=timeout, headers=headers) |
182 else: |
189 else: |
183 transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY basic transport)', credentials, timeout=timeout) |
190 transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY basic transport)', credentials, timeout=timeout, headers=headers) |
184 return xmlrpclib.Server(uri, transport=transport, verbose=verbose, allow_none=allow_none) |
191 return xmlrpclib.Server(uri, transport=transport, verbose=verbose, allow_none=allow_none) |
185 |
192 |
186 |
193 |
187 def getClientWithCookies(uri, credentials=(), verbose=False, allow_none=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
194 def getClientWithCookies(uri, credentials=(), verbose=False, allow_none=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None): |
188 """Get an XML-RPC client which supports authentication throught cookies""" |
195 """Get an XML-RPC client which supports authentication through cookies""" |
189 if uri.startswith('https:'): |
196 if uri.startswith('https:'): |
190 transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY secure cookie transport)', credentials, cookielib.CookieJar(), timeout) |
197 transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY secure cookie transport)', credentials, cookielib.CookieJar(), timeout, headers) |
191 else: |
198 else: |
192 transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY secure cookie transport)', credentials, cookielib.CookieJar(), timeout) |
199 transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY basic cookie transport)', credentials, cookielib.CookieJar(), timeout, headers) |
193 return xmlrpclib.Server(uri, transport=transport, verbose=verbose, allow_none=allow_none) |
200 return xmlrpclib.Server(uri, transport=transport, verbose=verbose, allow_none=allow_none) |