-
-
Notifications
You must be signed in to change notification settings - Fork 980
Feat/gh7137 content range #7856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nayte91
wants to merge
3
commits into
api-platform:main
Choose a base branch
from
Nayte91:feat/GH7137-content-range
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\State\Provider; | ||
|
|
||
| use ApiPlatform\Metadata\CollectionOperationInterface; | ||
| use ApiPlatform\Metadata\HttpOperation; | ||
| use ApiPlatform\Metadata\Operation; | ||
| use ApiPlatform\State\Pagination\Pagination; | ||
| use ApiPlatform\State\Pagination\PaginatorInterface; | ||
| use ApiPlatform\State\Pagination\PartialPaginatorInterface; | ||
| use ApiPlatform\State\ProviderInterface; | ||
| use ApiPlatform\State\Util\RequestParser; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
|
||
| /** | ||
| * Serves paginated collections as HTTP range requests (RFC 9110 §14), opt-in per operation | ||
| * through {@see HttpOperation::getRangeUnit()}. | ||
| * | ||
| * A range applies to a would-be 200 only (§14.2): it is ignored on other methods, other | ||
| * units, unparseable specifiers and whenever the request fails before reading. Choices the | ||
| * RFC leaves open: If-Range counts as a mismatch, collections carrying no validator | ||
| * (§13.1.5); the 206 is promised only once a paginator came back, every 206 having to | ||
| * carry a Content-Range (§15.3.7); a range pagination cannot serve as a page (misaligned, | ||
| * wider than the maximum) gets a 416 rather than being silently ignored, so that clients | ||
| * learn it. | ||
| * | ||
| * @see https://datatracker.ietf.org/doc/html/rfc9110#section-14 | ||
| * | ||
| * @author Julien Robic <nayte91@gmail.com> | ||
| */ | ||
| final class RangeHeaderProvider implements ProviderInterface | ||
| { | ||
| /** Int-range "<first>-<last>" only (§14.1.2): pagination can serve neither an open-ended nor a suffix range. */ | ||
| private const RANGE_PATTERN = '/^(?<unit>[A-Za-z0-9!#$%&\'*+\-.^_`|~]+)=(?<first>\d+)-(?<last>\d+)$/'; | ||
|
|
||
| public function __construct( | ||
| private readonly ProviderInterface $decorated, | ||
| private readonly Pagination $pagination, | ||
| ) { | ||
| } | ||
|
|
||
| public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
| { | ||
| $request = $context['request'] ?? null; | ||
|
|
||
| if ( | ||
| !$request instanceof Request | ||
| || !$operation instanceof HttpOperation | ||
| || !$operation instanceof CollectionOperationInterface | ||
| || null === ($unit = $operation->getRangeUnit()) | ||
| || !$request->isMethod('GET') | ||
| || !$request->headers->has('Range') | ||
| || $request->headers->has('If-Range') | ||
| || !\in_array($operation->getStatus(), [null, Response::HTTP_OK], true) | ||
| || !preg_match(self::RANGE_PATTERN, $request->headers->get('Range', ''), $range) | ||
| || strtolower($range['unit']) !== strtolower($unit) | ||
| ) { | ||
| return $this->decorated->provide($operation, $uriVariables, $context); | ||
| } | ||
|
|
||
| $first = (int) $range['first']; | ||
| $last = (int) $range['last']; | ||
|
|
||
| if ($first > $last) { | ||
| throw new HttpException(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE, \sprintf('The range first position (%d) must not exceed its last position (%d).', $first, $last)); | ||
| } | ||
|
|
||
| $length = $last - $first + 1; | ||
| $maximumItemsPerPage = $operation->getPaginationMaximumItemsPerPage() ?? $this->pagination->getOptions()['maximum_items_per_page']; | ||
|
|
||
| if (null !== $maximumItemsPerPage && $length > $maximumItemsPerPage) { | ||
| throw new HttpException(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE, \sprintf('A range must not span more than %d %s.', $maximumItemsPerPage, $unit)); | ||
| } | ||
|
|
||
| if (0 !== $first % $length) { | ||
| throw new HttpException(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE, \sprintf('The range first position must be a multiple of its length (%d).', $length)); | ||
| } | ||
|
|
||
| $options = $this->pagination->getOptions(); | ||
| $filters = $request->attributes->get('_api_filters'); | ||
| if (null === $filters) { | ||
| $queryString = RequestParser::getQueryString($request); | ||
| $filters = $queryString ? RequestParser::parseRequestParams($queryString) : []; | ||
| } | ||
|
|
||
| // Pagination::getLimit() ignores the items-per-page filter unless the client may choose it: | ||
| // set the operation too, so that the range wins over the query string. | ||
| $filters[$options['page_parameter_name']] = intdiv($first, $length) + 1; | ||
| $filters[$options['items_per_page_parameter_name']] = $length; | ||
| $request->attributes->set('_api_filters', $filters); | ||
|
|
||
| $operation = $operation->withPaginationItemsPerPage($length); | ||
| $request->attributes->set('_api_operation', $operation); | ||
|
|
||
| $data = $this->decorated->provide($operation, $uriVariables, $context); | ||
|
|
||
| if (!$data instanceof PartialPaginatorInterface) { | ||
| return $data; | ||
| } | ||
|
|
||
| if ($data instanceof PaginatorInterface) { | ||
| $totalItems = (int) $data->getTotalItems(); | ||
|
|
||
| if ($first >= $totalItems) { | ||
| throw new HttpException(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE, \sprintf('The range first position (%d) is beyond the collection (%d %s).', $first, $totalItems, $unit), null, ['Content-Range' => \sprintf('%s */%d', $unit, $totalItems)]); | ||
| } | ||
| } elseif (0 === \count($data)) { | ||
| throw new HttpException(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE, \sprintf('The range first position (%d) is beyond the collection.', $first)); | ||
| } | ||
|
|
||
| $request->attributes->set('_api_operation', $operation->withStatus(Response::HTTP_PARTIAL_CONTENT)); | ||
|
|
||
| return $data; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This enables the behavior globally. There is no framework setting or resource/operation metadata flag, so applications cannot opt in selectively and existing APIs gain new headers/Range semantics automatically. Given the provider and representation constraints, this should default to disabled and be enabled explicitly per operation (with an explicit range unit). Event-listener mode also does not wire this provider, while it still receives the response-trait changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 89356d2. The feature is opt-in per operation: without a
rangeUnittheRangeheader is ignored and noAccept-Rangesis emitted (testARangeIsIgnoredWithoutARangeUnit, on a second fixture operation left unconfigured). There is deliberately no global framework setting, since the unit has to be chosen per collection anyway.The provider is now wired in the event-listener mode as well (
symfony/events.php), at the same innermost priority, and the functional test runs green underUSE_SYMFONY_LISTENERS=1. It is also registered in the Laravel service provider, directly aroundReadProvider, which matches the Symfony position; that part is not covered by a Laravel test yet.