MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / parse_range_header

Function parse_range_header

21-async/mojifinder/bottle.py:2568–2584  ·  view source on GitHub ↗

Yield (start, end) ranges parsed from a HTTP Range header. Skip unsatisfiable ranges. The end index is non-inclusive.

(header, maxlen=0)

Source from the content-addressed store, hash-verified

2566 return None
2567
2568def parse_range_header(header, maxlen=0):
2569 ''' Yield (start, end) ranges parsed from a HTTP Range header. Skip
2570 unsatisfiable ranges. The end index is non-inclusive.'''
2571 if not header or header[:6] != 'bytes=': return
2572 ranges = [r.split('-', 1) for r in header[6:].split(',') if '-' in r]
2573 for start, end in ranges:
2574 try:
2575 if not start: # bytes=-100 -> last 100 bytes
2576 start, end = max(0, maxlen-int(end)), maxlen
2577 elif not end: # bytes=100- -> all but the first 99 bytes
2578 start, end = int(start), maxlen
2579 else: # bytes=100-200 -> bytes 100-200 (inclusive)
2580 start, end = int(start), min(int(end)+1, maxlen)
2581 if 0 <= start < end <= maxlen:
2582 yield start, end
2583 except ValueError:
2584 pass
2585
2586def _parse_qsl(qs):
2587 r = []

Callers 1

static_fileFunction · 0.85

Calls 1

maxFunction · 0.85

Tested by

no test coverage detected