Fix due to the added QueryString property in WebSocketService.cs
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using WebSocketSharp.Net;
|
||||
@@ -95,7 +96,7 @@ namespace WebSocketSharp.Server {
|
||||
try
|
||||
{
|
||||
var context = _listener.GetContext();
|
||||
respond(context);
|
||||
respondAsync(context);
|
||||
}
|
||||
catch (HttpListenerException)
|
||||
{
|
||||
@@ -104,7 +105,7 @@ namespace WebSocketSharp.Server {
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnError.Emit(this, new ErrorEventArgs(ex.Message));
|
||||
onError(ex.Message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -143,36 +144,17 @@ namespace WebSocketSharp.Server {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void respond(HttpListenerContext context)
|
||||
private void onError(string message)
|
||||
{
|
||||
WaitCallback respondCb = (state) =>
|
||||
{
|
||||
var req = context.Request;
|
||||
var res = context.Response;
|
||||
|
||||
try
|
||||
{
|
||||
if (isUpgrade(req, "websocket"))
|
||||
{
|
||||
if (upgradeToWebSocket(context))
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
respondToClient(context);
|
||||
}
|
||||
|
||||
res.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnError.Emit(this, new ErrorEventArgs(ex.Message));
|
||||
}
|
||||
};
|
||||
ThreadPool.QueueUserWorkItem(respondCb);
|
||||
#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));
|
||||
}
|
||||
|
||||
private void respondToClient(HttpListenerContext context)
|
||||
private void respond(HttpListenerContext context)
|
||||
{
|
||||
var req = context.Request;
|
||||
var res = context.Response;
|
||||
@@ -235,6 +217,36 @@ namespace WebSocketSharp.Server {
|
||||
res.StatusCode = (int)HttpStatusCode.NotImplemented;
|
||||
}
|
||||
|
||||
private void respondAsync(HttpListenerContext context)
|
||||
{
|
||||
WaitCallback respondCb = (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(respondCb);
|
||||
}
|
||||
|
||||
private void startAcceptRequestThread()
|
||||
{
|
||||
_acceptRequestThread = new Thread(new ThreadStart(acceptRequest));
|
||||
@@ -244,19 +256,17 @@ namespace WebSocketSharp.Server {
|
||||
|
||||
private bool upgradeToWebSocket(HttpListenerContext context)
|
||||
{
|
||||
var req = context.Request;
|
||||
var res = context.Response;
|
||||
|
||||
var path = req.RawUrl;
|
||||
var res = context.Response;
|
||||
var wsContext = context.AcceptWebSocket();
|
||||
var path = wsContext.Path.UrlDecode();
|
||||
if (!_services.ContainsKey(path))
|
||||
{
|
||||
res.StatusCode = (int)HttpStatusCode.NotImplemented;
|
||||
return false;
|
||||
}
|
||||
|
||||
var wsContext = context.AcceptWebSocket(path);
|
||||
var socket = wsContext.WebSocket;
|
||||
var service = _services[path];
|
||||
var socket = wsContext.WebSocket;
|
||||
var service = _services[path];
|
||||
service.BindWebSocket(socket);
|
||||
|
||||
return true;
|
||||
@@ -266,11 +276,18 @@ namespace WebSocketSharp.Server {
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void AddService<T>(string path)
|
||||
public void AddService<T>(string absPath)
|
||||
where T : WebSocketService, new()
|
||||
{
|
||||
string msg;
|
||||
if (!absPath.IsValidAbsolutePath(out msg))
|
||||
{
|
||||
onError(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
var service = new WebSocketServer<T>();
|
||||
_services.Add(path, service);
|
||||
_services.Add(absPath, service);
|
||||
}
|
||||
|
||||
public byte[] GetFile(string path)
|
||||
|
@@ -52,7 +52,33 @@ namespace WebSocketSharp.Server {
|
||||
}
|
||||
|
||||
public WebSocketServer(int port)
|
||||
: base(System.Net.IPAddress.Any, port)
|
||||
: this(System.Net.IPAddress.Any, port)
|
||||
{
|
||||
}
|
||||
|
||||
public WebSocketServer(string url)
|
||||
: base(url)
|
||||
{
|
||||
if (BaseUri.AbsolutePath != "/")
|
||||
{
|
||||
var msg = "Must not contain the path component: " + url;
|
||||
throw new ArgumentException(msg, "url");
|
||||
}
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
public WebSocketServer(System.Net.IPAddress address, int port)
|
||||
: base(address, port)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method
|
||||
|
||||
private void init()
|
||||
{
|
||||
_services = new Dictionary<string, IServiceHost>();
|
||||
}
|
||||
@@ -61,17 +87,20 @@ namespace WebSocketSharp.Server {
|
||||
|
||||
#region Protected Method
|
||||
|
||||
protected override void bindSocket(TcpClient client)
|
||||
protected override void AcceptWebSocket(TcpClient client)
|
||||
{
|
||||
var context = client.AcceptWebSocket();
|
||||
var socket = context.WebSocket;
|
||||
var path = context.RequestUri.ToString();
|
||||
var path = context.Path.UrlDecode();
|
||||
if (!_services.ContainsKey(path))
|
||||
{
|
||||
socket.Close(HttpStatusCode.NotImplemented);
|
||||
return;
|
||||
}
|
||||
|
||||
if (BaseUri.IsAbsoluteUri)
|
||||
socket.Url = new Uri(BaseUri, path);
|
||||
|
||||
var service = _services[path];
|
||||
service.BindWebSocket(socket);
|
||||
}
|
||||
@@ -80,11 +109,18 @@ namespace WebSocketSharp.Server {
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public void AddService<T>(string path)
|
||||
public void AddService<T>(string absPath)
|
||||
where T : WebSocketService, new()
|
||||
{
|
||||
string msg;
|
||||
if (!absPath.IsValidAbsolutePath(out msg))
|
||||
{
|
||||
Error(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
var service = new WebSocketServer<T>();
|
||||
_services.Add(path, service);
|
||||
_services.Add(absPath, service);
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
@@ -104,7 +140,6 @@ namespace WebSocketSharp.Server {
|
||||
#region Fields
|
||||
|
||||
private SessionManager _sessions;
|
||||
private Uri _uri;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -119,29 +154,25 @@ namespace WebSocketSharp.Server {
|
||||
|
||||
#region Public Constructors
|
||||
|
||||
public WebSocketServer(string url)
|
||||
: base(url)
|
||||
{
|
||||
_uri = url.ToUri();
|
||||
init();
|
||||
}
|
||||
|
||||
public WebSocketServer(int port)
|
||||
: this(port, "/")
|
||||
{
|
||||
}
|
||||
|
||||
public WebSocketServer(int port, string path)
|
||||
: base(System.Net.IPAddress.Any, port)
|
||||
public WebSocketServer(string url)
|
||||
: base(url)
|
||||
{
|
||||
var uri = path.ToUri();
|
||||
if (uri.IsAbsoluteUri)
|
||||
{
|
||||
var msg = "Not absolute path: " + path;
|
||||
throw new ArgumentException(msg, "path");
|
||||
}
|
||||
init();
|
||||
}
|
||||
|
||||
_uri = uri;
|
||||
public WebSocketServer(int port, string absPath)
|
||||
: this(System.Net.IPAddress.Any, port, absPath)
|
||||
{
|
||||
}
|
||||
|
||||
public WebSocketServer(System.Net.IPAddress address, int port, string absPath)
|
||||
: base(address, port, absPath)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
@@ -149,9 +180,10 @@ namespace WebSocketSharp.Server {
|
||||
|
||||
#region Property
|
||||
|
||||
public Uri Uri
|
||||
{
|
||||
get { return _uri; }
|
||||
public Uri Uri {
|
||||
get {
|
||||
return BaseUri;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -167,9 +199,20 @@ namespace WebSocketSharp.Server {
|
||||
|
||||
#region Protected Method
|
||||
|
||||
protected override void bindSocket(TcpClient client)
|
||||
protected override void AcceptWebSocket(TcpClient client)
|
||||
{
|
||||
var socket = new WebSocket(_uri, client);
|
||||
var context = client.AcceptWebSocket();
|
||||
var socket = context.WebSocket;
|
||||
var path = context.Path.UrlDecode();
|
||||
if (path != Uri.GetAbsolutePath().UrlDecode())
|
||||
{
|
||||
socket.Close(HttpStatusCode.NotImplemented);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Uri.IsAbsoluteUri)
|
||||
socket.Url = new Uri(Uri, path);
|
||||
|
||||
BindWebSocket(socket);
|
||||
}
|
||||
|
||||
|
@@ -34,8 +34,8 @@ using System.Threading;
|
||||
|
||||
namespace WebSocketSharp.Server {
|
||||
|
||||
public abstract class WebSocketServerBase
|
||||
{
|
||||
public abstract class WebSocketServerBase {
|
||||
|
||||
#region Fields
|
||||
|
||||
private Thread _acceptClientThread;
|
||||
@@ -43,6 +43,7 @@ namespace WebSocketSharp.Server {
|
||||
private bool _isSelfHost;
|
||||
private int _port;
|
||||
private TcpListener _tcpListener;
|
||||
private Uri _uri;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -55,31 +56,72 @@ namespace WebSocketSharp.Server {
|
||||
|
||||
protected WebSocketServerBase(string url)
|
||||
{
|
||||
init(url);
|
||||
if (url.IsNull())
|
||||
throw new ArgumentNullException("url");
|
||||
|
||||
Uri uri;
|
||||
string msg;
|
||||
if (!tryCreateUri(url, out uri, out msg))
|
||||
throw new ArgumentException(msg, "url");
|
||||
|
||||
init(uri);
|
||||
}
|
||||
|
||||
protected WebSocketServerBase(IPAddress address, int port)
|
||||
: this(address, port, "/")
|
||||
{
|
||||
}
|
||||
|
||||
protected WebSocketServerBase(IPAddress address, int port, string absPath)
|
||||
{
|
||||
if (address.IsNull())
|
||||
throw new ArgumentNullException("address");
|
||||
|
||||
if (absPath.IsNull())
|
||||
throw new ArgumentNullException("absPath");
|
||||
|
||||
string msg;
|
||||
if (!absPath.IsValidAbsolutePath(out msg))
|
||||
throw new ArgumentException(msg, "absPath");
|
||||
|
||||
_address = address;
|
||||
_port = port <= 0 ? 80 : port;
|
||||
_uri = absPath.ToUri();
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Property
|
||||
#region Protected Property
|
||||
|
||||
protected Uri BaseUri
|
||||
{
|
||||
get {
|
||||
return _uri;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public IPAddress Address {
|
||||
get { return _address; }
|
||||
get {
|
||||
return _address;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSelfHost {
|
||||
get { return _isSelfHost; }
|
||||
get {
|
||||
return _isSelfHost;
|
||||
}
|
||||
}
|
||||
|
||||
public int Port {
|
||||
get { return _port; }
|
||||
get {
|
||||
return _port;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -99,7 +141,7 @@ namespace WebSocketSharp.Server {
|
||||
try
|
||||
{
|
||||
var client = _tcpListener.AcceptTcpClient();
|
||||
acceptSocket(client);
|
||||
acceptSocketAsync(client);
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
@@ -108,36 +150,27 @@ namespace WebSocketSharp.Server {
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error(ex.Message);
|
||||
onError(ex.Message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void acceptSocket(TcpClient client)
|
||||
private void acceptSocketAsync(TcpClient client)
|
||||
{
|
||||
WaitCallback acceptSocketCb = (state) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
bindSocket(client);
|
||||
AcceptWebSocket(client);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error(ex.Message);
|
||||
onError(ex.Message);
|
||||
}
|
||||
};
|
||||
ThreadPool.QueueUserWorkItem(acceptSocketCb);
|
||||
}
|
||||
|
||||
private void error(string message)
|
||||
{
|
||||
#if DEBUG
|
||||
var callerFrame = new StackFrame(1);
|
||||
var caller = callerFrame.GetMethod();
|
||||
Console.WriteLine("WSSV: Error@{0}: {1}", caller.Name, message);
|
||||
#endif
|
||||
OnError.Emit(this, new ErrorEventArgs(message));
|
||||
ThreadPool.QueueUserWorkItem(acceptSocketCb);
|
||||
}
|
||||
|
||||
private void init()
|
||||
@@ -146,14 +179,9 @@ namespace WebSocketSharp.Server {
|
||||
_isSelfHost = true;
|
||||
}
|
||||
|
||||
private void init(string url)
|
||||
private void init(Uri uri)
|
||||
{
|
||||
var uri = url.ToUri();
|
||||
|
||||
string msg;
|
||||
if (!uri.IsValidWebSocketUri(out msg))
|
||||
throw new ArgumentException(msg, "url");
|
||||
|
||||
_uri = uri;
|
||||
var scheme = uri.Scheme;
|
||||
var port = uri.Port;
|
||||
var host = uri.DnsSafeHost;
|
||||
@@ -168,6 +196,16 @@ namespace WebSocketSharp.Server {
|
||||
init();
|
||||
}
|
||||
|
||||
private void onError(string message)
|
||||
{
|
||||
#if DEBUG
|
||||
var callerFrame = new StackFrame(1);
|
||||
var caller = callerFrame.GetMethod();
|
||||
Console.WriteLine("WSSV: Error@{0}: {1}", caller.Name, message);
|
||||
#endif
|
||||
OnError.Emit(this, new ErrorEventArgs(message));
|
||||
}
|
||||
|
||||
private void startAcceptClientThread()
|
||||
{
|
||||
_acceptClientThread = new Thread(new ThreadStart(acceptClient));
|
||||
@@ -175,11 +213,31 @@ namespace WebSocketSharp.Server {
|
||||
_acceptClientThread.Start();
|
||||
}
|
||||
|
||||
private bool tryCreateUri(string uriString, out Uri result, out string message)
|
||||
{
|
||||
if (!uriString.TryCreateWebSocketUri(out result, out message))
|
||||
return false;
|
||||
|
||||
if (!result.Query.IsNullOrEmpty())
|
||||
{
|
||||
result = null;
|
||||
message = "Must not contain the query component: " + uriString;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Method
|
||||
|
||||
protected abstract void bindSocket(TcpClient client);
|
||||
protected abstract void AcceptWebSocket(TcpClient client);
|
||||
|
||||
protected virtual void Error(string message)
|
||||
{
|
||||
onError(message);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
@@ -28,6 +28,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Threading;
|
||||
using WebSocketSharp.Frame;
|
||||
|
||||
@@ -53,9 +54,15 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Property
|
||||
#region Protected Properties
|
||||
|
||||
protected SessionManager sessions {
|
||||
protected NameValueCollection QueryString {
|
||||
get {
|
||||
return _socket.QueryString;
|
||||
}
|
||||
}
|
||||
|
||||
protected SessionManager Sessions {
|
||||
get {
|
||||
return _sessions;
|
||||
}
|
||||
@@ -91,19 +98,19 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
protected virtual void onOpen(object sender, EventArgs e)
|
||||
protected virtual void OnClose(object sender, CloseEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void onMessage(object sender, MessageEventArgs e)
|
||||
protected virtual void OnError(object sender, ErrorEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void onError(object sender, ErrorEventArgs e)
|
||||
protected virtual void OnMessage(object sender, MessageEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void onClose(object sender, CloseEventArgs e)
|
||||
protected virtual void OnOpen(object sender, EventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -117,10 +124,10 @@ namespace WebSocketSharp.Server
|
||||
_sessions = sessions;
|
||||
|
||||
defaultBind();
|
||||
_socket.OnOpen += onOpen;
|
||||
_socket.OnMessage += onMessage;
|
||||
_socket.OnError += onError;
|
||||
_socket.OnClose += onClose;
|
||||
_socket.OnOpen += OnOpen;
|
||||
_socket.OnMessage += OnMessage;
|
||||
_socket.OnError += OnError;
|
||||
_socket.OnClose += OnClose;
|
||||
|
||||
IsBound = true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user