soulbird commented on code in PR #7065:
URL: https://github.com/apache/apisix/pull/7065#discussion_r875397182


##########
apisix/plugins/redirect.lua:
##########
@@ -148,7 +150,30 @@ function _M.rewrite(conf, ctx)
     core.log.info("plugin rewrite phase, conf: ", core.json.delay_encode(conf))
 
     local ret_code = conf.ret_code
-    local ret_port = tonumber(ctx.var["var_x_forwarded_port"])
+    local ret_port = nil
+    local attr = plugin.plugin_attr(plugin_name)
+    if attr then
+        ret_port = attr.https_port
+    end
+
+    if not ret_port then
+        local local_conf = core.config.local_conf()
+        local ssl = core.table.try_read_attr(local_conf, "apisix", "ssl")
+        if ssl and ssl["enable"] then
+            ret_port = ssl["listen_port"]
+            if not ret_port then
+                local ret_ports = ssl["listen"]
+                if ret_ports and #ret_ports > 0 then
+                    local idx = math_random(1, #ret_ports)
+                    ret_port = ret_ports[idx]
+                    if type(ret_port) == "table" then
+                        ret_port = ret_port.port
+                    end
+                end
+            end
+        end
+    end
+

Review Comment:
   Personally, I feel that this code is too inelegant. Suggest:
   
   ```lua
   local function get_port(attr)
       local port
       if attr then
           port = attr.https.port
       end
   
       if port then
           return port
       end
   
       local local_conf = core.config.local_conf()
       local ssl = core.table.try_read_attr(local_conf, "apisix", "ssl")
       if not ssl or ssl["enable"] then
           return port
       end
   
       port = ssl["listen_port"]
       if port then
           return port
       end
   
       local ports = ssl["listen"]
       if ports and #ports > 0 then
           local idx = math_random(1, #ports)
           port = ports[idx]
           if type(port) == "table" then
               port = port.port
           end
       end
   
       return port
   end
   
   local ret_port = get_port(attr)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to