/usr/lib/python2.7/dist-packages/ubuntu-sso-client/ubuntu_sso/utils/webclient/libsoup.py is in python-ubuntu-sso-client 13.10-0ubuntu11.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | # -*- coding: utf-8 -*-
#
# Copyright 2011-2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the
# OpenSSL library under certain conditions as described in each
# individual source file, and distribute linked combinations
# including the two.
# You must obey the GNU General Public License in all respects
# for all of the code used other than OpenSSL. If you modify
# file(s) with this exception, you may extend this exception to your
# version of the file(s), but you are not obligated to do so. If you
# do not wish to do so, delete this exception statement from your
# version. If you delete this exception statement from all source
# files in the program, then also delete it here.
"""A webclient backend that uses libsoup."""
import httplib
from twisted.internet import defer
from ubuntu_sso.logger import setup_logging
from ubuntu_sso.utils.webclient.common import (
BaseWebClient,
HeaderDict,
Response,
ProxyUnauthorizedError,
UnauthorizedError,
WebClientError,
)
URI_ANONYMOUS_TEMPLATE = "http://{host}:{port}/"
URI_USERNAME_TEMPLATE = "http://{username}:{password}@{host}:{port}/"
logger = setup_logging("ubuntu_sso.utils.webclient.libsoup")
class WebClient(BaseWebClient):
"""A webclient with a libsoup backend."""
def __init__(self, *args, **kwargs):
"""Initialize this instance."""
super(WebClient, self).__init__(*args, **kwargs)
# pylint: disable=E0611,F0401
from gi.repository import Soup, SoupGNOME
self.soup = Soup
self.session = Soup.SessionAsync()
self.session.add_feature(SoupGNOME.ProxyResolverGNOME())
self.session.connect("authenticate", self._on_authenticate)
def _on_message(self, session, message, d):
"""Handle the result of an http message."""
logger.debug('_on_message status code is %s', message.status_code)
if message.status_code == httplib.OK:
headers = HeaderDict()
response_headers = message.get_property("response-headers")
add_header = lambda key, value, _: headers[key].append(value)
response_headers.foreach(add_header, None)
content = message.response_body.flatten().get_data()
response = Response(content, headers)
d.callback(response)
elif message.status_code == httplib.UNAUTHORIZED:
e = UnauthorizedError(message.reason_phrase)
d.errback(e)
elif message.status_code == httplib.PROXY_AUTHENTICATION_REQUIRED:
e = ProxyUnauthorizedError(message.reason_phrase)
d.errback(e)
else:
e = WebClientError(message.reason_phrase)
d.errback(e)
@defer.inlineCallbacks
def _on_authenticate(self, session, message, auth, retrying, data=None):
"""Handle the "authenticate" signal."""
self.session.pause_message(message)
try:
logger.debug('_on_authenticate: message status code is %s',
message.status_code)
if not retrying and self.username and self.password:
auth.authenticate(self.username, self.password)
if auth.is_for_proxy():
logger.debug('_on_authenticate auth is for proxy.')
got_creds = yield self.request_proxy_auth_credentials(
self.session.props.proxy_uri.host,
retrying)
if got_creds:
logger.debug('Got proxy credentials from user.')
auth.authenticate(self.proxy_username, self.proxy_password)
finally:
self.session.unpause_message(message)
@defer.inlineCallbacks
def _on_proxy_authenticate(self, failure, iri, method="GET",
extra_headers=None, oauth_credentials=None, post_content=None):
"""Deal with wrong settings."""
failure.trap(ProxyUnauthorizedError)
logger.debug('Proxy settings are wrong.')
got_creds = yield self.request_proxy_auth_credentials(
self.session.props.proxy_uri.host,
True)
if got_creds:
settings = dict(host=self.session.props.proxy_uri.host,
port=self.session.props.proxy_uri.port,
username=self.proxy_username,
password=self.proxy_password)
self.force_use_proxy(settings)
response = yield self.request(iri, method, extra_headers,
oauth_credentials, post_content)
defer.returnValue(response)
@defer.inlineCallbacks
def request(self, iri, method="GET", extra_headers=None,
oauth_credentials=None, post_content=None):
"""Return a deferred that will be fired with a Response object."""
uri = self.iri_to_uri(iri)
headers = yield self.build_request_headers(uri, method, extra_headers,
oauth_credentials)
d = defer.Deferred()
message = self.soup.Message.new(method, uri)
for key, value in headers.items():
message.request_headers.append(key, value)
if post_content:
message.request_body.append(post_content)
self.session.queue_message(message, self._on_message, d)
d.addErrback(self._on_proxy_authenticate, iri, method, extra_headers,
oauth_credentials, post_content)
response = yield d
defer.returnValue(response)
def force_use_proxy(self, settings):
"""Setup this webclient to use the given proxy settings."""
# pylint: disable=W0511
proxy_uri = self.get_proxy_uri(settings)
self.session.set_property("proxy-uri", proxy_uri)
def get_proxy_uri(self, settings):
"""Get a Soup.URI for the proxy, or None if disabled."""
if "host" in settings and "port" in settings:
template = URI_ANONYMOUS_TEMPLATE
if "username" in settings and "password" in settings:
template = URI_USERNAME_TEMPLATE
uri = template.format(**settings)
return self.soup.URI.new(uri)
else:
# If the proxy host is not set, use no proxy
return None
def shutdown(self):
"""End the soup session for this webclient."""
self.session.abort()
|