Skip to content

feat: make it support rewrite request body in external plugins#9864

Closed
kldcty wants to merge 0 commit into
apache:masterfrom
kldcty:master
Closed

feat: make it support rewrite request body in external plugins#9864
kldcty wants to merge 0 commit into
apache:masterfrom
kldcty:master

Conversation

@kldcty

@kldcty kldcty commented Jul 19, 2023

Copy link
Copy Markdown
Contributor

Description

make it support rewrite request body.

please update ext-plugin-proto first.

to make it be able to rewrite the request body in external plugins.

i don't have a full development environment for apisix. someone have the full development env can help to add the test code.

Fixes # (issue)

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@kldcty kldcty changed the title feat: make it support rewrite request body feat: make it support rewrite request body in external plugins Jul 19, 2023
@jiangfucheng

Copy link
Copy Markdown
Member

This PR is based on api7/ext-plugin-proto#35, maybe we should wait for the PR to merge and then update the dependency version of ext-plugin-proto. And, we also need to add corresponding test.

Comment thread apisix/plugins/ext-plugin/init.lua Outdated
local len = rewrite:BodyLength()
if len > 0 then
let body = rewrite:BodyAsString()
ngx.req.set_body_data(body)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ngx.req.set_body_data(body)
ngx.req.read_body()
ngx.req.set_body_data(body)

We need call ngx.req.read_body() before set_body_data

@jiangfucheng

jiangfucheng commented Jul 22, 2023

Copy link
Copy Markdown
Member

https://github.com/apache/apisix/blob/59c1d99a6a571a77d9c3f7995a23785d93a4a92c/t/lib/ext-plugin.lua#L473C13-L473C13

Before this line, we can mock rewrite_request_body case:

        elseif case.rewrite_request_body == true then
            local len = 4
            http_req_call_rewrite.StartBodyVector(builder, len)
            builder:PrependByte(string.byte("\n"))
            builder:PrependByte(string.byte("c"))
            builder:PrependByte(string.byte("b"))
            builder:PrependByte(string.byte("a"))
            local b = builder:EndVector(len)

            http_req_call_rewrite.Start(builder)
            http_req_call_rewrite.AddBody(builder, b)
            local action = http_req_call_rewrite.End(builder)
            build_action(action, http_req_call_action.Rewrite)

And then, we can add test cases in plugin/ext-plugin/http-req-call.t

=== TEST 27: add route
--- config
    location /t {
        content_by_lua_block {
            local json = require("toolkit.json")
            local t = require("lib.test_admin")

            local code, message, res = t.test('/apisix/admin/routes/1',
                ngx.HTTP_PUT,
                 [[{
                    "uri": "/echo",
                    "plugins": {
                        "ext-plugin-pre-req": {
                        }
                    },
                    "upstream": {
                        "nodes": {
                            "127.0.0.1:1980": 1
                        },
                        "type": "roundrobin"
                    }
                }]]
            )

            if code >= 300 then
                ngx.status = code
                ngx.say(message)
                return
            end

            ngx.say(message)
        }
    }
--- response_body
passed



=== TEST 29: test rewrite request body
--- request
GET /echo
--- response_body chomp
cat
--- extra_stream_config
    server {
        listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;

        content_by_lua_block {
            local ext = require("lib.ext-plugin")
            ext.go({rewrite_request_body = true})
        }
    }
--- response_body
abc

@jiangfucheng

Copy link
Copy Markdown
Member

We aslo should update the documents.

@juststillthinking juststillthinking added the wait for update wait for the author's response in this issue/PR label Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wait for update wait for the author's response in this issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants