/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/http_server.py

  • Committer: Jelmer Vernooij
  • Date: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import errno
18
 
try:
19
 
    import http.client as http_client
20
 
    import http.server as http_server
21
 
except ImportError:
22
 
    import httplib as http_client
23
 
    import SimpleHTTPServer as http_server
 
18
import http.client as http_client
 
19
import http.server as http_server
24
20
import os
25
21
import posixpath
26
22
import random
27
23
import re
28
24
import socket
29
 
try:
30
 
    from urlparse import urlparse
31
 
except ImportError:
32
 
    from urllib.parse import urlparse
 
25
import sys
 
26
from urllib.parse import urlparse
33
27
 
34
28
from .. import (
35
29
    osutils,
101
95
    def send_error(self, code, message=None):
102
96
        """Send and log an error reply.
103
97
 
104
 
        We redefine the python-provided version to be able to set a 
 
98
        We redefine the python-provided version to be able to set a
105
99
        ``Content-Length`` header as some http/1.1 clients complain otherwise
106
100
        (see bug #568421).
107
101
 
125
119
        self.send_header('Connection', 'close')
126
120
        self.end_headers()
127
121
        if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
128
 
            self.wfile.write(content)
 
122
            self.wfile.write(content.encode('utf-8'))
129
123
 
130
124
    def _handle_one_request(self):
131
125
        http_server.SimpleHTTPRequestHandler.handle_one_request(self)
232
226
        boundary = '%d' % random.randint(0, 0x7FFFFFFF)
233
227
        self.send_header('Content-Type',
234
228
                         'multipart/byteranges; boundary=%s' % boundary)
235
 
        boundary_line = '--%s\r\n' % boundary
 
229
        boundary_line = b'--%s\r\n' % boundary.encode('ascii')
236
230
        # Calculate the Content-Length
237
231
        content_length = 0
238
232
        for (start, end) in ranges:
241
235
                'Content-type', 'application/octet-stream')
242
236
            content_length += self._header_line_length(
243
237
                'Content-Range', 'bytes %d-%d/%d' % (start, end, file_size))
244
 
            content_length += len('\r\n') # end headers
 
238
            content_length += len('\r\n')  # end headers
245
239
            content_length += end - start + 1
246
240
        content_length += len(boundary_line)
247
241
        self.send_header('Content-length', content_length)
339
333
        # abandon query parameters
340
334
        path = urlparse(path)[2]
341
335
        path = posixpath.normpath(urlutils.unquote(path))
342
 
        path = path.decode('utf-8')
343
336
        words = path.split('/')
344
337
        path = self._cwd
345
338
        for num, word in enumerate(w for w in words if w):
346
339
            if num == 0:
347
340
                drive, word = os.path.splitdrive(word)
348
341
            head, word = os.path.split(word)
349
 
            if word in (os.curdir, os.pardir): continue
 
342
            if word in (os.curdir, os.pardir):
 
343
                continue
350
344
            path = os.path.join(path, word)
351
345
        return path
352
346
 
379
373
    server, we need an independent connection for each of them. We achieve that
380
374
    by spawning a new thread for each connection.
381
375
    """
 
376
 
382
377
    def __init__(self, server_address, request_handler_class,
383
378
                 test_case_server):
384
379
        test_server.TestingThreadingTCPServer.__init__(self, server_address,
446
441
        path_parts = path.split(os.path.sep)
447
442
        if os.path.isabs(path):
448
443
            if path_parts[:len(self._local_path_parts)] != \
449
 
                   self._local_path_parts:
 
444
                    self._local_path_parts:
450
445
                raise BadWebserverPath(path, self.test_dir)
451
446
            remote_path = '/'.join(path_parts[len(self._local_path_parts):])
452
447
        else:
490
485
        # this is chosen to try to prevent trouble with proxies, weird dns,
491
486
        # etc
492
487
        return self._url_protocol + '://127.0.0.1:1/'
493
 
 
494
 
 
495
 
class HttpServer_urllib(HttpServer):
496
 
    """Subclass of HttpServer that gives http+urllib urls.
497
 
 
498
 
    This is for use in testing: connections to this server will always go
499
 
    through urllib where possible.
500
 
    """
501
 
 
502
 
    # urls returned by this server should require the urllib client impl
503
 
    _url_protocol = 'http+urllib'