Support IP Hash LB policy
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / plugins / redirect-transformer / handler.lua
index 72e3330..f469aa0 100644 (file)
@@ -1,6 +1,6 @@
 --[[
 
-    Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+    Copyright (C) 201-2018 ZTE, Inc. and others. All rights reserved. (ZTE)
 
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -19,8 +19,9 @@
 local BasePlugin = require "plugins.base_plugin"
 local msbConf   =  require('conf.msbinit')
 local log_util  =  require('lib.utils.log_util')
+local url_matcher = require "plugins.redirect-transformer.url_matcher"
 local log = log_util.log
-
+local url_match_msb_route = url_matcher.is_match_msb_route
 local RedirectTransformerPluginHandler = BasePlugin:extend()
 
 function RedirectTransformerPluginHandler:new()
@@ -30,13 +31,40 @@ end
 function RedirectTransformerPluginHandler:header_filter()
        RedirectTransformerPluginHandler.super.header_filter(self)
        local originloc = ngx.header.Location
+       local newloc
        if(originloc) then
-               local newloc = ngx.re.sub(originloc, "^(https|http)(.*)", ngx.var.scheme.."$2", "oi")
+               log("origin location:",originloc)
+               local patten_conform,route_match = url_match_msb_route(originloc)
+               if not patten_conform then
+                       log("redirect-transformer output:","The redirect address may be outside msb, do nothing temporarily.")
+                       return
+               end
+
+               if route_match then
+                       --if the redirect address can be forwarded by msb,then donot modify it's url
+                       newloc = ngx.re.sub(originloc, "^(https|http)(.*)", ngx.var.scheme.."$2", "oi")
+               else
+                       --if the redirect address can not be forwarded by msb,then try to modify it's url
+                       local svc_pub_url = ngx.ctx.svc_pub_url
+                       local svc_url = ngx.ctx.svc_url
+                       if(svc_pub_url and svc_pub_url == "/") then
+                               --replace $svc_url with ""
+                               newloc = ngx.re.sub(originloc, "^(https|http)://([^/]+)"..svc_url, ngx.var.scheme.."://".."$2", "oi")
+                       else
+                               --replace $svc_url with $svc_pub_url
+                               newloc = ngx.re.sub(originloc, "^(https|http)://([^/]+)"..svc_url, ngx.var.scheme.."://".."$2"..svc_pub_url, "oi")
+                       end
+               end
+               -- replace the backend server with the host of msb
+               local last_peer = ngx.ctx.last_peer
+               if last_peer then
+                       local backend_ip = ngx.re.gsub(last_peer.ip, "\\.", "\\.", "o")
+                       newloc = ngx.re.sub(newloc, "^(https://|http://)"..backend_ip..":"..last_peer.port, "$1"..ngx.var.host..":"..ngx.var.server_port, "o")
+               end     
                ngx.header["Location"] = newloc
-               log("origin Location:",originloc)
-               log("req scheme:",ngx.var.scheme)
-               log("new Location:",newloc)
+               log("redirect-transformer output:","replace the redirect address to :"..newloc)
+               ngx.log(ngx.WARN, "redirect-transformer replace the redirect address to:"..newloc, " origin location:",originloc)
        end
 end
 
-return RedirectTransformerPluginHandler
\ No newline at end of file
+return RedirectTransformerPluginHandler