状态
这个状态机目前由11个状态组成,每一个连接逐步经过这些状态,有些状态比较特殊,可能会执行不到.
● connect
等待一个连接
● reqstart
初始化读空闲定时器
● read
读取HTTP请求头
● reqend
解析HTTP请求
● readpost
读取HTTP请求体
● handlereq
在内部处理请求(可能产生子请求)
● respstart
准备HTTP响应头
● write
发送HTTP响应头 + 响应内容
● respend
清理环境,输出 log
● error
重置连接(关闭连接)
● close
关闭连接(处理 lingering 延时)
A simple GET request (green path)
在 connect 状态下等待连接,如果没有连接到来会空转,一旦连接建立,切换至 reqstart 状态,初始化读取定时器,切换至 read 状态, 然后读取数据,直到读取到HTTP请求头的终止符(CRLFCRLF),快速解析这个请求头.
解析这个请求头的动作是在 handlereq 状态来处理的,处理成功后如果决定回应请求,切换状态到 reqstart 准备 HTTP响应头,在 write 状态发送准备的内容,当状态为 respend 说明HTTP响应内容已经发送完毕,输出日志并且析构各个插件,之后调用 close 关闭这个连接,把状态设置回 connect
Keep-Alive (blue path)
这个 Keep-Alive 操作的处理从 resend 直接切换到 reqstart,没有在 close 和 accept 中处理.
状态机中使用的函数
● state-engine
connection_state_machine()
● connect
(nothing)
● reqstart
(nothing)
● read
connection_handle_read_state()
connection_handle_read()
● reqend
http_request_parse()
● readpost
connection_handle_read_state()
connection_handle_read()
● handlereq
http_response_prepare()
● respstart
connection_handle_write_prepare()
● write
connection_handle_write()
● respend
plugins_call_handle_request_done()
plugins_call_handle_connection_close()
connection_close() (if not keep-alive)
connection_reset()
● error
plugins_call_handle_request_done()
plugins_call_handle_connection_close()
connection_reset()
● close
connection_close()