fac8a64fe41cefb9d59a23745c4658c08e69119b
[msb/apigateway.git] / msb-core / openresty-ext / src / assembly / resources / openresty / nginx / luaext / plugins / driver_manager.lua
1 --[[
2
3     Copyright 2016 2015-2016 OPEN-O. and others. All rights reserved.
4
5     Licensed under the Apache License, Version 2.0 (the "License");
6     you may not use this file except in compliance with the License.
7     You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11     Unless required by applicable law or agreed to in writing, software
12     distributed under the License is distributed on an "AS IS" BASIS,
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14     See the License for the specific language governing permissions and
15     limitations under the License.
16
17 ]]
18 local _M = {}
19 _M._VERSION = '1.0.0'
20 local _HEADER = "X-Driver-Parameter"
21
22 --extract driver header if present in request
23 local function get_driver_header()
24   local header = ""
25   local driver_header = ngx.req.get_headers()[_HEADER]  
26   if (driver_header ~= nil)
27   then
28     header = driver_header
29   end
30   return header
31 end
32
33 -- generate query url
34 local function get_query_url(x_driver_header)
35   local drivermgr_uri = '/openoapi/drivermgr/v1/drivers'
36   local url = drivermgr_uri.."?".._HEADER.."="..tostring(ngx.escape_uri(x_driver_header)).."&service_url="..ngx.var.uri
37   return url
38 end
39
40 -- generate driver url
41 local function get_driver_url(driver_header)
42   local cjson = require "cjson"
43   local query_url = get_query_url(driver_header)
44   local driver_header_value = get_driver_header();
45   ngx.req.clear_header(_HEADER)
46   local res = ngx.location.capture(query_url, { method = ngx.HTTP_GET})
47   ngx.req.set_header(_HEADER, driver_header_value);
48   ngx.log (ngx.ERR, "Driver manager resp url : ", tostring(res.body))
49   if (res.status == 200 and res.body ~= nil and res.body ~= '')
50   then
51     return tostring(cjson.new().decode(res.body).url)
52   else
53     return ''
54   end
55 end
56
57 -- get the ori query param string
58 local function get_ori_query()
59   local args = ngx.req.get_uri_args();
60   local ori_query_param = "?"..ngx.encode_args(args);
61   ngx.log(ngx.ERR, "The original request query parameter is ", ori_query_param);
62   return ori_query_param;
63 end
64
65 -- get headers
66 local function get_headers()
67   local headers = {}
68   local h = ngx.req.get_headers()
69   for k, value in pairs(h)
70   do
71      headers[k] = value
72   end
73   return headers
74 end
75
76 local function get_body_params()
77   ngx.req.read_body()
78   local actual_body = ""  
79   local body_param = ngx.req.get_body_data()
80   if(body_param ~= nil)
81   then
82     actual_body = tostring(body_param)
83   end
84   return actual_body
85 end
86
87 function _M.access()
88     ngx.log(ngx.ERR, "DRIVER MANAGER LUA", "***********************")
89
90     -- extract X-Driver-Parameter header param
91     local driver_header = get_driver_header()
92     ngx.log(ngx.ERR, "X-Driver-Parameter: ", driver_header)
93
94
95     -- ignore driver redirection if not driver manager request.
96     if (driver_header ~= "")
97     then 
98       
99       local driver_url = get_driver_url(driver_header)..get_ori_query();
100       ngx.log (ngx.ERR, "Driver manager URl:: ", driver_url)
101      
102       local http = require "resty.http"
103       local actual_headers = get_headers()
104       local actual_body = get_body_params()
105
106       ngx.log(ngx.ERR, "HTTP request to driver... ", " Request to driver manager")
107       local res, err = http.new():request_uri(driver_url, {
108             method = ngx.req.get_method(),
109             body = actual_body,
110             headers = actual_headers
111           })
112
113           if not res then
114             ngx.say("Request to driver failed : ", err)
115             return
116           end
117      ngx.log(ngx.ERR, "Response from driver : ", tostring(res.body))
118      ngx.say(res.body)
119
120     else
121       ngx.log(ngx.ERR, "X-Driver-Parameter not present", " Redirect to same url")
122     end
123 end
124
125 return _M