diff --git a/core/modules/rest/rest.services.yml b/core/modules/rest/rest.services.yml index e7b43f3..43a8359 100644 --- a/core/modules/rest/rest.services.yml +++ b/core/modules/rest/rest.services.yml @@ -27,3 +27,8 @@ services: arguments: ['@plugin.manager.rest', '@config.factory'] tags: - { name: 'event_subscriber' } + rest.options_subscriber: + class: Drupal\rest\Routing\OptionsRequestSubscriber + tags: + - {name: event_subscriber} + diff --git a/core/modules/rest/src/Routing/OptionsRequestSubscriber.php b/core/modules/rest/src/Routing/OptionsRequestSubscriber.php new file mode 100644 index 0000000..e60e3e8 --- /dev/null +++ b/core/modules/rest/src/Routing/OptionsRequestSubscriber.php @@ -0,0 +1,48 @@ +getRequest(); + if ($request->getMethod() == 'OPTIONS') { + $allowed_methods = $this->getAllowedMethods($request); + $response = new Response(NULL, 200, array('Allow' => $allowed_methods)); + $event->setResponse($response); + } + } + + /** + * Check which methods are allowed for the current request. + */ + protected function getAllowedMethods(Request $request) { + // @todo actually check access. + return 'OPTIONS GET HEAD PUT POST DELETE'; + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + return array(KernelEvents::REQUEST => array(array('onKernelRequest', 33))); + } + +} diff --git a/core/modules/rest/tests/src/Routing/OptionsRequestSubscriberTest.php b/core/modules/rest/tests/src/Routing/OptionsRequestSubscriberTest.php new file mode 100644 index 0000000..50115a9 --- /dev/null +++ b/core/modules/rest/tests/src/Routing/OptionsRequestSubscriberTest.php @@ -0,0 +1,27 @@ + 'Options request subscriber tests', + 'description' => 'Tests that OPTIONS requests work correctly.', + 'group' => 'REST', + ); + } +}