You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "soulbird (via GitHub)" <gi...@apache.org> on 2023/04/14 07:55:05 UTC

[GitHub] [apisix] soulbird commented on a diff in pull request #9194: feat: proxy_rewrite support miltiple regex pattern matching

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


##########
apisix/plugins/proxy-rewrite.lua:
##########
@@ -191,11 +194,17 @@ function _M.check_schema(conf)
     end
 
     if conf.regex_uri and #conf.regex_uri > 0 then
-        local _, _, err = re_sub("/fake_uri", conf.regex_uri[1],
-                                   conf.regex_uri[2], "jo")
-        if err then
-            return false, "invalid regex_uri(" .. conf.regex_uri[1] ..
-                            ", " .. conf.regex_uri[2] .. "): " .. err
+        -- remove this check util 'multipleOf' is supported

Review Comment:
   What‘s means?



##########
t/plugin/proxy-rewrite3.t:
##########
@@ -771,3 +771,127 @@ passed
 GET /test/plugin/proxy/rewrite HTTP/1.1
 --- response_headers
 X-Request-ID: test1///test2
+
+
+
+=== TEST 33: setting multiple regex_uris
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                      "plugins": {
+                          "proxy-rewrite": {
+                              "regex_uri": [
+                                  "^/test/(.*)/(.*)/(.*)/hello",
+                                  "/hello/$1_$2_$3",
+                                  "^/test/(.*)/(.*)/(.*)/world",
+                                  "/world/$1_$2_$3"
+                              ]
+                          }
+                      },
+                      "upstream": {
+                          "nodes": {
+                              "127.0.0.1:8125": 1
+                          },
+                          "type": "roundrobin"
+                      },
+                      "uri": "/test/*"
+                 }]]
+                 )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 34: hit
+--- request
+GET /test/plugin/proxy/rewrite/hello HTTP/1.1
+--- http_config
+    server {
+        listen 8125;

Review Comment:
   There are many URIs for testing in lib.server, usually, it is not necessary to start the server separately



##########
apisix/plugins/proxy-rewrite.lua:
##########
@@ -281,25 +288,36 @@ function _M.rewrite(conf, ctx)
         if not str_find(upstream_uri, "?") then
             separator_escaped = true
         end
-
-        local uri, _, err = re_sub(upstream_uri, conf.regex_uri[1],
-                                   conf.regex_uri[2], "jo")
-        if not uri then
-            local msg = "failed to substitute the uri " .. ctx.var.uri ..
-                        " (" .. conf.regex_uri[1] .. ") with " ..
-                        conf.regex_uri[2] .. " : " .. err
-            core.log.error(msg)
-            return 500, {message = msg}
+        local error_msg
+        for i = 1, #conf.regex_uri, 2 do
+            local captures, err = re_match(upstream_uri, conf.regex_uri[i], "jo")
+            if err then
+                error_msg = "failed to match the uri " .. ctx.var.uri ..
+                    " (" .. conf.regex_uri[i] .. ") " .. err
+                break
+            end
+            if captures then
+                ctx.proxy_rewrite_regex_uri_captures = captures
+                local uri, _, err = re_sub(upstream_uri,
+                    conf.regex_uri[i], conf.regex_uri[i + 1], "jo")
+                if uri then
+                    upstream_uri = uri
+                else
+                    error_msg = "failed to substitute the uri " .. ngx.var.uri ..
+                        " (" .. conf.regex_uri[i] .. ") with " ..
+                        conf.regex_uri[i + 1] .. " : " .. err
+                end
+                break
+            end
         end
-
-        local m, err = re_match(upstream_uri, conf.regex_uri[1], "jo")
-        if not m and err then
-            core.log.error("match error in proxy-rewrite plugin, please check: ", err)
-            return 500
+        if error_msg ~= nil and #error_msg > 0 then
+            core.log.error(error_msg)
+            return 500, { error_msg = error_msg }
         end
-        ctx.proxy_rewrite_regex_uri_captures = m
 
-        upstream_uri = uri
+        if ctx.proxy_rewrite_regex_uri_captures == nil and conf.none_match_abort then
+            return 404, { error_msg = "404 Route Not Found" }

Review Comment:
   This error message is wrong, but the rewrite was not successful, not that the route did not match successfully



-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org