[Modify] It will be removed

This commit is contained in:
sta 2017-06-19 15:29:37 +09:00
parent 4b5da9b3ff
commit 3984739b5d
2 changed files with 83 additions and 2 deletions

View File

@ -27,6 +27,8 @@
#endregion #endregion
using System; using System;
using System.IO;
using System.Text;
using WebSocketSharp.Net; using WebSocketSharp.Net;
namespace WebSocketSharp.Server namespace WebSocketSharp.Server
@ -54,14 +56,16 @@ namespace WebSocketSharp.Server
#region Private Fields #region Private Fields
private HttpListenerContext _context; private HttpListenerContext _context;
private string _rootPath;
#endregion #endregion
#region Internal Constructors #region Internal Constructors
internal HttpRequestEventArgs (HttpListenerContext context) internal HttpRequestEventArgs (HttpListenerContext context, string rootPath)
{ {
_context = context; _context = context;
_rootPath = rootPath;
} }
#endregion #endregion
@ -93,5 +97,81 @@ namespace WebSocketSharp.Server
} }
#endregion #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
/// <summary>
/// Reads the file with the specified <paramref name="path"/> from
/// the document folder of the <see cref="HttpServer"/>.
/// </summary>
/// <returns>
/// <para>
/// An array of <see cref="byte"/> or <see langword="null"/>
/// if not found.
/// </para>
/// <para>
/// That array receives the contents of the file.
/// </para>
/// </returns>
/// <param name="path">
/// A <see cref="string"/> that represents a virtual path to
/// the file to find from the document folder.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="path"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <para>
/// <paramref name="path"/> is an empty string.
/// </para>
/// <para>
/// -or-
/// </para>
/// <para>
/// <paramref name="path"/> is an invalid path.
/// </para>
/// </exception>
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
} }
} }

View File

@ -887,7 +887,7 @@ namespace WebSocketSharp.Server
: null; : null;
if (evt != null) if (evt != null)
evt (this, new HttpRequestEventArgs (context)); evt (this, new HttpRequestEventArgs (context, _rootPath));
else else
context.Response.StatusCode = (int) HttpStatusCode.NotImplemented; context.Response.StatusCode = (int) HttpStatusCode.NotImplemented;
@ -1354,6 +1354,7 @@ namespace WebSocketSharp.Server
/// <paramref name="path"/> is an invalid path. /// <paramref name="path"/> is an invalid path.
/// </para> /// </para>
/// </exception> /// </exception>
[Obsolete ("This method will be removed.")]
public byte[] GetFile (string path) public byte[] GetFile (string path)
{ {
if (path == null) if (path == null)