#region License /* * HttpRequestEventArgs.cs * * The MIT License * * Copyright (c) 2012-2015 sta.blockhead * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #endregion using System; using System.IO; using System.Text; using WebSocketSharp.Net; namespace WebSocketSharp.Server { /// /// Represents the event data for the HTTP request events of /// the . /// /// /// /// An HTTP request event occurs when the /// receives an HTTP request. /// /// /// You should access the property if you would /// like to get the request data sent from a client. /// /// /// And you should access the property if you would /// like to get the response data to return to the client. /// /// public class HttpRequestEventArgs : EventArgs { #region Private Fields private HttpListenerContext _context; private string _rootPath; #endregion #region Internal Constructors internal HttpRequestEventArgs (HttpListenerContext context, string rootPath) { _context = context; _rootPath = rootPath; } #endregion #region Public Properties /// /// Gets the HTTP request data sent from a client. /// /// /// A that represents the request data. /// public HttpListenerRequest Request { get { return _context.Request; } } /// /// Gets the HTTP response data to return to the client. /// /// /// A that represents the response data. /// public HttpListenerResponse Response { get { return _context.Response; } } #endregion #region Private Methods private string createFilePath (string childPath) { childPath = childPath.TrimStart ('/', '\\'); var buff = new StringBuilder (_rootPath, 32); if (_rootPath == "/" || _rootPath == "\\") buff.Append (childPath); else buff.AppendFormat ("/{0}", childPath); return buff.ToString ().Replace ('\\', '/'); } #endregion #region Public Methods /// /// Reads the file with the specified from /// the document folder of the . /// /// /// /// An array of or /// if not found. /// /// /// That array receives the contents of the file. /// /// /// /// A that represents a virtual path to /// the file to find from the document folder. /// /// /// is . /// /// /// /// is an empty string. /// /// /// -or- /// /// /// is an invalid path. /// /// public byte[] ReadFile (string path) { if (path == null) throw new ArgumentNullException ("path"); if (path.Length == 0) throw new ArgumentException ("An empty string.", "path"); if (path.IndexOf (':') > -1) throw new ArgumentException ("It contains ':'.", "path"); if (path.IndexOf ("..") > -1) throw new ArgumentException ("It contains '..'.", "path"); if (path.IndexOf ("//") > -1) throw new ArgumentException ("It contains '//'.", "path"); if (path.IndexOf ("\\\\") > -1) throw new ArgumentException ("It contains '\\\\'.", "path"); path = createFilePath (path); return File.Exists (path) ? File.ReadAllBytes (path) : null; } #endregion } }