Skip to content

Commit

Permalink
Adding enabled flag for loc conf
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Bedra <[email protected]>
  • Loading branch information
abedra committed Sep 21, 2014
1 parent 1ce27c8 commit 49cd335
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
3 changes: 2 additions & 1 deletion nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ events {
}

http {
auth_token_enabled off;
auth_token_redis_host "localhost";
auth_token_redis_port 6379;
auth_token_cookie_name "auth_token";
Expand All @@ -15,7 +16,7 @@ http {
server {
listen 8888;
location / {

auth_token_enabled on;
}

location /app {
Expand Down
46 changes: 43 additions & 3 deletions ngx_http_auth_token_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ typedef struct {
ngx_str_t redirect_location;
} auth_token_main_conf_t;

typedef struct {
ngx_flag_t enabled;
} auth_token_loc_conf_t;

ngx_module_t ngx_http_auth_token_module;

Expand Down Expand Up @@ -63,7 +66,11 @@ ngx_http_auth_token_handler(ngx_http_request_t *r)
return NGX_DECLINED;
}

r->main->internal = 1;
auth_token_loc_conf_t *loc = ngx_http_get_module_loc_conf(r, ngx_http_auth_token_module);

if (!loc->enabled || loc->enabled == NGX_CONF_UNSET) {
return NGX_DECLINED;
}

auth_token_main_conf_t *conf = ngx_http_get_module_main_conf(r, ngx_http_auth_token_module);

Expand Down Expand Up @@ -121,6 +128,31 @@ ngx_http_auth_token_create_main_conf(ngx_conf_t *cf)
return conf;
}

static void*
ngx_http_auth_token_create_loc_conf(ngx_conf_t *cf)
{
auth_token_loc_conf_t *conf;

conf = ngx_pcalloc(cf->pool, sizeof(auth_token_loc_conf_t));
if (conf == NULL) {
return NULL;
}

conf->enabled = NGX_CONF_UNSET;

return conf;
}

static char*
ngx_http_auth_token_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
auth_token_loc_conf_t *prev = (auth_token_loc_conf_t*)parent;
auth_token_loc_conf_t *conf = (auth_token_loc_conf_t*)child;

ngx_conf_merge_value(conf->enabled, prev->enabled, 0);

return NGX_CONF_OK;
}

static ngx_command_t ngx_http_auth_token_commands[] = {
{
Expand Down Expand Up @@ -155,6 +187,14 @@ static ngx_command_t ngx_http_auth_token_commands[] = {
offsetof(auth_token_main_conf_t, redirect_location),
NULL
},
{
ngx_string("auth_token_enabled"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(auth_token_loc_conf_t, enabled),
NULL
},

ngx_null_command
};
Expand All @@ -167,8 +207,8 @@ static ngx_http_module_t ngx_http_auth_token_module_ctx = {
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
ngx_http_auth_token_create_loc_conf, /* create location configuration */
ngx_http_auth_token_merge_loc_conf /* merge location configuration */
};


Expand Down

0 comments on commit 49cd335

Please sign in to comment.