using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Net; using System.Security.Principal; using MediaBrowser.Model.Cryptography; using MediaBrowser.Model.IO; using MediaBrowser.Model.Net; using MediaBrowser.Model.Services; using SocketHttpListener.Primitives; namespace SocketHttpListener.Net.WebSockets { /// /// Provides the properties used to access the information in a WebSocket connection request /// received by the . /// /// /// public class HttpListenerWebSocketContext : WebSocketContext { #region Private Fields private HttpListenerContext _context; private WebSocket _websocket; #endregion #region Internal Constructors internal HttpListenerWebSocketContext( HttpListenerContext context, string protocol, ICryptoProvider cryptoProvider, IMemoryStreamFactory memoryStreamFactory) { _context = context; _websocket = new WebSocket(this, protocol, cryptoProvider, memoryStreamFactory); } #endregion #region Internal Properties internal Stream Stream { get { return _context.Connection.Stream; } } #endregion #region Public Properties /// /// Gets the HTTP cookies included in the request. /// /// /// A that contains the cookies. /// public override CookieCollection CookieCollection { get { return _context.Request.Cookies; } } /// /// Gets the HTTP headers included in the request. /// /// /// A that contains the headers. /// public override QueryParamCollection Headers { get { return _context.Request.Headers; } } /// /// Gets the value of the Host header included in the request. /// /// /// A that represents the value of the Host header. /// public override string Host { get { return _context.Request.Headers["Host"]; } } /// /// Gets a value indicating whether the client is authenticated. /// /// /// true if the client is authenticated; otherwise, false. /// public override bool IsAuthenticated { get { return _context.Request.IsAuthenticated; } } /// /// Gets a value indicating whether the client connected from the local computer. /// /// /// true if the client connected from the local computer; otherwise, false. /// public override bool IsLocal { get { return _context.Request.IsLocal; } } /// /// Gets a value indicating whether the WebSocket connection is secured. /// /// /// true if the connection is secured; otherwise, false. /// public override bool IsSecureConnection { get { return _context.Connection.IsSecure; } } /// /// Gets a value indicating whether the request is a WebSocket connection request. /// /// /// true if the request is a WebSocket connection request; otherwise, false. /// public override bool IsWebSocketRequest { get { return _context.Request.IsWebSocketRequest; } } /// /// Gets the value of the Origin header included in the request. /// /// /// A that represents the value of the Origin header. /// public override string Origin { get { return _context.Request.Headers["Origin"]; } } /// /// Gets the query string included in the request. /// /// /// A that contains the query string parameters. /// public override QueryParamCollection QueryString { get { return _context.Request.QueryString; } } /// /// Gets the URI requested by the client. /// /// /// A that represents the requested URI. /// public override Uri RequestUri { get { return _context.Request.Url; } } /// /// Gets the value of the Sec-WebSocket-Key header included in the request. /// /// /// This property provides a part of the information used by the server to prove that it /// received a valid WebSocket connection request. /// /// /// A that represents the value of the Sec-WebSocket-Key header. /// public override string SecWebSocketKey { get { return _context.Request.Headers["Sec-WebSocket-Key"]; } } /// /// Gets the values of the Sec-WebSocket-Protocol header included in the request. /// /// /// This property represents the subprotocols requested by the client. /// /// /// An instance that provides /// an enumerator which supports the iteration over the values of the Sec-WebSocket-Protocol /// header. /// public override IEnumerable SecWebSocketProtocols { get { var protocols = _context.Request.Headers["Sec-WebSocket-Protocol"]; if (protocols != null) foreach (var protocol in protocols.Split(',')) yield return protocol.Trim(); } } /// /// Gets the value of the Sec-WebSocket-Version header included in the request. /// /// /// This property represents the WebSocket protocol version. /// /// /// A that represents the value of the Sec-WebSocket-Version header. /// public override string SecWebSocketVersion { get { return _context.Request.Headers["Sec-WebSocket-Version"]; } } /// /// Gets the server endpoint as an IP address and a port number. /// /// /// public override IPEndPoint ServerEndPoint { get { return _context.Connection.LocalEndPoint; } } /// /// Gets the client information (identity, authentication, and security roles). /// /// /// A that represents the client information. /// public override IPrincipal User { get { return _context.User; } } /// /// Gets the client endpoint as an IP address and a port number. /// /// /// public override IPEndPoint UserEndPoint { get { return _context.Connection.RemoteEndPoint; } } /// /// Gets the instance used for two-way communication /// between client and server. /// /// /// A . /// public override WebSocket WebSocket { get { return _websocket; } } #endregion #region Internal Methods internal void Close() { try { _context.Connection.Close(true); } catch { // catch errors sending the closing handshake } } internal void Close(HttpStatusCode code) { _context.Response.StatusCode = (int)code; _context.Response.OutputStream.Dispose(); } #endregion #region Public Methods /// /// Returns a that represents the current /// . /// /// /// A that represents the current /// . /// public override string ToString() { return _context.Request.ToString(); } #endregion } }