1.change the license header format
[msb/apigateway.git] / msb-core / openresty-ext / src / assembly / resources / openresty / nginx / luaext / plugins / driver_manager.lua
1 --[[
2
3     Copyright 2016 Huawei Technologies Co., Ltd.
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 local _HEADER = "X-Driver-Parameter"
18
19 --extract driver header if present in request
20 function get_driver_header()
21   local header = ""
22   local driver_header = ngx.req.get_headers()[_HEADER]  
23   if (driver_header ~= nil)
24   then
25     header = driver_header
26   end
27   return header
28 end
29
30 -- generate query url
31 function get_query_url(x_driver_header)
32   local drivermgr_uri = '/openoapi/drivermgr/v1/drivers'
33   local url = drivermgr_uri.."?".._HEADER.."="..tostring(ngx.escape_uri(x_driver_header)).."&service_url="..ngx.var.uri
34   return url
35 end
36
37 -- generate driver url
38 function get_driver_url(driver_header)
39   local cjson = require "cjson"
40   local query_url = get_query_url(driver_header)
41   local res = ngx.location.capture(query_url, { method = ngx.HTTP_GET})
42   ngx.log (ngx.ERR, "Driver manager resp url : ", tostring(res.body))
43   if (res.status == 200 and res.body ~= nil and res.body ~= '')
44   then
45     return tostring(cjson.new().decode(res.body).url)
46   else
47     return ''
48   end
49 end
50
51 -- get headers
52 function get_headers()
53   local headers = {}
54   local h = ngx.req.get_headers()
55   for k, value in pairs(h)
56   do
57      headers[k] = value
58   end
59   return headers
60 end
61
62 function get_body_params()
63   ngx.req.read_body()
64   local actual_body = ""  
65   local body_param = ngx.req.get_body_data()
66   if(body_param ~= nil)
67   then
68     actual_body = tostring(body_param)
69   end
70   return actual_body
71 end
72
73
74
75 ngx.log(ngx.INFO, "DRIVER MANAGER LUA", "***********************")
76
77 -- extract X-Driver-Parameter header param
78 local driver_header = get_driver_header()
79 ngx.log(ngx.ERR, "X-Driver-Parameter: ", driver_header)
80
81
82 -- ignore driver redirection if not driver manager request.
83 if (driver_header ~= "")
84 then 
85   
86   local driver_url = get_driver_url(driver_header)
87   ngx.log (ngx.ERR, "Driver manager URl:: ", driver_url)
88  
89   local http = require "resty.http"
90   local actual_headers = get_headers()
91   local actual_body = get_body_params()
92
93   ngx.log(ngx.ERR, "HTTP request to driver... ", " Request to driver manager")
94   local res, err = http.new():request_uri(driver_url, {
95         method = ngx.req.get_method(),
96         body = actual_body,
97         headers = actual_headers
98       })
99
100       if not res then
101         ngx.say("Request to driver failed : ", err)
102         return
103       end
104  ngx.log(ngx.ERR, "Response from driver : ", tostring(res.body))
105  ngx.say(res.body)
106
107 else
108   ngx.log(ngx.ERR, "X-Driver-Parameter not present", " Redirect to same url")
109 end