2、HTTP框架
继续上面这个例子,比如当nginx收到一个http请求时,我的module需要处理这个请求,那么我应该怎么做?实际这个问题还要再细分。如果是希望nginx收到完整的HTTP请求,再交给我的module处理?又或者只需要接收到完整的http header就给我呢?我把接收完header就交给module处理的code列下,再说下它的处理流程。
首先我要在ngx_XXX_init里注册对这种请求的处理函数。
~~~
static char * ngx_XXX_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
//... code 省略
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_XXX_handler;
return NGX_CONF_OK;
}
~~~
好了,现在有个简单的GET请求进入后,nginx在接收完header,就来调用ngx_XXX_handler方法了。好,现在我们调试下,看看nginx是如何进入ngx_XXX_handler方法的。
~~~
#0 ngx_XXX_handler (r=0x6d5650)
at nginx_XXX_module/ngx_XXX_module.c:646
#1 0x0000000000429e77 in ngx_http_core_content_phase (r=0x6d5650, ph=0x6e17d8)
at src/http/ngx_http_core_module.c:1262
#2 0x00000000004292d0 in ngx_http_core_run_phases (r=0x6d5650)
at src/http/ngx_http_core_module.c:800
#3 0x0000000000429284 in ngx_http_handler (r=0x6d5650)
at src/http/ngx_http_core_module.c:783
#4 0x000000000043165c in ngx_http_process_request (r=0x6d5650)
at src/http/ngx_http_request.c:1615
#5 0x0000000000430845 in ngx_http_process_request_headers (rev=0x6d60d0)
at src/http/ngx_http_request.c:1064
#6 0x00000000004303a0 in ngx_http_process_request_line (rev=0x6e3730)
at src/http/ngx_http_request.c:869
#7 0x000000000042fa8a in ngx_http_init_request (rev=0x6e3730)
at src/http/ngx_http_request.c:510
#8 0x0000000000422d6f in ngx_epoll_process_events (cycle=0x6cb1d0, timer=Variable "timer" is not available.
)
at src/event/modules/ngx_epoll_module.c:518
#9 0x00000000004181cb in ngx_process_events_and_timers (cycle=0x6cb1d0)
at src/event/ngx_event.c:245
#10 0x0000000000420351 in ngx_worker_process_cycle (cycle=0x6cb1d0, data=Variable "data" is not available.
)
at src/os/unix/ngx_process_cycle.c:791
#11 0x000000000041e19b in ngx_spawn_process (cycle=0x6cb1d0,
proc=0x420271 <ngx_worker_process_cycle>, data=0x0, name=0x5171a4 "worker process",
respawn=-3) at src/os/unix/ngx_process.c:194
#12 0x000000000041f8df in ngx_start_worker_processes (cycle=0x6cb1d0, n=1, type=-3)
at src/os/unix/ngx_process_cycle.c:355
#13 0x000000000041f24d in ngx_master_process_cycle (cycle=0x6cb1d0)
at src/os/unix/ngx_process_cycle.c:136
#14 0x0000000000403dea in main (argc=1, argv=Variable "argv" is not available.
) at src/core/nginx.c:396
~~~
上面这个栈信息,可以初步看到nginx是如果调用ngx_XXX_handler,我再详细说下。
ngx_epoll_process_events接收到网络IO事件EPOLLIN后(socket上有数据可读),rev->handler(rev);调用了这个回调方法(ngx_http_init_request)。ngx_http_init_request开始处理这个事件,首先它把基本的ngx_http_request_t变量(nginx HTTP框架中由始至终的东东)初始化,并从内存池中分配了第一块接收内存,然后开始调用ngx_http_process_request_line方法处理具体内容。
ngx_http_process_request_line开始处理具体接收到的消息了,它首先会调用ngx_http_read_request_header方法去读取socket上的字节,读完后(仅仅是当前socket上的缓冲)调用ngx_http_parse_request_line方法分析协议,ngx_http_parse_request_line方法就是一个HTTP协议的状态机实现,分析HTTP协议并赋值到ngx_http_request_t相应的字段里,如果不完整,就返回epoll等待这个socket上的下一次EPOLLIN事件。如果HTTP协议头完整了,就开始调用ngx_http_process_request_headers处理http header的内容了。
ngx_http_process_request_headers方法会先调用ngx_http_parse_header_line去分析http header。ngx_http_parse_header_line也是一个状态机,仅用来解析http header,之后开始调用ngx_http_process_request方法处理已经解析成功的ngx_http_request_t对象。ngx_http_process_request简单的调用ngx_http_handler方法,ngx_http_handler方法把事件发布到ngx_http_core_run_phases阶段,开始回调我们在ngx_XXX_init方法中注册的ngx_XXX_handle方法。
现在大家清楚一个最基本的module处理流程是怎样的了吧?如果大家还是不大明白,我画个活动图帮助大家理解下:
![](https://box.kancloud.cn/2016-04-07_570624fbe3ca3.gif)