Skip to content

Commit

Permalink
Merge pull request #217 from matsumoto-r/fix-210
Browse files Browse the repository at this point in the history
Don't allow post_read directives on location context
  • Loading branch information
matsumotory authored Sep 28, 2016
2 parents adffb4b + c1fbd24 commit 0d2679e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
33 changes: 20 additions & 13 deletions src/http/ngx_http_mruby_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static ngx_command_t ngx_http_mruby_commands[] = {
NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_mruby_loc_conf_t, add_handler), NULL},

{ngx_string("mruby_post_read_handler"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE12,
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE12,
ngx_http_mruby_post_read_phase, NGX_HTTP_LOC_CONF_OFFSET, 0, NULL},

{ngx_string("mruby_server_rewrite_handler"),
Expand All @@ -229,7 +229,7 @@ static ngx_command_t ngx_http_mruby_commands[] = {
ngx_http_mruby_log_phase, NGX_HTTP_LOC_CONF_OFFSET, 0, NULL},

{ngx_string("mruby_post_read_handler_code"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE1,
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE1,
ngx_http_mruby_post_read_inline, NGX_HTTP_LOC_CONF_OFFSET, 0, ngx_http_mruby_post_read_inline_handler},

{ngx_string("mruby_server_rewrite_handler_code"),
Expand Down Expand Up @@ -1317,12 +1317,11 @@ static char *ngx_http_mruby_output_filter_error(ngx_conf_t *cf, ngx_command_t *c
static char *ngx_http_mruby_post_read_phase(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_mruby_main_conf_t *mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_mruby_module);
ngx_http_mruby_loc_conf_t *mlcf;
ngx_http_mruby_loc_conf_t *mlcf = conf;
ngx_str_t *value;
ngx_mrb_code_t *code;
ngx_int_t rc;

mlcf = conf;
/* mmcf->state is initialized in ngx_http_mruby_preinit() */
mlcf->state = mmcf->state;

Expand Down Expand Up @@ -1355,11 +1354,10 @@ static char *ngx_http_mruby_server_rewrite_phase(ngx_conf_t *cf, ngx_command_t *
{
ngx_http_mruby_main_conf_t *mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_mruby_module);
ngx_str_t *value;
ngx_http_mruby_loc_conf_t *mlcf;
ngx_http_mruby_loc_conf_t *mlcf = conf;
ngx_mrb_code_t *code;
ngx_int_t rc;

mlcf = conf;
/* mmcf->state is initialized in ngx_http_mruby_preinit() */
mlcf->state = mmcf->state;

Expand Down Expand Up @@ -1913,21 +1911,22 @@ static char *ngx_http_mruby_set_inline(ngx_conf_t *cf, ngx_command_t *cmd, void
// ngx_mruby mruby handler functions
*/

#define NGX_MRUBY_DEFINE_METHOD_NGX_HANDLER(handler_name, code) \
#define NGX_MRUBY_DEFINE_METHOD_NGX_HANDLER(handler_name, _code) \
static ngx_int_t ngx_http_mruby_##handler_name##_handler(ngx_http_request_t *r) \
{ \
ngx_http_mruby_main_conf_t *mmcf = ngx_http_get_module_main_conf(r, ngx_http_mruby_module); \
ngx_http_mruby_loc_conf_t *mlcf = ngx_http_get_module_loc_conf(r, ngx_http_mruby_module); \
if (mmcf->state == NGX_CONF_UNSET_PTR) { \
return NGX_DECLINED; \
} \
if (code == NGX_CONF_UNSET_PTR) { \
if (_code == NGX_CONF_UNSET_PTR) { \
return NGX_DECLINED; \
} \
if (!code->cache) { \
NGX_MRUBY_STATE_REINIT_IF_NOT_CACHED(mlcf->cached, mmcf->state, code, ngx_http_mruby_state_reinit_from_file); \
if (!_code->cache) { \
NGX_MRUBY_STATE_REINIT_IF_NOT_CACHED(mlcf->cached, mmcf->state, _code, ngx_http_mruby_state_reinit_from_file); \
} \
return ngx_mrb_run(r, mmcf->state, code, mlcf->cached, NULL); \
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "hooked mruby file-based " #handler_name " code: %s", _code->code.file); \
return ngx_mrb_run(r, mmcf->state, _code, mlcf->cached, NULL); \
}

NGX_MRUBY_DEFINE_METHOD_NGX_HANDLER(post_read, mlcf->post_read_code)
Expand Down Expand Up @@ -1972,15 +1971,23 @@ static ngx_int_t ngx_http_mruby_content_handler(ngx_http_request_t *r)
if (!code->cache) {
NGX_MRUBY_STATE_REINIT_IF_NOT_CACHED(mlcf->cached, mmcf->state, code, ngx_http_mruby_state_reinit_from_file);
}
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "hooked mruby file-based content code: %s", code->code.file);
return ngx_mrb_run(r, mmcf->state, code, mlcf->cached, NULL);
}

#define NGX_MRUBY_DEFINE_METHOD_NGX_INLINE_HANDLER(handler_name, code) \
#define NGX_MRUBY_DEFINE_METHOD_NGX_INLINE_HANDLER(handler_name, _code) \
static ngx_int_t ngx_http_mruby_##handler_name##_inline_handler(ngx_http_request_t *r) \
{ \
ngx_http_mruby_main_conf_t *mmcf = ngx_http_get_module_main_conf(r, ngx_http_mruby_module); \
ngx_http_mruby_loc_conf_t *mlcf = ngx_http_get_module_loc_conf(r, ngx_http_mruby_module); \
return ngx_mrb_run(r, mmcf->state, code, 1, NULL); \
if (mmcf->state == NGX_CONF_UNSET_PTR) { \
return NGX_DECLINED; \
} \
if (_code == NGX_CONF_UNSET_PTR) { \
return NGX_DECLINED; \
} \
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "hooked mruby inline " #handler_name " code: %s", _code->code.string); \
return ngx_mrb_run(r, mmcf->state, _code, 1, NULL); \
}

NGX_MRUBY_DEFINE_METHOD_NGX_INLINE_HANDLER(post_read, mlcf->post_read_inline_code)
Expand Down
10 changes: 9 additions & 1 deletion test/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ events {

daemon off;
master_process off;
error_log logs/error.log debug;
error_log logs/error.log info;

http {
include mime.types;
Expand Down Expand Up @@ -694,6 +694,14 @@ http {
location /nginx_false_true {
mruby_content_handler_code 'Nginx.rputs (Nginx::FALSE + Nginx::TRUE)';
}

mruby_post_read_handler_code 'Nginx::Request.new.headers_out["hoge"] = "fuga"';
location /issue_210 {
mruby_content_handler_code 'Nginx.rputs "hello"';
}
location /issue_210_2 {
mruby_content_handler_code 'Nginx.rputs "hello2"';
}
}

server {
Expand Down
1 change: 1 addition & 0 deletions test/html/issue-210.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nginx::Request.new.headers_out["fuga"] = "hoge"
12 changes: 12 additions & 0 deletions test/t/ngx_mruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ def base_ssl(port)
t.assert_equal "false", res["body"]
end

t.assert('ngx_mruby - bug; mruby_post_read_handler not running in 1.18.3+', 'location /issue_210') do
res = HttpRequest.new.get base + '/issue_210'
t.assert_equal "fuga", res["hoge"]
t.assert_equal "hello", res["body"]
end

t.assert('ngx_mruby - bug; mruby_post_read_handler not running in 1.18.3+ for file code', 'location /issue_210_2') do
res = HttpRequest.new.get base + '/issue_210_2'
t.assert_equal "fuga", res["hoge"]
t.assert_equal "hello2", res["body"]
end

p nginx_version

if nginx_version.split(".")[1].to_i > 6
Expand Down

0 comments on commit 0d2679e

Please sign in to comment.