From 30522f632c4a5a5bdb8714a33d3aff473e31737c Mon Sep 17 00:00:00 2001 From: yangxueqi Date: Mon, 16 Jul 2018 17:22:24 +0800 Subject: [PATCH 1/2] fix StaticHandler process http ranges header bug --- vibora/static.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/vibora/static.py b/vibora/static.py index 439f2ea..4933968 100644 --- a/vibora/static.py +++ b/vibora/static.py @@ -113,16 +113,27 @@ def parse_response(self, request: Request, cache: CacheEntry): if range_header: pieces = self.get_range_pieces(range_header) if len(pieces) == 2: - start, end = int(pieces[0]), int(pieces[1]) + if pieces[0] == "": + start = cache.content_length - int(pieces[1]) + end = cache.content_length - 1 + elif pieces[1] == "": + start = int(pieces[0]) + end = cache.content_length - 1 + else: + start, end = int(pieces[0]), int(pieces[1]) + + if start < 0 or start > end or end >= cache.content_length: + return Response(b"", headers={}, status_code=416) + headers = { "Content-Range": "bytes {0}-{1}/{2}".format(start, end, cache.content_length), - "Content-Length": str((end - start)), + "Content-Length": str((end - start + 1)), "Accept-Ranges": "bytes", } if request.method == "HEAD": return Response(b"", headers=headers, status_code=206) else: - return StreamingResponse(RangeFile(cache.path, start, end).stream, headers=headers, status_code=206) + return StreamingResponse(RangeFile(cache.path, start, end + 1).stream, headers=headers, status_code=206) # Handling HEAD requests if request.method == "HEAD": From 5a24481a7d8302f084640f8ddd3d217c273a5314 Mon Sep 17 00:00:00 2001 From: yangxueqi Date: Mon, 16 Jul 2018 17:49:18 +0800 Subject: [PATCH 2/2] formate code fix 'E501 line too long (124 > 120 characters)' --- vibora/static.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vibora/static.py b/vibora/static.py index 4933968..d2314f7 100644 --- a/vibora/static.py +++ b/vibora/static.py @@ -133,7 +133,8 @@ def parse_response(self, request: Request, cache: CacheEntry): if request.method == "HEAD": return Response(b"", headers=headers, status_code=206) else: - return StreamingResponse(RangeFile(cache.path, start, end + 1).stream, headers=headers, status_code=206) + return StreamingResponse(RangeFile(cache.path, start, end + 1).stream, headers=headers, + status_code=206) # Handling HEAD requests if request.method == "HEAD":