PageRenderTime 81ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/websocket-sharp/Net/WebSockets/HttpListenerWebSocketContext.cs

https://gitlab.com/OriumVR/websocket-sharp
C# | 331 lines | 142 code | 37 blank | 152 comment | 3 complexity | 5b1af98a80a5d7bf6fc3b56b17238884 MD5 | raw file
  1. #region License
  2. /*
  3. * HttpListenerWebSocketContext.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012-2015 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.Security.Principal;
  33. namespace WebSocketSharp.Net.WebSockets
  34. {
  35. /// <summary>
  36. /// Provides the properties used to access the information in a WebSocket connection request
  37. /// received by the <see cref="HttpListener"/>.
  38. /// </summary>
  39. public class HttpListenerWebSocketContext : WebSocketContext
  40. {
  41. #region Private Fields
  42. private HttpListenerContext _context;
  43. private WebSocket _websocket;
  44. #endregion
  45. #region Internal Constructors
  46. internal HttpListenerWebSocketContext (HttpListenerContext context, string protocol)
  47. {
  48. _context = context;
  49. _websocket = new WebSocket (this, protocol);
  50. }
  51. #endregion
  52. #region Internal Properties
  53. internal Logger Log {
  54. get {
  55. return _context.Listener.Log;
  56. }
  57. }
  58. internal Stream Stream {
  59. get {
  60. return _context.Connection.Stream;
  61. }
  62. }
  63. #endregion
  64. #region Public Properties
  65. /// <summary>
  66. /// Gets the HTTP cookies included in the request.
  67. /// </summary>
  68. /// <value>
  69. /// A <see cref="WebSocketSharp.Net.CookieCollection"/> that contains the cookies.
  70. /// </value>
  71. public override CookieCollection CookieCollection {
  72. get {
  73. return _context.Request.Cookies;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets the HTTP headers included in the request.
  78. /// </summary>
  79. /// <value>
  80. /// A <see cref="NameValueCollection"/> that contains the headers.
  81. /// </value>
  82. public override NameValueCollection Headers {
  83. get {
  84. return _context.Request.Headers;
  85. }
  86. }
  87. /// <summary>
  88. /// Gets the value of the Host header included in the request.
  89. /// </summary>
  90. /// <value>
  91. /// A <see cref="string"/> that represents the value of the Host header.
  92. /// </value>
  93. public override string Host {
  94. get {
  95. return _context.Request.Headers["Host"];
  96. }
  97. }
  98. /// <summary>
  99. /// Gets a value indicating whether the client is authenticated.
  100. /// </summary>
  101. /// <value>
  102. /// <c>true</c> if the client is authenticated; otherwise, <c>false</c>.
  103. /// </value>
  104. public override bool IsAuthenticated {
  105. get {
  106. return _context.User != null;
  107. }
  108. }
  109. /// <summary>
  110. /// Gets a value indicating whether the client connected from the local computer.
  111. /// </summary>
  112. /// <value>
  113. /// <c>true</c> if the client connected from the local computer; otherwise, <c>false</c>.
  114. /// </value>
  115. public override bool IsLocal {
  116. get {
  117. return _context.Request.IsLocal;
  118. }
  119. }
  120. /// <summary>
  121. /// Gets a value indicating whether the WebSocket connection is secured.
  122. /// </summary>
  123. /// <value>
  124. /// <c>true</c> if the connection is secured; otherwise, <c>false</c>.
  125. /// </value>
  126. public override bool IsSecureConnection {
  127. get {
  128. return _context.Connection.IsSecure;
  129. }
  130. }
  131. /// <summary>
  132. /// Gets a value indicating whether the request is a WebSocket connection request.
  133. /// </summary>
  134. /// <value>
  135. /// <c>true</c> if the request is a WebSocket connection request; otherwise, <c>false</c>.
  136. /// </value>
  137. public override bool IsWebSocketRequest {
  138. get {
  139. return _context.Request.IsWebSocketRequest;
  140. }
  141. }
  142. /// <summary>
  143. /// Gets the value of the Origin header included in the request.
  144. /// </summary>
  145. /// <value>
  146. /// A <see cref="string"/> that represents the value of the Origin header.
  147. /// </value>
  148. public override string Origin {
  149. get {
  150. return _context.Request.Headers["Origin"];
  151. }
  152. }
  153. /// <summary>
  154. /// Gets the query string included in the request.
  155. /// </summary>
  156. /// <value>
  157. /// A <see cref="NameValueCollection"/> that contains the query string parameters.
  158. /// </value>
  159. public override NameValueCollection QueryString {
  160. get {
  161. return _context.Request.QueryString;
  162. }
  163. }
  164. /// <summary>
  165. /// Gets the URI requested by the client.
  166. /// </summary>
  167. /// <value>
  168. /// A <see cref="Uri"/> that represents the requested URI.
  169. /// </value>
  170. public override Uri RequestUri {
  171. get {
  172. return _context.Request.Url;
  173. }
  174. }
  175. /// <summary>
  176. /// Gets the value of the Sec-WebSocket-Key header included in the request.
  177. /// </summary>
  178. /// <remarks>
  179. /// This property provides a part of the information used by the server to prove that it
  180. /// received a valid WebSocket connection request.
  181. /// </remarks>
  182. /// <value>
  183. /// A <see cref="string"/> that represents the value of the Sec-WebSocket-Key header.
  184. /// </value>
  185. public override string SecWebSocketKey {
  186. get {
  187. return _context.Request.Headers["Sec-WebSocket-Key"];
  188. }
  189. }
  190. /// <summary>
  191. /// Gets the values of the Sec-WebSocket-Protocol header included in the request.
  192. /// </summary>
  193. /// <remarks>
  194. /// This property represents the subprotocols requested by the client.
  195. /// </remarks>
  196. /// <value>
  197. /// An <see cref="T:System.Collections.Generic.IEnumerable{string}"/> instance that provides
  198. /// an enumerator which supports the iteration over the values of the Sec-WebSocket-Protocol
  199. /// header.
  200. /// </value>
  201. public override IEnumerable<string> SecWebSocketProtocols {
  202. get {
  203. var protocols = _context.Request.Headers["Sec-WebSocket-Protocol"];
  204. if (protocols != null)
  205. foreach (var protocol in protocols.Split (','))
  206. yield return protocol.Trim ();
  207. }
  208. }
  209. /// <summary>
  210. /// Gets the value of the Sec-WebSocket-Version header included in the request.
  211. /// </summary>
  212. /// <remarks>
  213. /// This property represents the WebSocket protocol version.
  214. /// </remarks>
  215. /// <value>
  216. /// A <see cref="string"/> that represents the value of the Sec-WebSocket-Version header.
  217. /// </value>
  218. public override string SecWebSocketVersion {
  219. get {
  220. return _context.Request.Headers["Sec-WebSocket-Version"];
  221. }
  222. }
  223. /// <summary>
  224. /// Gets the server endpoint as an IP address and a port number.
  225. /// </summary>
  226. /// <value>
  227. /// A <see cref="System.Net.IPEndPoint"/> that represents the server endpoint.
  228. /// </value>
  229. public override System.Net.IPEndPoint ServerEndPoint {
  230. get {
  231. return _context.Connection.LocalEndPoint;
  232. }
  233. }
  234. /// <summary>
  235. /// Gets the client information (identity, authentication, and security roles).
  236. /// </summary>
  237. /// <value>
  238. /// A <see cref="IPrincipal"/> instance that represents the client information.
  239. /// </value>
  240. public override IPrincipal User {
  241. get {
  242. return _context.User;
  243. }
  244. }
  245. /// <summary>
  246. /// Gets the client endpoint as an IP address and a port number.
  247. /// </summary>
  248. /// <value>
  249. /// A <see cref="System.Net.IPEndPoint"/> that represents the client endpoint.
  250. /// </value>
  251. public override System.Net.IPEndPoint UserEndPoint {
  252. get {
  253. return _context.Connection.RemoteEndPoint;
  254. }
  255. }
  256. /// <summary>
  257. /// Gets the <see cref="WebSocketSharp.WebSocket"/> instance used for two-way communication
  258. /// between client and server.
  259. /// </summary>
  260. /// <value>
  261. /// A <see cref="WebSocketSharp.WebSocket"/>.
  262. /// </value>
  263. public override WebSocket WebSocket {
  264. get {
  265. return _websocket;
  266. }
  267. }
  268. #endregion
  269. #region Internal Methods
  270. internal void Close ()
  271. {
  272. _context.Connection.Close (true);
  273. }
  274. internal void Close (HttpStatusCode code)
  275. {
  276. _context.Response.Close (code);
  277. }
  278. #endregion
  279. #region Public Methods
  280. /// <summary>
  281. /// Returns a <see cref="string"/> that represents the current
  282. /// <see cref="HttpListenerWebSocketContext"/>.
  283. /// </summary>
  284. /// <returns>
  285. /// A <see cref="string"/> that represents the current
  286. /// <see cref="HttpListenerWebSocketContext"/>.
  287. /// </returns>
  288. public override string ToString ()
  289. {
  290. return _context.Request.ToString ();
  291. }
  292. #endregion
  293. }
  294. }