nginx http模块开发实例(3) - 回显请求体

2018-07-30 17:19:10

本例子实现了回显 post 请求的请求体的功能。

模块指令

Syntax:	my_post;
Default: ;
Context: location

源代码

文件名 ngx_http_mypost_module.c

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


static char *ngx_http_mypost(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_mypost_handler(ngx_http_request_t *r);
void
ngx_http_mypost_init(ngx_http_request_t *r);

typedef struct {
	ngx_uint_t on;
	ngx_str_t content;

	ngx_uint_t sendf;
	ngx_str_t fname;
} ngx_http_mypost_conf_t;

static ngx_command_t  ngx_http_mypost_commands[] = {

    { ngx_string("my_post"),
      NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
	  ngx_http_mypost,
      0,
      0,
      NULL },

      ngx_null_command
};


static ngx_http_module_t  ngx_http_mypost_module_ctx = {
    NULL,                          /* preconfiguration */
    NULL,                          /* postconfiguration */

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

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

	NULL,    /* create location configuration */
    NULL                           /* merge location configuration */
};


ngx_module_t  ngx_http_mypost_module = {
    NGX_MODULE_V1,
    &ngx_http_mypost_module_ctx,      /* module context */
	ngx_http_mypost_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 char *
ngx_http_mypost(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
	//设置回调函数
	ngx_http_core_loc_conf_t  *clcf;

	clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
	clcf->handler = ngx_http_mypost_handler;

    return NGX_CONF_OK;
}

static ngx_int_t
ngx_http_mypost_handler(ngx_http_request_t *r)
{
	//ngx_buf_t *b;
	//ngx_chain_t out;

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

	// 不处理chunked或请求体长度超过10000个字节
	if (r->headers_in.content_length_n > 10000 || r->headers_in.chunked) {
		r->request_body_no_buffering = 0;

		return NGX_DECLINED;
	}


	ngx_int_t rc = ngx_http_read_client_request_body(r, ngx_http_mypost_init);
	if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
		return rc;
	}

	return NGX_DONE;
}

void
ngx_http_mypost_init(ngx_http_request_t *r){

	//ngx_buf_t *b;
	//ngx_chain_t out;
	ngx_int_t rc;

	r->headers_out.status = NGX_HTTP_OK;
	r->headers_out.content_length_n = r->headers_in.content_length_n;
	r->headers_out.content_type.data = (u_char *)"text/html";
	r->headers_out.content_type.len = strlen("text/html");

	//发送响应头
	ngx_http_send_header(r);

	rc = ngx_http_output_filter(r, r->request_body->bufs);

	ngx_http_finalize_request(r, rc);

}
[root@192 ~]# ll /tmp/nginx_m/post/
total 8
-rw-r--r--. 1 root root  163 Jul 26 10:56 config
-rw-r--r--. 1 root root 3167 Jul 30 17:03 ngx_http_mypost_module.c

[root@192 ~]# cat /tmp/nginx_m/post/config 
ngx_addon_name=ngx_http_mypost_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mypost_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mypost_module.c"

上面的目录和文件自己新建,然后进入 nginx 源码目录进行编译  

./configure --prefix=/usr/local/nginx --add-module=/tmp/nginx_m/post/

make

make install

配置

location /post {
    my_post;
}
[root@192 ~]# curl -F "name=dailei" -F "age=22" -F "f=@time.txt" localhost/post
------------------------------24c30356c721
Content-Disposition: form-data; name="name"

dailei
------------------------------24c30356c721
Content-Disposition: form-data; name="age"

22
------------------------------24c30356c721
Content-Disposition: form-data; name="f"; filename="time.txt"
Content-Type: text/plain

i am time.txt

------------------------------24c30356c721--


备注

1.测试环境centos7 64位,nginx版本为 1.14.0。
2..原文地址http://www.freecls.com/a/2712/ea


©著作权归作者所有
收藏
推荐阅读
简介
天降大任于斯人也,必先苦其心志。