Skip to content

Commit aa9ec6b

Browse files
lyc8503picnixz
andauthored
Apply suggestions from code review
Co-authored-by: Bénédikt Tran <[email protected]>
1 parent c2cc2d4 commit aa9ec6b

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

Doc/library/http.server.rst

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,17 @@ provides three different variants:
213213
readable description of the error. The *explain* argument can be used to
214214
provide more detailed information about the error; it will be formatted
215215
using the :attr:`error_message_format` attribute and emitted, after
216-
a complete set of headers, as the response body. The *extra_headers*
217-
argument can be a key-value tuple list which specifies extra headers to
218-
be sent in the response. The :attr:`responses` attribute holds the
219-
default values for *message* and *explain* that will be used if no value
220-
is provided; for unknown codes the default value for both is the string
221-
``???``. The body will be empty if the method is HEAD or the response
222-
code is one of the following: :samp:`1{xx}`, ``204 No Content``,
223-
``205 Reset Content``, ``304 Not Modified``.
216+
a complete set of headers, as the response body.
217+
218+
The *extra_headers* argument can be a key-value tuple list which
219+
specifies additional headers to be sent in the response (for
220+
instance, ``[("Content-Range", "bytes 3-14/42")]``).
221+
222+
The :attr:`responses` attribute holds the default values for *message*
223+
and *explain* that will be used if no value is provided; for unknown codes
224+
the default value for both is the string ``???``. The body will be empty if
225+
the method is HEAD or the response code is one of the following: :samp:`1{xx}`,
226+
``204 No Content``, ``205 Reset Content``, or ``304 Not Modified``.
224227

225228
.. versionchanged:: 3.4
226229
The error response includes a Content-Length header.

Lib/http/server.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
"""
133133

134134
DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
135-
RANGE_REGEX_PATTERN = re.compile(r'bytes=(\d*)-(\d*)$', re.IGNORECASE | re.ASCII)
135+
RANGE_REGEX_PATTERN = re.compile(r'bytes=(\d*)-(\d*)$', re.ASCII | re.IGNORECASE)
136136

137137
class HTTPServer(socketserver.TCPServer):
138138

@@ -495,8 +495,8 @@ def send_error(self, code, message=None, explain=None, extra_headers=None):
495495
self.send_header("Content-Type", self.error_content_type)
496496
self.send_header('Content-Length', str(len(body)))
497497
if extra_headers is not None:
498-
for (keyword, value) in extra_headers:
499-
self.send_header(keyword, value)
498+
for name, value in extra_headers:
499+
self.send_header(name, value)
500500
self.end_headers()
501501

502502
if self.command != 'HEAD' and body:
@@ -779,9 +779,9 @@ def send_head(self):
779779
if self._range:
780780
start, end = self._range
781781
if start is None:
782-
# `end` here means suffix length
783782
# parse_range() collapses (None, None) to None
784-
# and thus `end` can not be None here
783+
assert end is not None
784+
# `end` here means suffix length
785785
start = max(0, fs.st_size - end)
786786
end = fs.st_size - 1
787787
if start >= fs.st_size:
@@ -958,8 +958,10 @@ def guess_type(self, path):
958958

959959
def parse_range(self):
960960
"""Return a tuple of (start, end) representing the range header in
961-
the HTTP request. If the range header is missing or not resolvable,
962-
None is returned. This only supports single part ranges.
961+
the HTTP request. If the range header is missing, not resolvable,
962+
or trivial (namely "byte=-"), this returns None.
963+
964+
This currently only supports single part ranges.
963965
964966
"""
965967
range_header = self.headers.get('range')

0 commit comments

Comments
 (0)