IHttpRequestParser.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using OSHttpServer.Exceptions;
  3. using OSHttpServer.Parser;
  4. namespace OSHttpServer
  5. {
  6. /// <summary>
  7. /// Event driven parser used to parse incoming HTTP requests.
  8. /// </summary>
  9. /// <remarks>
  10. /// The parser supports partial messages and keeps the states between
  11. /// each parsed buffer. It's therefore important that the parser gets
  12. /// <see cref="Clear"/>ed if a client disconnects.
  13. /// </remarks>
  14. public interface IHttpRequestParser
  15. {
  16. /// <summary>
  17. /// Current state in parser.
  18. /// </summary>
  19. RequestParserState CurrentState { get; }
  20. /// <summary>
  21. /// Parse partial or complete message.
  22. /// </summary>
  23. /// <param name="buffer">buffer containing incoming bytes</param>
  24. /// <param name="offset">where in buffer that parsing should start</param>
  25. /// <param name="count">number of bytes to parse</param>
  26. /// <returns>Unparsed bytes left in buffer.</returns>
  27. /// <exception cref="BadRequestException"><c>BadRequestException</c>.</exception>
  28. int Parse(byte[] buffer, int offset, int count);
  29. /// <summary>
  30. /// A request have been successfully parsed.
  31. /// </summary>
  32. event EventHandler RequestCompleted;
  33. /// <summary>
  34. /// More body bytes have been received.
  35. /// </summary>
  36. event EventHandler<BodyEventArgs> BodyBytesReceived;
  37. /// <summary>
  38. /// Request line have been received.
  39. /// </summary>
  40. event EventHandler<RequestLineEventArgs> RequestLineReceived;
  41. /// <summary>
  42. /// A header have been received.
  43. /// </summary>
  44. event EventHandler<HeaderEventArgs> HeaderReceived;
  45. /// <summary>
  46. /// Clear parser state.
  47. /// </summary>
  48. void Clear();
  49. /// <summary>
  50. /// Gets or sets the log writer.
  51. /// </summary>
  52. ILogWriter LogWriter { get; set; }
  53. }
  54. /// <summary>
  55. /// Current state in the parsing.
  56. /// </summary>
  57. public enum RequestParserState
  58. {
  59. /// <summary>
  60. /// Should parse the request line
  61. /// </summary>
  62. FirstLine,
  63. /// <summary>
  64. /// Searching for a complete header name
  65. /// </summary>
  66. HeaderName,
  67. /// <summary>
  68. /// Searching for colon after header name (ignoring white spaces)
  69. /// </summary>
  70. AfterName,
  71. /// <summary>
  72. /// Searching for start of header value (ignoring white spaces)
  73. /// </summary>
  74. Between,
  75. /// <summary>
  76. /// Searching for a complete header value (can span over multiple lines, as long as they are prefixed with one/more whitespaces)
  77. /// </summary>
  78. HeaderValue,
  79. /// <summary>
  80. /// Adding bytes to body
  81. /// </summary>
  82. Body
  83. }
  84. }