websocket-sharp/websocket-sharp/Server/HttpServer.cs

454 lines
12 KiB
C#
Raw Normal View History

2012-09-10 00:36:22 +08:00
#region MIT License
2013-01-11 19:32:38 +08:00
/*
2012-09-10 00:36:22 +08:00
* HttpServer.cs
*
* The MIT License
*
* Copyright (c) 2012-2013 sta.blockhead
2012-09-10 00:36:22 +08:00
*
* 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.Collections.Generic;
2012-09-10 00:36:22 +08:00
using System.Configuration;
using System.Diagnostics;
2012-09-10 00:36:22 +08:00
using System.IO;
using System.Threading;
using WebSocketSharp.Net;
namespace WebSocketSharp.Server {
/// <summary>
/// Provides the functions of a simple HTTP server that allows to accept the WebSocket connection requests.
/// </summary>
/// <remarks>
/// <para>
/// The HttpServer class provides the multi WebSocket service.
/// </para>
/// <para>
/// <para>
/// The HttpServer class needs the application configuration file to configure the server root path.
/// </para>
/// <code lang="xml">
/// &lt;?xml version="1.0" encoding="utf-8"?&gt;
/// &lt;configuration&gt;
/// &lt;appSettings&gt;
/// &lt;add key="RootPath" value="./Public" /&gt;
/// &lt;/appSettings&gt;
/// &lt;/configuration&gt;
/// </code>
/// </para>
/// </remarks>
public class HttpServer {
2012-09-10 00:36:22 +08:00
#region Fields
private bool _isWindows;
private HttpListener _listener;
private int _port;
private Thread _receiveRequestThread;
private string _rootPath;
private ServiceHostManager _svcHosts;
2012-09-10 00:36:22 +08:00
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="HttpServer"/> class that listens for incoming requests
/// on port 80.
/// </summary>
2012-09-10 00:36:22 +08:00
public HttpServer()
: this(80)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpServer"/> class that listens for incoming requests
/// on the specified <paramref name="port"/>.
/// </summary>
/// <param name="port">
/// An <see cref="int"/> that contains a port number.
/// </param>
2012-09-10 00:36:22 +08:00
public HttpServer(int port)
{
_port = port;
2012-09-13 10:18:52 +08:00
init();
2012-09-10 00:36:22 +08:00
}
#endregion
2012-10-26 13:58:50 +08:00
#region Properties
2012-09-10 00:36:22 +08:00
/// <summary>
/// Gets the port on which to listen for incoming requests.
/// </summary>
/// <value>
/// An <see cref="int"/> that contains a port number.
/// </value>
2012-09-10 00:36:22 +08:00
public int Port {
get { return _port; }
}
/// <summary>
/// Gets the collection of paths associated with the every WebSocket services that the server provides.
/// </summary>
/// <value>
/// An IEnumerable&lt;string&gt; that contains the collection of paths.
/// </value>
public IEnumerable<string> ServicePaths {
get {
return _svcHosts.Paths;
}
}
/// <summary>
/// Gets or sets a value indicating whether the server cleans up the inactive WebSocket service instances periodically.
/// </summary>
/// <value>
/// <c>true</c> if the server cleans up the inactive WebSocket service instances every 60 seconds;
/// otherwise, <c>false</c>. The default value is <c>true</c>.
/// </value>
2012-10-26 13:58:50 +08:00
public bool Sweeped {
get {
return _svcHosts.Sweeped;
2012-10-26 13:58:50 +08:00
}
set {
_svcHosts.Sweeped = value;
2012-10-26 13:58:50 +08:00
}
}
2012-09-10 00:36:22 +08:00
#endregion
#region Events
/// <summary>
/// Occurs when the server gets an error.
/// </summary>
public event EventHandler<ErrorEventArgs> OnError;
/// <summary>
/// Occurs when the server receives an HTTP CONNECT request.
/// </summary>
public event EventHandler<ResponseEventArgs> OnResponseToConnect;
/// <summary>
/// Occurs when the server receives an HTTP DELETE request.
/// </summary>
public event EventHandler<ResponseEventArgs> OnResponseToDelete;
/// <summary>
/// Occurs when the server receives an HTTP GET request.
/// </summary>
public event EventHandler<ResponseEventArgs> OnResponseToGet;
/// <summary>
/// Occurs when the server receives an HTTP HEAD request.
/// </summary>
public event EventHandler<ResponseEventArgs> OnResponseToHead;
/// <summary>
/// Occurs when the server receives an HTTP OPTIONS request.
/// </summary>
public event EventHandler<ResponseEventArgs> OnResponseToOptions;
/// <summary>
/// Occurs when the server receives an HTTP PATCH request.
/// </summary>
public event EventHandler<ResponseEventArgs> OnResponseToPatch;
/// <summary>
/// Occurs when the server receives an HTTP POST request.
/// </summary>
public event EventHandler<ResponseEventArgs> OnResponseToPost;
/// <summary>
/// Occurs when the server receives an HTTP PUT request.
/// </summary>
public event EventHandler<ResponseEventArgs> OnResponseToPut;
/// <summary>
/// Occurs when the server receives an HTTP TRACE request.
/// </summary>
public event EventHandler<ResponseEventArgs> OnResponseToTrace;
2012-09-10 00:36:22 +08:00
#endregion
#region Private Methods
2012-09-11 12:21:47 +08:00
private void configureFromConfigFile()
{
_rootPath = ConfigurationManager.AppSettings["RootPath"];
}
2012-09-13 10:18:52 +08:00
private void init()
{
_isWindows = false;
_listener = new HttpListener();
_svcHosts = new ServiceHostManager();
2012-09-13 10:18:52 +08:00
var os = Environment.OSVersion;
if (os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX)
_isWindows = true;
var prefix = String.Format(
"http{0}://*:{1}/", _port == 443 ? "s" : String.Empty, _port);
_listener.Prefixes.Add(prefix);
configureFromConfigFile();
}
2012-09-12 11:29:42 +08:00
private bool isUpgrade(HttpListenerRequest request, string value)
{
if (!request.Headers.Exists("Upgrade", value))
return false;
if (!request.Headers.Exists("Connection", "Upgrade"))
return false;
return true;
}
private void onError(string message)
2012-09-10 00:36:22 +08:00
{
#if DEBUG
var callerFrame = new StackFrame(1);
var caller = callerFrame.GetMethod();
Console.WriteLine("HTTPSV: Error@{0}: {1}", caller.Name, message);
#endif
OnError.Emit(this, new ErrorEventArgs(message));
2012-09-10 00:36:22 +08:00
}
private void receiveRequest()
{
while (true)
{
try
{
var context = _listener.GetContext();
respondAsync(context);
}
catch (HttpListenerException)
{
// HttpListener has been closed.
break;
}
catch (Exception ex)
{
onError(ex.Message);
break;
}
}
}
private void respond(HttpListenerContext context)
2012-09-11 12:21:47 +08:00
{
var req = context.Request;
var res = context.Response;
var eventArgs = new ResponseEventArgs(context);
if (req.HttpMethod == "GET" && !OnResponseToGet.IsNull())
2012-09-11 12:21:47 +08:00
{
OnResponseToGet(this, eventArgs);
2012-09-11 12:21:47 +08:00
return;
}
if (req.HttpMethod == "HEAD" && !OnResponseToHead.IsNull())
2012-09-11 12:21:47 +08:00
{
OnResponseToHead(this, eventArgs);
2012-09-11 12:21:47 +08:00
return;
}
if (req.HttpMethod == "POST" && !OnResponseToPost.IsNull())
2012-09-11 12:21:47 +08:00
{
OnResponseToPost(this, eventArgs);
2012-09-11 12:21:47 +08:00
return;
}
if (req.HttpMethod == "PUT" && !OnResponseToPut.IsNull())
2012-09-11 12:21:47 +08:00
{
OnResponseToPut(this, eventArgs);
2012-09-11 12:21:47 +08:00
return;
}
if (req.HttpMethod == "DELETE" && !OnResponseToDelete.IsNull())
2012-09-11 12:21:47 +08:00
{
OnResponseToDelete(this, eventArgs);
2012-09-11 12:21:47 +08:00
return;
}
if (req.HttpMethod == "OPTIONS" && !OnResponseToOptions.IsNull())
2012-09-11 12:21:47 +08:00
{
OnResponseToOptions(this, eventArgs);
2012-09-11 12:21:47 +08:00
return;
}
if (req.HttpMethod == "TRACE" && !OnResponseToTrace.IsNull())
2012-09-11 12:21:47 +08:00
{
OnResponseToTrace(this, eventArgs);
2012-09-11 12:21:47 +08:00
return;
}
if (req.HttpMethod == "CONNECT" && !OnResponseToConnect.IsNull())
2012-09-11 12:21:47 +08:00
{
OnResponseToConnect(this, eventArgs);
2012-09-11 12:21:47 +08:00
return;
}
if (req.HttpMethod == "PATCH" && !OnResponseToPatch.IsNull())
2012-09-11 12:21:47 +08:00
{
OnResponseToPatch(this, eventArgs);
2012-09-11 12:21:47 +08:00
return;
}
res.StatusCode = (int)HttpStatusCode.NotImplemented;
}
private void respondAsync(HttpListenerContext context)
{
WaitCallback callback = (state) =>
{
var req = context.Request;
var res = context.Response;
try
{
if (isUpgrade(req, "websocket"))
{
if (upgradeToWebSocket(context))
return;
}
else
{
respond(context);
}
res.Close();
}
catch (Exception ex)
{
onError(ex.Message);
}
};
ThreadPool.QueueUserWorkItem(callback);
}
private void startReceiveRequestThread()
2012-09-10 00:36:22 +08:00
{
_receiveRequestThread = new Thread(new ThreadStart(receiveRequest));
_receiveRequestThread.IsBackground = true;
_receiveRequestThread.Start();
2012-09-10 00:36:22 +08:00
}
private bool upgradeToWebSocket(HttpListenerContext context)
2012-09-10 00:36:22 +08:00
{
var res = context.Response;
var wsContext = context.AcceptWebSocket();
var socket = wsContext.WebSocket;
var path = wsContext.Path.UrlDecode();
IServiceHost svcHost;
if (!_svcHosts.TryGetServiceHost(path, out svcHost))
2012-09-20 19:28:49 +08:00
{
res.StatusCode = (int)HttpStatusCode.NotImplemented;
return false;
2012-09-20 19:28:49 +08:00
}
svcHost.BindWebSocket(socket);
return true;
2012-09-10 00:36:22 +08:00
}
#endregion
#region Public Methods
/// <summary>
/// Adds the specified type WebSocket service.
/// </summary>
/// <param name="absPath">
/// A <see cref="string"/> that contains an absolute path associated with the WebSocket service.
/// </param>
/// <typeparam name="T">
/// The type of the WebSocket service. The T must inherit the <see cref="WebSocketService"/> class.
/// </typeparam>
public void AddWebSocketService<T>(string absPath)
where T : WebSocketService, new()
{
string msg;
if (!absPath.IsValidAbsolutePath(out msg))
{
onError(msg);
return;
}
var svcHost = new WebSocketServiceHost<T>();
svcHost.Uri = absPath.ToUri();
if (!Sweeped)
svcHost.Sweeped = Sweeped;
_svcHosts.Add(absPath, svcHost);
}
/// <summary>
/// Gets the contents of the specified file.
/// </summary>
/// <returns>
/// An array of <see cref="byte"/> that contains the contents of the file.
/// </returns>
/// <param name="path">
/// A <see cref="string"/> that contains a virtual path to the file to get.
/// </param>
2012-09-10 00:36:22 +08:00
public byte[] GetFile(string path)
{
var filePath = _rootPath + path;
2012-09-13 10:18:52 +08:00
if (_isWindows)
2012-09-12 21:52:02 +08:00
filePath = filePath.Replace("/", "\\");
return File.Exists(filePath)
? File.ReadAllBytes(filePath)
: null;
2012-09-10 00:36:22 +08:00
}
/// <summary>
/// Starts to receive incoming requests.
/// </summary>
2012-09-10 00:36:22 +08:00
public void Start()
{
_listener.Start();
startReceiveRequestThread();
2012-09-10 00:36:22 +08:00
}
/// <summary>
/// Stops receiving incoming requests.
/// </summary>
2012-09-10 00:36:22 +08:00
public void Stop()
{
_listener.Close();
_receiveRequestThread.Join(5 * 1000);
_svcHosts.Stop();
2012-09-10 00:36:22 +08:00
}
#endregion
}
}