~alecu/ubuntuone-storage-protocol/timestamp-server

« back to all changes in this revision

Viewing changes to ubuntuone/storageprotocol/proxy_tunnel.py

  • Committer: Tarmac
  • Author(s): Rodney Dawes
  • Date: 2010-11-16 15:10:48 UTC
  • mfrom: (121.1.4 use-packages)
  • Revision ID: tarmac-20101116151048-b0e20j7lorb4yhe1
Switch to using packaged mocker and ubuntuone-dev-tools
Use pyflakes with u1lint and also run pep8
Fix a lot of pylint/pyflakes/pep8 errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#
5
5
# Copyright 2009 Canonical Ltd.
6
6
#
7
 
# This program is free software: you can redistribute it and/or modify it 
 
7
# This program is free software: you can redistribute it and/or modify it
8
8
# under the terms of the GNU Affero General Public License version 3,
9
9
# as published by the Free Software Foundation.
10
10
#
11
 
# This program is distributed in the hope that it will be useful, but 
12
 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
13
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
14
# PURPOSE.  See the GNU Affero General Public License for more details.
15
15
#
16
16
# You should have received a copy of the GNU Affero General Public License
21
21
from twisted.internet.protocol import Protocol, ClientFactory, connectionDone
22
22
from twisted.internet import reactor, ssl
23
23
 
 
24
 
24
25
class ProxyTunnelClient(Protocol):
25
26
    """ Protocol to tunnel connections through a https proxy using
26
27
    the connect method.
28
29
    We send the command, do the auth and then proxy our calls to the
29
30
    original protocol.
30
31
    """
31
 
    MAX_LINE_LEN = 2**14
 
32
    MAX_LINE_LEN = 2 ** 14
32
33
 
33
34
    def __init__(self, *args, **kwargs):
34
35
        """Initialize the class."""
56
57
        self.client_protocol = None
57
58
        self.__buffer = ""
58
59
        # send request
59
 
        line = "CONNECT %s:%s HTTP/1.0\r\n"%  \
 
60
        line = "CONNECT %s:%s HTTP/1.0\r\n" % \
60
61
                (self.factory.host, self.factory.port)
61
62
        self.transport.write(line)
62
63
        # send headers
64
65
        # do auth
65
66
        user = self.factory.user
66
67
        if user is not None:
67
 
            auth_str = base64.b64encode("%s:%s"%(user, self.factory.passwd))
68
 
            self.sendHeader("Proxy-Authorization", "Basic %s"%auth_str)
 
68
            auth_str = base64.b64encode("%s:%s" % (user, self.factory.passwd))
 
69
            self.sendHeader("Proxy-Authorization", "Basic %s" % auth_str)
69
70
        self.transport.write("\r\n")
70
71
 
71
72
    def error(self, reason):
78
79
        if self.negotiation_done:
79
80
            self.client_protocol.dataReceived(data)
80
81
        else:
81
 
            self.__buffer = self.__buffer+data
 
82
            self.__buffer = self.__buffer + data
82
83
 
83
84
            while not self.negotiation_done:
84
85
                try:
98
99
        """Received a line."""
99
100
        if line:
100
101
            parts = line.split(" ", 2)
101
 
            # pylint: disable-msg=W0612
102
102
            # pylint: disable=W0612
103
103
            version, status = parts[:2]
104
104
            if len(parts) == 3:
122
122
        self.client_protocol = protocol
123
123
        protocol.makeConnection(self.transport)
124
124
 
 
125
 
125
126
class ProxyTunnelFactory(ClientFactory):
126
127
    """Factory class for the proxy tunnel."""
127
128
    protocol = ProxyTunnelClient
137
138
    def clientConnectionFailed(self, connector, reason):
138
139
        """Proxy client connection failed."""
139
140
        self.factory.clientConnectionFailed(connector,
140
 
            "Proxy connection error: %s"%(str(reason)))
 
141
            "Proxy connection error: %s" % (str(reason)))
141
142
 
142
143
 
143
144
def connectHTTPS(proxy_host, proxy_port, host, port, factory,