commit a3f3b143f4fe5221158125be67ba5e877653e525 Author: hladu357 Date: Wed May 15 13:41:57 2024 +0200 compilable diff --git a/config b/config new file mode 100644 index 0000000..13da777 --- /dev/null +++ b/config @@ -0,0 +1,7 @@ +ngx_module_type=HTTP_AUX_FILTER +ngx_module_name=ngx_http_key_header_module +ngx_module_srcs="$ngx_addon_dir/http_key_header_module.c" + +. auto/module + +ngx_addon_name=$ngx_module_name \ No newline at end of file diff --git a/http_key_header_module.c b/http_key_header_module.c new file mode 100644 index 0000000..b03cf75 --- /dev/null +++ b/http_key_header_module.c @@ -0,0 +1,108 @@ +#include +#include +#include + + +static ngx_http_output_header_filter_pt ngx_http_next_header_filter; + +static ngx_int_t ngx_http_key_header_filter(ngx_http_request_t*); + +static ngx_int_t ngx_http_key_header_init(ngx_conf_t*); +static void* ngx_http_key_header_create_conf(ngx_conf_t*); +static char* ngx_http_key_header_merge_loc_conf(ngx_conf_t*, void* parent, void* child); + +typedef struct +{ + ngx_flag_t enabled; +} +ngx_http_key_header_loc_conf_t; + + + +static ngx_command_t +ngx_http_key_header_filter_commands[] = +{ + { + ngx_string("send_cache_key"), + NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_FLAG, + ngx_conf_set_flag_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_key_header_loc_conf_t, enabled), + NULL + }, + + ngx_null_command +}; + + + +static ngx_http_module_t +ngx_http_key_header_filter_module_ctx = +{ + NULL, /* Pre config */ + ngx_http_key_header_init, /* Post config */ + NULL, /* Create main config */ + NULL, /* Init main config */ + NULL, /* Create server config */ + NULL, /* Merge server config */ + ngx_http_key_header_create_conf, /* Create loc config */ + ngx_http_key_header_merge_loc_conf /* Merge loc config */ +}; + + + +static ngx_int_t +ngx_http_key_header_init(ngx_conf_t*) +{ + ngx_http_next_header_filter = ngx_http_top_header_filter; + ngx_http_top_header_filter = ngx_http_key_header_filter; + return NGX_OK; +} + + +static void* +ngx_http_key_header_create_conf(ngx_conf_t *cf) +{ + ngx_http_key_header_loc_conf_t *conf; + conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_key_header_loc_conf_t)); + if(conf == NULL) { + return NGX_CONF_ERROR; + } + + conf->enabled = NGX_CONF_UNSET; + return conf; +} + + +static char* +ngx_http_key_header_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) +{ + ngx_http_key_header_loc_conf_t *prev = parent; + ngx_http_key_header_loc_conf_t *conf = child; + + ngx_conf_merge_value(conf->enabled, prev->enabled, 0); + + return NGX_CONF_OK; +} + +ngx_module_t ngx_http_key_header_module = +{ + NGX_MODULE_V1, + &ngx_http_key_header_filter_module_ctx, /* module context */ + ngx_http_key_header_filter_commands, /* module directives */ + NGX_HTTP_MODULE, /* module type */ + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NGX_MODULE_V1_PADDING +}; + +static ngx_int_t +ngx_http_key_header_filter(ngx_http_request_t *r ) +{ + return ngx_http_next_header_filter(r); +} \ No newline at end of file