Skip to content

Commit

Permalink
Merge branch '2.x.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Mar 20, 2018
2 parents ebc7787 + 167ce61 commit 4730c81
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 287 deletions.
20 changes: 3 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,18 @@ language: php

matrix:
include:
- php: 5.6
env:
- DEPS=lowest
- php: 5.6
env:
- DEPS=latest
- php: 7.0
env:
- DEPS=lowest
- php: 7.0
env:
- DEPS=latest
- php: 7.1
env:
- DEPS=lowest
- php: 7.1
env:
- DEPS=latest
- php: hhvm
- php: 7.2
env:
- DEPS=lowest
- php: hhvm
- php: 7.2
env:
- DEPS=latest
allow_failures:
- php: hhvm

before_script:
- mkdir -p build/logs
Expand All @@ -38,7 +24,7 @@ before_script:

script:
- vendor/bin/phpunit --coverage-text
- php ./vendor/bin/coveralls -v --exclude-no-stmt
- if [[ $DEPS == 'latest' ]]; then php ./vendor/bin/php-coveralls -v --exclude-no-stmt ; fi

notifications:
email: false
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ ExpressiveRedirectHandler
[![Coverage Status](https://coveralls.io/repos/samsonasik/ExpressiveRedirectHandler/badge.svg?branch=master)](https://coveralls.io/r/samsonasik/ExpressiveRedirectHandler)
[![Downloads](https://img.shields.io/packagist/dt/samsonasik/expressive-redirect-handler.svg?style=flat-square)](https://packagist.org/packages/samsonasik/expressive-redirect-handler)

> This is README for version ^1.0 which only support ZF Expressive version 3 with php ^7.1.
> For version 0.*, you can read at [version 0.* readme](https://github.com/samsonasik/ExpressiveRedirectHandler/tree/0.x.x) which still ZF Expressive version 1 and 2 with php ^5.6|^7.0 support.
*ExpressiveRedirectHandler* is a package that contains [zend-expressive](https://github.com/zendframework/zend-expressive) middleware for handling redirect that fit with [zend-expressive-skeleton](https://github.com/zendframework/zend-expressive-skeleton) for following conditions:

1. When the given url to `RedirectResponse` is not registered in routing config
Expand Down Expand Up @@ -41,9 +45,6 @@ return [
'exclude_hosts' => [
// 'www.github.com'
],
'exclude_domains' => [
// 'google.com',
],
],
],

Expand All @@ -53,9 +54,9 @@ return [

It means, we can't allow to make redirect to outside registered routes, whenever found un-registered url in routes, then we will be redirected to default_url. It also disable redirect to self, so you can't redirect to self.

For specific urls that exceptional ( allowed to be redirected even not registered in routes), you can register at `exclude_urls`/`exclude_hosts`/`exclude_domains` options.
For specific urls that exceptional ( allowed to be redirected even not registered in routes), you can register at `exclude_urls`/`exclude_hosts` options.

> if you define exclude_urls/exclude_hosts/exclude_domains options, which one of them is your own current url/host/domain, its your risk to still get "infinite" redirection loops. so, make sure exclude_urls/exclude_hosts/exclude_domains is not your current own.
> if you define exclude_urls/exclude_hosts options, which one of them is your own current url/host/domain, its your risk to still get "infinite" redirection loops. so, make sure exclude_urls/exclude_hosts is not your current own.
2. When you want to redirect to specific url based on header status code
------------------------------------------------------------------------
Expand Down Expand Up @@ -92,7 +93,7 @@ composer require samsonasik/expressive-redirect-handler

- Copy `vendor/samsonasik/expressive-redirect-handler/config/expressive-redirect-handler.local.php.dist` to `config/autoload/expressive-redirect-handler.local.php` and modify on our needs.

For [zend-expressive-skeleton](https://github.com/zendframework/zend-expressive-skeleton) ^2.0 (upcoming), you need to open `config/pipeline.php` and add:
- Open `config/pipeline.php` and add:

```php
$app->pipe(ExpressiveRedirectHandler\Middleware\RedirectHandlerAction::class);
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
}
],
"require": {
"php": "^5.6|^7.0",
"jeremykendall/php-domain-parser": "^3.0"
"php": "^7.1",
"jeremykendall/php-domain-parser": "^5.0",
"zendframework/zend-diactoros": "^1.7.1"
},
"require-dev": {
"zendframework/zend-expressive": "^1.0|^2.0",
"phpunit/phpunit": "^4.5|^5.0|^6.0",
"satooshi/php-coveralls": "^1.0"
"zendframework/zend-expressive": "^3.0",
"phpunit/phpunit": "^7.0",
"satooshi/php-coveralls": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace ExpressiveRedirectHandler;

class ConfigProvider
Expand Down
105 changes: 42 additions & 63 deletions src/Middleware/RedirectHandlerAction.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -20,13 +22,16 @@
namespace ExpressiveRedirectHandler\Middleware;

use InvalidArgumentException;
use Pdp\Parser;
use Pdp\PublicSuffixListManager;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\RedirectResponse;
use Zend\Expressive\Router\RouterInterface;
use Zend\Diactoros\Uri;
use Zend\Expressive\Router\RouterInterface;

class RedirectHandlerAction
class RedirectHandlerAction implements MiddlewareInterface
{
private $config;

Expand All @@ -38,17 +43,12 @@ public function __construct(array $config, RouterInterface $router)
$this->router = $router;
}

public function __invoke($request, $response, callable $next = null)
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
if (null === $next) {
return $response;
}

$response = $next($request, $response);
$response = $handler->handle($request);

if (isset($this->config['header_handler']['enable'])
&& $this->config['header_handler']['enable'] === true
&& ! $response instanceof RedirectResponse
&& ! empty($this->config['header_handler']['headers'])
) {
$statusCode = $response->getStatusCode();
Expand All @@ -65,68 +65,47 @@ public function __invoke($request, $response, callable $next = null)
}
}

if ($response instanceof RedirectResponse) {
$allow_not_routed_url = (isset($this->config['allow_not_routed_url'])) ? $this->config['allow_not_routed_url'] : false;
$exclude_urls = (isset($this->config['options']['exclude_urls'])) ? $this->config['options']['exclude_urls'] : [];
$exclude_hosts = (isset($this->config['options']['exclude_hosts'])) ? $this->config['options']['exclude_hosts'] : [];
$exclude_domains = (isset($this->config['options']['exclude_domains'])) ? $this->config['options']['exclude_domains'] : [];

$uriTarget = $response->getHeader('location')[0];
$uriTargetHost = (new Uri($uriTarget))->getHost();

$redirectOfDomain = false;
foreach ($exclude_domains as $key => $domain) {

if ($key === 0) {
$pslManager = new PublicSuffixListManager();
$parser = new Parser($pslManager->getList());
}

if (! $parser->isSuffixValid($domain)) {
throw new InvalidArgumentException(\sprintf(
'%s is not a valid domain',
$domain
));
}

if ($parser->parseUrl($uriTarget)->host->registerableDomain === $domain) {
$redirectOfDomain = true;
}
if (! $response instanceof RedirectResponse) {
return $response;
}

}
$allow_not_routed_url = $this->config['allow_not_routed_url'] ?? false;
$exclude_urls = $this->config['options']['exclude_urls'] ?? [];
$exclude_hosts = $this->config['options']['exclude_hosts'] ?? [];

if (true === $allow_not_routed_url ||
\in_array($uriTarget, $exclude_urls) ||
\in_array($uriTargetHost, $exclude_hosts) ||
$redirectOfDomain
) {
return $response;
}
$uriTarget = $response->getHeader('location')[0];
$uriTargetHost = (new Uri($uriTarget))->getHost();

$default_url = (isset($this->config['default_url'])) ? $this->config['default_url'] : '/';
$currentPath = $request->getUri()->getPath();
if (true === $allow_not_routed_url ||
\in_array($uriTarget, $exclude_urls) ||
\in_array($uriTargetHost, $exclude_hosts)
) {
return $response;
}

$newUri = new Uri($uriTarget);
$request = $request->withUri($newUri);
$uriTargetPath = $newUri->getPath();
$match = $this->router->match($request);
$default_url = $this->config['default_url'] ?? '/';
$currentPath = $request->getUri()->getPath();

if ($currentPath === $default_url
|| $uriTarget === $default_url
|| $uriTargetPath === $default_url
) {
return;
}
$newUri = new Uri($uriTarget);
$request = $request->withUri($newUri);
$uriTargetPath = $newUri->getPath();
$match = $this->router->match($request);

if ($match->isFailure()
|| ($match->isSuccess()
if ($match->isFailure()
|| ($match->isSuccess()
&& $uriTargetPath === $currentPath
&& $uriTarget !== $default_url
&& $uriTargetPath !== $default_url
)
) {
return $response->withHeader('location', $default_url);
}
) {
return $response->withHeader('location', $default_url);
}

if ($currentPath === $default_url
|| $uriTarget === $default_url
|| $uriTargetPath === $default_url
) {
return new Response();
}

return $response;
Expand Down
14 changes: 5 additions & 9 deletions src/Middleware/RedirectHandlerActionFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -19,21 +21,15 @@

namespace ExpressiveRedirectHandler\Middleware;

use Interop\Container\ContainerInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Router\RouterInterface;

class RedirectHandlerActionFactory
{
/**
* @param ContainerInterface|PsrContainerInterface $container
*/
public function __invoke($container)
public function __invoke(ContainerInterface $container) : RedirectHandlerAction
{
$config = $container->get('config');
$expressive_redirect_handler_config = (isset($config['expressive-redirect-handler']))
? $config['expressive-redirect-handler']
: [];
$expressive_redirect_handler_config = $config['expressive-redirect-handler'] ?? [];
$router = $container->get(RouterInterface::class);

return new RedirectHandlerAction(
Expand Down
15 changes: 3 additions & 12 deletions test/Middleware/RedirectHandlerActionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@

use ExpressiveRedirectHandler\Middleware\RedirectHandlerAction;
use ExpressiveRedirectHandler\Middleware\RedirectHandlerActionFactory;
use Interop\Container\ContainerInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Zend\Expressive\Router\RouterInterface;
use PHPUnit\Framework\TestCase;

if (! class_exists(TestCase::class)) {
class_alias(\PHPUnit_Framework_TestCase::class, TestCase::class);
}
use Psr\Container\ContainerInterface;
use Zend\Expressive\Router\RouterInterface;

class RedirectHandlerActionFactoryTest extends TestCase
{
Expand All @@ -37,11 +32,7 @@ class RedirectHandlerActionFactoryTest extends TestCase

protected function setUp()
{
if (interface_exists(PsrContainerInterface::class)) {
$this->container = $this->prophesize(PsrContainerInterface::class);
} else {
$this->container = $this->prophesize(ContainerInterface::class);
}
$this->container = $this->prophesize(ContainerInterface::class);
}

public function testFactory()
Expand Down

0 comments on commit 4730c81

Please sign in to comment.