1048
1048
self.assertEqual('single', t._range_hint)
1051
class TruncatedBeforeBoundaryRequestHandler(
1052
http_server.TestingHTTPRequestHandler):
1053
"""Truncation before a boundary, like in bug 198646"""
1055
_truncated_ranges = 1
1057
def get_multiple_ranges(self, file, file_size, ranges):
1058
self.send_response(206)
1059
self.send_header('Accept-Ranges', 'bytes')
1061
self.send_header('Content-Type',
1062
'multipart/byteranges; boundary=%s' % boundary)
1063
boundary_line = '--%s\r\n' % boundary
1064
# Calculate the Content-Length
1066
for (start, end) in ranges:
1067
content_length += len(boundary_line)
1068
content_length += self._header_line_length(
1069
'Content-type', 'application/octet-stream')
1070
content_length += self._header_line_length(
1071
'Content-Range', 'bytes %d-%d/%d' % (start, end, file_size))
1072
content_length += len('\r\n') # end headers
1073
content_length += end - start # + 1
1074
content_length += len(boundary_line)
1075
self.send_header('Content-length', content_length)
1078
# Send the multipart body
1080
for (start, end) in ranges:
1081
if cur + self._truncated_ranges >= len(ranges):
1082
# Abruptly ends the response and close the connection
1083
self.close_connection = 1
1085
self.wfile.write(boundary_line)
1086
self.send_header('Content-type', 'application/octet-stream')
1087
self.send_header('Content-Range', 'bytes %d-%d/%d'
1088
% (start, end, file_size))
1090
self.send_range_content(file, start, end - start + 1)
1093
self.wfile.write(boundary_line)
1096
class TestTruncatedBeforeBoundary(TestSpecificRequestHandler):
1097
"""Tests the case of bug 198646, disconnecting before a boundary."""
1099
_req_handler_class = TruncatedBeforeBoundaryRequestHandler
1102
super(TestTruncatedBeforeBoundary, self).setUp()
1103
self.build_tree_contents([('a', '0123456789')],)
1105
def test_readv_with_short_reads(self):
1106
server = self.get_readonly_server()
1107
t = self.get_readonly_transport()
1108
# Force separate ranges for each offset
1109
t._bytes_to_read_before_seek = 0
1110
ireadv = iter(t.readv('a', ((0, 1), (2, 1), (4, 2), (9, 1))))
1111
self.assertEqual((0, '0'), ireadv.next())
1112
self.assertEqual((2, '2'), ireadv.next())
1113
self.assertEqual((4, '45'), ireadv.next())
1114
self.assertEqual((9, '9'), ireadv.next())
1051
1117
class LimitedRangeRequestHandler(http_server.TestingHTTPRequestHandler):
1052
1118
"""Errors out when range specifiers exceed the limit"""