Divide the MSB source codes into two repos
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / plugins / redirect-transformer / handler.lua
1 --[[
2
3     Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
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
19 local BasePlugin = require "plugins.base_plugin"
20 local msbConf   =  require('conf.msbinit')
21 local log_util  =  require('lib.utils.log_util')
22 local log = log_util.log
23
24 local RedirectTransformerPluginHandler = BasePlugin:extend()
25
26 function RedirectTransformerPluginHandler:new()
27         RedirectTransformerPluginHandler.super.new(self, "redirect-transformer-plugin")
28 end
29
30 function RedirectTransformerPluginHandler:header_filter()
31         RedirectTransformerPluginHandler.super.header_filter(self)
32         local originloc = ngx.header.Location
33         if(originloc) then
34                 local newloc = ngx.re.sub(originloc, "^(https|http)(.*)", ngx.var.scheme.."$2", "oi")
35                 ngx.header["Location"] = newloc
36                 log("origin Location:",originloc)
37                 log("req scheme:",ngx.var.scheme)
38                 log("new Location:",newloc)
39         end
40 end
41
42 return RedirectTransformerPluginHandler