diff --git a/websocket-sharp/Server/HttpRequestEventArgs.cs b/websocket-sharp/Server/HttpRequestEventArgs.cs index bcf48dcc..439432fb 100644 --- a/websocket-sharp/Server/HttpRequestEventArgs.cs +++ b/websocket-sharp/Server/HttpRequestEventArgs.cs @@ -189,6 +189,64 @@ namespace WebSocketSharp.Server return File.Exists (path) ? File.ReadAllBytes (path) : null; } + /// + /// Tries to read a file with the specified + /// from the document folder of the . + /// + /// + /// true if the file could successfully be read; + /// otherwise, false. + /// + /// + /// A that represents a virtual path to + /// the file to read. + /// + /// + /// + /// When this method returns, an array of or + /// if the file could not be read. + /// + /// + /// That array receives the contents of the file. + /// + /// + /// + /// is . + /// + /// + /// + /// is an empty string. + /// + /// + /// -or- + /// + /// + /// is an invalid path. + /// + /// + public bool TryReadFile (string path, out byte[] contents) + { + 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"); + + return tryReadFile (createFilePath (path), out contents); + } + #endregion } }