openresty / echo-nginx-module

An Nginx module for bringing the power of "echo", "sleep", "time" and more to Nginx's config file
http://wiki.nginx.org/NginxHttpEchoModule
BSD 2-Clause "Simplified" License
1.17k stars 254 forks source link

echo_read_request_body in content phase #38

Closed chirkin closed 9 years ago

chirkin commented 9 years ago

In some cases, $request_body very useful, for example - send json body into database query in ngx_postgres module. Echo could fill in $request_body variable, but content phase it's too late for initialize it. Can you move this directive into rewrite phase like in "form input module"?

agentzh commented 9 years ago

@chirkin Please use the ngx_lua module instead. It is just a single line for your use case:

rewrite_by_lua 'ngx.req.read_body()';

Simple enough. Although ngx_lua is way more powerful than just that.

chirkin commented 9 years ago

@agentzh Thank you, it is really more powerful. I think the performance is not much affected.

chirkin commented 9 years ago

@agentzh I found that this solution has drawbacks. In particular postgres_escape function is called before than the ngx.req.read_body() processed. I need to protect sql query. Do you have any solutions?

agentzh commented 9 years ago

@chirkin Well, just use the ndk.set_var.set_quote_pgsql_str() API function in Lua to do the escaping. You'll need the ngx_set_misc module too. See

https://github.com/openresty/lua-nginx-module#ndkset_vardirective

https://github.com/openresty/set-misc-nginx-module#set_quote_pgsql_str

chirkin commented 9 years ago

@agentzh Bravo! Thank you!