Skip to content

Commit

Permalink
make_range method for all responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ondratu committed Oct 31, 2023
1 parent 993adf1 commit 676118d
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions poorwsgi/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,40 @@ def make_partial(self, ranges: Optional[RangeList] = None, units="bytes"):
elif (start, end) not in self._ranges:
self._ranges.append((start, end))

def make_range(self, ranges: RangeList, units="bytes", full="*"):
"""Just set Content-Range header values and units attribute.
Content-Range is set in __start_response__ method for HTTP_OK status.
This method is need, when you want to make partial response by
yourself. This method needs response with HTTP_PARTIAL_CONTENT status.
"""
if self.__status_code != HTTP_PARTIAL_CONTENT:
stack_record = stack()[1]
# pylint: disable=logging-format-interpolation
log.warning("%s status code can't be partial.\n"
" File {1}, line {2}, in {3} \n"
"{0}".format((stack_record[4] or [''])[0],
*stack_record[1:4]),
self.__status_code)
return

self._units = units
self._ranges.clear()
for start, end in ranges:
if start is None or end is None:
log.warning("PartialResponse needs full range")
elif end < start:
log.warning("Inconsistent range %d - %d", start, end)
elif (start, end) not in self._ranges:
self._ranges.append((start, end))
if len(ranges) != 1:
log.warning("Only one range will be used!")
if len(ranges) >= 1:
del self.headers['Content-Range']
start, end = self.ranges[0]
content_range = ContentRange(start, end, full, units)
self.headers.add("Content-Range", str(content_range))

@property
def ranges(self):
"""Tuple of ranges set in make_partial method."""
Expand Down Expand Up @@ -365,25 +399,6 @@ def make_partial(self, ranges: Optional[RangeList] = None, units="bytes"):
"""
log.warning("PartialResponse is partial yet. Use make_range method.")

def make_range(self, ranges: RangeList, units="bytes", full="*"):
"""Just set Content-Range header values and units attribute."""
self._units = units
self._ranges.clear()
for start, end in ranges:
if start is None or end is None:
log.warning("PartialResponse needs full range")
elif end < start:
log.warning("Inconsistent range %d - %d", start, end)
elif (start, end) not in self._ranges:
self._ranges.append((start, end))
if len(ranges) != 1:
log.warning("Only one range will be used!")
if len(ranges) >= 1:
del self.headers['Content-Range']
start, end = self.ranges[0]
content_range = ContentRange(start, end, full, units)
self.headers.add("Content-Range", str(content_range))


class JSONResponse(Response):
"""Simple application/json response.
Expand Down Expand Up @@ -517,6 +532,7 @@ class FileResponse(FileObjResponse):
This object adds Last-Modified header, if is not set.
"""

def __init__(self, path: str, content_type: Optional[str] = None,
headers: Optional[Union[Headers, HeadersList]] = None,
status_code: int = HTTP_OK):
Expand Down

0 comments on commit 676118d

Please sign in to comment.