Hello, developers, there is a strange problem that I am facing right now which is when I try to get the range from the header I am getting the total size. I don’t know how it is possible and why this is happening here Is a small portion of my code please help me if anyone knows anything about this :
range_header = request.headers.get("Range") start, end = range_header.split("=")[1].split("-")
Also, i am using so many conditions and if else statements just to be clear if somehow the start is empty then set it to 0 and end to totalSize - 1 and check this code below i know it is not a proper way of writing the code but I do this to solve this issue :
if range_header:
print(f"Range : {range_header}")
try:
start, end = range_header.split("=")[1].split("-")
except:
start = 0
end = fileSize - 1
try:
start = int(start)
except:
start = 0
try:
end = int(end)
except:
end = fileSize - 1
requested_chunk_size = end - start + 1
else:
start = 0
requested_chunk_size = fileSize
end = fileSize - 1
try:
int(end)
except:
end = fileSize - 1
try:
int(requested_chunk_size)
except:
requested_chunk_size = fileSize
try:
int(start)
except:
start = 0
headers = {
"Content-Type": mimeType,
"Content-Disposition": condisp,
"Content-Length" : str(fileSize),
"Cache-Control" : "no-store",
"Accept-Ranges" : "bytes",
"Content-Range" : f"bytes {start}-{end}/{requested_chunk_size}"
}
print(f">>>>> Mime Type : {mimeType} : Content-Range : {start}-{end}/{requested_chunk_size}")
response = await request.respond(content_type=mimeType, headers=headers)
if start + end > requested_chunk_size:
start = 0
end = requested_chunk_size - 1
requested_chunk_size = fileSize
headers["Content-Range"] = f"bytes {start}-{end}/{requested_chunk_size}"
if range_header:
print(f"Range : {range_header}")
try:
start, end = range_header.split("=")[1].split("-")
except:
start = 0
end = fileSize - 1
try:
start = int(start)
except:
start = 0
try:
end = int(end)
except:
end = fileSize - 1
requested_chunk_size = end - start + 1
else:
start = 0
requested_chunk_size = fileSize
end = fileSize - 1
try:
int(end)
except:
end = fileSize - 1
try:
int(requested_chunk_size)
except:
requested_chunk_size = fileSize
try:
int(start)
except:
start = 0
headers = {
"Content-Type": mimeType,
"Content-Disposition": condisp,
"Content-Length" : str(fileSize),
"Cache-Control" : "no-store",
"Accept-Ranges" : "bytes",
"Content-Range" : f"bytes {start}-{end}/{requested_chunk_size}"
}
print(f">>>>> Mime Type : {mimeType} : Content-Range : {start}-{end}/{requested_chunk_size}")
response = await request.respond(content_type=mimeType, headers=headers)
if start + end > requested_chunk_size:
start = 0
end = requested_chunk_size - 1
requested_chunk_size = fileSize
headers["Content-Range"] = f"bytes {start}-{end}/{requested_chunk_size}"
And If the total file size is smaller then added size of the start and end then i am setting it to 0 and fileSize - 1 and then i am getting this error :
[2024-03-24 12:18:05 +0000] [20321] [ERROR] The response object returned by the route handler will not be sent to client. The request has already been responded to.
[2024-03-24 12:18:05 +0000] [20321] [ERROR] Response was smaller than content-length
Traceback (most recent call last):
File "handle_request", line 135, in handle_request
Sanic("This is not legal")
File "/usr/local/lib/python3.9/dist-packages/sanic/response/types.py", line 139, in send
await self.stream.send(
File "/usr/local/lib/python3.9/dist-packages/sanic/http/http1.py", line 397, in http1_response_normal
raise ServerError("Response was smaller than content-length")
sanic.exceptions.ServerError: Response was smaller than content-length
Could anyone please help to solve this issue?