Skip to content

Commit

Permalink
upgrade "ngx_http_utils_module"
Browse files Browse the repository at this point in the history
  • Loading branch information
jobs committed Apr 19, 2017
1 parent 7de5966 commit 229e245
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 70 deletions.
2 changes: 1 addition & 1 deletion nginx/Config.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
./configure --with-cc-opt="-g3 -O0" --with-ld-opt="-lzlog -lpthread" --prefix=/data/hustlog --add-module=src/addon
export LD_LIBRARY_PATH=/usr/local/lib; ./configure --with-cc-opt="-g3 -O0" --with-ld-opt="-lzlog -lpthread" --prefix=/data/hustlog --add-module=src/addon
4 changes: 0 additions & 4 deletions nginx/src/addon/hustlog_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

#include "hustlog_utils.h"

ngx_int_t hustlog_reload_handler(ngx_str_t * backend_uri, ngx_http_request_t *r);
ngx_int_t hustlog_getconf_handler(ngx_str_t * backend_uri, ngx_http_request_t *r);
ngx_int_t hustlog_setconf_handler(ngx_str_t * backend_uri, ngx_http_request_t *r);

ngx_int_t hustlog_post_handler(ngx_str_t * backend_uri, ngx_http_request_t *r);

#endif // __hustlog_handler_20150720144219_h__
170 changes: 127 additions & 43 deletions nginx/src/addon/ngx_http_hustlog_module.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include <c_dict.h>
#include "hustlog_handler.h"
#include "ngx_http_utils_module.h"
static ngx_int_t ngx_http_hustlog_handler(ngx_http_request_t *r);
static char *ngx_http_hustlog(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_hustlog_init_module(ngx_cycle_t * cycle);
static ngx_int_t ngx_http_hustlog_init_process(ngx_cycle_t * cycle);
static void ngx_http_hustlog_exit_process(ngx_cycle_t * cycle);
static void ngx_http_hustlog_exit_master(ngx_cycle_t * cycle);

static ngx_http_request_item_t hustlog_handler_dict[] =
{
Expand All @@ -12,48 +19,88 @@ static ngx_http_request_item_t hustlog_handler_dict[] =

static size_t hustlog_handler_dict_len = sizeof(hustlog_handler_dict) / sizeof(ngx_http_request_item_t);

static ngx_int_t ngx_http_hustlog_handler(ngx_http_request_t *r);
static char *ngx_http_hustlog(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
typedef c_dict_t(ngx_http_request_item_t *) addon_handler_dict_t;
static addon_handler_dict_t * addon_handler_dict = NULL;

static ngx_command_t ngx_http_hustlog_commands[] =
static ngx_bool_t __init_addon_handler_dict(ngx_http_request_item_t dict[], size_t size)
{
{
ngx_string("hustlog"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_hustlog,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
if (addon_handler_dict)
{
return true;
}
addon_handler_dict = malloc(sizeof(addon_handler_dict_t));
if (!addon_handler_dict)
{
return false;
}
c_dict_init(addon_handler_dict);
size_t i = 0;
for (i = 0; i < size; ++i)
{
ngx_http_request_item_t ** it = c_dict_get(addon_handler_dict, (const char *) dict[i].uri.data);
if (it && *it)
{
return false;
}
c_dict_set(addon_handler_dict, (const char *) dict[i].uri.data, &dict[i]);
}
return true;
}

static void __uninit_addon_handler_dict()
{
if (!addon_handler_dict)
{
return;
}
c_dict_deinit(addon_handler_dict);
if (addon_handler_dict)
{
free(addon_handler_dict);
addon_handler_dict = NULL;
}
}

static ngx_command_t ngx_http_hustlog_commands[] =
{
{
ngx_string("hustlog"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_hustlog,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},

ngx_null_command
};

static ngx_http_module_t ngx_http_hustlog_module_ctx =
static ngx_http_module_t ngx_http_hustlog_module_ctx =
{
NULL, // ngx_int_t (*preconfiguration)(ngx_conf_t *cf);
NULL, // ngx_int_t (*postconfiguration)(ngx_conf_t *cf);
NULL, // ngx_int_t (*preconfiguration)(ngx_conf_t *cf);
NULL, // ngx_int_t (*postconfiguration)(ngx_conf_t *cf);
NULL, // void *(*create_main_conf)(ngx_conf_t *cf);
NULL, // char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
NULL, // void *(*create_srv_conf)(ngx_conf_t *cf);
NULL, // char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);
NULL, // void *(*create_loc_conf)(ngx_conf_t *cf);
NULL // char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);
NULL, // void * (*create_srv_conf)(ngx_conf_t *cf);
NULL, // char * (*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);
NULL, // void * (*create_loc_conf)(ngx_conf_t *cf);
NULL // char * (*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);
};

ngx_module_t ngx_http_hustlog_module =
{
NGX_MODULE_V1,
&ngx_http_hustlog_module_ctx,
ngx_http_hustlog_commands,
NGX_HTTP_MODULE,
NULL, // ngx_int_t (*init_master)(ngx_log_t *log);
NULL, // ngx_int_t (*init_module)(ngx_cycle_t *cycle);
NULL, // ngx_int_t (*init_process)(ngx_cycle_t *cycle);
NULL, // ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
NULL, // void (*exit_thread)(ngx_cycle_t *cycle);
NULL, // void (*exit_process)(ngx_cycle_t *cycle);
NULL, // void (*exit_master)(ngx_cycle_t *cycle);
NGX_MODULE_V1_PADDING
ngx_module_t ngx_http_hustlog_module =
{
NGX_MODULE_V1,
&ngx_http_hustlog_module_ctx,
ngx_http_hustlog_commands,
NGX_HTTP_MODULE,
NULL, // ngx_int_t (*init_master)(ngx_log_t *log);
ngx_http_hustlog_init_module,
ngx_http_hustlog_init_process,
NULL, // ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
NULL, // void (*exit_thread)(ngx_cycle_t *cycle);
ngx_http_hustlog_exit_process,
ngx_http_hustlog_exit_master,
NGX_MODULE_V1_PADDING
};

static ngx_str_t g_zlog_conf_path = { 0, 0 };
Expand All @@ -69,21 +116,58 @@ int zlog_load_conf(zlog_handler handler)
return handler((const char *)g_zlog_conf_path.data);
}

static char * ngx_http_hustlog(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
static char *ngx_http_hustlog(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t * clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_hustlog_handler;
return NGX_CONF_OK;
ngx_http_core_loc_conf_t * clcf = ngx_http_conf_get_module_loc_conf(
cf, ngx_http_core_module);
clcf->handler = ngx_http_hustlog_handler;
return NGX_CONF_OK;
}

static ngx_int_t ngx_http_hustlog_handler(ngx_http_request_t *r)
{
ngx_http_request_item_t * it = ngx_http_get_request_item(hustlog_handler_dict, hustlog_handler_dict_len, &r->uri);
if (!it)
{
return NGX_ERROR;
}
return it->handler(&it->backend_uri, r);
if (!r->uri.data)
{
return NGX_ERROR;
}
u_char tmp = r->uri.data[r->uri.len];
r->uri.data[r->uri.len] = '\0';
ngx_http_request_item_t ** it = c_dict_get(addon_handler_dict, (const char *)r->uri.data);
r->uri.data[r->uri.len] = tmp;

if (!it || !*it)
{
return NGX_ERROR;
}
return (*it)->handler(&(*it)->backend_uri, r);
}

static ngx_int_t ngx_http_hustlog_init_module(ngx_cycle_t * cycle)
{
// TODO: initialize in master process
return NGX_OK;
}

static ngx_int_t ngx_http_hustlog_init_process(ngx_cycle_t * cycle)
{
if (!__init_addon_handler_dict(hustlog_handler_dict, hustlog_handler_dict_len))
{
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, "init addon_handler_dict error\n");
return NGX_ERROR;
}
// TODO: initialize in worker process
return NGX_OK;
}

static void ngx_http_hustlog_exit_process(ngx_cycle_t * cycle)
{
__uninit_addon_handler_dict(hustlog_handler_dict, hustlog_handler_dict_len);
// TODO: uninitialize in worker process
}

static void ngx_http_hustlog_exit_master(ngx_cycle_t * cycle)
{
// TODO: uninitialize in master process
}

void * ngx_http_get_addon_module_ctx(ngx_http_request_t * r)
Expand Down
55 changes: 39 additions & 16 deletions nginx/src/http/modules/ngx_http_utils_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@
#include "ngx_http_upstream_check_module.h"
#endif

ngx_http_request_item_t * ngx_http_get_request_item(
ngx_http_request_item_t dict[],
size_t size,
const ngx_str_t * uri)
{
size_t i = 0;
for (i = 0; i < size; ++i)
{
if (ngx_http_str_eq(uri, &dict[i].uri))
{
return dict + i;
}
}
return NULL;
}

ngx_str_t ngx_http_get_conf_path(ngx_cycle_t * cycle, ngx_str_t * name)
{
ngx_str_t path = { 0, 0 };
Expand Down Expand Up @@ -324,6 +308,45 @@ static ngx_str_t __remove_mid_param(const ngx_str_t * args, ngx_http_str_pos_t *
return result;
}

ngx_bool_t ngx_http_append_arg(const ngx_str_t * key, const ngx_str_t * val, ngx_http_request_t *r)
{
if (!key || !val || !key->data || !val->data || !r || !r->args.data)
{
return false;
}

static ngx_str_t TAG_AND = ngx_string("&");
static ngx_str_t TAG_EQ = ngx_string("=");

const ngx_str_t * arglist[] = { &r->args, &TAG_AND, key, &TAG_EQ, val };
size_t arglist_size = sizeof(arglist) / sizeof(ngx_str_t *);

size_t size = 0;
size_t i = 0;
for (i = 0; i < arglist_size; ++i)
{
size += arglist[i]->len;
}

ngx_str_t args = ngx_null_string;
args.data = ngx_palloc(r->pool, size + 1);
if (!args.data)
{
return false;
}
memset(args.data, 0, size + 1);

size_t off = 0;
for (i = 0; i < arglist_size; ++i)
{
memcpy(args.data + off, arglist[i]->data, arglist[i]->len);
off += arglist[i]->len;
}
args.len = size;
r->args = args;
return true;
}

ngx_str_t ngx_http_remove_param(const ngx_str_t * args, const ngx_str_t * key, ngx_pool_t * pool)
{
ngx_str_t result = ngx_null_string;
Expand Down
7 changes: 1 addition & 6 deletions nginx/src/http/modules/ngx_http_utils_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ typedef struct
ngx_int_t (*handler) (ngx_str_t * backend_uri, ngx_http_request_t *r);
} ngx_http_request_item_t;

ngx_http_request_item_t * ngx_http_get_request_item(
ngx_http_request_item_t dict[],
size_t size,
const ngx_str_t * uri);


ngx_str_t ngx_http_get_conf_path(ngx_cycle_t * cycle, ngx_str_t * name);
int ngx_http_get_flag_slot(ngx_conf_t * cf);
ngx_bool_t ngx_http_str_eq(const ngx_str_t * src, const ngx_str_t * dst);
Expand All @@ -58,6 +52,7 @@ ngx_http_str_pos_t ngx_http_match_key(const ngx_str_t * args, const char * key,
size_t ngx_http_get_buf_size(const ngx_buf_t * buf);
char * ngx_http_get_param_val(const ngx_str_t * args, const char * key, ngx_pool_t * pool);
ngx_str_t ngx_http_get_empty_str(ngx_pool_t * pool);
ngx_bool_t ngx_http_append_arg(const ngx_str_t * key, const ngx_str_t * val, ngx_http_request_t *r);
ngx_str_t ngx_http_remove_param(const ngx_str_t * args, const ngx_str_t * key, ngx_pool_t * pool);
ngx_bool_t ngx_http_check_key(ngx_http_request_t *r);

Expand Down

0 comments on commit 229e245

Please sign in to comment.