state.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ============================
  2. The State Engine of lighttpd
  3. ============================
  4. ------------
  5. Module: core
  6. ------------
  7. :Author: Jan Kneschke
  8. :Date: $Date: 2004/08/01 07:01:29 $
  9. :Revision: $Revision: 1.1 $
  10. :abstract:
  11. This is a short summary of the state-engine which is driving the lighttpd
  12. webserver. It describes the basic concepts and the way the different parts
  13. of the server are connected.
  14. .. meta::
  15. :keywords: lighttpd, state-engine
  16. .. contents:: Table of Contents
  17. Description
  18. ===========
  19. States
  20. ------
  21. The state-engine is currently made of 11 states which are walk-through on
  22. the way each connection. Some of them are specific for a special operation
  23. and some may never be hit at all.
  24. :connect:
  25. waiting for a connection
  26. :reqstart:
  27. init the read-idle timer
  28. :read:
  29. read http-request-header from network
  30. :reqend:
  31. parse request
  32. :readpost:
  33. read http-request-content from network
  34. :handlereq:
  35. handle the request internally (might result in sub-requests)
  36. :respstart:
  37. prepare response header
  38. :write:
  39. write response-header + content to network
  40. :respend:
  41. cleanup environment, log request
  42. :error:
  43. reset connection (incl. close())
  44. :close:
  45. close connection (handle lingering close)
  46. .. image:: state.png
  47. A simple GET request (green path)
  48. ---------------------------------
  49. The connection is idling in the 'connect' state waiting for a connection.
  50. As soon as the connection is set up we init the read-timer in 'reqstart'
  51. and start to read data from the network. As soon as we get the
  52. HTTP-request terminator (CRLFCRLF) we forward the header to the parser.
  53. The parsed request is handled by 'handlereq' and as soon as a decision out
  54. the request is made it is sent to 'respstart' to prepare the
  55. HTTP-response header. In the 'write' state the prepare content is sent out
  56. to the network. When everything is sent 'respend' is entered to log the
  57. request and cleanup the environment. After the close() call the connection
  58. is set back to the 'connect' state again.
  59. Keep-Alive (blue path)
  60. ----------------------
  61. The Keep-Alive handling is implemented by going from the 'respend'
  62. directly to 'reqstart' without the close() and the accept() calls.
  63. POST requests (grey path)
  64. -------------------------
  65. As requests might contain a request-body the state 'readpost' entered as
  66. soon as the header is parsed and we know how much data we expect.
  67. Pipelining
  68. ----------
  69. HTTP/1.1 supportes pipelining (sending multiple requests without waiting
  70. for the response of the first request). This is handled transparently by
  71. the 'read' state.
  72. Unexpected errors (red path)
  73. ----------------------------
  74. For really hard errors we use the 'error' state which resets the
  75. connection and can be call from every state. It is only use if there is no
  76. other way to handle the issue (e.g. client-side close of the connection).
  77. If possible we should use http-status 500 ('internal server error') and
  78. log the issue in the errorlog.
  79. If we have to take care of some data which is coming in after we ran into
  80. the error condition the 'close' state is used the init a half-close and
  81. read all the delay packet from the network.
  82. Sub-Requests (lightblue)
  83. ------------------------
  84. The FastCGI, CGI, ... integration is done by introducing a loop in
  85. 'handlereq' to handle all aspect which are necessary to find out what has
  86. to be sent back to the client.
  87. Functions
  88. =========
  89. Important functions used by the state-engine
  90. :state-engine:
  91. - ``connection_state_machine()``
  92. :connect:
  93. - (nothing)
  94. :reqstart:
  95. - (nothing)
  96. :read:
  97. - ``connection_handle_read_state()``
  98. - ``connection_handle_read()``
  99. :reqend:
  100. - ``http_request_parse()``
  101. :readpost:
  102. - ``connection_handle_read_state()``
  103. - ``connection_handle_read()``
  104. :handlereq:
  105. - ``http_response_prepare()``
  106. :respstart:
  107. - ``connection_handle_write_prepare()``
  108. :write:
  109. - ``connection_handle_write()``
  110. :respend:
  111. - ``plugins_call_handle_request_done()``
  112. - ``plugins_call_handle_connection_close()``
  113. - ``connection_close()`` (if not keep-alive)
  114. - ``connection_reset()``
  115. :error:
  116. - ``plugins_call_handle_request_done()``
  117. - ``plugins_call_handle_connection_close()``
  118. - ``connection_reset()``
  119. :close:
  120. - ``connection_close()``