From b8205bcd34309e5b7728ead043517767ffb708c2 Mon Sep 17 00:00:00 2001
From: Yuriy Gerasimov <yuri.gerasimov@gmail.com>
Date: Sun, 7 Aug 2011 12:15:44 +0300
Subject: [PATCH] Refactor determine formatter.

---
 servers/rest_server/includes/RESTServer.inc |   26 ++++++++++----------------
 1 files changed, 10 insertions(+), 16 deletions(-)

diff --git servers/rest_server/includes/RESTServer.inc servers/rest_server/includes/RESTServer.inc
index d88041b..47fd373 100755
--- servers/rest_server/includes/RESTServer.inc
+++ servers/rest_server/includes/RESTServer.inc
@@ -27,31 +27,25 @@ class RESTServer {
     if ($method=='POST' && (isset($_GET['_method']) && $_GET['_method'])) {
       $method = $_GET['_method'];
     }
-    $path = explode('/', $canonical_path);
-    $leaf = array_pop($path);
-    $resource_name = array_shift($path);
-    // Extract response format info from the path
+
+    // Extract response format info from the canonical path.
     $matches=array();
-    if ($leaf && preg_match('/^(.+)\.([^\.]+)$/', $leaf, $matches)) {
-      $leaf = $matches[1];
+    $response_format = '';
+    if (preg_match('/^(.+)\.([^\.^\/]+)$/', $canonical_path, $matches)) {
+      $canonical_path = $matches[1];
       $response_format = $matches[2];
     }
+
+    // Prepare $path arraty and $resource_name.
+    $path = explode('/', $canonical_path);
+    $resource_name = array_shift($path);
+
     // Response will vary with accept headers
     // if no format was supplied as path suffix
     if (empty($response_format)) {
       drupal_add_http_header('Vary', 'Accept');
     }
 
-    // Return the leaf to the path array if it's not the resource name
-    if (isset($leaf)) {
-      if (!$resource_name) {
-        $resource_name = $leaf;
-      }
-      else {
-        array_push($path, $leaf);
-      }
-    }
-
     $endpoint = services_get_server_info('endpoint', '');
     $endpoint_definition = services_endpoint_load($endpoint);
 
-- 
1.7.4.1

