From 64c3ea2af9b5c513c03b542dca0d60a4bb7a86d4 Mon Sep 17 00:00:00 2001 From: Colan Schwartz Date: Thu, 9 Feb 2017 13:07:00 -0500 Subject: [PATCH] Issue #2808923 by colan: Include extensions in canonical path if they are not acceptable formats. --- servers/rest_server/includes/RESTServer.inc | 12 +++++++---- .../includes/ServicesContentTypeNegotiator.inc | 25 ++++++++++++++-------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/servers/rest_server/includes/RESTServer.inc b/servers/rest_server/includes/RESTServer.inc index 1c07ca2..4816680 100644 --- a/servers/rest_server/includes/RESTServer.inc +++ b/servers/rest_server/includes/RESTServer.inc @@ -136,7 +136,7 @@ class RESTServer { $canonical_path = &drupal_static('RESTServerGetCanonicalPath'); if (empty($canonical_path)) { $canonical_path = $this->context->getCanonicalPath(); - $canonical_path = $this->negotiator->getParsedCanonicalPath($canonical_path); + $canonical_path = $this->negotiator->getParsedCanonicalPath($canonical_path, array_keys($this->formatters)); } return $canonical_path; } @@ -183,7 +183,7 @@ class RESTServer { $mime_type = ''; $canonical_path_not_parsed = $this->context->getCanonicalPath(); - $response_format = $this->getResponseFormatFromURL($canonical_path_not_parsed); + $response_format = $this->getResponseFormatFromURL($canonical_path_not_parsed, array_keys($this->formatters)); if (empty($response_format)) { $response_format = $this->getResponseFormatContentTypeNegotiations($mime_type, $canonical_path_not_parsed, $this->formatters); @@ -217,11 +217,15 @@ class RESTServer { * For example /. * * @param $canonical_path + * Canonical path of the request. + * @param array $allowed_formats + * List of formats that are acceptable. * * @return string + * The response format. */ - protected function getResponseFormatFromURL($canonical_path) { - return $this->negotiator->getResponseFormatFromURL($canonical_path); + protected function getResponseFormatFromURL($canonical_path, $allowed_formats) { + return $this->negotiator->getResponseFormatFromURL($canonical_path, $allowed_formats); } /** diff --git a/servers/rest_server/includes/ServicesContentTypeNegotiator.inc b/servers/rest_server/includes/ServicesContentTypeNegotiator.inc index 4b2f1fb..447f6b6 100644 --- a/servers/rest_server/includes/ServicesContentTypeNegotiator.inc +++ b/servers/rest_server/includes/ServicesContentTypeNegotiator.inc @@ -1,7 +1,7 @@ /. * * @param $canonical_path - * + * Canonical path of the request. + * @param array $allowed_formats + * List of formats that are acceptable. * @return string + * The response format. */ - public function getResponseFormatFromURL($canonical_path) { - $matches = $this->getCanonicalPathMatches($canonical_path); + public function getResponseFormatFromURL($canonical_path, $allowed_formats) { + $matches = $this->getCanonicalPathMatches($canonical_path, $allowed_formats); return $matches[2]; } @@ -30,12 +33,14 @@ class ServicesContentTypeNegotiator implements ServicesContentTypeNegotiatorInte * This function returns canonical path without extension. * * @param string $canonical_path - * + * Canonical path of the request. + * @param array $allowed_formats + * List of formats that are acceptable. * @return string * Canonical path without extension. */ - public function getParsedCanonicalPath($canonical_path) { - $matches = $this->getCanonicalPathMatches($canonical_path); + public function getParsedCanonicalPath($canonical_path, $allowed_formats) { + $matches = $this->getCanonicalPathMatches($canonical_path, $allowed_formats); return $matches[1]; } @@ -44,12 +49,14 @@ class ServicesContentTypeNegotiator implements ServicesContentTypeNegotiatorInte * * @param string $canonical_path * Canonical path with extension. + * @param array $allowed_formats + * List of formats that are acceptable. * @return array * Array of matches. */ - public function getCanonicalPathMatches($canonical_path) { + public function getCanonicalPathMatches($canonical_path, $allowed_formats) { $matches = array(); - if (preg_match('/^(.+)\.([^\.^\/]+)$/', $canonical_path, $matches)) { + if (preg_match('/^(.+)\.([^\.^\/]+)$/', $canonical_path, $matches) && in_array($matches[2], $allowed_formats)) { return $matches; } return array('', $canonical_path, ''); -- 2.7.4