[RFC] dummy body inspection module

Luka Perkov nginx at lukaperkov.net
Fri Nov 23 00:32:11 UTC 2012


Hi,

I wanted to check with you guys what is the best way to look into
request body inside nginx module. Example use case:

Client sends HTTP POST message; nginx module looks at the message and if
it's ok it redirects internaly HTTP request to "/ok/" and if it's not it
will redirect it to "/error/". Nginx justs looks at the contents and
forwards it to correct destination.

I have made a dummy module for this purpose. Please take a look at the
code bellow and let me know what you think.

Luka


/*
 * Copyright (C) 2012 Luka Perkov <freeacs-ng at lukaperkov.net>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdio.h>

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

static ngx_int_t ngx_http_post_inspect_init(ngx_conf_t *cf);
static void * ngx_http_post_inspect_create_loc_conf(ngx_conf_t *cf);
static char * ngx_http_post_inspect_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);

typedef struct {
	ngx_flag_t	inspect;
} ngx_http_post_inspect_loc_conf_t;

static ngx_command_t ngx_http_post_inspect_commands[] = {
	{
		ngx_string("post_inspect"),
		NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
		ngx_conf_set_flag_slot,
		NGX_HTTP_LOC_CONF_OFFSET,
		offsetof(ngx_http_post_inspect_loc_conf_t, inspect),
		NULL
	},

	ngx_null_command
};

static ngx_http_module_t ngx_http_post_inspect_module_ctx = {
	NULL,					/* preconfiguration */
	ngx_http_post_inspect_init,		/* postconfiguration */

	NULL,					/* create main configuration */
	NULL,					/* init main configuration */

	NULL,					/* create server configuration */
	NULL,					/* merge server configuration */

	ngx_http_post_inspect_create_loc_conf,	/* create location configuration */
	ngx_http_post_inspect_merge_loc_conf	/* merge location configuration */

};

ngx_module_t ngx_http_post_inspect_module = {
	NGX_MODULE_V1,
	&ngx_http_post_inspect_module_ctx,	/* module context */
	ngx_http_post_inspect_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
};

typedef struct {
	ngx_flag_t done:1;
	ngx_flag_t waiting_more_body:1;
} ngx_http_post_inspect_ctx_t;


static ngx_int_t
ngx_http_post_inspect_deep_inspection(ngx_http_request_t *r)
{
	ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, \
		"mod_post_inspect ngx_http_post_inspect_deep_inspection()");

	u_char *p;
	size_t len;
	ngx_buf_t *buf;
	ngx_chain_t *cl;
	ngx_str_t msg;

	if (r->request_body == NULL
	    || r->request_body->bufs == NULL
	    || r->request_body->temp_file)
	{
		ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, \
			"mod_post_inspect ngx_http_post_inspect_deep_inspection(): body is not found");

		/*
		 * Exit because no body was found or body is in temp_file.
		 * TODO: extend this
		 */
		return NGX_OK;
	}

	cl = r->request_body->bufs;
	buf = cl->buf;

	if (cl->next == NULL) {
		/* Body data is only in one buffer. */

		msg.len = buf->last - buf->pos;
		msg.data = buf->pos;

	} else {

		len = buf->last - buf->pos;
		cl = cl->next;

		for ( /* void */ ; cl; cl = cl->next) {
			buf = cl->buf;
			len += buf->last - buf->pos;
		}

		p = ngx_pnalloc(r->pool, len);
		if (p == NULL) {
			ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, \
				"mod_post_inspect ngx_http_post_inspect_deep_inspection(): ngx_pnalloc error");

			return NGX_ERROR;
		}

		msg.data = p;
		cl = r->request_body->bufs;

		for ( /* void */ ; cl; cl = cl->next) {
			buf = cl->buf;
			p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
		}

		msg.len = len;
	}

	/*
	 * We can do some magic here with POST data in msg.
	 * After that we can redirect to some internal url.
	 */

	ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, \
		"mod_post_inspect ngx_http_post_inspect_deep_inspection(): \"%V\"", &msg);

	ngx_str_t redirect_to = ngx_string("/redirect1/");

	ngx_http_internal_redirect(r, &redirect_to , &r->args);
	(void) ngx_http_discard_request_body(r);

	return NGX_HTTP_OK;
}

void
ngx_http_post_inspect_payload_handler(ngx_http_request_t *r)
{
	ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, \
			"mod_post_inspect ngx_http_post_inspect_payload_handler()");

	ngx_http_post_inspect_ctx_t *ctx;
	ctx = ngx_http_get_module_ctx(r, ngx_http_post_inspect_module);

	r->read_event_handler = ngx_http_request_empty_handler;
	r->main->count--;

	ctx->done = 1;
	if (ctx->waiting_more_body) {
		ctx->waiting_more_body = 0;
		ngx_http_core_run_phases(r);
	}
}

static ngx_int_t
ngx_http_post_inspect_access_handler(ngx_http_request_t *r)
{
	ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, \
			"mod_post_inspect ngx_http_post_inspect_access_handler()");

	ngx_int_t rc;

	ngx_http_post_inspect_ctx_t *ctx;
	ctx = ngx_http_get_module_ctx(r, ngx_http_post_inspect_module);

	if (ctx != NULL) {
		if (ctx->done) {
			return NGX_DECLINED;
		}
		return NGX_DONE;
	}

	if (r->method != NGX_HTTP_POST) {
		return NGX_DECLINED;
	}

	ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_post_inspect_ctx_t));

	if (ctx == NULL) {
		ngx_http_finalize_request(r, NGX_ERROR);
		return NGX_ERROR;
	}

	ngx_http_set_ctx(r, ctx, ngx_http_post_inspect_module);

	r->request_body_in_single_buf = 1;
	r->request_body_in_persistent_file = 1;
	r->request_body_in_clean_file = 1;

	if (r->request_body_in_file_only) {
		r->request_body_file_log_level = 0;
	}

	rc = ngx_http_read_client_request_body(r, ngx_http_post_inspect_payload_handler);

	if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
		return rc;
	}

	if (rc == NGX_AGAIN) {
		ctx->waiting_more_body = 1;
		return NGX_DONE;
	}

	return NGX_DECLINED;
}

static ngx_int_t
ngx_http_post_inspect_init(ngx_conf_t *cf)
{
	ngx_http_handler_pt *h;
	ngx_http_core_main_conf_t *cmcf;

	cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

	h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
	if (h == NULL) return NGX_ERROR;

	*h = ngx_http_post_inspect_access_handler;

	return NGX_OK;
}

static void *
ngx_http_post_inspect_create_loc_conf(ngx_conf_t *cf)
{
	ngx_http_post_inspect_loc_conf_t *conf;

	conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_post_inspect_loc_conf_t));
	if (conf == NULL) return NGX_CONF_ERROR;

	conf->inspect = NGX_CONF_UNSET;

	return conf;
}

static char *
ngx_http_post_inspect_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
	ngx_http_core_loc_conf_t *clcf;
	ngx_http_post_inspect_loc_conf_t *prev = parent;
	ngx_http_post_inspect_loc_conf_t *conf = child;

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

	if (conf->inspect) {
		clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
		clcf->handler = ngx_http_post_inspect_deep_inspection;
	}

	return NGX_CONF_OK;
}



More information about the nginx-devel mailing list