これはリクエストボディを調べてもしX
文字が見つかった場合に403
エラーを返すボディフィルタのとても基本的な例です。
有効および無効にするためにディレクティブcatch_body
を使います。
このモジュールの完全なソースは以下で見つかります: http://mdounin.ru/hg/ngx_http_catch_body_filter_module/
最初に必要とする機能を提供するためにNGINXのソースコードから幾つかのインクルードファイルを使う必要があります。
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
次に、catch_body
フラグ状態を含むだろう構造を作成する必要があります。それは各設定ブロックの中で使われるでしょう。
typedef struct {
ngx_flag_t enable;
} ngx_http_catch_body_conf_t;
コードの中で後で使うために2、3の関数のプロトタイプ宣言が必要です。
static void *ngx_http_catch_body_create_conf(ngx_conf_t *cf);
static char *ngx_http_catch_body_merge_conf(ngx_conf_t *cf, void *parent,
void *child);
static ngx_int_t ngx_http_catch_body_init(ngx_conf_t *cf);
ここで、catch_body
フラグを定義するためにngx_command_t
を使います。
NGX_HTTP_MAIN_CONF
declares that it can be used inside the http
configuration block, NGX_HTTP_SRV_CONF
declares that it can be used in the server
configuration block, NGX_HTTP_LOC_CONF
declares that it can be used in the location
configuration block. NGX_CONF_FLAG
オプションは単純な真偽値フラグとしてこれを宣言します。
ngx_conf_set_flag_slot
コールバックは真偽値フラグを有効/無効にするために設定ファイル内で on
および off
を使うことができます。
NGX_HTTP_LOC_CONF_OFFSET
は値がlocationブロックのコンテキスト内で格納されるべきであることを宣言します。
offsetof
はファイルの一番上あたりで宣言される構造に関してこのオプションのメモリの場所を宣言します。
最後に、コマンドのリストは ngx_null_command
を使って終了されていなければなりません。
static ngx_command_t ngx_http_catch_body_commands[] = {
{ ngx_string("catch_body"),
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_catch_body_conf_t, enable),
NULL },
ngx_null_command
};
ngx_http_module_t
構造はモジュールのコンテキストとモジュールのためのコールバックをセットアップするために使われます。For this module we are interested in the postconfiguration and location block configuration callbacks.
static ngx_http_module_t ngx_http_catch_body_module_ctx = {
NULL, /* preconfiguration */
ngx_http_catch_body_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_catch_body_create_conf, /* create location configuration */
ngx_http_catch_body_merge_conf /* merge location configuration */
};
ngx_module_t
構造はモジュールをロードする時にNGINXがモジュールをセットアップするために必要な全てを宣言するために探すものです。この構造のためのインスタンスのための名前がモジュールに付属する config
ファイル内で宣言されるものと同じであることが重要です。
この構造はヘッダとしてNGX_MODULE_V1
を持ち、フッタとしてNGX_MODULE_V1_PADDING
を持たなければなりません。上で宣言されたモジュールのコンテキストはここと、宣言した設定ディレクティブの配列を示します。
モジュールはNGX_HTTP_MODULE
を使って宣言されたHTTPモジュール soです。このモジュールのためのスレッドとプロセスのコールバックを必要としません。
ngx_module_t ngx_http_catch_body_filter_module = {
NGX_MODULE_V1,
&ngx_http_catch_body_module_ctx, /* module context */
ngx_http_catch_body_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
};
後で、ボディフィルタのコールバックチェーンを再整理する必要があるでしょう。これをするために、チェーン内で次のフィルタを保持するポインタが必要です。
static ngx_http_request_body_filter_pt ngx_http_next_request_body_filter;
これはNGINXに渡される各リクエストボディ上で呼ばれるだろうフィルタ関数です。このソースファイルの最後でこれをボディフィルタチェーン内にセットアップするでしょう。
static ngx_int_t
ngx_http_catch_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
u_char *p;
ngx_chain_t *cl;
ngx_http_catch_body_conf_t *conf;
リクエストによって使われる現在の場所のぶおっくのための設定を取得するために ngx_http_get_module_loc_conf()
を呼びます。If the configuration has this directive turned on for the block we continue, otherwise we skip to the next filter.
conf = ngx_http_get_module_loc_conf(r, ngx_http_catch_body_filter_module);
if (!conf->enable) {
return ngx_http_next_request_body_filter(r, in);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"catch request body filter");
リクエストのためのボディがチェーン内に格納されます。We cycle through the links in the chain reading the buffer contents searching for the character X
.
X
が見つかった場合、HTTP Return Codes内で見つかったとしてNGX_HTTP_FORBIDDEN
を返します。そうでなければ、次のボディフィルタに進みます。
for (cl = in; cl; cl = cl->next) {
p = cl->buf->pos;
for (p = cl->buf->pos; p < cl->buf->last; p++) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"catch body in:%02Xd:%c", *p, *p);
if (*p == 'X') {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"catch body: found");
/*
+ As we return NGX_HTTP_FORBIDDEN, the r->keepalive flag
+ won't be reset by ngx_http_special_response_handler().
+ Make sure to reset it to prevent processing of unread
+ parts of the request body.
*/
r->keepalive = 0;
return NGX_HTTP_FORBIDDEN;
}
}
}
return ngx_http_next_request_body_filter(r, in);
}
create conf コールバックは設定ディレクティブのために必要なメモリを割り当て、デフォルトを設定します。
static void *
ngx_http_catch_body_create_conf(ngx_conf_t *cf)
{
ngx_http_catch_body_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_catch_body_conf_t));
if (conf == NULL) {
return NULL;
}
conf->enable = NGX_CONF_UNSET;
return conf;
}
merge conf コールバックは、親ブロックが設定している場合は子ブロックにenable
フラグを設定させます。
static char *
ngx_http_catch_body_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_catch_body_conf_t *prev = parent;
ngx_http_catch_body_conf_t *conf = child;
ngx_conf_merge_value(conf->enable, prev->enable, 0);
return NGX_CONF_OK;
}
初期化の間に、このモジュール内のフィルタチェーンへフィルタを追加したいとします。このフィルタをチェーンの最初にすることでそれをすることができます(モジュールのロード順に比例、あるいは事前に定義された順番)。そして、このモジュール内のフィルタは完了した時に以前一番上にあったフィルタを呼ぶでしょう。
static ngx_int_t
ngx_http_catch_body_init(ngx_conf_t *cf)
{
ngx_http_next_request_body_filter = ngx_http_top_request_body_filter;
ngx_http_top_request_body_filter = ngx_http_catch_body_filter;
return NGX_OK;
}