#region License
/*
* HttpRequestEventArgs.cs
*
* The MIT License
*
* Copyright (c) 2012-2021 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.Security.Principal;
using System.Text;
using WebSocketSharp.Net;
namespace WebSocketSharp.Server
{
///
/// Represents the event data for the HTTP request events of the
/// class.
///
///
///
/// An HTTP request event occurs when the
/// instance 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 _docRootPath;
#endregion
#region Internal Constructors
internal HttpRequestEventArgs (
HttpListenerContext context, string documentRootPath
)
{
_context = context;
_docRootPath = documentRootPath;
}
#endregion
#region Public Properties
///
/// Gets the request data sent from a client.
///
///
/// A that provides the methods and
/// properties for the request data.
///
public HttpListenerRequest Request {
get {
return _context.Request;
}
}
///
/// Gets the response data to return to the client.
///
///
/// A that provides the methods and
/// properties for the response data.
///
public HttpListenerResponse Response {
get {
return _context.Response;
}
}
///
/// Gets the information for the client.
///
///
///
/// A instance or
/// if not authenticated.
///
///
/// That instance describes the identity, authentication scheme,
/// and security roles for the client.
///
///
public IPrincipal User {
get {
return _context.User;
}
}
#endregion
#region Private Methods
private string createFilePath (string childPath)
{
childPath = childPath.TrimStart ('/', '\\');
return new StringBuilder (_docRootPath, 32)
.AppendFormat ("/{0}", childPath)
.ToString ()
.Replace ('\\', '/');
}
private static bool tryReadFile (string path, out byte[] contents)
{
contents = null;
if (!File.Exists (path))
return false;
try {
contents = File.ReadAllBytes (path);
}
catch {
return false;
}
return true;
}
#endregion
#region Public Methods
///
/// Reads the specified file from the document folder of the
/// class.
///
///
///
/// An array of or
/// if it fails.
///
///
/// That array receives the contents of the file.
///
///
///
/// A that specifies a virtual path to
/// find the file from the document folder.
///
///
/// is .
///
///
///
/// is an empty string.
///
///
/// -or-
///
///
/// contains "..".
///
///
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");
path = createFilePath (path);
byte[] contents;
tryReadFile (path, out contents);
return contents;
}
///
/// Tries to read the specified file from the document folder of
/// the class.
///
///
/// true if it succeeds to read; otherwise, false.
///
///
/// A that specifies a virtual path to find
/// the file from the document folder.
///
///
///
/// When this method returns, an array of or
/// if it fails.
///
///
/// That array receives the contents of the file.
///
///
///
/// is .
///
///
///
/// is an empty string.
///
///
/// -or-
///
///
/// contains "..".
///
///
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");
path = createFilePath (path);
return tryReadFile (path, out contents);
}
#endregion
}
}