Nginx学习(7)—http过滤模块(2)
来源:互联网 发布:上海软件研发质量咨询 编辑:程序博客网 时间:2024/05/16 07:57
实例
场景:用户请求由static静态文件模块进行处理,根据URI返回磁盘文件给用户。我们开发的过滤模块会在返回给用户的响应包体前加一段字符串:“[my filter prefix]”
关于实例的执行流程,具体包括头部处理与包体处理,具体如下:
整个ngx_http_mytest_filter_module.c文件如下:
- /*
- * Copyright (C) Igor Sysoev
- * Copyright (C) Nginx, Inc.
- */
- #include <ngx_config.h>
- #include <ngx_core.h>
- #include <ngx_http.h>
- typedef struct {
- ngx_flag_t enable;
- } ngx_http_mytest_filter_conf_t;
- typedef struct {
- ngx_int_t add_prefix;
- } ngx_http_mytest_filter_ctx_t;
- static ngx_int_t
- ngx_http_mytest_fliter_init(ngx_conf_t *cf);
- static void *
- ngx_http_mytest_filter_create_conf(ngx_conf_t *cf);
- static void *
- ngx_http_mytest_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child);
- static ngx_command_t ngx_http_mytest_filter_commands[] = {
- { ngx_string("add_prefix"),
- NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|
- NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
- ngx_conf_set_flag_slot,
- NGX_HTTP_LOC_CONF_OFFSET,
- offsetof(ngx_http_mytest_filter_conf_t, enable),
- NULL},
- ngx_null_command
- };
- static ngx_http_module_t ngx_http_mytest_filter_module_ctx = {
- NULL, /* preconfiguration */
- ngx_http_mytest_fliter_init, /* postconfiguration */
- NULL, /* create main configuration */
- NULL, /* init main configuration */
- NULL, /* create server configuration */
- NULL, /* merge server configuration */
- ngx_http_mytest_filter_create_conf, /* create location configuration */
- ngx_http_mytest_filter_merge_conf /* merge location configuration */
- };
- ngx_module_t ngx_http_mytest_module = {
- NGX_MODULE_V1,
- &ngx_http_mytest_filter_module_ctx, /* module context */
- ngx_http_mytest_filter_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_output_header_filter_pt ngx_http_next_header_filter;
- static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
- static ngx_str_t filter_prefix = ngx_string("[my filter prefix]");
- static ngx_int_t
- ngx_http_mytest_header_filter(ngx_http_request_t *r)
- {
- ngx_http_mytest_filter_ctx_t *ctx;
- ngx_http_mytest_filter_conf_t *conf;
- if (r->headers_out.status != NGX_HTTP_OK) {
- return ngx_http_next_header_filter(r);
- }
- /* 获取HTTP上下文 */
- ctx = ngx_http_get_module_ctx(r, ngx_http_mytest_filter_module);
- if (ctx) {
- /* 如果已存在,说明已被调用 */
- return ngx_http_next_header_filter(r);
- }
- conf = ngx_http_get_module_loc_conf(r, ngx_http_mytest_filter_module);
- if (conf->enable == 0) {
- return ngx_http_next_header_filter(r);
- }
- ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_mytest_filter_ctx_t));
- if (ctx == NULL) {
- return NGX_ERROR;
- }
- ctx->add_prefix = 0;
- ngx_http_set_ctx(r, ctx, ngx_http_mytest_filter_module);
- if (r->headers_out.content_type.len >= sizeof("text/plain") - 1
- && ngx_strncasecmp(r->headers_out.content_type.data,
- (u_char *)"text/plain", sizeof("text/plain") - 1) == 0) {
- ctx->add_prefix = 1;
- if (r->headers_out.content_length_n > 0) {
- r->headers_out.content_length_n += filter_prefix.len;
- }
- return ngx_http_next_header_filter(r);
- }
- }
- static ngx_int_t
- ngx_http_mytest_body_filter(ngx_http_request_t *r)
- {
- ngx_http_mytest_filter_ctx_t *ctx;
- ctx = ngx_http_get_module_ctx(r, ngx_http_mytest_filter_module);
- if (ctx == NULL || ctx->add_prefix != 1) {
- return ngx_http_next_body_filter(r);
- }
- ctx->add_prefix = 2;
- ngx_buf_t *b = ngx_create_temp_buf(r->pool, filter_prefix.len);
- b->start = b->pos = filter_prefix.data;
- b->last = b->pos + filter_prefix.len;
- ngx_chain_t *cl = ngx_alloc_chain_link(r->pool);
- cl->buf = b;
- cl->next = NULL;
- return ngx_http_next_body_filter(r, cl);
- }
- static ngx_int_t
- ngx_http_mytest_fliter_init(ngx_conf_t *cf)
- {
- ngx_http_next_header_filter = ngx_http_top_header_filter;
- ngx_http_top_header_filter = ngx_http_mytest_header_filter;
- ngx_http_next_header_filter = ngx_http_top_body_filter;
- ngx_http_top_header_filter = ngx_http_mytest_body_filter;
- return NGX_OK;
- }
- static void *
- ngx_http_mytest_filter_create_conf(ngx_conf_t *cf)
- {
- ngx_http_mytest_filter_conf_t *mycf;
- /* 创建配置结构体 */
- mycf = (ngx_http_mytest_filter_conf_t *)ngx_pcalloc(cf->pool,
- sizeof(ngx_http_mytest_filter_conf_t));
- if (mycf == NULL) {
- return NULL;
- }
- mycf->enable = NGX_CONF_UNSET;
- return mycf;
- }
- static void *
- ngx_http_mytest_filter_merge_conf(ngx_conf_t *cf, void *parent, void *child)
- {
- ngx_http_mytest_filter_conf_t *prev = (ngx_http_mytest_filter_conf_t *)parent;
- ngx_http_mytest_filter_conf_t *conf = (ngx_http_mytest_filter_conf_t *)child;
- ngx_conf_merge_value(conf->enable, prev->enable, 0);
- return NGX_OK;
- }
总结
基本上是对HTTP模块开发流程的复习,另外怎么查看执行这个程序执行后的结果啊。好吧,这个我再查查。。下章开始Nginx高级数据结构的学习。再之后就开始第三部分的源码深入了。这才是重点,前面都只是铺垫。。。
0 0
- Nginx学习(7)—http过滤模块(2)
- Nginx学习(7)—http过滤模块(1)
- Nginx学习笔记(八):HTTP过滤模块
- nginx http模块 http过滤模块
- Nginx HTTP过滤模块开发
- nginx开发学习——过滤模块
- 【Nginx】开发一个HTTP过滤模块
- Nginx基础. 开发HTTP过滤模块
- Nginx模块开发(5)————开发简单的HTTP过滤模块
- Nginx基础. HTTP过滤模块添加方式探讨
- Nginx开发一个简单的HTTP过滤模块
- Nginx开发一个简单的HTTP过滤模块
- nginx--过滤模块开发
- nginx 过滤模块
- nginx学习笔记(2):开发一个简单的HTTP模块
- nginx学习(五) http模块
- nginx:将自己编写HTTP过滤模块融入nginx时遇到的问题
- nginx源码分析-过滤模块
- iOS设计模式:工厂方法
- 20个经典bootsrtap后台html网站模板推荐
- Android使用百度地图SDK时,发生错误 com.baidu.platform.comjni.map.basemap.JNIBaseMap.Create
- C练习4.14
- jbpm 不使用zip发布流程
- Nginx学习(7)—http过滤模块(2)
- 外网访问内网的FTP服务器
- ASP广告作弊程序代码
- Java多线程 阻塞队列和并发集合
- Search for a Range
- 大数据时代的IT架构设计
- 编程需要注意的一些问题
- js获取浏览器高度和宽度值
- UVA 1351 String Compression(区间DP)