-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
55a98ff
commit e843eb5
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters