1
# Copyright (C) 2005 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
17
from cStringIO import StringIO
 
 
19
from SimpleHTTPServer import SimpleHTTPRequestHandler
 
 
22
from bzrlib.tests import TestCaseWithTransport
 
 
23
from bzrlib.tests.HttpServer import (
 
 
25
    TestingHTTPRequestHandler,
 
 
27
from bzrlib.transport import (
 
 
33
class WallRequestHandler(TestingHTTPRequestHandler):
 
 
34
    """Whatever request comes in, close the connection"""
 
 
36
    def handle_one_request(self):
 
 
37
        """Handle a single HTTP request, by abruptly closing the connection"""
 
 
38
        self.close_connection = 1
 
 
41
class BadStatusRequestHandler(TestingHTTPRequestHandler):
 
 
42
    """Whatever request comes in, returns a bad status"""
 
 
44
    def parse_request(self):
 
 
45
        """Fakes handling a single HTTP request, returns a bad status"""
 
 
46
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
 
48
            self.send_response(0, "Bad status")
 
 
50
        except socket.error, e:
 
 
51
            if (len(e.args) > 0) and (e.args[0] == errno.EPIPE):
 
 
52
                # We don't want to pollute the test reuslts with
 
 
53
                # spurious server errors while test succeed. In
 
 
54
                # our case, it may occur that the test have
 
 
55
                # already read the 'Bad Status' and closed the
 
 
56
                # socket while we are still trying to send some
 
 
57
                # headers... So the test is ok but if we raise
 
 
58
                # the exception the output is dirty. So we don't
 
 
59
                # raise, but we close the connection, just to be
 
 
61
                self.close_connection = 1
 
 
68
class InvalidStatusRequestHandler(TestingHTTPRequestHandler):
 
 
69
    """Whatever request comes in, returns am invalid status"""
 
 
71
    def parse_request(self):
 
 
72
        """Fakes handling a single HTTP request, returns a bad status"""
 
 
73
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
 
74
        self.wfile.write("Invalid status line\r\n")
 
 
78
class BadProtocolRequestHandler(TestingHTTPRequestHandler):
 
 
79
    """Whatever request comes in, returns a bad protocol version"""
 
 
81
    def parse_request(self):
 
 
82
        """Fakes handling a single HTTP request, returns a bad status"""
 
 
83
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
 
84
        # Returns an invalid protocol version, but curl just
 
 
85
        # ignores it and those cannot be tested.
 
 
86
        self.wfile.write("%s %d %s\r\n" % ('HTTP/0.0',
 
 
88
                                           'Look at my protocol version'))
 
 
92
class ForbiddenRequestHandler(TestingHTTPRequestHandler):
 
 
93
    """Whatever request comes in, returns a 403 code"""
 
 
95
    def parse_request(self):
 
 
96
        """Handle a single HTTP request, by replying we cannot handle it"""
 
 
97
        ignored = TestingHTTPRequestHandler.parse_request(self)
 
 
102
class HTTPServerWithSmarts(HttpServer):
 
 
103
    """HTTPServerWithSmarts extends the HttpServer with POST methods that will
 
 
104
    trigger a smart server to execute with a transport rooted at the rootdir of
 
 
109
        HttpServer.__init__(self, SmartRequestHandler)
 
 
112
class SmartRequestHandler(TestingHTTPRequestHandler):
 
 
113
    """Extend TestingHTTPRequestHandler to support smart client POSTs."""
 
 
116
        """Hand the request off to a smart server instance."""
 
 
117
        self.send_response(200)
 
 
118
        self.send_header("Content-type", "application/octet-stream")
 
 
119
        transport = get_transport(self.server.test_case._home_dir)
 
 
120
        # TODO: We might like to support streaming responses.  1.0 allows no
 
 
121
        # Content-length in this case, so for integrity we should perform our
 
 
122
        # own chunking within the stream.
 
 
123
        # 1.1 allows chunked responses, and in this case we could chunk using
 
 
124
        # the HTTP chunking as this will allow HTTP persistence safely, even if
 
 
125
        # we have to stop early due to error, but we would also have to use the
 
 
126
        # HTTP trailer facility which may not be widely available.
 
 
127
        out_buffer = StringIO()
 
 
128
        smart_protocol_request = smart.SmartServerRequestProtocolOne(
 
 
129
                transport, out_buffer.write)
 
 
130
        # if this fails, we should return 400 bad request, but failure is
 
 
131
        # failure for now - RBC 20060919
 
 
132
        data_length = int(self.headers['Content-Length'])
 
 
133
        # Perhaps there should be a SmartServerHTTPMedium that takes care of
 
 
134
        # feeding the bytes in the http request to the smart_protocol_request,
 
 
135
        # but for now it's simpler to just feed the bytes directly.
 
 
136
        smart_protocol_request.accept_bytes(self.rfile.read(data_length))
 
 
137
        assert smart_protocol_request.next_read_size() == 0, (
 
 
138
            "not finished reading, but all data sent to protocol.")
 
 
139
        self.send_header("Content-Length", str(len(out_buffer.getvalue())))
 
 
141
        self.wfile.write(out_buffer.getvalue())
 
 
144
class SingleRangeRequestHandler(TestingHTTPRequestHandler):
 
 
145
    """Always reply to range request as if they were single.
 
 
147
    Don't be explicit about it, just to annoy the clients.
 
 
150
    def get_multiple_ranges(self, file, file_size, ranges):
 
 
151
        """Answer as if it was a single range request and ignores the rest"""
 
 
152
        (start, end) = ranges[0]
 
 
153
        return self.get_single_range(file, file_size, start, end)
 
 
156
class NoRangeRequestHandler(TestingHTTPRequestHandler):
 
 
157
    """Ignore range requests without notice"""
 
 
159
    # Just bypass the range handling done by TestingHTTPRequestHandler
 
 
160
    do_GET = SimpleHTTPRequestHandler.do_GET
 
 
163
class TestCaseWithWebserver(TestCaseWithTransport):
 
 
164
    """A support class that provides readonly urls that are http://.
 
 
166
    This is done by forcing the readonly server to be an http
 
 
167
    one. This will currently fail if the primary transport is not
 
 
168
    backed by regular disk files.
 
 
171
        super(TestCaseWithWebserver, self).setUp()
 
 
172
        self.transport_readonly_server = HttpServer