IHttpClientContext.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /// <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. /// <summary>
  26. /// Disconnect from client
  27. /// </summary>
  28. /// <param name="error">error to report in the <see cref="Disconnected"/> event.</param>
  29. void Disconnect(SocketError error);
  30. /// <summary>
  31. /// Send a response.
  32. /// </summary>
  33. /// <param name="httpVersion">Either <see cref="HttpHelper.HTTP10"/> or <see cref="HttpHelper.HTTP11"/></param>
  34. /// <param name="statusCode">HTTP status code</param>
  35. /// <param name="reason">reason for the status code.</param>
  36. /// <param name="body">HTML body contents, can be null or empty.</param>
  37. /// <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>
  38. /// <exception cref="ArgumentException">If <paramref name="httpVersion"/> is invalid.</exception>
  39. void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body, string contentType);
  40. /// <summary>
  41. /// Send a response.
  42. /// </summary>
  43. /// <param name="httpVersion">Either <see cref="HttpHelper.HTTP10"/> or <see cref="HttpHelper.HTTP11"/></param>
  44. /// <param name="statusCode">HTTP status code</param>
  45. /// <param name="reason">reason for the status code.</param>
  46. void Respond(string httpVersion, HttpStatusCode statusCode, string reason);
  47. /// <summary>
  48. /// send a whole buffer
  49. /// </summary>
  50. /// <param name="buffer">buffer to send</param>
  51. /// <exception cref="ArgumentNullException"></exception>
  52. bool Send(byte[] buffer);
  53. /// <summary>
  54. /// Send data using the stream
  55. /// </summary>
  56. /// <param name="buffer">Contains data to send</param>
  57. /// <param name="offset">Start position in buffer</param>
  58. /// <param name="size">number of bytes to send</param>
  59. /// <exception cref="ArgumentNullException"></exception>
  60. /// <exception cref="ArgumentOutOfRangeException"></exception>
  61. bool Send(byte[] buffer, int offset, int size);
  62. Task<bool> SendAsync(byte[] buffer, int offset, int size);
  63. /// <summary>
  64. /// Closes the streams and disposes of the unmanaged resources
  65. /// </summary>
  66. void Close();
  67. /// <summary>
  68. /// The context have been disconnected.
  69. /// </summary>
  70. /// <remarks>
  71. /// Event can be used to clean up a context, or to reuse it.
  72. /// </remarks>
  73. event EventHandler<DisconnectedEventArgs> Disconnected;
  74. /// <summary>
  75. /// A request have been received in the context.
  76. /// </summary>
  77. event EventHandler<RequestEventArgs> RequestReceived;
  78. HTTPNetworkContext GiveMeTheNetworkStreamIKnowWhatImDoing();
  79. void StartSendResponse(HttpResponse response);
  80. void ContinueSendResponse();
  81. void ReqResponseAboutToSend(uint requestID);
  82. void ReqResponseSent(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. }