IHttpClientContext.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace OSHttpServer
  5. {
  6. /// <summary>
  7. /// Contains a connection to a browser/client.
  8. /// </summary>
  9. public interface IHttpClientContext
  10. {
  11. /// <summary>
  12. /// Get SSL commonName of remote peer
  13. /// </summary>
  14. string SSLCommonName { get; }
  15. IPEndPoint LocalIPEndPoint {get; set;}
  16. /// <summary>
  17. /// Using SSL or other encryption method.
  18. /// </summary>
  19. bool IsSecured { get; }
  20. int contextID {get;}
  21. int TimeoutKeepAlive {get; set; }
  22. int MaxRequests{get; set; }
  23. bool CanSend();
  24. bool IsSending();
  25. bool IsClosing {get ;}
  26. /// <summary>
  27. /// Disconnect from client
  28. /// </summary>
  29. /// <param name="error">error to report in the <see cref="Disconnected"/> event.</param>
  30. void Disconnect(SocketError error);
  31. /// <summary>
  32. /// Send a response.
  33. /// </summary>
  34. /// <param name="httpVersion">Either <see cref="HttpHelper.HTTP10"/> or <see cref="HttpHelper.HTTP11"/></param>
  35. /// <param name="statusCode">HTTP status code</param>
  36. /// <param name="reason">reason for the status code.</param>
  37. /// <param name="body">HTML body contents, can be null or empty.</param>
  38. /// <param name="contentType">A content type to return the body as, i.e. 'text/html' or 'text/plain', defaults to 'text/html' if null or empty</param>
  39. /// <exception cref="ArgumentException">If <paramref name="httpVersion"/> is invalid.</exception>
  40. void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body, string contentType);
  41. /// <summary>
  42. /// Send a response.
  43. /// </summary>
  44. /// <param name="httpVersion">Either <see cref="HttpHelper.HTTP10"/> or <see cref="HttpHelper.HTTP11"/></param>
  45. /// <param name="statusCode">HTTP status code</param>
  46. /// <param name="reason">reason for the status code.</param>
  47. void Respond(string httpVersion, HttpStatusCode statusCode, string reason);
  48. /// <summary>
  49. /// send a whole buffer
  50. /// </summary>
  51. /// <param name="buffer">buffer to send</param>
  52. /// <exception cref="ArgumentNullException"></exception>
  53. bool Send(byte[] buffer);
  54. /// <summary>
  55. /// Send data using the stream
  56. /// </summary>
  57. /// <param name="buffer">Contains data to send</param>
  58. /// <param name="offset">Start position in buffer</param>
  59. /// <param name="size">number of bytes to send</param>
  60. /// <exception cref="ArgumentNullException"></exception>
  61. /// <exception cref="ArgumentOutOfRangeException"></exception>
  62. bool Send(byte[] buffer, int offset, int size);
  63. bool SendAsyncStart(byte[] buffer, int offset, int size);
  64. /// <summary>
  65. /// Closes the streams and disposes of the unmanaged resources
  66. /// </summary>
  67. void Close();
  68. /// <summary>
  69. /// The context have been disconnected.
  70. /// </summary>
  71. /// <remarks>
  72. /// Event can be used to clean up a context, or to reuse it.
  73. /// </remarks>
  74. event EventHandler<DisconnectedEventArgs> Disconnected;
  75. /// <summary>
  76. /// A request have been received in the context.
  77. /// </summary>
  78. event EventHandler<RequestEventArgs> RequestReceived;
  79. HTTPNetworkContext GiveMeTheNetworkStreamIKnowWhatImDoing();
  80. void StartSendResponse(HttpResponse response);
  81. void ContinueSendResponse();
  82. void EndSendResponse(uint requestID, ConnectionType connection);
  83. bool TrySendResponse(int limit);
  84. }
  85. public class HTTPNetworkContext
  86. {
  87. public NetworkStream Stream;
  88. public Socket Socket;
  89. }
  90. /// <summary>
  91. /// A <see cref="IHttpClientContext"/> have been disconnected.
  92. /// </summary>
  93. public class DisconnectedEventArgs : EventArgs
  94. {
  95. /// <summary>
  96. /// Gets reason to why client disconnected.
  97. /// </summary>
  98. public SocketError Error { get; private set; }
  99. /// <summary>
  100. /// Initializes a new instance of the <see cref="DisconnectedEventArgs"/> class.
  101. /// </summary>
  102. /// <param name="error">Reason to disconnection.</param>
  103. public DisconnectedEventArgs(SocketError error)
  104. {
  105. Error = error;
  106. }
  107. }
  108. /// <summary>
  109. ///
  110. /// </summary>
  111. public class RequestEventArgs : EventArgs
  112. {
  113. /// <summary>
  114. /// Gets received request.
  115. /// </summary>
  116. public IHttpRequest Request { get; private set; }
  117. /// <summary>
  118. /// Initializes a new instance of the <see cref="RequestEventArgs"/> class.
  119. /// </summary>
  120. /// <param name="request">The request.</param>
  121. public RequestEventArgs(IHttpRequest request)
  122. {
  123. Request = request;
  124. }
  125. }
  126. }