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; 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 () internal static HttpResponse CreateWebSocketResponse ()
{ {
var res = new HttpResponse (HttpStatusCode.SwitchingProtocols); 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 /// Provides the properties used to access the information in a WebSocket connection request
/// received by the <see cref="TcpListener"/>. /// received by the <see cref="TcpListener"/>.
/// </summary> /// </summary>
/// <remarks> internal class TcpListenerWebSocketContext : WebSocketContext
/// </remarks>
public class TcpListenerWebSocketContext : WebSocketContext
{ {
#region Private Fields #region Private Fields
private TcpClient _client;
private CookieCollection _cookies; private CookieCollection _cookies;
private NameValueCollection _queryString; private NameValueCollection _queryString;
private HttpRequest _request; private HttpRequest _request;
private bool _secure; private bool _secure;
private WebSocketStream _stream; private WebSocketStream _stream;
private TcpClient _tcpClient;
private Uri _uri; private Uri _uri;
private IPrincipal _user; private IPrincipal _user;
private WebSocket _websocket; private WebSocket _websocket;
@ -61,14 +59,14 @@ namespace WebSocketSharp.Net.WebSockets
#region Internal Constructors #region Internal Constructors
internal TcpListenerWebSocketContext ( 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; _secure = secure;
_stream = WebSocketStream.CreateServerStream (client, secure, cert); _stream = WebSocketStream.CreateServerStream (tcpClient, secure, certificate);
_request = _stream.ReadHttpRequest (90000); _request = _stream.ReadHttpRequest (90000);
_uri = HttpUtility.CreateRequestUrl ( _uri = HttpUtility.CreateRequestUrl (
_request.RequestUri, _request.Headers ["Host"], _request.IsWebSocketRequest, secure); _request.RequestUri, _request.Headers["Host"], _request.IsWebSocketRequest, secure);
_websocket = new WebSocket (this, protocol, logger); _websocket = new WebSocket (this, protocol, logger);
} }
@ -119,7 +117,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override string Host { public override string Host {
get { get {
return _request.Headers ["Host"]; return _request.Headers["Host"];
} }
} }
@ -179,7 +177,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override string Origin { public override string Origin {
get { get {
return _request.Headers ["Origin"]; return _request.Headers["Origin"];
} }
} }
@ -193,7 +191,7 @@ namespace WebSocketSharp.Net.WebSockets
get { get {
return _queryString ?? return _queryString ??
(_queryString = HttpUtility.ParseQueryStringInternally ( (_queryString = HttpUtility.ParseQueryStringInternally (
_uri != null ? _uri.Query : null, Encoding.UTF8)); _uri != null ? _uri.Query : null, Encoding.UTF8));
} }
} }
@ -221,7 +219,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override string SecWebSocketKey { public override string SecWebSocketKey {
get { get {
return _request.Headers ["Sec-WebSocket-Key"]; return _request.Headers["Sec-WebSocket-Key"];
} }
} }
@ -238,7 +236,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override IEnumerable<string> SecWebSocketProtocols { public override IEnumerable<string> SecWebSocketProtocols {
get { get {
var protocols = _request.Headers ["Sec-WebSocket-Protocol"]; var protocols = _request.Headers["Sec-WebSocket-Protocol"];
if (protocols != null) if (protocols != null)
foreach (var protocol in protocols.Split (',')) foreach (var protocol in protocols.Split (','))
yield return protocol.Trim (); yield return protocol.Trim ();
@ -256,7 +254,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override string SecWebSocketVersion { public override string SecWebSocketVersion {
get { get {
return _request.Headers ["Sec-WebSocket-Version"]; return _request.Headers["Sec-WebSocket-Version"];
} }
} }
@ -268,7 +266,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override System.Net.IPEndPoint ServerEndPoint { public override System.Net.IPEndPoint ServerEndPoint {
get { get {
return (System.Net.IPEndPoint) _client.Client.LocalEndPoint; return (System.Net.IPEndPoint) _tcpClient.Client.LocalEndPoint;
} }
} }
@ -292,7 +290,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override System.Net.IPEndPoint UserEndPoint { public override System.Net.IPEndPoint UserEndPoint {
get { 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 () internal void Close ()
{ {
_stream.Close (); _stream.Close ();
_client.Close (); _tcpClient.Close ();
} }
internal void Close (HttpStatusCode code) internal void Close (HttpStatusCode code)
@ -324,11 +322,9 @@ namespace WebSocketSharp.Net.WebSockets
_websocket.Close (HttpResponse.CreateCloseResponse (code)); _websocket.Close (HttpResponse.CreateCloseResponse (code));
} }
internal void SendAuthChallenge (string challenge) internal void SendAuthenticationChallenge (string challenge)
{ {
var res = new HttpResponse (HttpStatusCode.Unauthorized); _stream.WriteHttp (HttpResponse.CreateUnauthorizedResponse (challenge));
res.Headers ["WWW-Authenticate"] = challenge;
_stream.WriteBytes (res.ToByteArray ());
_request = _stream.ReadHttpRequest (15000); _request = _stream.ReadHttpRequest (15000);
} }

View File

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