/*
* xwrt_demo.c — A small Nginx HTTP module generated by XWRT.
*
* Generated by XWRT (https://xwrt.dev/tools/nginx-module).
* Nginx HTTP module template. Review and test before production use.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
extern ngx_module_t ngx_http_xwrt_demo_module;
#define XWRT_DEMO_VERSION "1.0.0"
#define XWRT_DEMO_AUTHOR "XWRT"
typedef struct {
ngx_flag_t content;
ngx_str_t body;
ngx_str_t content_type;
ngx_int_t status;
ngx_flag_t header_filter;
ngx_str_t header_name;
ngx_str_t header_value;
ngx_flag_t body_filter;
ngx_flag_t access;
ngx_str_t access_header;
ngx_str_t access_value;
ngx_str_t variable_value;
ngx_flag_t log;
ngx_str_t log_message;
} ngx_http_xwrt_demo_loc_conf_t;
static ngx_int_t
ngx_http_xwrt_demo_content_handler(ngx_http_request_t *r)
{
ngx_http_xwrt_demo_loc_conf_t *conf;
ngx_buf_t *b;
ngx_chain_t out;
conf = ngx_http_get_module_loc_conf(r, ngx_http_xwrt_demo_module);
if (!conf->content) {
return NGX_DECLINED;
}
r->headers_out.status = (ngx_uint_t) conf->status;
r->headers_out.content_type = conf->content_type;
r->headers_out.content_length_n = conf->body.len;
if (r->method == NGX_HTTP_HEAD) {
return ngx_http_send_header(r);
}
b = ngx_calloc_buf(r->pool);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->pos = conf->body.data;
b->last = conf->body.data + conf->body.len;
b->memory = 1;
b->last_buf = 1;
b->last_in_chain = 1;
out.buf = b;
out.next = NULL;
if (ngx_http_send_header(r) != NGX_OK) {
return NGX_ERROR;
}
return ngx_http_output_filter(r, &out);
}
static ngx_http_output_header_filter_pt ngx_http_xwrt_demo_next_header_filter;
static ngx_int_t
ngx_http_xwrt_demo_header_filter(ngx_http_request_t *r)
{
ngx_http_xwrt_demo_loc_conf_t *conf;
ngx_table_elt_t *h;
conf = ngx_http_get_module_loc_conf(r, ngx_http_xwrt_demo_module);
if (!conf->header_filter) {
return ngx_http_xwrt_demo_next_header_filter(r);
}
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
return NGX_ERROR;
}
h->hash = 1;
h->key = conf->header_name;
h->value = conf->header_value;
return ngx_http_xwrt_demo_next_header_filter(r);
}
static ngx_int_t
ngx_http_xwrt_demo_log_handler(ngx_http_request_t *r)
{
ngx_http_xwrt_demo_loc_conf_t *conf;
conf = ngx_http_get_module_loc_conf(r, ngx_http_xwrt_demo_module);
if (!conf->log) {
return NGX_DECLINED;
}
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"%V: uri=%V status=%ui",
&conf->log_message, &r->uri, r->headers_out.status);
return NGX_OK;
}
static ngx_int_t
ngx_http_xwrt_demo_postconfiguration(ngx_conf_t *cf)
{
ngx_http_core_main_conf_t *cmcf;
ngx_http_handler_pt *h;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
ngx_http_xwrt_demo_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_xwrt_demo_header_filter;
h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_xwrt_demo_log_handler;
return NGX_OK;
}
static void *
ngx_http_xwrt_demo_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_xwrt_demo_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_xwrt_demo_loc_conf_t));
if (conf == NULL) {
return NULL;
}
conf->content = NGX_CONF_UNSET;
conf->status = NGX_CONF_UNSET;
conf->header_filter = NGX_CONF_UNSET;
conf->body_filter = NGX_CONF_UNSET;
conf->access = NGX_CONF_UNSET;
conf->log = NGX_CONF_UNSET;
return conf;
}
static char *
ngx_http_xwrt_demo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_xwrt_demo_loc_conf_t *prev = parent;
ngx_http_xwrt_demo_loc_conf_t *conf = child;
ngx_conf_merge_value(conf->content, prev->content, 0);
ngx_conf_merge_str_value(conf->body, prev->body, "Hello from an XWRT Nginx module!\n");
ngx_conf_merge_str_value(conf->content_type, prev->content_type, "text/plain; charset=utf-8");
ngx_conf_merge_value(conf->status, prev->status, NGX_HTTP_OK);
ngx_conf_merge_value(conf->header_filter, prev->header_filter, 0);
ngx_conf_merge_str_value(conf->header_name, prev->header_name, "X-XWRT-Module");
ngx_conf_merge_str_value(conf->header_value, prev->header_value, "xwrt-demo");
ngx_conf_merge_value(conf->body_filter, prev->body_filter, 0);
ngx_conf_merge_value(conf->access, prev->access, 0);
ngx_conf_merge_str_value(conf->access_header, prev->access_header, "X-XWRT-Token");
ngx_conf_merge_str_value(conf->access_value, prev->access_value, "change-me");
ngx_conf_merge_str_value(conf->variable_value, prev->variable_value, "generated-by-xwrt");
ngx_conf_merge_value(conf->log, prev->log, 0);
ngx_conf_merge_str_value(conf->log_message, prev->log_message, "xwrt_demo");
if (conf->content) {
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_xwrt_demo_content_handler;
}
return NGX_CONF_OK;
}
static ngx_http_module_t ngx_http_xwrt_demo_module_ctx = {
NULL, /* preconfiguration */
ngx_http_xwrt_demo_postconfiguration, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_xwrt_demo_create_loc_conf, /* create location configuration */
ngx_http_xwrt_demo_merge_loc_conf /* merge location configuration */
};
static ngx_command_t ngx_http_xwrt_demo_module_commands[] = {
{ ngx_string("xwrt_demo_content"),
NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_xwrt_demo_loc_conf_t, content),
NULL },
{ ngx_string("xwrt_demo_body"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_xwrt_demo_loc_conf_t, body),
NULL },
{ ngx_string("xwrt_demo_content_type"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_xwrt_demo_loc_conf_t, content_type),
NULL },
{ ngx_string("xwrt_demo_status"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_xwrt_demo_loc_conf_t, status),
NULL },
{ ngx_string("xwrt_demo_header_filter"),
NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_xwrt_demo_loc_conf_t, header_filter),
NULL },
{ ngx_string("xwrt_demo_header_name"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_xwrt_demo_loc_conf_t, header_name),
NULL },
{ ngx_string("xwrt_demo_header_value"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_xwrt_demo_loc_conf_t, header_value),
NULL },
{ ngx_string("xwrt_demo_log"),
NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_xwrt_demo_loc_conf_t, log),
NULL },
{ ngx_string("xwrt_demo_log_message"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_xwrt_demo_loc_conf_t, log_message),
NULL },
};
ngx_module_t ngx_http_xwrt_demo_module = {
NGX_MODULE_V1,
&ngx_http_xwrt_demo_module_ctx, /* module context */
ngx_http_xwrt_demo_module_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};