|
|
|
@ -42,7 +42,7 @@ namespace WebSocketSharp
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#region Private Const Fields
|
|
|
|
#region Private Const Fields
|
|
|
|
|
|
|
|
|
|
|
|
private const int _httpHeadersLimitLen = 8192;
|
|
|
|
private const int _headersMaxLength = 8192;
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
@ -65,20 +65,6 @@ namespace WebSocketSharp
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Public Constructors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public WebSocketStream (NetworkStream innerStream)
|
|
|
|
|
|
|
|
: this (innerStream, false)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public WebSocketStream (SslStream innerStream)
|
|
|
|
|
|
|
|
: this (innerStream, true)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Public Properties
|
|
|
|
#region Public Properties
|
|
|
|
|
|
|
|
|
|
|
|
public bool DataAvailable {
|
|
|
|
public bool DataAvailable {
|
|
|
|
@ -99,7 +85,7 @@ namespace WebSocketSharp
|
|
|
|
|
|
|
|
|
|
|
|
#region Private Methods
|
|
|
|
#region Private Methods
|
|
|
|
|
|
|
|
|
|
|
|
private static byte [] readHttpEntityBody (Stream stream, string length)
|
|
|
|
private static byte[] readEntityBody (Stream stream, string length)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
long len;
|
|
|
|
long len;
|
|
|
|
if (!Int64.TryParse (length, out len))
|
|
|
|
if (!Int64.TryParse (length, out len))
|
|
|
|
@ -115,17 +101,17 @@ namespace WebSocketSharp
|
|
|
|
: null;
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string [] readHttpHeaders (Stream stream)
|
|
|
|
private static string[] readHeaders (Stream stream, int maxLength)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var buff = new List<byte> ();
|
|
|
|
var buff = new List<byte> ();
|
|
|
|
var count = 0;
|
|
|
|
var cnt = 0;
|
|
|
|
Action<int> add = i => {
|
|
|
|
Action<int> add = i => {
|
|
|
|
buff.Add ((byte) i);
|
|
|
|
buff.Add ((byte) i);
|
|
|
|
count++;
|
|
|
|
cnt++;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var read = false;
|
|
|
|
var read = false;
|
|
|
|
while (count < _httpHeadersLimitLen) {
|
|
|
|
while (cnt < maxLength) {
|
|
|
|
if (stream.ReadByte ().EqualsWith ('\r', add) &&
|
|
|
|
if (stream.ReadByte ().EqualsWith ('\r', add) &&
|
|
|
|
stream.ReadByte ().EqualsWith ('\n', add) &&
|
|
|
|
stream.ReadByte ().EqualsWith ('\n', add) &&
|
|
|
|
stream.ReadByte ().EqualsWith ('\r', add) &&
|
|
|
|
stream.ReadByte ().EqualsWith ('\r', add) &&
|
|
|
|
@ -137,27 +123,23 @@ namespace WebSocketSharp
|
|
|
|
|
|
|
|
|
|
|
|
if (!read)
|
|
|
|
if (!read)
|
|
|
|
throw new WebSocketException (
|
|
|
|
throw new WebSocketException (
|
|
|
|
"The header part of a HTTP data is greater than the limit length.");
|
|
|
|
"The header part of a HTTP data is greater than the max length.");
|
|
|
|
|
|
|
|
|
|
|
|
var crlf = "\r\n";
|
|
|
|
var crlf = "\r\n";
|
|
|
|
return Encoding.UTF8.GetString (buff.ToArray ())
|
|
|
|
return Encoding.UTF8.GetString (buff.ToArray ())
|
|
|
|
.Replace (crlf + " ", " ")
|
|
|
|
.Replace (crlf + " ", " ")
|
|
|
|
.Replace (crlf + "\t", " ")
|
|
|
|
.Replace (crlf + "\t", " ")
|
|
|
|
.Split (new [] { crlf }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
.Split (new[] { crlf }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
private static T readHttp<T> (Stream stream, Func<string[], T> parser, int millisecondsTimeout)
|
|
|
|
|
|
|
|
|
|
|
|
#region Internal Methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal T ReadHttp<T> (Func<string [], T> parser, int millisecondsTimeout)
|
|
|
|
|
|
|
|
where T : HttpBase
|
|
|
|
where T : HttpBase
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var timeout = false;
|
|
|
|
var timeout = false;
|
|
|
|
var timer = new Timer (
|
|
|
|
var timer = new Timer (
|
|
|
|
state => {
|
|
|
|
state => {
|
|
|
|
timeout = true;
|
|
|
|
timeout = true;
|
|
|
|
_innerStream.Close ();
|
|
|
|
stream.Close ();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
millisecondsTimeout,
|
|
|
|
millisecondsTimeout,
|
|
|
|
@ -166,10 +148,10 @@ namespace WebSocketSharp
|
|
|
|
T http = null;
|
|
|
|
T http = null;
|
|
|
|
Exception exception = null;
|
|
|
|
Exception exception = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
http = parser (readHttpHeaders (_innerStream));
|
|
|
|
http = parser (readHeaders (stream, _headersMaxLength));
|
|
|
|
var contentLen = http.Headers ["Content-Length"];
|
|
|
|
var contentLen = http.Headers["Content-Length"];
|
|
|
|
if (contentLen != null && contentLen.Length > 0)
|
|
|
|
if (contentLen != null && contentLen.Length > 0)
|
|
|
|
http.EntityBodyData = readHttpEntityBody (_innerStream, contentLen);
|
|
|
|
http.EntityBodyData = readEntityBody (stream, contentLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
catch (Exception ex) {
|
|
|
|
exception = ex;
|
|
|
|
exception = ex;
|
|
|
|
@ -191,95 +173,128 @@ namespace WebSocketSharp
|
|
|
|
return http;
|
|
|
|
return http;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal bool Write (byte [] data)
|
|
|
|
private static HttpResponse sendHttpRequest (
|
|
|
|
|
|
|
|
Stream stream, HttpRequest request, int millisecondsTimeout)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock (_forWrite) {
|
|
|
|
var buff = request.ToByteArray ();
|
|
|
|
try {
|
|
|
|
stream.Write (buff, 0, buff.Length);
|
|
|
|
_innerStream.Write (data, 0, data.Length);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return readHttp<HttpResponse> (stream, HttpResponse.Parse, millisecondsTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
catch {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Public Methods
|
|
|
|
#region Internal Methods
|
|
|
|
|
|
|
|
|
|
|
|
public void Close ()
|
|
|
|
internal static WebSocketStream CreateClientStream (
|
|
|
|
|
|
|
|
Uri targetUri,
|
|
|
|
|
|
|
|
Uri proxyUri,
|
|
|
|
|
|
|
|
NetworkCredential proxyCredentials,
|
|
|
|
|
|
|
|
bool secure,
|
|
|
|
|
|
|
|
System.Net.Security.RemoteCertificateValidationCallback validationCallback,
|
|
|
|
|
|
|
|
out TcpClient tcpClient)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_innerStream.Close ();
|
|
|
|
var proxy = proxyUri != null;
|
|
|
|
|
|
|
|
tcpClient = proxy
|
|
|
|
|
|
|
|
? new TcpClient (proxyUri.DnsSafeHost, proxyUri.Port)
|
|
|
|
|
|
|
|
: new TcpClient (targetUri.DnsSafeHost, targetUri.Port);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var netStream = tcpClient.GetStream ();
|
|
|
|
|
|
|
|
if (proxy) {
|
|
|
|
|
|
|
|
var req = HttpRequest.CreateConnectRequest (targetUri);
|
|
|
|
|
|
|
|
var res = sendHttpRequest (netStream, req, 90000);
|
|
|
|
|
|
|
|
if (res.IsProxyAuthenticationRequired) {
|
|
|
|
|
|
|
|
var authChal = res.ProxyAuthenticationChallenge;
|
|
|
|
|
|
|
|
if (authChal != null && proxyCredentials != null) {
|
|
|
|
|
|
|
|
var authRes = new AuthenticationResponse (authChal, proxyCredentials, 0);
|
|
|
|
|
|
|
|
req.Headers["Proxy-Authorization"] = authRes.ToString ();
|
|
|
|
|
|
|
|
res = sendHttpRequest (netStream, req, 15000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static WebSocketStream CreateClientStream (
|
|
|
|
if (res.IsProxyAuthenticationRequired)
|
|
|
|
TcpClient client,
|
|
|
|
throw new WebSocketException ("Proxy authentication is required.");
|
|
|
|
bool secure,
|
|
|
|
}
|
|
|
|
string host,
|
|
|
|
|
|
|
|
System.Net.Security.RemoteCertificateValidationCallback validationCallback)
|
|
|
|
var code = res.StatusCode;
|
|
|
|
{
|
|
|
|
if (code.Length != 3 || code[0] != '2')
|
|
|
|
var netStream = client.GetStream ();
|
|
|
|
throw new WebSocketException (
|
|
|
|
if (secure) {
|
|
|
|
String.Format (
|
|
|
|
if (validationCallback == null)
|
|
|
|
"The proxy has failed a connection to the requested host and port. ({0})", code));
|
|
|
|
validationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var sslStream = new SslStream (netStream, false, validationCallback);
|
|
|
|
if (secure) {
|
|
|
|
sslStream.AuthenticateAsClient (host);
|
|
|
|
var sslStream = new SslStream (
|
|
|
|
|
|
|
|
netStream,
|
|
|
|
|
|
|
|
false,
|
|
|
|
|
|
|
|
validationCallback ?? ((sender, certificate, chain, sslPolicyErrors) => true));
|
|
|
|
|
|
|
|
|
|
|
|
return new WebSocketStream (sslStream);
|
|
|
|
sslStream.AuthenticateAsClient (targetUri.DnsSafeHost);
|
|
|
|
|
|
|
|
return new WebSocketStream (sslStream, secure);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new WebSocketStream (netStream);
|
|
|
|
return new WebSocketStream (netStream, secure);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static WebSocketStream CreateServerStream (
|
|
|
|
internal static WebSocketStream CreateServerStream (
|
|
|
|
TcpClient client, bool secure, X509Certificate cert)
|
|
|
|
TcpClient tcpClient, bool secure, X509Certificate certificate)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var netStream = client.GetStream ();
|
|
|
|
var netStream = tcpClient.GetStream ();
|
|
|
|
if (secure) {
|
|
|
|
if (secure) {
|
|
|
|
var sslStream = new SslStream (netStream, false);
|
|
|
|
var sslStream = new SslStream (netStream, false);
|
|
|
|
sslStream.AuthenticateAsServer (cert);
|
|
|
|
sslStream.AuthenticateAsServer (certificate);
|
|
|
|
|
|
|
|
|
|
|
|
return new WebSocketStream (sslStream);
|
|
|
|
return new WebSocketStream (sslStream, secure);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new WebSocketStream (netStream);
|
|
|
|
return new WebSocketStream (netStream, secure);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
internal HttpRequest ReadHttpRequest (int millisecondsTimeout)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_innerStream.Dispose ();
|
|
|
|
return readHttp<HttpRequest> (_innerStream, HttpRequest.Parse, millisecondsTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public WebSocketFrame ReadFrame ()
|
|
|
|
internal HttpResponse ReadHttpResponse (int millisecondsTimeout)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return WebSocketFrame.Parse (_innerStream, true);
|
|
|
|
return readHttp<HttpResponse> (_innerStream, HttpResponse.Parse, millisecondsTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ReadFrameAsync (Action<WebSocketFrame> completed, Action<Exception> error)
|
|
|
|
internal WebSocketFrame ReadWebSocketFrame ()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
WebSocketFrame.ParseAsync (_innerStream, true, completed, error);
|
|
|
|
return WebSocketFrame.Parse (_innerStream, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public HttpRequest ReadHandshakeRequest ()
|
|
|
|
internal void ReadWebSocketFrameAsync (
|
|
|
|
|
|
|
|
Action<WebSocketFrame> completed, Action<Exception> error)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return ReadHttp<HttpRequest> (HttpRequest.Parse, 90000);
|
|
|
|
WebSocketFrame.ParseAsync (_innerStream, true, completed, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public HttpResponse ReadHandshakeResponse ()
|
|
|
|
internal bool WriteBytes (byte[] data)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return ReadHttp<HttpResponse> (HttpResponse.Parse, 90000);
|
|
|
|
lock (_forWrite) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
_innerStream.Write (data, 0, data.Length);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool WriteFrame (WebSocketFrame frame)
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Public Methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Close ()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Write (frame.ToByteArray ());
|
|
|
|
_innerStream.Close ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool WriteHandshake (HttpBase handshake)
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Write (handshake.ToByteArray ());
|
|
|
|
_innerStream.Dispose ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|