Skip to content

Commit 9efae2d

Browse files
committed
Write the Handler really work part of the module
1 parent aa8d35e commit 9efae2d

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ typedef struct {
4646
2. Initialize a configuration structure
4747
![image](https://wx1.sinaimg.cn/large/005LOzcmly1fgjerqnq71j30zd08fmyd.jpg)
4848
3. Will the father block configuration information incorporated into this structure Implement the configuration of inheritance
49-
![image](https://wx3.sinaimg.cn/large/005LOzcmly1fgjes12fy5j30ya08qgn7.jpg)
49+
![image](https://wx3.sinaimg.cn/large/005LOzcmly1fgjes12fy5j30ya08qgn7.jpg)
50+
51+
#
52+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjfosnvf5j31hy0q6wlb.jpg)

README_zh.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ typedef struct {
4545
2. 初始化一个配置结构体
4646
![image](https://wx1.sinaimg.cn/large/005LOzcmly1fgjerqnq71j30zd08fmyd.jpg)
4747
3. 将其父block的配置信息合并到此结构体 实现了配置的继承
48-
![image](https://wx3.sinaimg.cn/large/005LOzcmly1fgjes12fy5j30ya08qgn7.jpg)
48+
![image](https://wx3.sinaimg.cn/large/005LOzcmly1fgjes12fy5j30ya08qgn7.jpg)
49+
50+
# Write the Handler really work part of the module
51+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjfosnvf5j31hy0q6wlb.jpg)

src/nginx_moudle_echo.c

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,84 @@ ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
104104
ngx_http_echo_loc_conf_t *conf = child;
105105
ngx_conf_merge_str_value(conf->ed, prev->ed, '"');
106106
return NGX_CONF_OK;
107-
}
107+
}
108+
109+
/**
110+
* handler会接收一个ngx_http_request_t指针类型的参数,这个参数指向一个ngx_http_request_t结构体,此结构体存储了这次HTTP请求的一些信息,这个结构定义在https://github.com/nginx/nginx/blob/master/src/http/ngx_http_request.h中:
111+
*
112+
* 第一步是获取模块配置信息,这一块只要简单使用ngx_http_get_module_loc_conf就可以了。
113+
* 第二步是功能逻辑,因为echo模块非常简单,只是简单输出一个字符串,所以这里没有功能逻辑代码。
114+
* 第三步是设置response header。Header内容可以通过填充headers_out实现,我们这里只设置了Content-type和Content-length等基本内容,ngx_http_headers_out_t定义了所有可以设置的HTTP Response Header信息 这个结构体在https://github.com/nginx/nginx/blob/master/src/http/ngx_http_request.h 设置好头信息后使用ngx_http_send_header就可以将头信息输出,ngx_http_send_header接受一个ngx_http_request_t类型的参数。
115+
* 第四步也是最重要的一步是输出Response body。 Nginx的I/O机制 Nginx允许handler一次产生一组输出,可以产生多次,Nginx将输出组织成一个单链表结构,链表中的每个节点是一个chain_t,定义在https://github.com/nginx/nginx/blob/master/src/core/ngx_buf.h
116+
* @param r ngx_http_request_t指针
117+
* @return
118+
*/
119+
static ngx_int_t
120+
ngx_http_echo_handler(ngx_http_request_t *r)
121+
{
122+
ngx_int_t rc;
123+
ngx_buf_t *b;
124+
ngx_chain_t out;
125+
ngx_http_echo_loc_conf_t *elcf;
126+
elcf = ngx_http_get_module_loc_conf(r,ngx_http_echo_module);
127+
if(!(r->method & (NGX_HTTP_HEAD|NGX_HTTP_GET|NGX_HTTP_POST)))
128+
{
129+
return NGX_HTTP_NOT_ALLOWED;
130+
}
131+
r->headers_out.content_type.len= sizeof("text/html") - 1;
132+
r->headers_out.content_type.data = (u_char *) "text/html";
133+
r->headers_out.status = NGX_HTTP_OK;
134+
r->headers_out.content_length_n = elcf->ed.len;
135+
if(r->method == NGX_HTTP_HEAD)
136+
{
137+
rc = ngx_http_send_header(r);
138+
if(rc != NGX_OK)
139+
{
140+
return rc;
141+
}
142+
}
143+
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
144+
if(b == NULL)
145+
{
146+
ngx_log_error(NGX_LOG_ERROR, r->connection->log, 0, "Failed to allocate response buffer.");
147+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
148+
}
149+
out.buf = b;
150+
put.next = NULL;
151+
b->pos = elcf->ed.data;
152+
b->last = elcf->ed.data + (elcf->ed.len);
153+
b->memory = 1;
154+
b->last_buf = 1;
155+
rc = ngx_http_send_header(r);
156+
if(rc != NGX_OK)
157+
{
158+
return rc;
159+
}
160+
return ngx_http_output_filter(r, &out);
161+
}
162+
163+
164+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
187+

0 commit comments

Comments
 (0)