Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/resty/proxy-wasm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ do
end
end

local body
if body_p[0].len > 0 then
body = ffi_str(body_p[0].data, body_p[0].len)
end

local timeout = tonumber(timeout_p[0])

ngx.log(ngx.NOTICE, "send http request to ", uri, ", method: ", method,
Expand All @@ -170,6 +175,7 @@ do
local res, err = httpc:request_uri(uri, {
method = method,
headers = headers,
body = body,
})
if not res then
ngx.log(ngx.ERR, "http call failed: ", err, ", uri: ", uri)
Expand Down
6 changes: 6 additions & 0 deletions src/http/ngx_http_wasm_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,10 @@ ngx_http_wasm_call_get(ngx_http_request_t *r, ngx_str_t *method, ngx_str_t *sche

/* mark the end */
headers->key.len = 0;

if (callout->body) {
*body = *callout->body;
} else {
body->len = 0;
}
}
6 changes: 6 additions & 0 deletions t/WASM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ add_block_preprocessor(sub {
return a[1] < b[1]
end)
ngx.log(ngx.WARN, "hit with headers ", cjson.encode(res))

if ngx.var.request_method == "POST" then
ngx.req.read_body()
local body = ngx.req.get_body_data()
ngx.log(ngx.WARN, "hit with body ", body)
end
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions t/http_call.t
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,29 @@ location /t {
--- error_log
http call failed: timeout
called for contextID =



=== TEST 15: body
--- config
location /t {
content_by_lua_block {
local json = require("cjson")
local wasm = require("resty.proxy-wasm")
local plugin = assert(wasm.load("plugin", "t/testdata/http_call/main.go.wasm"))
local conf = {host = "127.0.0.1:1980", method = "POST"}
for _, e in ipairs({
"blahblah",
"",
}) do
conf.body = e
local ctx = assert(wasm.on_configure(plugin, json.encode(conf)))
assert(wasm.on_http_request_headers(ctx))
end
}
}
--- grep_error_log eval
qr/hit with body \w+/
--- grep_error_log_out
hit with body blahblah
hit with body nil
4 changes: 3 additions & 1 deletion t/testdata/http_call/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (ctx *httpContext) dispatchHttpCall(elem *fastjson.Value) {
method := elem.GetStringBytes("method")
scheme := elem.GetStringBytes("scheme")
headers := elem.GetArray("headers")
body := elem.GetStringBytes("body")

timeout := uint32(elem.GetInt("timeout"))
if timeout == 0 {
Expand Down Expand Up @@ -83,7 +84,8 @@ func (ctx *httpContext) dispatchHttpCall(elem *fastjson.Value) {
hs = append(hs, [2]string{k, v})
}
}
calloutID, err := proxywasm.DispatchHttpCall(string(host), hs, nil, nil,

calloutID, err := proxywasm.DispatchHttpCall(string(host), hs, body, nil,
timeout, ctx.callback)
if err != nil {
proxywasm.LogErrorf("httpcall failed: %v", err)
Expand Down