~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/HTTPTestUtil.py

  • Committer: v.ladeuil+lp at free
  • Date: 2006-10-13 10:50:33 UTC
  • mfrom: (2077 +trunk)
  • mto: (2145.1.1 keepalive)
  • mto: This revision was merged to the branch mainline in revision 2146.
  • Revision ID: v.ladeuil+lp@free.fr-20061013105033-7e091ff8dcc0ed0c
Merge bzr.dev. Including http modifications by "smart" related code

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
from cStringIO import StringIO
17
18
import errno
18
19
import socket
19
20
 
22
23
    HttpServer,
23
24
    TestingHTTPRequestHandler,
24
25
    )
 
26
from bzrlib.transport import (
 
27
    get_transport,
 
28
    smart,
 
29
    )
25
30
 
26
31
 
27
32
class WallRequestHandler(TestingHTTPRequestHandler):
93
98
        return False
94
99
 
95
100
 
 
101
class HTTPServerWithSmarts(HttpServer):
 
102
    """HTTPServerWithSmarts extends the HttpServer with POST methods that will
 
103
    trigger a smart server to execute with a transport rooted at the rootdir of
 
104
    the HTTP server.
 
105
    """
 
106
 
 
107
    def __init__(self):
 
108
        HttpServer.__init__(self, SmartRequestHandler)
 
109
 
 
110
 
 
111
class SmartRequestHandler(TestingHTTPRequestHandler):
 
112
    """Extend TestingHTTPRequestHandler to support smart client POSTs."""
 
113
 
 
114
    def do_POST(self):
 
115
        """Hand the request off to a smart server instance."""
 
116
        self.send_response(200)
 
117
        self.send_header("Content-type", "application/octet-stream")
 
118
        transport = get_transport(self.server.test_case._home_dir)
 
119
        # TODO: We might like to support streaming responses.  1.0 allows no
 
120
        # Content-length in this case, so for integrity we should perform our
 
121
        # own chunking within the stream.
 
122
        # 1.1 allows chunked responses, and in this case we could chunk using
 
123
        # the HTTP chunking as this will allow HTTP persistence safely, even if
 
124
        # we have to stop early due to error, but we would also have to use the
 
125
        # HTTP trailer facility which may not be widely available.
 
126
        out_buffer = StringIO()
 
127
        smart_protocol_request = smart.SmartServerRequestProtocolOne(
 
128
                transport, out_buffer.write)
 
129
        # if this fails, we should return 400 bad request, but failure is
 
130
        # failure for now - RBC 20060919
 
131
        data_length = int(self.headers['Content-Length'])
 
132
        # Perhaps there should be a SmartServerHTTPMedium that takes care of
 
133
        # feeding the bytes in the http request to the smart_protocol_request,
 
134
        # but for now it's simpler to just feed the bytes directly.
 
135
        smart_protocol_request.accept_bytes(self.rfile.read(data_length))
 
136
        assert smart_protocol_request.next_read_size() == 0, (
 
137
            "not finished reading, but all data sent to protocol.")
 
138
        self.send_header("Content-Length", str(len(out_buffer.getvalue())))
 
139
        self.end_headers()
 
140
        self.wfile.write(out_buffer.getvalue())
 
141
 
 
142
 
96
143
class TestCaseWithWebserver(TestCaseWithTransport):
97
144
    """A support class that provides readonly urls that are http://.
98
145