3 Copyright 2016 2015-2016 OPEN-O. and others. All rights reserved.
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
20 local _HEADER = "X-Driver-Parameter"
22 --extract driver header if present in request
23 local function get_driver_header()
25 local driver_header = ngx.req.get_headers()[_HEADER]
26 if (driver_header ~= nil)
28 header = driver_header
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
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 ~= '')
51 return tostring(cjson.new().decode(res.body).url)
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;
66 local function get_headers()
68 local h = ngx.req.get_headers()
69 for k, value in pairs(h)
76 local function get_body_params()
78 local actual_body = ""
79 local body_param = ngx.req.get_body_data()
82 actual_body = tostring(body_param)
88 ngx.log(ngx.ERR, "DRIVER MANAGER LUA", "***********************")
90 -- extract X-Driver-Parameter header param
91 local driver_header = get_driver_header()
92 ngx.log(ngx.ERR, "X-Driver-Parameter: ", driver_header)
95 -- ignore driver redirection if not driver manager request.
96 if (driver_header ~= "")
99 local driver_url = get_driver_url(driver_header)..get_ori_query();
100 ngx.log (ngx.ERR, "Driver manager URl:: ", driver_url)
102 local http = require "resty.http"
103 local actual_headers = get_headers()
104 local actual_body = get_body_params()
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(),
110 headers = actual_headers
114 ngx.say("Request to driver failed : ", err)
117 ngx.log(ngx.ERR, "Response from driver : ", tostring(res.body))
121 ngx.log(ngx.ERR, "X-Driver-Parameter not present", " Redirect to same url")