diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs
index da4a5708..6ba24ece 100644
--- a/websocket-sharp/Ext.cs
+++ b/websocket-sharp/Ext.cs
@@ -224,6 +224,35 @@ namespace WebSocketSharp {
: stream.ToByteArray();
}
+ //
+ // Determines whether the specified equals the specified ,
+ // and invokes the specified Action<int> delegate at the same time.
+ //
+ //
+ // true if equals ; otherwise, false.
+ //
+ //
+ // An to compare.
+ //
+ //
+ // A to compare.
+ //
+ //
+ // An Action<int> delegate that references the method(s) called at the same time as when comparing.
+ // An parameter to pass to the method(s) is .
+ //
+ //
+ // is less than 0, or greater than 255.
+ //
+ internal static bool EqualsWith(this int value, char c, Action action)
+ {
+ if (value < 0 || value > 255)
+ throw new ArgumentOutOfRangeException("value");
+
+ action(value);
+ return value == c - 0;
+ }
+
internal static string GetNameInternal(this string nameAndValue, string separator)
{
int i = nameAndValue.IndexOf(separator);
@@ -272,27 +301,27 @@ namespace WebSocketSharp {
}
//
- // Determines whether the specified object is .
- // And invokes the specified delegate if the specified object is .
+ // Determines whether the specified object is ,
+ // and invokes the specified delegate if the object is .
//
//
- // true if the parameter is ; otherwise, false.
+ // true if is ; otherwise, false.
//
//
// A class to test.
//
- //
- // An delegate that contains the method(s) called if the is .
+ //
+ // An delegate that references the method(s) called if is .
//
//
- // The type of the parameter.
+ // The type of .
//
- internal static bool IsNullDo(this T obj, Action act)
+ internal static bool IsNullDo(this T obj, Action action)
where T : class
{
if (obj == null)
{
- act();
+ action();
return true;
}
@@ -662,36 +691,6 @@ namespace WebSocketSharp {
eventHandler(sender, e);
}
- ///
- /// Determines whether the specified equals the specified as .
- /// And save this specified as to the specified List<byte>.
- ///
- ///
- /// true if the parameter equals the parameter as ; otherwise, false.
- ///
- ///
- /// An to compare.
- ///
- ///
- /// A to compare.
- ///
- ///
- /// A List<byte> to save the as .
- ///
- ///
- /// Is thrown when the parameter passed to a method is invalid because it is outside the allowable range of values as .
- ///
- public static bool EqualsAndSaveTo(this int value, char c, List dest)
- {
- if (value < 0 || value > 255)
- throw new ArgumentOutOfRangeException("value");
-
- var b = (byte)value;
- dest.Add(b);
-
- return b == Convert.ToByte(c);
- }
-
///
/// Gets the absolute path from the specified .
///
diff --git a/websocket-sharp/WebSocketException.cs b/websocket-sharp/WebSocketException.cs
index 2f26529d..f9f448c7 100644
--- a/websocket-sharp/WebSocketException.cs
+++ b/websocket-sharp/WebSocketException.cs
@@ -47,6 +47,11 @@ namespace WebSocketSharp {
{
}
+ internal WebSocketException(string message)
+ : this(CloseStatusCode.NO_STATUS_CODE, message)
+ {
+ }
+
internal WebSocketException(CloseStatusCode code, string message)
: base(message)
{
diff --git a/websocket-sharp/WsStream.cs b/websocket-sharp/WsStream.cs
index b6f8ddc5..a1f1720f 100644
--- a/websocket-sharp/WsStream.cs
+++ b/websocket-sharp/WsStream.cs
@@ -40,6 +40,12 @@ namespace WebSocketSharp {
internal class WsStream : IDisposable
{
+ #region Private Const Fields
+
+ private const int _handshakeLimitLen = 8192;
+
+ #endregion
+
#region Private Fields
private Stream _innerStream;
@@ -53,7 +59,7 @@ namespace WebSocketSharp {
private WsStream()
{
- _forRead = new object();
+ _forRead = new object();
_forWrite = new object();
}
@@ -64,21 +70,21 @@ namespace WebSocketSharp {
public WsStream(NetworkStream innerStream)
: this()
{
- if (innerStream.IsNull())
+ if (innerStream == null)
throw new ArgumentNullException("innerStream");
_innerStream = innerStream;
- _isSecure = false;
+ _isSecure = false;
}
public WsStream(SslStream innerStream)
: this()
{
- if (innerStream.IsNull())
+ if (innerStream == null)
throw new ArgumentNullException("innerStream");
_innerStream = innerStream;
- _isSecure = true;
+ _isSecure = true;
}
#endregion
@@ -103,40 +109,6 @@ namespace WebSocketSharp {
#region Private Methods
- private int read(byte[] buffer, int offset, int size)
- {
- var readLen = _innerStream.Read(buffer, offset, size);
- if (readLen < size)
- {
- var msg = String.Format("Data can not be read from {0}.", _innerStream.GetType().Name);
- throw new IOException(msg);
- }
-
- return readLen;
- }
-
- private int readByte()
- {
- return _innerStream.ReadByte();
- }
-
- private string[] readHandshake()
- {
- var buffer = new List();
- while (true)
- {
- if (readByte().EqualsAndSaveTo('\r', buffer) &&
- readByte().EqualsAndSaveTo('\n', buffer) &&
- readByte().EqualsAndSaveTo('\r', buffer) &&
- readByte().EqualsAndSaveTo('\n', buffer))
- break;
- }
-
- return Encoding.UTF8.GetString(buffer.ToArray())
- .Replace("\r\n", "\n").Replace("\n\n", "\n").TrimEnd('\n')
- .Split('\n');
- }
-
private bool write(byte[] data)
{
lock (_forWrite)
@@ -181,7 +153,7 @@ namespace WebSocketSharp {
if (secure)
{
var sslStream = new SslStream(netStream, false);
- var certPath = ConfigurationManager.AppSettings["ServerCertPath"];
+ var certPath = ConfigurationManager.AppSettings["ServerCertPath"];
sslStream.AuthenticateAsServer(new X509Certificate2(certPath));
return new WsStream(sslStream);
@@ -192,7 +164,7 @@ namespace WebSocketSharp {
internal static WsStream CreateServerStream(WebSocketSharp.Net.HttpListenerContext context)
{
- var conn = context.Connection;
+ var conn = context.Connection;
var stream = conn.Stream;
return conn.IsSecure
@@ -236,17 +208,30 @@ namespace WebSocketSharp {
public string[] ReadHandshake()
{
- lock (_forRead)
+ var read = false;
+ var buffer = new List();
+ Action add = i => buffer.Add((byte)i);
+ while (buffer.Count < _handshakeLimitLen)
{
- try
+ if (_innerStream.ReadByte().EqualsWith('\r', add) &&
+ _innerStream.ReadByte().EqualsWith('\n', add) &&
+ _innerStream.ReadByte().EqualsWith('\r', add) &&
+ _innerStream.ReadByte().EqualsWith('\n', add))
{
- return readHandshake();
- }
- catch
- {
- return null;
+ read = true;
+ break;
}
}
+
+ if (!read)
+ throw new WebSocketException("The length of the handshake is greater than the limit length.");
+
+ return Encoding.UTF8.GetString(buffer.ToArray())
+ .Replace("\r\n", "\n")
+ .Replace("\n ", " ")
+ .Replace("\n\t", " ")
+ .Replace("\n\n", "")
+ .Split('\n');
}
public bool Write(WsFrame frame)