Refactored TcpListenerWebSocketContext.cs

This commit is contained in:
sta 2014-07-20 22:04:44 +09:00
parent 1ef05fe0c3
commit 7cb6499c3a
3 changed files with 27 additions and 23 deletions

View File

@ -140,6 +140,14 @@ namespace WebSocketSharp
return res;
}
internal static HttpResponse CreateUnauthorizedResponse (string challenge)
{
var res = new HttpResponse (HttpStatusCode.Unauthorized);
res.Headers["WWW-Authenticate"] = challenge;
return res;
}
internal static HttpResponse CreateWebSocketResponse ()
{
var res = new HttpResponse (HttpStatusCode.SwitchingProtocols);

View File

@ -40,18 +40,16 @@ namespace WebSocketSharp.Net.WebSockets
/// Provides the properties used to access the information in a WebSocket connection request
/// received by the <see cref="TcpListener"/>.
/// </summary>
/// <remarks>
/// </remarks>
public class TcpListenerWebSocketContext : WebSocketContext
internal class TcpListenerWebSocketContext : WebSocketContext
{
#region Private Fields
private TcpClient _client;
private CookieCollection _cookies;
private NameValueCollection _queryString;
private HttpRequest _request;
private bool _secure;
private WebSocketStream _stream;
private TcpClient _tcpClient;
private Uri _uri;
private IPrincipal _user;
private WebSocket _websocket;
@ -61,11 +59,11 @@ namespace WebSocketSharp.Net.WebSockets
#region Internal Constructors
internal TcpListenerWebSocketContext (
TcpClient client, string protocol, bool secure, X509Certificate cert, Logger logger)
TcpClient tcpClient, string protocol, bool secure, X509Certificate certificate, Logger logger)
{
_client = client;
_tcpClient = tcpClient;
_secure = secure;
_stream = WebSocketStream.CreateServerStream (client, secure, cert);
_stream = WebSocketStream.CreateServerStream (tcpClient, secure, certificate);
_request = _stream.ReadHttpRequest (90000);
_uri = HttpUtility.CreateRequestUrl (
_request.RequestUri, _request.Headers["Host"], _request.IsWebSocketRequest, secure);
@ -268,7 +266,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value>
public override System.Net.IPEndPoint ServerEndPoint {
get {
return (System.Net.IPEndPoint) _client.Client.LocalEndPoint;
return (System.Net.IPEndPoint) _tcpClient.Client.LocalEndPoint;
}
}
@ -292,7 +290,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value>
public override System.Net.IPEndPoint UserEndPoint {
get {
return (System.Net.IPEndPoint) _client.Client.RemoteEndPoint;
return (System.Net.IPEndPoint) _tcpClient.Client.RemoteEndPoint;
}
}
@ -316,7 +314,7 @@ namespace WebSocketSharp.Net.WebSockets
internal void Close ()
{
_stream.Close ();
_client.Close ();
_tcpClient.Close ();
}
internal void Close (HttpStatusCode code)
@ -324,11 +322,9 @@ namespace WebSocketSharp.Net.WebSockets
_websocket.Close (HttpResponse.CreateCloseResponse (code));
}
internal void SendAuthChallenge (string challenge)
internal void SendAuthenticationChallenge (string challenge)
{
var res = new HttpResponse (HttpStatusCode.Unauthorized);
res.Headers ["WWW-Authenticate"] = challenge;
_stream.WriteBytes (res.ToByteArray ());
_stream.WriteHttp (HttpResponse.CreateUnauthorizedResponse (challenge));
_request = _stream.ReadHttpRequest (15000);
}

View File

@ -546,7 +546,7 @@ namespace WebSocketSharp.Server
var header = context.Headers ["Authorization"];
if (header == null || !header.StartsWith (expected, StringComparison.OrdinalIgnoreCase)) {
context.SendAuthChallenge (challenge);
context.SendAuthenticationChallenge (challenge);
return auth ();
}
@ -554,7 +554,7 @@ namespace WebSocketSharp.Server
if (context.IsAuthenticated)
return true;
context.SendAuthChallenge (challenge);
context.SendAuthenticationChallenge (challenge);
return auth ();
};