IHttpClientContext.cs 5.0 KB

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