HttpListenerContext.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #region License
  2. /*
  3. * HttpListenerContext.cs
  4. *
  5. * This code is derived from HttpListenerContext.cs (System.Net) of Mono
  6. * (http://www.mono-project.com).
  7. *
  8. * The MIT License
  9. *
  10. * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  11. * Copyright (c) 2012-2022 sta.blockhead
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. #endregion
  32. #region Authors
  33. /*
  34. * Authors:
  35. * - Gonzalo Paniagua Javier <gonzalo@novell.com>
  36. */
  37. #endregion
  38. using System;
  39. using System.Security.Principal;
  40. using System.Text;
  41. using WebSocketSharp.Net.WebSockets;
  42. namespace WebSocketSharp.Net
  43. {
  44. /// <summary>
  45. /// Provides the access to the HTTP request and response objects used by
  46. /// the <see cref="HttpListener"/> class.
  47. /// </summary>
  48. /// <remarks>
  49. /// This class cannot be inherited.
  50. /// </remarks>
  51. public sealed class HttpListenerContext
  52. {
  53. #region Private Fields
  54. private HttpConnection _connection;
  55. private string _errorMessage;
  56. private int _errorStatusCode;
  57. private HttpListener _listener;
  58. private HttpListenerRequest _request;
  59. private HttpListenerResponse _response;
  60. private IPrincipal _user;
  61. private HttpListenerWebSocketContext _websocketContext;
  62. #endregion
  63. #region Internal Constructors
  64. internal HttpListenerContext (HttpConnection connection)
  65. {
  66. _connection = connection;
  67. _errorStatusCode = 400;
  68. _request = new HttpListenerRequest (this);
  69. _response = new HttpListenerResponse (this);
  70. }
  71. #endregion
  72. #region Internal Properties
  73. internal HttpConnection Connection {
  74. get {
  75. return _connection;
  76. }
  77. }
  78. internal string ErrorMessage {
  79. get {
  80. return _errorMessage;
  81. }
  82. set {
  83. _errorMessage = value;
  84. }
  85. }
  86. internal int ErrorStatusCode {
  87. get {
  88. return _errorStatusCode;
  89. }
  90. set {
  91. _errorStatusCode = value;
  92. }
  93. }
  94. internal bool HasErrorMessage {
  95. get {
  96. return _errorMessage != null;
  97. }
  98. }
  99. internal HttpListener Listener {
  100. get {
  101. return _listener;
  102. }
  103. set {
  104. _listener = value;
  105. }
  106. }
  107. #endregion
  108. #region Public Properties
  109. /// <summary>
  110. /// Gets the HTTP request object that represents a client request.
  111. /// </summary>
  112. /// <value>
  113. /// A <see cref="HttpListenerRequest"/> that represents the client request.
  114. /// </value>
  115. public HttpListenerRequest Request {
  116. get {
  117. return _request;
  118. }
  119. }
  120. /// <summary>
  121. /// Gets the HTTP response object used to send a response to the client.
  122. /// </summary>
  123. /// <value>
  124. /// A <see cref="HttpListenerResponse"/> that represents a response to
  125. /// the client request.
  126. /// </value>
  127. public HttpListenerResponse Response {
  128. get {
  129. return _response;
  130. }
  131. }
  132. /// <summary>
  133. /// Gets the client information.
  134. /// </summary>
  135. /// <value>
  136. /// <para>
  137. /// A <see cref="IPrincipal"/> instance that represents identity,
  138. /// authentication, and security roles for the client.
  139. /// </para>
  140. /// <para>
  141. /// <see langword="null"/> if the client is not authenticated.
  142. /// </para>
  143. /// </value>
  144. public IPrincipal User {
  145. get {
  146. return _user;
  147. }
  148. }
  149. #endregion
  150. #region Private Methods
  151. private static string createErrorContent (
  152. int statusCode, string statusDescription, string message
  153. )
  154. {
  155. return message != null && message.Length > 0
  156. ? String.Format (
  157. "<html><body><h1>{0} {1} ({2})</h1></body></html>",
  158. statusCode,
  159. statusDescription,
  160. message
  161. )
  162. : String.Format (
  163. "<html><body><h1>{0} {1}</h1></body></html>",
  164. statusCode,
  165. statusDescription
  166. );
  167. }
  168. #endregion
  169. #region Internal Methods
  170. internal HttpListenerWebSocketContext GetWebSocketContext (string protocol)
  171. {
  172. _websocketContext = new HttpListenerWebSocketContext (this, protocol);
  173. return _websocketContext;
  174. }
  175. internal void SendAuthenticationChallenge (
  176. AuthenticationSchemes scheme, string realm
  177. )
  178. {
  179. _response.StatusCode = 401;
  180. var chal = new AuthenticationChallenge (scheme, realm).ToString ();
  181. _response.Headers.InternalSet ("WWW-Authenticate", chal, true);
  182. _response.Close ();
  183. }
  184. internal void SendError ()
  185. {
  186. try {
  187. _response.StatusCode = _errorStatusCode;
  188. _response.ContentType = "text/html";
  189. var content = createErrorContent (
  190. _errorStatusCode,
  191. _response.StatusDescription,
  192. _errorMessage
  193. );
  194. var enc = Encoding.UTF8;
  195. var entity = enc.GetBytes (content);
  196. _response.ContentEncoding = enc;
  197. _response.ContentLength64 = entity.LongLength;
  198. _response.Close (entity, true);
  199. }
  200. catch {
  201. _connection.Close (true);
  202. }
  203. }
  204. internal void SendError (int statusCode)
  205. {
  206. _errorStatusCode = statusCode;
  207. SendError ();
  208. }
  209. internal void SendError (int statusCode, string message)
  210. {
  211. _errorStatusCode = statusCode;
  212. _errorMessage = message;
  213. SendError ();
  214. }
  215. internal bool SetUser (
  216. AuthenticationSchemes scheme,
  217. string realm,
  218. Func<IIdentity, NetworkCredential> credentialsFinder
  219. )
  220. {
  221. var user = HttpUtility.CreateUser (
  222. _request.Headers["Authorization"],
  223. scheme,
  224. realm,
  225. _request.HttpMethod,
  226. credentialsFinder
  227. );
  228. if (user == null)
  229. return false;
  230. if (!user.Identity.IsAuthenticated)
  231. return false;
  232. _user = user;
  233. return true;
  234. }
  235. internal void Unregister ()
  236. {
  237. if (_listener == null)
  238. return;
  239. _listener.UnregisterContext (this);
  240. }
  241. #endregion
  242. #region Public Methods
  243. /// <summary>
  244. /// Accepts a WebSocket connection.
  245. /// </summary>
  246. /// <returns>
  247. /// A <see cref="HttpListenerWebSocketContext"/> that represents
  248. /// the WebSocket handshake request.
  249. /// </returns>
  250. /// <param name="protocol">
  251. /// <para>
  252. /// A <see cref="string"/> that specifies the name of the subprotocol
  253. /// supported on the WebSocket connection.
  254. /// </para>
  255. /// <para>
  256. /// <see langword="null"/> if not necessary.
  257. /// </para>
  258. /// </param>
  259. /// <exception cref="InvalidOperationException">
  260. /// <para>
  261. /// This method has already been done.
  262. /// </para>
  263. /// <para>
  264. /// -or-
  265. /// </para>
  266. /// <para>
  267. /// The client request is not a WebSocket handshake request.
  268. /// </para>
  269. /// </exception>
  270. /// <exception cref="ArgumentException">
  271. /// <para>
  272. /// <paramref name="protocol"/> is empty.
  273. /// </para>
  274. /// <para>
  275. /// -or-
  276. /// </para>
  277. /// <para>
  278. /// <paramref name="protocol"/> contains an invalid character.
  279. /// </para>
  280. /// </exception>
  281. public HttpListenerWebSocketContext AcceptWebSocket (string protocol)
  282. {
  283. return AcceptWebSocket (protocol, null);
  284. }
  285. /// <summary>
  286. /// Accepts a WebSocket connection with initializing the WebSocket
  287. /// interface.
  288. /// </summary>
  289. /// <returns>
  290. /// A <see cref="HttpListenerWebSocketContext"/> that represents
  291. /// the WebSocket handshake request.
  292. /// </returns>
  293. /// <param name="protocol">
  294. /// <para>
  295. /// A <see cref="string"/> that specifies the name of the subprotocol
  296. /// supported on the WebSocket connection.
  297. /// </para>
  298. /// <para>
  299. /// <see langword="null"/> if not necessary.
  300. /// </para>
  301. /// </param>
  302. /// <param name="initializer">
  303. /// <para>
  304. /// An <see cref="T:System.Action{WebSocket}"/> delegate.
  305. /// </para>
  306. /// <para>
  307. /// It specifies the delegate that invokes the method called when
  308. /// initializing a new WebSocket instance.
  309. /// </para>
  310. /// </param>
  311. /// <exception cref="InvalidOperationException">
  312. /// <para>
  313. /// This method has already been done.
  314. /// </para>
  315. /// <para>
  316. /// -or-
  317. /// </para>
  318. /// <para>
  319. /// The client request is not a WebSocket handshake request.
  320. /// </para>
  321. /// </exception>
  322. /// <exception cref="ArgumentException">
  323. /// <para>
  324. /// <paramref name="protocol"/> is empty.
  325. /// </para>
  326. /// <para>
  327. /// -or-
  328. /// </para>
  329. /// <para>
  330. /// <paramref name="protocol"/> contains an invalid character.
  331. /// </para>
  332. /// <para>
  333. /// -or-
  334. /// </para>
  335. /// <para>
  336. /// <paramref name="initializer"/> caused an exception.
  337. /// </para>
  338. /// </exception>
  339. public HttpListenerWebSocketContext AcceptWebSocket (
  340. string protocol, Action<WebSocket> initializer
  341. )
  342. {
  343. if (_websocketContext != null) {
  344. var msg = "The method has already been done.";
  345. throw new InvalidOperationException (msg);
  346. }
  347. if (!_request.IsWebSocketRequest) {
  348. var msg = "The request is not a WebSocket handshake request.";
  349. throw new InvalidOperationException (msg);
  350. }
  351. if (protocol != null) {
  352. if (protocol.Length == 0) {
  353. var msg = "An empty string.";
  354. throw new ArgumentException (msg, "protocol");
  355. }
  356. if (!protocol.IsToken ()) {
  357. var msg = "It contains an invalid character.";
  358. throw new ArgumentException (msg, "protocol");
  359. }
  360. }
  361. var ret = GetWebSocketContext (protocol);
  362. var ws = ret.WebSocket;
  363. if (initializer != null) {
  364. try {
  365. initializer (ws);
  366. }
  367. catch (Exception ex) {
  368. if (ws.ReadyState == WebSocketState.New)
  369. _websocketContext = null;
  370. var msg = "It caused an exception.";
  371. throw new ArgumentException (msg, "initializer", ex);
  372. }
  373. }
  374. ws.Accept ();
  375. return ret;
  376. }
  377. #endregion
  378. }
  379. }