Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Routing / ContentTypeHeaderMatcher.php
1 <?php
2
3 namespace Drupal\Core\Routing;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
7 use Symfony\Component\Routing\Route;
8 use Symfony\Component\Routing\RouteCollection;
9
10 /**
11  * Filters routes based on the HTTP Content-type header.
12  */
13 class ContentTypeHeaderMatcher implements RouteFilterInterface {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function filter(RouteCollection $collection, Request $request) {
19     // The Content-type header does not make sense on GET requests, because GET
20     // requests do not carry any content. Nothing to filter in this case.
21     if ($request->isMethod('GET')) {
22       return $collection;
23     }
24
25     $format = $request->getContentType();
26
27     foreach ($collection as $name => $route) {
28       $supported_formats = array_filter(explode('|', $route->getRequirement('_content_type_format')));
29       if (empty($supported_formats)) {
30         // No restriction on the route, so we move the route to the end of the
31         // collection by re-adding it. That way generic routes sink down in the
32         // list and exact matching routes stay on top.
33         $collection->add($name, $route);
34       }
35       elseif (!in_array($format, $supported_formats)) {
36         $collection->remove($name);
37       }
38     }
39     if (count($collection)) {
40       return $collection;
41     }
42     // We do not throw a
43     // \Symfony\Component\Routing\Exception\ResourceNotFoundException here
44     // because we don't want to return a 404 status code, but rather a 415.
45     if (!$request->headers->has('Content-Type')) {
46       throw new UnsupportedMediaTypeHttpException('No "Content-Type" request header specified');
47     }
48     else {
49       throw new UnsupportedMediaTypeHttpException('No route found that matches "Content-Type: ' . $request->headers->get('Content-Type') . '"');
50     }
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function applies(Route $route) {
57     return TRUE;
58   }
59
60 }