Merge pull request #57 from apimesh/locationFix

Fix location matching and proxy APIs
diff --git a/api-gateway-config/scripts/lua/lib/filemgmt.lua b/api-gateway-config/scripts/lua/lib/filemgmt.lua
index e04c391..c8f7b85 100644
--- a/api-gateway-config/scripts/lua/lib/filemgmt.lua
+++ b/api-gateway-config/scripts/lua/lib/filemgmt.lua
@@ -61,7 +61,7 @@
 
   local updatedPath = ngx.unescape_uri(gatewayPath):gsub("%{(%w*)%}", utils.convertTemplatedPathParam)
 
-  local location = utils.concatStrings({"location ~ ^/api/", tenant, "/", updatedPath, " {\n",
+  local location = utils.concatStrings({"location ~ ^/api/", tenant, "/", updatedPath, "(\\b) {\n",
                                         prefix,
                                         outgoingResource,
                                         "}\n"})
diff --git a/api-gateway-config/scripts/lua/routing.lua b/api-gateway-config/scripts/lua/routing.lua
index f85ff22..3278d16 100644
--- a/api-gateway-config/scripts/lua/routing.lua
+++ b/api-gateway-config/scripts/lua/routing.lua
@@ -53,11 +53,7 @@
       end
       -- Parse backend url
       local u = url.parse(opFields.backendUrl)
-      -- Check for path
-      if u.path == nil or u.path == '' then
-        u.path = '/'
-      end
-      ngx.req.set_uri(u.path)
+      ngx.req.set_uri(getUriPath(u.path))
       -- Set upstream - add port if it's in the backendURL
       local upstream = utils.concatStrings({u.scheme, '://', u.host})
       if u.port ~= nil and u.port ~= '' then
@@ -103,11 +99,30 @@
     ngx.req.set_method(ngx.HTTP_PUT)
   elseif (string.lower(v) == 'delete') then
     ngx.req.set_method(ngx.HTTP_DELETE)
+  elseif (string.lower(v) == 'patch') then
+      ngx.req.set_method(ngx.HTTP_PATH)
+  elseif (string.lower(v) == 'head') then
+      ngx.req.set_method(ngx.HTTP_HEAD)
+  elseif (string.lower(v) == 'options') then
+      ngx.req.set_method(ngx.HTTP_OPTIONS)
   else
     ngx.req.set_method(ngx.HTTP_GET)
   end
 end
 
+function getUriPath(backendPath)
+  local uriPath
+  local i, j = ngx.var.uri:find(ngx.var.gatewayPath)
+  local incomingPath = ngx.var.uri:sub(j + 1)
+  -- Check for backendUrl path
+  if backendPath == nil or backendPath== '' or backendPath== '/' then
+    uriPath = (incomingPath and incomingPath ~= '') and incomingPath or '/'
+  else
+    uriPath = utils.concatStrings({backendPath, incomingPath})
+  end
+  return uriPath
+end
+
 _M.processCall = processCall
 
 return _M