Project

General

Profile

Docs InternalHTTPStates » History » Revision 8

Revision 7 (moo, 2007-07-07 15:30) → Revision 8/12 (stbuehler, 2009-02-17 10:04)

h1. [[TracNav(DocsToc)]] 
 '''This information is outdated for Lighttpd v1.5.*''' 



 <pre> 

 #!rst 

 ============================ 
 The State Engine of lighttpd 
 ============================ 


 ------------ 
 Module: core 
 ------------ 
  
 .. contents:: Table of Contents 

 {{>toc}} 

 _This information is outdated for Lighttpd v1.5.*_ 

 h2. Description 
 =========== 

 h3. States 
 ------ 

 The state-engine is currently made of 11 states which are walk-through on 
 the way each connection. Some of them are specific for a special operation 
 and some may never be hit at all. 

 * connect :connect: 
   waiting for a connection 
 * reqstart :reqstart: 
   init the read-idle timer 
 * read :read: 
   read http-request-header from network 
 * reqend :reqend: 
   parse request 
 * readpost :readpost: 
   read http-request-content from network 
 * handlereq :handlereq: 
   handle the request internally (might result in sub-requests) 
 * respstart :respstart: 
   prepare response header 
 * write :write: 
   write response-header + content to network 
 * respend :respend: 
   cleanup environment, log request 
 * error :error: 
   reset connection (incl. close()) 
 * close :close: 
   close connection (handle lingering close) 
  
 h3. A simple GET request (green path) 
 --------------------------------- 
  
 The connection is idling in the 'connect' state waiting for a connection. 
 As soon as the connection is set up we init the read-timer in 'reqstart' 
 and start to read data from the network. As soon as we get the 
 HTTP-request terminator (CRLFCRLF) we forward the header to the parser. 
  
 The parsed request is handled by 'handlereq' and as soon as a decision out 
 the request is made it is sent to 'respstart' to prepare the 
 HTTP-response header. In the 'write' state the prepare content is sent out 
 to the network. When everything is sent 'respend' is entered to log the 
 request and cleanup the environment. After the close() call the connection  
 is set back to the 'connect' state again. 
  
 h3. Keep-Alive (blue path) 
 ---------------------- 
  
 The Keep-Alive handling is implemented by going from the 'respend' 
 directly to 'reqstart' without the close() and the accept() calls. 
  
 h3. POST requests (grey path) 
 ------------------------- 
  
 As requests might contain a request-body the state 'readpost' entered as 
 soon as the header is parsed and we know how much data we expect. 
  
 h3. Pipelining 
 ---------- 
  
 HTTP/1.1 supportes pipelining (sending multiple requests without waiting 
 for the response of the first request). This is handled transparently by 
 the 'read' state. 
  
 h3. Unexpected errors (red path) 
 ---------------------------- 
  
 For really hard errors we use the 'error' state which resets the 
 connection and can be call from every state. It is only use if there is no 
 other way to handle the issue (e.g. client-side close of the connection). 
 If possible we should use http-status 500 ('internal server error') and 
 log the issue in the errorlog. 
  
 If we have to take care of some data which is coming in after we ran into 
 the error condition the 'close' state is used the init a half-close and 
 read all the delay packet from the network. 
  
 h3. Sub-Requests (lightblue) 
 ------------------------ 
  
 The FastCGI, CGI, ... intergration is done by introducing a loop in 
 'handlereq' to handle all aspect which are neccesary to find out what has 
 to be sent back to the client. 
  
 h2. Functions 
 ========= 

 Important functions used by the state-engine 
 
 * state-engine 
 ** @connection_state_machine()@ 
 * connect 
   :state-engine: 

 - ``connection_state_machine()`` 
  
 :connect: 

 - (nothing) 
 * reqstart 
   

 :reqstart: 

 - (nothing) 

 :read: 

 - ``connection_handle_read_state()`` 
 * read - ``connection_handle_read()`` 

 :reqend: 

 - ``http_request_parse()`` 

 :readpost: 

 - ``connection_handle_read_state()`` 
 ** @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()`` 
 ** @connection_handle_read()@ - ``plugins_call_handle_connection_close()`` 
 * reqend - ``connection_close()`` (if not keep-alive) 
 ** @http_request_parse()@ - ``connection_reset()`` 

 :error: 

 - ``plugins_call_handle_request_done()`` 
 * readpost - ``plugins_call_handle_connection_close()`` 
 ** @connection_handle_read_state()@ - ``connection_reset()`` 

 :close: 

 - ``connection_close()`` 

 </pre> 

 <pre> 

 #!graphviz 
 ** @connection_handle_read()@ 
 * digraph state { 
   edge [color=green]; 
   connect -> reqstart -> read -> reqend -> handlereq 
 ** @http_response_prepare()@ 
 * -> respstart 
 ** @connection_handle_write_prepare()@ 
 * -> write 
 ** @connection_handle_write()@ 
 * -> respend -> connect; 
   edge [color=grey]; 
   reqend -> readpost -> handlereq [ label="POST" ]; 
   edge [ color=blue] 
   respend -> reqstart [ label="keep-alive" ]; 
   edge [ color=lightblue] 
   handlereq -> handlereq [ label="sub-request" ]; 
   edge [style=dashed, color=red]; 
   error -> close -> connect; 
   error -> connect; 
   handlereq -> error; 
   read -> error; 
   readpost -> error; 
   write -> error; 
   connect [shape=box]; 
 ** @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()@ 

 !internal-http-states.png! </pre>