HttpRequestEventArgs.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #region License
  2. /*
  3. * HttpRequestEventArgs.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012-2021 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.IO;
  30. using System.Security.Principal;
  31. using System.Text;
  32. using WebSocketSharp.Net;
  33. namespace WebSocketSharp.Server
  34. {
  35. /// <summary>
  36. /// Represents the event data for the HTTP request events of the
  37. /// <see cref="HttpServer"/> class.
  38. /// </summary>
  39. /// <remarks>
  40. /// <para>
  41. /// An HTTP request event occurs when the <see cref="HttpServer"/>
  42. /// instance receives an HTTP request.
  43. /// </para>
  44. /// <para>
  45. /// You should access the <see cref="Request"/> property if you would
  46. /// like to get the request data sent from a client.
  47. /// </para>
  48. /// <para>
  49. /// And you should access the <see cref="Response"/> property if you
  50. /// would like to get the response data to return to the client.
  51. /// </para>
  52. /// </remarks>
  53. public class HttpRequestEventArgs : EventArgs
  54. {
  55. #region Private Fields
  56. private HttpListenerContext _context;
  57. private string _docRootPath;
  58. #endregion
  59. #region Internal Constructors
  60. internal HttpRequestEventArgs (
  61. HttpListenerContext context, string documentRootPath
  62. )
  63. {
  64. _context = context;
  65. _docRootPath = documentRootPath;
  66. }
  67. #endregion
  68. #region Public Properties
  69. /// <summary>
  70. /// Gets the request data sent from a client.
  71. /// </summary>
  72. /// <value>
  73. /// A <see cref="HttpListenerRequest"/> that provides the methods and
  74. /// properties for the request data.
  75. /// </value>
  76. public HttpListenerRequest Request {
  77. get {
  78. return _context.Request;
  79. }
  80. }
  81. /// <summary>
  82. /// Gets the response data to return to the client.
  83. /// </summary>
  84. /// <value>
  85. /// A <see cref="HttpListenerResponse"/> that provides the methods and
  86. /// properties for the response data.
  87. /// </value>
  88. public HttpListenerResponse Response {
  89. get {
  90. return _context.Response;
  91. }
  92. }
  93. /// <summary>
  94. /// Gets the information for the client.
  95. /// </summary>
  96. /// <value>
  97. /// <para>
  98. /// A <see cref="IPrincipal"/> instance or <see langword="null"/>
  99. /// if not authenticated.
  100. /// </para>
  101. /// <para>
  102. /// That instance describes the identity, authentication scheme,
  103. /// and security roles for the client.
  104. /// </para>
  105. /// </value>
  106. public IPrincipal User {
  107. get {
  108. return _context.User;
  109. }
  110. }
  111. #endregion
  112. #region Private Methods
  113. private string createFilePath (string childPath)
  114. {
  115. childPath = childPath.TrimStart ('/', '\\');
  116. return new StringBuilder (_docRootPath, 32)
  117. .AppendFormat ("/{0}", childPath)
  118. .ToString ()
  119. .Replace ('\\', '/');
  120. }
  121. private static bool tryReadFile (string path, out byte[] contents)
  122. {
  123. contents = null;
  124. if (!File.Exists (path))
  125. return false;
  126. try {
  127. contents = File.ReadAllBytes (path);
  128. }
  129. catch {
  130. return false;
  131. }
  132. return true;
  133. }
  134. #endregion
  135. #region Public Methods
  136. /// <summary>
  137. /// Reads the specified file from the document folder of the
  138. /// <see cref="HttpServer"/> class.
  139. /// </summary>
  140. /// <returns>
  141. /// <para>
  142. /// An array of <see cref="byte"/> or <see langword="null"/>
  143. /// if it fails.
  144. /// </para>
  145. /// <para>
  146. /// That array receives the contents of the file.
  147. /// </para>
  148. /// </returns>
  149. /// <param name="path">
  150. /// A <see cref="string"/> that specifies a virtual path to
  151. /// find the file from the document folder.
  152. /// </param>
  153. /// <exception cref="ArgumentNullException">
  154. /// <paramref name="path"/> is <see langword="null"/>.
  155. /// </exception>
  156. /// <exception cref="ArgumentException">
  157. /// <para>
  158. /// <paramref name="path"/> is an empty string.
  159. /// </para>
  160. /// <para>
  161. /// -or-
  162. /// </para>
  163. /// <para>
  164. /// <paramref name="path"/> contains "..".
  165. /// </para>
  166. /// </exception>
  167. public byte[] ReadFile (string path)
  168. {
  169. if (path == null)
  170. throw new ArgumentNullException ("path");
  171. if (path.Length == 0)
  172. throw new ArgumentException ("An empty string.", "path");
  173. if (path.IndexOf ("..") > -1)
  174. throw new ArgumentException ("It contains '..'.", "path");
  175. path = createFilePath (path);
  176. byte[] contents;
  177. tryReadFile (path, out contents);
  178. return contents;
  179. }
  180. /// <summary>
  181. /// Tries to read the specified file from the document folder of
  182. /// the <see cref="HttpServer"/> class.
  183. /// </summary>
  184. /// <returns>
  185. /// <c>true</c> if it succeeds to read; otherwise, <c>false</c>.
  186. /// </returns>
  187. /// <param name="path">
  188. /// A <see cref="string"/> that specifies a virtual path to find
  189. /// the file from the document folder.
  190. /// </param>
  191. /// <param name="contents">
  192. /// <para>
  193. /// When this method returns, an array of <see cref="byte"/> or
  194. /// <see langword="null"/> if it fails.
  195. /// </para>
  196. /// <para>
  197. /// That array receives the contents of the file.
  198. /// </para>
  199. /// </param>
  200. /// <exception cref="ArgumentNullException">
  201. /// <paramref name="path"/> is <see langword="null"/>.
  202. /// </exception>
  203. /// <exception cref="ArgumentException">
  204. /// <para>
  205. /// <paramref name="path"/> is an empty string.
  206. /// </para>
  207. /// <para>
  208. /// -or-
  209. /// </para>
  210. /// <para>
  211. /// <paramref name="path"/> contains "..".
  212. /// </para>
  213. /// </exception>
  214. public bool TryReadFile (string path, out byte[] contents)
  215. {
  216. if (path == null)
  217. throw new ArgumentNullException ("path");
  218. if (path.Length == 0)
  219. throw new ArgumentException ("An empty string.", "path");
  220. if (path.IndexOf ("..") > -1)
  221. throw new ArgumentException ("It contains '..'.", "path");
  222. path = createFilePath (path);
  223. return tryReadFile (path, out contents);
  224. }
  225. #endregion
  226. }
  227. }