Skip to content

Commit

Permalink
FEATURE: support OPTIONS request
Browse files Browse the repository at this point in the history
Adds a simple HTTP Component that responds to OPTIONS request with
a corresponding HTTP response if a corresponding endpoint is configured::

   HTTP/1.1 200 OK
   Allow: GET, POST
   Content-Length: 0
  • Loading branch information
bwaidelich committed Aug 19, 2016
1 parent 55a98ff commit e843eb5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Classes/Http/HttpOptionsComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace Wwwision\GraphQL\Http;

use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Http\Component\ComponentChain;
use TYPO3\Flow\Http\Component\ComponentContext;
use TYPO3\Flow\Http\Component\ComponentInterface;

/**
* A simple HTTP Component that captures OPTIONS requests and reponds with a general "Allow: GET, POST" header if a matching graphQL endpoint is configured
*/
class HttpOptionsComponent implements ComponentInterface
{
/**
* @Flow\InjectConfiguration(path="endpoints")
* @var array
*/
protected $endpoints;

/**
* @param ComponentContext $componentContext
* @return void
*/
public function handle(ComponentContext $componentContext)
{
$httpRequest = $componentContext->getHttpRequest();
// no OPTIONS request => skip
if ($httpRequest->getMethod() !== 'OPTIONS') {
return;
}
// no matching graphQL endpoint configured => skip
if (!isset($this->endpoints[$httpRequest->getRelativePath()])) {
return;
}
$httpResponse = $componentContext->getHttpResponse();
$httpResponse->setHeader('Allow', 'GET, POST');
$componentContext->setParameter(ComponentChain::class, 'cancel', true);
}
}
9 changes: 9 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
TYPO3:
Flow:
http:
chain:
'process':
chain:
'graphQLOptions':
position: 'before routing'
component: 'Wwwision\GraphQL\Http\HttpOptionsComponent'
Wwwision:
GraphQL:
endpoints: []
Expand Down

0 comments on commit e843eb5

Please sign in to comment.