Fix due to the modified WsStream.cs

This commit is contained in:
sta 2012-12-11 15:49:01 +09:00
parent daeaebcc4b
commit 093a0a102b
64 changed files with 105 additions and 61 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -82,15 +82,18 @@ namespace WebSocketSharp {
/// <param name="client"> /// <param name="client">
/// A <see cref="TcpClient"/> that contains a TCP connection to accept a WebSocket connection from. /// A <see cref="TcpClient"/> that contains a TCP connection to accept a WebSocket connection from.
/// </param> /// </param>
/// <param name="secure">
/// A <see cref="bool"/> that indicates a secure connection or not. (<c>true</c> indicates a secure connection.)
/// </param>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// Is thrown when the <paramref name="client"/> parameter passed to a method is invalid because it is <see langword="null"/>. /// Is thrown when the <paramref name="client"/> parameter passed to a method is invalid because it is <see langword="null"/>.
/// </exception> /// </exception>
public static TcpListenerWebSocketContext AcceptWebSocket(this TcpClient client) public static TcpListenerWebSocketContext AcceptWebSocket(this TcpClient client, bool secure)
{ {
if (client.IsNull()) if (client.IsNull())
throw new ArgumentNullException("client"); throw new ArgumentNullException("client");
return new TcpListenerWebSocketContext(client); return new TcpListenerWebSocketContext(client, secure);
} }
/// <summary> /// <summary>

View File

@ -43,11 +43,11 @@ namespace WebSocketSharp.Net.Sockets {
private WebSocket _socket; private WebSocket _socket;
private WsStream _stream; private WsStream _stream;
internal TcpListenerWebSocketContext(TcpClient client) internal TcpListenerWebSocketContext(TcpClient client, bool secure)
{ {
_client = client; _client = client;
_stream = WsStream.CreateServerStream(client); _isSecure = secure;
_isSecure = _stream.IsSecure; _stream = WsStream.CreateServerStream(client, secure);
_request = RequestHandshake.Parse(_stream.ReadHandshake()); _request = RequestHandshake.Parse(_stream.ReadHandshake());
_socket = new WebSocket(this); _socket = new WebSocket(this);
} }

View File

@ -67,8 +67,18 @@ namespace WebSocketSharp.Server {
init(); init();
} }
public WebSocketServer(int port, bool secure)
: this(System.Net.IPAddress.Any, port, secure)
{
}
public WebSocketServer(System.Net.IPAddress address, int port) public WebSocketServer(System.Net.IPAddress address, int port)
: base(address, port) : this(address, port, port == 443 ? true : false)
{
}
public WebSocketServer(System.Net.IPAddress address, int port, bool secure)
: base(address, port, "/", secure)
{ {
init(); init();
} }
@ -112,7 +122,7 @@ namespace WebSocketSharp.Server {
protected override void AcceptWebSocket(TcpClient client) protected override void AcceptWebSocket(TcpClient client)
{ {
var context = client.AcceptWebSocket(); var context = client.AcceptWebSocket(IsSecure);
var socket = context.WebSocket; var socket = context.WebSocket;
var path = context.Path.UrlDecode(); var path = context.Path.UrlDecode();

View File

@ -40,6 +40,7 @@ namespace WebSocketSharp.Server {
private Thread _acceptClientThread; private Thread _acceptClientThread;
private IPAddress _address; private IPAddress _address;
private bool _isSecure;
private bool _isSelfHost; private bool _isSelfHost;
private int _port; private int _port;
private TcpListener _tcpListener; private TcpListener _tcpListener;
@ -67,12 +68,7 @@ namespace WebSocketSharp.Server {
init(uri); init(uri);
} }
protected WebSocketServerBase(IPAddress address, int port) protected WebSocketServerBase(IPAddress address, int port, string absPath, bool secure)
: this(address, port, "/")
{
}
protected WebSocketServerBase(IPAddress address, int port, string absPath)
{ {
if (address.IsNull()) if (address.IsNull())
throw new ArgumentNullException("address"); throw new ArgumentNullException("address");
@ -84,9 +80,20 @@ namespace WebSocketSharp.Server {
if (!absPath.IsValidAbsolutePath(out msg)) if (!absPath.IsValidAbsolutePath(out msg))
throw new ArgumentException(msg, "absPath"); throw new ArgumentException(msg, "absPath");
_address = address; if ((port == 80 && secure) ||
_port = port <= 0 ? 80 : port; (port == 443 && !secure))
_uri = absPath.ToUri(); {
msg = String.Format(
"Invalid pair of 'port' and 'secure': {0}, {1}", port, secure);
throw new ArgumentException(msg);
}
_address = address;
_port = port > 0
? port
: secure ? 443 : 80;
_uri = absPath.ToUri();
_isSecure = secure;
init(); init();
} }
@ -116,6 +123,12 @@ namespace WebSocketSharp.Server {
} }
} }
public bool IsSecure {
get {
return _isSecure;
}
}
public bool IsSelfHost { public bool IsSelfHost {
get { get {
return _isSelfHost; return _isSelfHost;
@ -185,17 +198,17 @@ namespace WebSocketSharp.Server {
private void init(Uri uri) private void init(Uri uri)
{ {
_uri = uri;
var scheme = uri.Scheme; var scheme = uri.Scheme;
var port = uri.Port;
var host = uri.DnsSafeHost; var host = uri.DnsSafeHost;
var port = uri.Port;
var addrs = Dns.GetHostAddresses(host); var addrs = Dns.GetHostAddresses(host);
if (port <= 0) _uri = uri;
port = scheme == "ws" ? 80 : 443; _address = addrs[0];
_isSecure = scheme == "wss" ? true : false;
_address = addrs[0]; _port = port > 0
_port = port; ? port
: _isSecure ? 443 : 80;
init(); init();
} }
@ -234,7 +247,7 @@ namespace WebSocketSharp.Server {
#endregion #endregion
#region Protected Method #region Protected Methods
protected abstract void AcceptWebSocket(TcpClient client); protected abstract void AcceptWebSocket(TcpClient client);

View File

@ -66,13 +66,28 @@ namespace WebSocketSharp.Server {
init(); init();
} }
public WebSocketServiceHost(int port, bool secure)
: this(port, "/", secure)
{
}
public WebSocketServiceHost(int port, string absPath) public WebSocketServiceHost(int port, string absPath)
: this(System.Net.IPAddress.Any, port, absPath) : this(System.Net.IPAddress.Any, port, absPath)
{ {
} }
public WebSocketServiceHost(int port, string absPath, bool secure)
: this(System.Net.IPAddress.Any, port, absPath, secure)
{
}
public WebSocketServiceHost(System.Net.IPAddress address, int port, string absPath) public WebSocketServiceHost(System.Net.IPAddress address, int port, string absPath)
: base(address, port, absPath) : this(address, port, absPath, port == 443 ? true : false)
{
}
public WebSocketServiceHost(System.Net.IPAddress address, int port, string absPath, bool secure)
: base(address, port, absPath, secure)
{ {
init(); init();
} }
@ -116,7 +131,7 @@ namespace WebSocketSharp.Server {
protected override void AcceptWebSocket(TcpClient client) protected override void AcceptWebSocket(TcpClient client)
{ {
var context = client.AcceptWebSocket(); var context = client.AcceptWebSocket(IsSecure);
var socket = context.WebSocket; var socket = context.WebSocket;
var path = context.Path.UrlDecode(); var path = context.Path.UrlDecode();
if (path != Uri.GetAbsolutePath().UrlDecode()) if (path != Uri.GetAbsolutePath().UrlDecode())

View File

@ -515,11 +515,12 @@ namespace WebSocketSharp {
private void createClientStream() private void createClientStream()
{ {
var host = _uri.DnsSafeHost; var host = _uri.DnsSafeHost;
var port = _uri.Port; var port = _uri.Port > 0
if (port <= 0) ? _uri.Port
port = IsSecure ? 443 : 80; : _isSecure ? 443 : 80;
_wsStream = WsStream.CreateClientStream(host, port, out _tcpClient); _tcpClient = new TcpClient(host, port);
_wsStream = WsStream.CreateClientStream(_tcpClient, host, _isSecure);
} }
private WsFrame createFrame(Fin fin, Opcode opcode, PayloadData payloadData) private WsFrame createFrame(Fin fin, Opcode opcode, PayloadData payloadData)
@ -1046,7 +1047,7 @@ namespace WebSocketSharp {
var count = rem == 0 ? quo - 2 : quo - 1; var count = rem == 0 ? quo - 2 : quo - 1;
// First // First
var buffer = new byte[_fragmentLen]; var buffer = new byte[_fragmentLen];
long readLen = stream.Read(buffer, 0, _fragmentLen); long readLen = stream.Read(buffer, 0, _fragmentLen);
send(Fin.MORE, opcode, buffer); send(Fin.MORE, opcode, buffer);

View File

@ -44,23 +44,42 @@ namespace WebSocketSharp {
#region Fields #region Fields
private Stream _innerStream; private Stream _innerStream;
private Type _innerStreamType;
private bool _isSecure; private bool _isSecure;
private Object _forRead; private Object _forRead;
private Object _forWrite; private Object _forWrite;
#endregion #endregion
#region Constructors #region Private Constructor
private WsStream()
{
_forRead = new object();
_forWrite = new object();
}
#endregion
#region Public Constructors
public WsStream(NetworkStream innerStream) public WsStream(NetworkStream innerStream)
: this()
{ {
init(innerStream); if (innerStream.IsNull())
throw new ArgumentNullException("innerStream");
_innerStream = innerStream;
_isSecure = false;
} }
public WsStream(SslStream innerStream) public WsStream(SslStream innerStream)
: this()
{ {
init(innerStream); if (innerStream.IsNull())
throw new ArgumentNullException("innerStream");
_innerStream = innerStream;
_isSecure = true;
} }
#endregion #endregion
@ -69,7 +88,7 @@ namespace WebSocketSharp {
public bool DataAvailable { public bool DataAvailable {
get { get {
return _innerStreamType == typeof(SslStream) return _isSecure
? ((SslStream)_innerStream).DataAvailable ? ((SslStream)_innerStream).DataAvailable
: ((NetworkStream)_innerStream).DataAvailable; : ((NetworkStream)_innerStream).DataAvailable;
} }
@ -83,26 +102,14 @@ namespace WebSocketSharp {
#endregion #endregion
#region Private Method #region Private Methods
private void init(Stream innerStream)
{
if (innerStream == null)
throw new ArgumentNullException("innerStream");
_innerStream = innerStream;
_innerStreamType = innerStream.GetType();
_isSecure = _innerStreamType == typeof(SslStream) ? true : false;
_forRead = new object();
_forWrite = new object();
}
private int read(byte[] buffer, int offset, int size) private int read(byte[] buffer, int offset, int size)
{ {
var readLen = _innerStream.Read(buffer, offset, size); var readLen = _innerStream.Read(buffer, offset, size);
if (readLen < size) if (readLen < size)
{ {
var msg = String.Format("Data can not be read from {0}.", _innerStreamType); var msg = String.Format("Data can not be read from {0}.", _innerStream.GetType().Name);
throw new IOException(msg); throw new IOException(msg);
} }
@ -145,12 +152,10 @@ namespace WebSocketSharp {
#region Internal Methods #region Internal Methods
internal static WsStream CreateClientStream(string hostname, int port, out TcpClient client) internal static WsStream CreateClientStream(TcpClient client, string host, bool secure)
{ {
client = new TcpClient(hostname, port);
var netStream = client.GetStream(); var netStream = client.GetStream();
if (secure)
if (port == 443)
{ {
System.Net.Security.RemoteCertificateValidationCallback validationCb = (sender, certificate, chain, sslPolicyErrors) => System.Net.Security.RemoteCertificateValidationCallback validationCb = (sender, certificate, chain, sslPolicyErrors) =>
{ {
@ -159,7 +164,7 @@ namespace WebSocketSharp {
}; };
var sslStream = new SslStream(netStream, false, validationCb); var sslStream = new SslStream(netStream, false, validationCb);
sslStream.AuthenticateAsClient(hostname); sslStream.AuthenticateAsClient(host);
return new WsStream(sslStream); return new WsStream(sslStream);
} }
@ -167,16 +172,13 @@ namespace WebSocketSharp {
return new WsStream(netStream); return new WsStream(netStream);
} }
internal static WsStream CreateServerStream(TcpClient client) internal static WsStream CreateServerStream(TcpClient client, bool secure)
{ {
var netStream = client.GetStream(); var netStream = client.GetStream();
if (secure)
var port = ((IPEndPoint)client.Client.LocalEndPoint).Port;
if (port == 443)
{ {
var sslStream = new SslStream(netStream, false); var sslStream = new SslStream(netStream, false);
var certPath = ConfigurationManager.AppSettings["ServerCertPath"];
var certPath = ConfigurationManager.AppSettings["ServerCertPath"];
sslStream.AuthenticateAsServer(new X509Certificate2(certPath)); sslStream.AuthenticateAsServer(new X509Certificate2(certPath));
return new WsStream(sslStream); return new WsStream(sslStream);

Binary file not shown.