Skip to content
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

Fix Proxy response caching #247

Merged
merged 2 commits into from
Aug 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function __construct(ProxyRegister $register)
/**
* @Route("/packages.json", host="repo.{domain}", name="packages", methods={"GET"}, defaults={"domain"="%domain%"}, requirements={"domain"="%domain%"})
*/
public function packages(): JsonResponse
public function packages(Request $request): JsonResponse
{
return (new JsonResponse([
$response = (new JsonResponse([
'notify-batch' => $this->generateUrl('package_downloads', [], RouterInterface::ABSOLUTE_URL),
'providers-url' => '/p/%package%$%hash%.json',
'metadata-url' => '/p2/%package%.json',
Expand All @@ -50,6 +50,10 @@ public function packages(): JsonResponse
->setPublic()
->setTtl(86400)
;

$response->isNotModified($request);

return $response;
}

/**
Expand All @@ -60,7 +64,7 @@ public function packages(): JsonResponse
* requirements={"package"="%package_name_pattern%","domain"="%domain%"},
* methods={"GET"})
*/
public function legacyMetadata(string $package): Response
public function legacyMetadata(string $package, Request $request): Response
{
/** @var Metadata $metadata */
$metadata = $this->register->all()
Expand All @@ -69,7 +73,7 @@ public function legacyMetadata(string $package): Response
->map(fn (Option $option) => $option->get())
->getOrElse(Metadata::fromString('{"packages": {}}'));

return (new StreamedResponse(ResponseCallback::fromStream($metadata->stream()), 200, [
$response = (new StreamedResponse(ResponseCallback::fromStream($metadata->stream()), 200, [
'Accept-Ranges' => 'bytes',
'Content-Type' => 'application/json',
/* @phpstan-ignore-next-line */
Expand All @@ -78,6 +82,10 @@ public function legacyMetadata(string $package): Response
->setPublic()
->setLastModified((new \DateTime())->setTimestamp($metadata->timestamp()))
;

$response->isNotModified($request);

return $response;
}

/**
Expand All @@ -88,7 +96,7 @@ public function legacyMetadata(string $package): Response
* requirements={"package"="%package_name_pattern%","domain"="%domain%"},
* methods={"GET"})
*/
public function metadata(string $package): Response
public function metadata(string $package, Request $request): Response
{
/** @var Metadata $metadata */
$metadata = $this->register->all()
Expand All @@ -97,7 +105,7 @@ public function metadata(string $package): Response
->map(fn (Option $option) => $option->get())
->getOrElseThrow(new NotFoundHttpException());

return (new StreamedResponse(ResponseCallback::fromStream($metadata->stream()), 200, [
$response = (new StreamedResponse(ResponseCallback::fromStream($metadata->stream()), 200, [
'Accept-Ranges' => 'bytes',
'Content-Type' => 'application/json',
/* @phpstan-ignore-next-line */
Expand All @@ -106,6 +114,10 @@ public function metadata(string $package): Response
->setPublic()
->setLastModified((new \DateTime())->setTimestamp($metadata->timestamp()))
;

$response->isNotModified($request);

return $response;
}

/**
Expand All @@ -116,7 +128,7 @@ public function metadata(string $package): Response
* requirements={"package"="%package_name_pattern%","ref"="[a-f0-9]*?","type"="zip|tar","domain"="%domain%"},
* methods={"GET"})
*/
public function distribution(string $package, string $version, string $ref, string $type): Response
public function distribution(string $package, string $version, string $ref, string $type, Request $request): Response
{
/** @var resource $stream */
$stream = $this->register->all()
Expand All @@ -125,7 +137,7 @@ public function distribution(string $package, string $version, string $ref, stri
->map(fn (Option $option) => $option->get())
->getOrElseThrow(new NotFoundHttpException('This distribution file can not be found or downloaded from origin url.'));

return (new StreamedResponse(ResponseCallback::fromStream($stream), 200, [
$response = (new StreamedResponse(ResponseCallback::fromStream($stream), 200, [
'Accept-Ranges' => 'bytes',
'Content-Type' => 'application/zip',
/* @phpstan-ignore-next-line */
Expand All @@ -134,6 +146,10 @@ public function distribution(string $package, string $version, string $ref, stri
->setPublic()
->setEtag($ref)
;

$response->isNotModified($request);

return $response;
}

/**
Expand Down