主要处理函数的代码如下(因为在反复测试,代码略微有点乱):
static ngx_int_t ngx_http_gunzip_request_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_gunzip_request_body_filter is called");
ngx_http_gunzip_request_loc_conf_t *conf =
ngx_http_get_module_loc_conf(r, ngx_http_gunzip_request_filter_module);
if (!conf->gunzip_request) {
return ngx_http_next_request_body_filter(r, in);
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "gunzip_request -> on");
ngx_chain_t *cl = NULL, *last_buffer = NULL;
for (cl = in; cl; cl = cl->next)
{
if (1 == cl->buf->last_buf)
{
last_buffer = cl;
break;
}
}
if (NULL != last_buffer)
{
ngx_buf_t *b;
ngx_chain_t *added_link;
ngx_str_t new_body;
u_char content[1024] = {0};
new_body.data = "" style="color:#0f9795;">->pool, 10);
if (NULL == new_body.data)
{
return NGX_ERROR;
}
ngx_memcpy(new_body.data, (u_char*)"1234567890", new_body.len);
new_body.len = 10;
b = ngx_calloc_buf(r->pool);
if (b == NULL)
{
return NGX_ERROR;
}
b->pos = new_body.data;
b->last = b->pos + new_body.len - 1;
b->last_buf = 1;
b->tag = (ngx_buf_tag_t) &ngx_http_gunzip_request_filter_module;
added_link = ngx_alloc_chain_link(r->pool);
if (added_link == NULL)
return NGX_ERROR;
added_link->buf = b;
added_link->next = NULL;
// ngx_chain_add_copy(r->pool, &last_buffer, added_link);
last_buffer->next = added_link;
last_buffer->buf->last_buf = 0;
r->headers_in.content_length_n += sizeof("1234567890") - 1;
r->headers_in.content_length->value.data = "" style="color:#0f9795;">->pool, NGX_OFF_T_LEN + 1);
if (r->headers_in.content_length->value.data == NULL) {
return NGX_ERROR;
}
//memcpy(r->headers_in.content_length->value.data, content_length.data, content_length.len);
r->headers_in.content_length->value.len =
ngx_sprintf(r->headers_in.content_length->value.data, "%O", r->headers_in.content_length_n)
- r->headers_in.content_length->value.data;
r->headers_in.content_length->value.data[r->headers_in.content_length->value.len] = '\0';
memcpy(content, r->headers_in.content_length->value.data, r->headers_in.content_length->value.len);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "r->headers_in.content_length->value.data = "" %d, %d",
content, r->headers_in.content_length->value.len, r->headers_in.content_length_n);
//ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "request_body->bufs=%d, in=%d, added_link=%d",
// r->request_body->bufs, in, added_link); */
}
return ngx_http_next_request_body_filter(r, in);
}