Skip to content

Commit

Permalink
fix: handle spaces within ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Mar 1, 2025
1 parent abae41c commit d773d9c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 1.2.1 - 2025-03-01

### Added

- Nothing.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Accept and properly parse spaces within ranges ([#7](https://github.com/ramsey/http-range/issues/7))

## 1.2.0 - 2025-03-01

### Added
Expand Down
5 changes: 4 additions & 1 deletion src/Unit/AbstractUnitRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
use Ramsey\Http\Range\Exception\ParseException;

use function array_filter;
use function array_map;
use function assert;
use function ctype_digit;
use function explode;
use function is_scalar;
use function trim;

/**
* `AbstractUnitRange` provides a basic implementation for unit ranges.
Expand Down Expand Up @@ -133,13 +135,14 @@ public function getTotalSize()
*/
private function parseRange(string $range, $totalSize): array
{
$points = explode('-', $range, 2);
$points = explode('-', trim($range), 2);

if (!isset($points[1])) {
// Assume the request is for a single item.
$points[1] = $points[0];
}

$points = array_map('trim', $points);
$isValidRangeValue = fn (string $value): bool => ctype_digit($value) || $value === '';

if (
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/BytesUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public static function validValuesProvider(): array
['start' => 101, 'end' => 102],
],
],
[
' 0 - 199, 210-250 , 500- ',
1000,
[
['start' => 0, 'end' => 199],
['start' => 210, 'end' => 250],
['start' => 500, 'end' => 999],
],
],
];
}
}
10 changes: 10 additions & 0 deletions tests/Unit/GenericUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public static function validValuesProvider(): array
['start' => 101, 'end' => 102],
],
],
[
'items',
' 0 - 199, 210-250 , 500- ',
1000,
[
['start' => 0, 'end' => 199],
['start' => 210, 'end' => 250],
['start' => 500, 'end' => 999],
],
],
];
}
}

0 comments on commit d773d9c

Please sign in to comment.