* cors+preflight

* timeout change
This commit is contained in:
Dávid Danyi 2017-08-01 18:16:33 +02:00
parent f196244f7c
commit e23054315b
6 changed files with 71 additions and 4 deletions

View File

@ -14,6 +14,7 @@ use Zend\Stratigility\Middleware\ErrorHandler;
// The error handler should be the first (most outer) middleware to catch
// all Exceptions.
$app->pipe(ErrorHandler::class);
$app->pipe(App\Middleware\PreFlightMiddleware::class);
$app->pipe(LosMiddleware\BasePath\BasePath::class);
$app->pipe(ServerUrlMiddleware::class);

View File

@ -2,11 +2,11 @@
namespace App\Action;
use App\Response\JsonCorsResponse;
use App\Service\DataCollectorService;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class KanbanAction implements ServerMiddlewareInterface
{
@ -20,6 +20,6 @@ class KanbanAction implements ServerMiddlewareInterface
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$kanbanResult = $this->dataCollector->getKanbanBoard();
return new JsonResponse($kanbanResult);
return new JsonCorsResponse($kanbanResult);
}
}

View File

@ -75,7 +75,7 @@ class KanbanEntry implements \JsonSerializable
/**
* JIRA: customfield_10847
* @var string
* @var bool
*/
private $mhwebHot;

View File

@ -0,0 +1,34 @@
<?php
namespace App\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class PreFlightMiddleware
{
const ALLOW_HEADERS = [
'DNT',
'X-CustomHeader',
'Keep-Alive',
'User-Agent',
'X-Requested-With',
'If-Modified-Since',
'Cache-Control',
'Content-Type',
'Authorization',
];
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$requestMethod = strtoupper($request->getMethod());
if ($requestMethod == 'OPTIONS') {
return $response
->withHeader('Accept', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Methods', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
->withHeader('Access-Control-Allow-Headers', implode(",", self::ALLOW_HEADERS));
}
return $next($request, $response);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Response;
use Zend\Diactoros\Response\JsonResponse;
class JsonCorsResponse extends JsonResponse
{
const ALLOW_HEADERS = [
'DNT',
'X-CustomHeader',
'Keep-Alive',
'User-Agent',
'X-Requested-With',
'If-Modified-Since',
'Cache-Control',
'Content-Type',
'Authorization',
];
public function __construct(
$data,
$status = 200,
array $headers = [],
$encodingOptions = self::DEFAULT_JSON_FLAGS
) {
$headers['Access-Control-Allow-Origin'] = '*';
$headers['Access-Control-Allow-Methods'] = 'OPTIONS,GET,POST,PUT,PATCH,DELETE';
$headers['Access-Control-Allow-Headers'] = implode(",", self::ALLOW_HEADERS);
parent::__construct($data, $status, $headers, $encodingOptions);
}
}

View File

@ -14,7 +14,7 @@ class DataCollectorServiceFactory
$config = new Config($configArray['app.config']);
$httpClient = new Client();
$httpClient->setAdapter($curlAdapter = new Client\Adapter\Curl());
$curlAdapter->setOptions(['timeout' => 60]);
$curlAdapter->setOptions(['timeout' => 300]);
if($config->get('http.proxy.enabled', false)) {
$curlAdapter
->setCurlOption(CURLOPT_PROXYTYPE, $config->get('http.proxy.type'))