5.3.2 请求上下文
本节介绍的例子就必须要使用上下文才能正确地解析upstream上游服务器的响应包,因为upstream模块每次接收到一段TCP流时都会回调mytest模块实现的process_header方法解析,这样就需要有一个上下文保存解析状态。在解析HTTP响应行时,可以使用HTTP框架提供的ngx_http_status_t结构,如下所示。
typedef struct{
ngx_uint_t
code;
ngx_uint_t
count;
u_char
*start;
u_char
*end;
}ngx_http_status_t;
把ngx_http_status_t结构放到上下文中,并在process_header解析响应行时使用,如下所示。
typedef struct{
ngx_http_status_t
status;
}ngx_http_mytest_ctx_t;
在5.3.4节实现process_header的代码中,可以学会如何使用ngx_http_status_t结构。