Yield (start, end) ranges parsed from a HTTP Range header. Skip unsatisfiable ranges. The end index is non-inclusive.
(header, maxlen=0)
| 2566 | return None |
| 2567 | |
| 2568 | def 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 | |
| 2586 | def _parse_qsl(qs): |
| 2587 | r = [] |