src/pyams_utils/protocol/xmlrpc.py
changeset 289 c8e21d7dd685
child 292 b338586588ad
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_utils/protocol/xmlrpc.py	Wed Dec 05 12:45:56 2018 +0100
@@ -0,0 +1,167 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+import base64
+import http.client
+import http.cookiejar
+import socket
+import urllib.request
+import xmlrpc.client
+
+try:
+    import gzip
+except ImportError:
+    gzip = None #python can be built without zlib/gzip support
+
+# import interfaces
+
+# import packages
+
+
+class XMLRPCCookieAuthTransport(xmlrpc.client.Transport):
+    """An XML-RPC transport handling authentication via cookies"""
+
+    _http_connection = http.client.HTTPConnection
+    verbose = False
+
+    def __init__(self, user_agent, credentials=(), cookies=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None):
+        xmlrpc.client.Transport.__init__(self)
+        self.user_agent = user_agent
+        self.credentials = credentials
+        self.cookies = cookies
+        self.timeout = timeout
+        self.headers = headers
+
+    def request(self, host, handler, request_body, verbose=False):
+        self.verbose = verbose
+        # issue XML-RPC request
+        connection = self.send_request(host, handler, request_body, verbose)
+        # get response
+        return self.get_response(connection, host, handler)
+
+    def make_connection(self, host):
+        # This is the make_connection that runs under Python 2.7 and newer.
+        # The code is pulled straight from 2.7 xmlrpclib, except replacing
+        # HTTPConnection with self._http_connection
+        if self._connection and host == self._connection[0]:
+            return self._connection[1]
+        chost, self._extra_headers, _x509 = self.get_host_info(host)
+        self._connection = host, self._http_connection(chost, timeout=self.timeout)
+        return self._connection[1]
+
+    def send_request(self, host, handler, request_body, debug):
+        connection = self.make_connection(host)
+        headers = self._extra_headers[:]
+        if debug:
+            connection.set_debuglevel(1)
+        if self.accept_gzip_encoding and gzip:
+            connection.putrequest("POST", handler, skip_accept_encoding=True)
+            headers.append(("Accept-Encoding", "gzip"))
+        else:
+            connection.putrequest("POST", handler)
+        self.send_auth(connection)
+        self.send_content_type(connection)
+        self.send_user_agent(connection)
+        self.send_headers(connection, headers)
+        self.send_content(connection, request_body)
+        return connection
+
+    # override the send_host hook to also send authentication info
+    def send_auth(self, connection):
+        if (self.cookies is not None) and (len(self.cookies) > 0):
+            for cookie in self.cookies:
+                connection.putheader('Cookie', '%s=%s' % (cookie.name, cookie.value))
+        elif self.credentials:
+            creds = base64.encodebytes(("%s:%s" % self.credentials).encode()).strip().decode()
+            auth = 'Basic %s' % creds
+            connection.putheader('Authorization', auth)
+
+    # send content type
+    def send_content_type(self, connection):
+        connection.putheader('Content-Type', 'text/xml')
+
+    # send user agent
+    def send_user_agent(self, connection):
+        connection.putheader('User-Agent', self.user_agent)
+
+    # send custom headers
+    def send_headers(self, connection, headers):
+        xmlrpc.client.Transport.send_headers(self, connection, headers)
+        for k, v in (self.headers or {}).items():
+            connection.putheader(k, v)
+
+    # dummy request class for extracting cookies
+    class CookieRequest(urllib.request.Request):
+        pass
+
+    # dummy response info headers helper
+    class CookieResponseHelper:
+        def __init__(self, response):
+            self.response = response
+        def getheaders(self, header):
+            return self.response.msg.getallmatchingheaders(header)
+
+    # dummy response class for extracting cookies
+    class CookieResponse:
+        def __init__(self, response):
+            self.response = response
+        def info(self):
+            return XMLRPCCookieAuthTransport.CookieResponseHelper(self.response)
+
+    def get_response(self, connection, host, handler):
+        response = connection.getresponse()
+        # extract cookies from response headers
+        if self.cookies is not None:
+            crequest = XMLRPCCookieAuthTransport.CookieRequest('http://%s/' % host)
+            cresponse = XMLRPCCookieAuthTransport.CookieResponse(response)
+            for cookie in self.cookies.make_cookies(cresponse, crequest):
+                if cookie.name.startswith('Set-Cookie'):
+                    cookie.name = cookie.name.split(': ', 1)[1]
+                self.cookies.set_cookie(cookie)
+        if response.status != 200:
+            raise xmlrpc.client.ProtocolError(host + handler, response.status, response.reason, response.getheaders())
+        return self.parse_response(response)
+
+
+class SecureXMLRPCCookieAuthTransport(XMLRPCCookieAuthTransport):
+    """Secure XML-RPC transport"""
+
+    _http_connection = http.client.HTTPSConnection
+
+
+def get_client(uri, credentials=(), verbose=False, allow_none=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None):
+    """Get an XML-RPC client which supports basic authentication"""
+    if uri.startswith('https:'):
+        transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (PyAMS secure transport)', credentials,
+                                                    timeout=timeout, headers=headers)
+    else:
+        transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (PyAMS basic transport)', credentials,
+                                              timeout=timeout, headers=headers)
+    return xmlrpc.client.Server(uri, transport=transport, verbose=verbose, allow_none=allow_none)
+
+
+def get_client_with_cookies(uri, credentials=(), verbose=False, allow_none=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
+                            headers=None, cookies=None):
+    """Get an XML-RPC client which supports authentication through cookies"""
+    if cookies is None:
+        cookies = http.cookiejar.CookieJar()
+    if uri.startswith('https:'):
+        transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (PyAMS secure cookie transport)',
+                                                    credentials, cookies, timeout, headers)
+    else:
+        transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (PyAMS basic cookie transport)',
+                                              credentials, cookies, timeout, headers)
+    return xmlrpc.client.Server(uri, transport=transport, verbose=verbose, allow_none=allow_none)