~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/HttpServer.py

fix NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
60
60
    def handle_one_request(self):
61
61
        """Handle a single HTTP request.
62
62
 
63
 
        We catch all socket errors occurring when the client close the
64
 
        connection early to avoid polluting the test results.
 
63
        You normally don't need to override this method; see the class
 
64
        __doc__ string for information on how to handle specific HTTP
 
65
        commands such as GET and POST.
 
66
 
65
67
        """
66
 
        try:
67
 
            SimpleHTTPRequestHandler.handle_one_request(self)
68
 
        except socket.error, e:
69
 
            if (len(e.args) > 0
70
 
                and e.args[0] in (errno.EPIPE, errno.ECONNRESET,
71
 
                                  errno.ECONNABORTED,)):
72
 
                self.close_connection = 1
73
 
                pass
74
 
            else:
 
68
        for i in xrange(1,11): # Don't try more than 10 times
 
69
            try:
 
70
                self.raw_requestline = self.rfile.readline()
 
71
            except socket.error, e:
 
72
                if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK):
 
73
                    # omitted for now because some tests look at the log of
 
74
                    # the server and expect to see no errors.  see recent
 
75
                    # email thread. -- mbp 20051021. 
 
76
                    ## self.log_message('EAGAIN (%d) while reading from raw_requestline' % i)
 
77
                    time.sleep(0.01)
 
78
                    continue
75
79
                raise
 
80
            else:
 
81
                break
 
82
        if not self.raw_requestline:
 
83
            self.close_connection = 1
 
84
            return
 
85
        if not self.parse_request(): # An error code has been sent, just exit
 
86
            return
 
87
        mname = 'do_' + self.command
 
88
        if getattr(self, mname, None) is None:
 
89
            self.send_error(501, "Unsupported method (%r)" % self.command)
 
90
            return
 
91
        method = getattr(self, mname)
 
92
        method()
76
93
 
77
94
    _range_regexp = re.compile(r'^(?P<start>\d+)-(?P<end>\d+)$')
78
95
    _tail_regexp = re.compile(r'^-(?P<tail>\d+)$')
142
159
                                                                  file_size))
143
160
            self.end_headers()
144
161
            self.send_range_content(file, start, end - start + 1)
145
 
        # Final boundary
146
 
        self.wfile.write("--%s\r\n" % boundary)
 
162
            self.wfile.write("--%s\r\n" % boundary)
147
163
 
148
164
    def do_GET(self):
149
165
        """Serve a GET request.
150
166
 
151
167
        Handles the Range header.
152
168
        """
153
 
        # Update statistics
154
 
        self.server.test_case_server.GET_request_nb += 1
155
169
 
156
170
        path = self.translate_path(self.path)
157
171
        ranges_header_value = self.headers.get('Range')
277
291
        # the tests cases.
278
292
        self.test_case_server = test_case_server
279
293
 
280
 
    def server_close(self):
281
 
        """Called to clean-up the server.
282
 
 
283
 
        Since the server may be in a blocking read, we shutdown the socket
284
 
        before closing it.
285
 
        """
286
 
        self.socket.shutdown(socket.SHUT_RDWR)
287
 
        BaseHTTPServer.HTTPServer.server_close(self)
288
 
 
289
294
 
290
295
class HttpServer(Server):
291
296
    """A test server for http transports.
307
312
        self.host = 'localhost'
308
313
        self.port = 0
309
314
        self._httpd = None
310
 
        # Allows tests to verify number of GET requests issued
311
 
        self.GET_request_nb = 0
312
315
 
313
316
    def _get_httpd(self):
314
317
        if self._httpd is None:
324
327
                                               self.host,
325
328
                                               self.port)
326
329
        self._http_starting.release()
 
330
        httpd.socket.settimeout(0.1)
327
331
 
328
332
        while self._http_running:
329
333
            try:
376
380
 
377
381
    def tearDown(self):
378
382
        """See bzrlib.transport.Server.tearDown."""
379
 
        self._httpd.server_close()
380
383
        self._http_running = False
381
384
        self._http_thread.join()
382
385