in lib/src/static_handler.dart [214:258]
Response? _fileRangeResponse(
Request request, File file, Map<String, Object> headers) {
final range = request.headers[HttpHeaders.rangeHeader];
if (range == null) return null;
final matches = RegExp(r'^bytes=(\d*)\-(\d*)$').firstMatch(range);
// Ignore ranges other than bytes
if (matches == null) return null;
final actualLength = file.lengthSync();
final startMatch = matches[1]!;
final endMatch = matches[2]!;
if (startMatch.isEmpty && endMatch.isEmpty) return null;
int start; // First byte position - inclusive.
int end; // Last byte position - inclusive.
if (startMatch.isEmpty) {
start = actualLength - int.parse(endMatch);
if (start < 0) start = 0;
end = actualLength - 1;
} else {
start = int.parse(startMatch);
end = endMatch.isEmpty ? actualLength - 1 : int.parse(endMatch);
}
// If the range is syntactically invalid the Range header
// MUST be ignored (RFC 2616 section 14.35.1).
if (start > end) return null;
if (end >= actualLength) {
end = actualLength - 1;
}
if (start >= actualLength) {
return Response(
HttpStatus.requestedRangeNotSatisfiable,
headers: headers,
);
}
return Response(
HttpStatus.partialContent,
body: request.method == 'HEAD' ? null : file.openRead(start, end + 1),
headers: headers
..[HttpHeaders.contentLengthHeader] = (end - start + 1).toString()
..[HttpHeaders.contentRangeHeader] = 'bytes $start-$end/$actualLength',
);
}