using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace OSHttpServer
{
///
/// Contains a connection to a browser/client.
///
public interface IHttpClientContext
{
///
/// Get SSL commonName of remote peer
///
string SSLCommonName { get; }
IPEndPoint LocalIPEndPoint {get; set;}
///
/// Using SSL or other encryption method.
///
bool IsSecured { get; }
int contextID {get;}
int TimeoutKeepAlive {get; set; }
int MaxRequests{get; set; }
bool CanSend();
bool IsSending();
bool IsClosing {get ;}
///
/// Disconnect from client
///
/// error to report in the event.
void Disconnect(SocketError error);
///
/// Send a response.
///
/// Either or
/// HTTP status code
/// reason for the status code.
/// HTML body contents, can be null or empty.
/// A content type to return the body as, i.e. 'text/html' or 'text/plain', defaults to 'text/html' if null or empty
/// If is invalid.
void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body, string contentType);
///
/// Send a response.
///
/// Either or
/// HTTP status code
/// reason for the status code.
void Respond(string httpVersion, HttpStatusCode statusCode, string reason);
///
/// send a whole buffer
///
/// buffer to send
///
bool Send(byte[] buffer);
///
/// Send data using the stream
///
/// Contains data to send
/// Start position in buffer
/// number of bytes to send
///
///
bool Send(byte[] buffer, int offset, int size);
Task SendAsync(byte[] buffer, int offset, int size);
///
/// Closes the streams and disposes of the unmanaged resources
///
void Close();
///
/// The context have been disconnected.
///
///
/// Event can be used to clean up a context, or to reuse it.
///
event EventHandler Disconnected;
///
/// A request have been received in the context.
///
event EventHandler RequestReceived;
HTTPNetworkContext GiveMeTheNetworkStreamIKnowWhatImDoing();
void StartSendResponse(HttpResponse response);
void ContinueSendResponse(bool notThrottled);
void EndSendResponse(uint requestID, ConnectionType connection);
bool TrySendResponse(int limit);
}
public class HTTPNetworkContext
{
public NetworkStream Stream;
public Socket Socket;
}
///
/// A have been disconnected.
///
public class DisconnectedEventArgs : EventArgs
{
///
/// Gets reason to why client disconnected.
///
public SocketError Error { get; private set; }
///
/// Initializes a new instance of the class.
///
/// Reason to disconnection.
public DisconnectedEventArgs(SocketError error)
{
Error = error;
}
}
///
///
///
public class RequestEventArgs : EventArgs
{
///
/// Gets received request.
///
public IHttpRequest Request { get; private set; }
///
/// Initializes a new instance of the class.
///
/// The request.
public RequestEventArgs(IHttpRequest request)
{
Request = request;
}
}
}