Skip to content

Commit

Permalink
Merge pull request #202 from yyamano/access-handler-in-server-scope
Browse files Browse the repository at this point in the history
Enable to use mruby_access_handler in server scope.
  • Loading branch information
matsumotory authored Aug 17, 2016
2 parents 3252a35 + e74cadc commit 9668ce6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 28 deletions.
3 changes: 2 additions & 1 deletion build_config_dynamic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

conf.gembox 'full-core'
conf.cc do |cc|
cc.flags = [ENV['CFLAGS'], '-fPIC']
cc.flags << '-fPIC'
cc.flags << ENV['NGX_MRUBY_CFLAGS'] if ENV['NGX_MRUBY_CFLAGS']
end

#
Expand Down
44 changes: 22 additions & 22 deletions src/http/ngx_http_mruby_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#define ON 1
#define OFF 0

#define NGX_MRUBY_MERGE_CODE(prev_code, conf_code) \
if (prev_code == NGX_CONF_UNSET_PTR) { \
prev_code = conf_code; \
#define NGX_MRUBY_MERGE_CODE(conf_code, prev_code) \
if (conf_code == NGX_CONF_UNSET_PTR) { \
conf_code = prev_code; \
}

#define NGX_MRUBY_CODE_MRBC_CONTEXT_FREE(mrb, code) \
Expand Down Expand Up @@ -398,7 +398,7 @@ static char *ngx_http_mruby_merge_srv_conf(ngx_conf_t *cf, void *parent, void *c
ngx_http_mruby_srv_conf_t *conf = child;
ngx_http_ssl_srv_conf_t *sscf;

NGX_MRUBY_MERGE_CODE(prev->ssl_handshake_code, conf->ssl_handshake_code);
NGX_MRUBY_MERGE_CODE(conf->ssl_handshake_code, prev->ssl_handshake_code);

if (conf->ssl_handshake_code != NGX_CONF_UNSET_PTR) {
sscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_ssl_module);
Expand Down Expand Up @@ -514,24 +514,24 @@ static char *ngx_http_mruby_merge_loc_conf(ngx_conf_t *cf, void *parent, void *c
ngx_http_mruby_loc_conf_t *prev = parent;
ngx_http_mruby_loc_conf_t *conf = child;

NGX_MRUBY_MERGE_CODE(prev->post_read_code, conf->post_read_code);
NGX_MRUBY_MERGE_CODE(prev->server_rewrite_code, conf->server_rewrite_code);
NGX_MRUBY_MERGE_CODE(prev->rewrite_code, conf->rewrite_code);
NGX_MRUBY_MERGE_CODE(prev->access_code, conf->access_code);
NGX_MRUBY_MERGE_CODE(prev->content_code, conf->content_code);
NGX_MRUBY_MERGE_CODE(prev->log_code, conf->log_code);

NGX_MRUBY_MERGE_CODE(prev->post_read_inline_code, conf->post_read_inline_code);
NGX_MRUBY_MERGE_CODE(prev->server_rewrite_inline_code, conf->server_rewrite_inline_code);
NGX_MRUBY_MERGE_CODE(prev->rewrite_inline_code, conf->rewrite_inline_code);
NGX_MRUBY_MERGE_CODE(prev->access_inline_code, conf->access_inline_code);
NGX_MRUBY_MERGE_CODE(prev->content_inline_code, conf->content_inline_code);
NGX_MRUBY_MERGE_CODE(prev->log_inline_code, conf->log_inline_code);

NGX_MRUBY_MERGE_CODE(prev->body_filter_code, conf->body_filter_code);
NGX_MRUBY_MERGE_CODE(prev->body_filter_inline_code, conf->body_filter_inline_code);
NGX_MRUBY_MERGE_CODE(prev->header_filter_code, conf->header_filter_code);
NGX_MRUBY_MERGE_CODE(prev->header_filter_inline_code, conf->header_filter_inline_code);
NGX_MRUBY_MERGE_CODE(conf->post_read_code, prev->post_read_code);
NGX_MRUBY_MERGE_CODE(conf->server_rewrite_code, prev->server_rewrite_code);
NGX_MRUBY_MERGE_CODE(conf->rewrite_code, prev->rewrite_code);
NGX_MRUBY_MERGE_CODE(conf->access_code, prev->access_code);
NGX_MRUBY_MERGE_CODE(conf->content_code, prev->content_code);
NGX_MRUBY_MERGE_CODE(conf->log_code, prev->log_code);

NGX_MRUBY_MERGE_CODE(conf->post_read_inline_code, prev->post_read_inline_code);
NGX_MRUBY_MERGE_CODE(conf->server_rewrite_inline_code, prev->server_rewrite_inline_code);
NGX_MRUBY_MERGE_CODE(conf->rewrite_inline_code, prev->rewrite_inline_code);
NGX_MRUBY_MERGE_CODE(conf->access_inline_code, prev->access_inline_code);
NGX_MRUBY_MERGE_CODE(conf->content_inline_code, prev->content_inline_code);
NGX_MRUBY_MERGE_CODE(conf->log_inline_code, prev->log_inline_code);

NGX_MRUBY_MERGE_CODE(conf->body_filter_code, prev->body_filter_code);
NGX_MRUBY_MERGE_CODE(conf->body_filter_inline_code, prev->body_filter_inline_code);
NGX_MRUBY_MERGE_CODE(conf->header_filter_code, prev->header_filter_code);
NGX_MRUBY_MERGE_CODE(conf->header_filter_inline_code, prev->header_filter_inline_code);

ngx_conf_merge_value(conf->cached, prev->cached, 0);
ngx_conf_merge_value(conf->add_handler, prev->add_handler, 0);
Expand Down
26 changes: 25 additions & 1 deletion test/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ http {
';
}

# test for control nginx internal varable between mruby and nginx
# test for control nginx internal variable between mruby and nginx
location /inter_var_file {
set $fuga "200";
mruby_set $hoge "build/nginx/html/set.rb" cache;
Expand Down Expand Up @@ -636,7 +636,31 @@ http {
location /nginx_false_true {
mruby_content_handler_code 'Nginx.rputs (Nginx::FALSE + Nginx::TRUE)';
}

}

server {
listen 58084;
server_name localhost;

mruby_access_handler_code 'Nginx.return Nginx::HTTP_FORBIDDEN';
location /access_handler_in_server_scope {
mruby_content_handler_code '
Nginx.rputs "OK" # See https://github.com/matsumoto-r/ngx_mruby/issues/200
Nginx::return 200
';
}

location /override_access_handler_in_server_scope {
mruby_access_handler_code 'Nginx.return Nginx::OK';
mruby_content_handler_code '
Nginx.rputs "OK" # See https://github.com/matsumoto-r/ngx_mruby/issues/200
Nginx::return 200
';
}

}

upstream mruby_upstream {
server 127.0.0.1:80;
mruby_upstream_keepalive 16;
Expand Down
19 changes: 15 additions & 4 deletions test/t/ngx_mruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# ngx_mruby test
#

def http_host
"127.0.0.1:58080"
def http_host(port = 58080)
"127.0.0.1:#{port}"
end

def base
"http://#{http_host}"
def base(port = 58080)
"http://#{http_host(port)}"
end

def base_ssl(port)
Expand Down Expand Up @@ -431,6 +431,17 @@ def base_ssl(port)
t.assert_equal "01", res["body"]
end

t.assert('ngx_mruby - access_handler in server scope', 'location /access_handler_in_server_scope') do
res = HttpRequest.new.get base(58084) + '/access_handler_in_server_scope/'
t.assert_equal 403, res["code"]
end

t.assert('ngx_mruby - override access_handler in server scope', 'location /override_access_handler_in_server_scope') do
res = HttpRequest.new.get base(58084) + '/override_access_handler_in_server_scope/'
t.assert_equal 200, res["code"]
t.assert_equal "OK", res["body"]
end

#
# nginx stream test verison 1.9.6 later
#
Expand Down

0 comments on commit 9668ce6

Please sign in to comment.