Skip to content

Commit

Permalink
Add option for path/params combo when validating HTTP signatures.
Browse files Browse the repository at this point in the history
Move request arguments resolution to new RequestArgumentsResolver class.
  • Loading branch information
reinink committed May 22, 2015
1 parent 05bc4ba commit 615861a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 61 deletions.
34 changes: 34 additions & 0 deletions src/Http/RequestArgumentsResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace League\Glide\Http;

use InvalidArgumentException;
use Symfony\Component\HttpFoundation\Request;

class RequestArgumentsResolver
{
/**
* Resolve request object.
* @param array $args Array of supplied arguments.
* @return Request The request object.
*/
public function getRequest($args)
{
if (isset($args[0]) and $args[0] instanceof Request) {
return $args[0];
}

if (isset($args[0]) and is_string($args[0])) {
$path = $args[0];
$params = [];

if (isset($args[1]) and is_array($args[1])) {
$params = $args[1];
}

return RequestFactory::create($path, $params);
}

throw new InvalidArgumentException('Not a valid path or Request object.');
}
}
6 changes: 4 additions & 2 deletions src/Http/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public function addSignature($path, array $params)

/**
* Validate a request signature.
* @param Request $request The request object.
* @param mixed
* @throws SignatureException
*/
public function validateRequest(Request $request)
public function validateRequest()
{
$request = (new RequestArgumentsResolver())->getRequest(func_get_args());

if (is_null($request->get('s'))) {
throw new SignatureException('Signature is missing.');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/SignatureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public function addSignature($path, array $params);

/**
* Validate a request signature.
* @param Request $request The request object.
* @param mixed
* @throws SignatureException
*/
public function validateRequest(Request $request);
public function validateRequest();
}
87 changes: 30 additions & 57 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace League\Glide;

use InvalidArgumentException;
use League\Flysystem\FileExistsException;
use League\Flysystem\FilesystemInterface;
use League\Glide\Api\ApiInterface;
use League\Glide\Filesystem\FilesystemException;
use League\Glide\Http\NotFoundException;
use League\Glide\Http\RequestFactory;
use League\Glide\Http\RequestArgumentsResolver;
use League\Glide\Http\ResponseFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
Expand Down Expand Up @@ -57,7 +56,7 @@ class Server
* @param FilesystemInterface|null $cache The cache file system.
* @param ApiInterface $api The image manipulation API.
*/
public function __construct(FilesystemInterface $source, $cache, ApiInterface $api)
public function __construct(FilesystemInterface $source, FilesystemInterface $cache = null, ApiInterface $api)
{
$this->setSource($source);
$this->setCache($cache);
Expand Down Expand Up @@ -108,7 +107,7 @@ public function getSourcePathPrefix()
*/
public function getSourcePath()
{
$request = $this->resolveRequestObject(func_get_args());
$request = (new RequestArgumentsResolver())->getRequest(func_get_args());

$path = trim($request->getPathInfo(), '/');

Expand All @@ -134,7 +133,7 @@ public function getSourcePath()
*/
public function sourceFileExists()
{
$request = $this->resolveRequestObject(func_get_args());
$request = (new RequestArgumentsResolver())->getRequest(func_get_args());

return $this->source->has($this->getSourcePath($request));
}
Expand All @@ -143,7 +142,7 @@ public function sourceFileExists()
* Set the cache file system.
* @param FilesystemInterface|null $cache The cache file system.
*/
public function setCache($cache)
public function setCache(FilesystemInterface $cache = null)
{
$this->cache = $cache;
}
Expand Down Expand Up @@ -182,7 +181,7 @@ public function getCachePathPrefix()
*/
public function getCachePath()
{
$request = $this->resolveRequestObject(func_get_args());
$request = (new RequestArgumentsResolver())->getRequest(func_get_args());

$path = md5($this->getSourcePath($request).'?'.http_build_query($request->query->all()));

Expand All @@ -204,7 +203,7 @@ public function cacheFileExists()
return false;
}

$request = $this->resolveRequestObject(func_get_args());
$request = (new RequestArgumentsResolver())->getRequest(func_get_args());

return $this->cache->has($this->getCachePath($request));
}
Expand Down Expand Up @@ -252,7 +251,7 @@ public function getBaseUrl()
*/
public function outputImage()
{
$request = $this->resolveRequestObject(func_get_args());
$request = (new RequestArgumentsResolver())->getRequest(func_get_args());

$this->makeImage($request);

Expand All @@ -274,7 +273,7 @@ public function outputImage()
*/
public function getImageResponse()
{
$request = $this->resolveRequestObject(func_get_args());
$request = (new RequestArgumentsResolver())->getRequest(func_get_args());

$this->makeImage($request);

Expand All @@ -288,7 +287,7 @@ public function getImageResponse()
*/
public function makeImage()
{
$request = $this->resolveRequestObject(func_get_args());
$request = (new RequestArgumentsResolver())->getRequest(func_get_args());

if ($this->cacheFileExists($request) === true) {
return $request;
Expand All @@ -310,62 +309,36 @@ public function makeImage()
);
}

try {
$written = $this->writeCache($request, $source);
} catch (FileExistsException $exception) {
// Cache file failed to write. Fail silently.
return $request;
}

if ($written === false) {
throw new FilesystemException(
'Could not write the image `'.$this->getCachePath($request).'`.'
);
if ($this->cache) {
$this->writeCache($request, $source);
}

return $request;
}

/**
* Resolve request object.
* @param array $args Array of supplied arguments.
* @return Request The request object.
*/
protected function resolveRequestObject($args)
{
if (isset($args[0]) and $args[0] instanceof Request) {
return $args[0];
}

if (isset($args[0]) and is_string($args[0])) {
$path = $args[0];
$params = [];

if (isset($args[1]) and is_array($args[1])) {
$params = $args[1];
}

return RequestFactory::create($path, $params);
}

throw new InvalidArgumentException('Not a valid path or Request object.');
}

/**
* Write the cache file if caching is enabled.
* @param Request $request
* @param string $source
* @param Request $request
* @param string $source
* @return mixed
*/
private function writeCache(Request $request, $source)
protected function writeCache(Request $request, $source)
{
if ($this->cache === null) {
return null;
}
try {
$written = $this->cache->write(
$this->getCachePath($request),
$this->api->run($request, $source)
);

return $this->cache->write(
$this->getCachePath($request),
$this->api->run($request, $source)
);
if ($written === false) {
throw new FilesystemException(
'Could not write the image `'.$this->getCachePath($request).'`.'
);
}
} catch (FileExistsException $exception) {
// This edge case occurs when the target already exists
// because it's currently be written to disk in another
// request. It's best to just fail silently.
}
}
}
53 changes: 53 additions & 0 deletions tests/Http/RequestArgumentsResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace League\Glide\Http;

class RequestArgumentsResolverTest extends \PHPUnit_Framework_TestCase
{
private $resolver;

public function setUp()
{
$this->resolver = new RequestArgumentsResolver();
}

public function testCreateRequestArgumentsResolver()
{
$this->assertInstanceOf(
'League\Glide\Http\RequestArgumentsResolver',
$this->resolver
);
}

public function testRequestObjectArg()
{
$request = $this->resolver->getRequest([
RequestFactory::create('image.jpg', ['w' => 100]),
]);

$this->assertEquals('image.jpg', $request->getPathInfo());
$this->assertEquals(['w' => 100], $request->query->all());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $request);
}

public function testRequestParamsArgs()
{
$request = $this->resolver->getRequest([
'image.jpg', ['w' => 100],
]);

$this->assertEquals('image.jpg', $request->getPathInfo());
$this->assertEquals(['w' => 100], $request->query->all());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $request);
}

public function testInvalidArgs()
{
$this->setExpectedException(
'InvalidArgumentException',
'Not a valid path or Request object.'
);

$this->resolver->getRequest([]);
}
}

0 comments on commit 615861a

Please sign in to comment.