diff --git a/websocket-sharp/Server/HttpRequestEventArgs.cs b/websocket-sharp/Server/HttpRequestEventArgs.cs index 5e5c11cd..89485042 100644 --- a/websocket-sharp/Server/HttpRequestEventArgs.cs +++ b/websocket-sharp/Server/HttpRequestEventArgs.cs @@ -27,6 +27,8 @@ #endregion using System; +using System.IO; +using System.Text; using WebSocketSharp.Net; namespace WebSocketSharp.Server @@ -54,14 +56,16 @@ namespace WebSocketSharp.Server #region Private Fields private HttpListenerContext _context; + private string _rootPath; #endregion #region Internal Constructors - internal HttpRequestEventArgs (HttpListenerContext context) + internal HttpRequestEventArgs (HttpListenerContext context, string rootPath) { _context = context; + _rootPath = rootPath; } #endregion @@ -93,5 +97,81 @@ namespace WebSocketSharp.Server } #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 } } diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index 9fbfaddc..65122956 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -887,7 +887,7 @@ namespace WebSocketSharp.Server : null; if (evt != null) - evt (this, new HttpRequestEventArgs (context)); + evt (this, new HttpRequestEventArgs (context, _rootPath)); else context.Response.StatusCode = (int) HttpStatusCode.NotImplemented; @@ -1354,6 +1354,7 @@ namespace WebSocketSharp.Server /// is an invalid path. /// /// + [Obsolete ("This method will be removed.")] public byte[] GetFile (string path) { if (path == null)