Modified opening and closing handshake

This commit is contained in:
sta 2013-08-06 21:31:21 +09:00
parent 7deddda2f9
commit 69c9be3eb5
9 changed files with 514 additions and 502 deletions

View File

@ -56,7 +56,7 @@ namespace WebSocketSharp
{ {
_code = getCodeFrom (data); _code = getCodeFrom (data);
_reason = getReasonFrom (data); _reason = getReasonFrom (data);
_clean = true; _clean = false;
} }
#endregion #endregion

View File

@ -225,6 +225,11 @@ namespace WebSocketSharp {
: stream.ToByteArray(); : stream.ToByteArray();
} }
internal static bool Equals (this string value, CompressionMethod method)
{
return value == method.ToCompressionExtension ();
}
// <summary> // <summary>
// Determines whether the specified <see cref="int"/> equals the specified <see cref="char"/>, // Determines whether the specified <see cref="int"/> equals the specified <see cref="char"/>,
// and invokes the specified Action&lt;int&gt; delegate at the same time. // and invokes the specified Action&lt;int&gt; delegate at the same time.
@ -289,6 +294,11 @@ namespace WebSocketSharp {
return new TcpListenerWebSocketContext(client, secure, cert); return new TcpListenerWebSocketContext(client, secure, cert);
} }
internal static bool IsCompressionExtension (this string value)
{
return value.StartsWith ("permessage-");
}
// <summary> // <summary>
// Determines whether the specified object is <see langword="null"/>. // Determines whether the specified object is <see langword="null"/>.
// </summary> // </summary>
@ -514,6 +524,22 @@ namespace WebSocketSharp {
} }
} }
internal static string ToCompressionExtension (this CompressionMethod method)
{
return method != CompressionMethod.NONE
? String.Format ("permessage-{0}", method.ToString ().ToLower ())
: String.Empty;
}
internal static CompressionMethod ToCompressionMethod (this string value)
{
foreach (CompressionMethod method in Enum.GetValues (typeof (CompressionMethod)))
if (value.Equals (method))
return method;
return CompressionMethod.NONE;
}
internal static string Unquote(this string value) internal static string Unquote(this string value)
{ {
var start = value.IndexOf('\"'); var start = value.IndexOf('\"');

View File

@ -78,19 +78,10 @@ namespace WebSocketSharp
public bool IsWebSocketRequest { public bool IsWebSocketRequest {
get { get {
return HttpMethod != "GET" return HttpMethod == "GET" &&
? false ProtocolVersion >= HttpVersion.Version11 &&
: ProtocolVersion < HttpVersion.Version11 Headers.Contains ("Upgrade", "websocket") &&
? false Headers.Contains ("Connection", "Upgrade");
: !ContainsHeader ("Upgrade", "websocket")
? false
: !ContainsHeader ("Connection", "Upgrade")
? false
: !ContainsHeader ("Host")
? false
: !ContainsHeader ("Sec-WebSocket-Key")
? false
: ContainsHeader ("Sec-WebSocket-Version");
} }
} }
@ -158,16 +149,6 @@ namespace WebSocketSharp
}; };
} }
public static HandshakeRequest Parse (WebSocketContext context)
{
return new HandshakeRequest {
Headers = context.Headers,
HttpMethod = "GET",
RequestUri = context.RequestUri,
ProtocolVersion = HttpVersion.Version11
};
}
public void SetCookies (CookieCollection cookies) public void SetCookies (CookieCollection cookies)
{ {
if (cookies == null || cookies.Count == 0) if (cookies == null || cookies.Count == 0)

View File

@ -57,8 +57,9 @@ namespace WebSocketSharp
public AuthenticationChallenge AuthChallenge { public AuthenticationChallenge AuthChallenge {
get { get {
return ContainsHeader ("WWW-Authenticate") var challenge = Headers ["WWW-Authenticate"];
? AuthenticationChallenge.Parse (Headers ["WWW-Authenticate"]) return !challenge.IsNullOrEmpty ()
? AuthenticationChallenge.Parse (challenge)
: null; : null;
} }
} }
@ -77,15 +78,11 @@ namespace WebSocketSharp
public bool IsWebSocketResponse { public bool IsWebSocketResponse {
get { get {
return ProtocolVersion < HttpVersion.Version11 return ProtocolVersion >= HttpVersion.Version11 &&
? false StatusCode == "101" &&
: StatusCode != "101" Headers.Contains ("Upgrade", "websocket") &&
? false Headers.Contains ("Connection", "Upgrade") &&
: !ContainsHeader ("Upgrade", "websocket") !Headers ["Sec-WebSocket-Accept"].IsNullOrEmpty ();
? false
: !ContainsHeader ("Connection", "Upgrade")
? false
: ContainsHeader ("Sec-WebSocket-Accept");
} }
} }

View File

@ -7,7 +7,7 @@
// Gonzalo Paniagua Javier (gonzalo@novell.com) // Gonzalo Paniagua Javier (gonzalo@novell.com)
// //
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com) // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
// Copyright (c) 2012-2013 sta.blockhead (sta.blockhead@gmail.com) // Copyright (c) 2012-2013 sta.blockhead
// //
// Permission is hereby granted, free of charge, to any person obtaining // Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the // a copy of this software and associated documentation files (the
@ -40,16 +40,16 @@ using System.Net;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
namespace WebSocketSharp.Net { namespace WebSocketSharp.Net
{
/// <summary> /// <summary>
/// Provides access to a request to a <see cref="HttpListener"/> instance. /// Provides access to a request to a <see cref="HttpListener"/> instance.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The HttpListenerRequest class cannot be inherited. /// The HttpListenerRequest class cannot be inherited.
/// </remarks> /// </remarks>
public sealed class HttpListenerRequest { public sealed class HttpListenerRequest
{
#region Private Static Fields #region Private Static Fields
private static byte [] _100continue = Encoding.ASCII.GetBytes ("HTTP/1.1 100 Continue\r\n\r\n"); private static byte [] _100continue = Encoding.ASCII.GetBytes ("HTTP/1.1 100 Continue\r\n\r\n");
@ -282,19 +282,10 @@ namespace WebSocketSharp.Net {
/// </value> /// </value>
public bool IsWebSocketRequest { public bool IsWebSocketRequest {
get { get {
return _method != "GET" return _method == "GET" &&
? false _version >= HttpVersion.Version11 &&
: _version < HttpVersion.Version11 _headers.Contains ("Upgrade", "websocket") &&
? false _headers.Contains ("Connection", "Upgrade");
: !_headers.Contains("Upgrade", "websocket")
? false
: !_headers.Contains("Connection", "Upgrade")
? false
: !_headers.Contains("Host")
? false
: !_headers.Contains("Sec-WebSocket-Key")
? false
: _headers.Contains("Sec-WebSocket-Version");
} }
} }
@ -744,6 +735,23 @@ namespace WebSocketSharp.Net {
throw new NotImplementedException (); throw new NotImplementedException ();
} }
/// <summary>
/// Returns a <see cref="string"/> that represents the current <see cref="HttpListenerRequest"/>.
/// </summary>
/// <returns>
/// A <see cref="string"/> that represents the current <see cref="HttpListenerRequest"/>.
/// </returns>
public override string ToString ()
{
var buffer = new StringBuilder (64);
buffer.AppendFormat ("{0} {1} HTTP/{2}\r\n", _method, _rawUrl, _version);
foreach (string key in _headers.AllKeys)
buffer.AppendFormat ("{0}: {1}\r\n", key, _headers [key]);
buffer.Append ("\r\n");
return buffer.ToString ();
}
#endregion #endregion
} }
} }

View File

@ -31,10 +31,10 @@ using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Security.Principal; using System.Security.Principal;
namespace WebSocketSharp.Net.WebSockets { namespace WebSocketSharp.Net.WebSockets
{
/// <summary> /// <summary>
/// Provides access to the WebSocket connection request objects received by the <see cref="HttpListener"/> class. /// Provides access to the WebSocket connection request objects received by the <see cref="HttpListener"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// </remarks> /// </remarks>
@ -44,17 +44,17 @@ namespace WebSocketSharp.Net.WebSockets {
private HttpListenerContext _context; private HttpListenerContext _context;
private WebSocket _websocket; private WebSocket _websocket;
private WsStream _wsStream; private WsStream _stream;
#endregion #endregion
#region Internal Constructors #region Internal Constructors
internal HttpListenerWebSocketContext(HttpListenerContext context) internal HttpListenerWebSocketContext (HttpListenerContext context)
{ {
_context = context; _context = context;
_wsStream = WsStream.CreateServerStream(context); _stream = WsStream.CreateServerStream (context);
_websocket = new WebSocket(this); _websocket = new WebSocket (this);
} }
#endregion #endregion
@ -63,7 +63,7 @@ namespace WebSocketSharp.Net.WebSockets {
internal WsStream Stream { internal WsStream Stream {
get { get {
return _wsStream; return _stream;
} }
} }
@ -95,6 +95,18 @@ namespace WebSocketSharp.Net.WebSockets {
} }
} }
/// <summary>
/// Gets the value of the Host header field used in the WebSocket opening handshake.
/// </summary>
/// <value>
/// A <see cref="string"/> that contains the value of the Host header field.
/// </value>
public override string Host {
get {
return _context.Request.Headers ["Host"];
}
}
/// <summary> /// <summary>
/// Gets a value indicating whether the client is authenticated. /// Gets a value indicating whether the client is authenticated.
/// </summary> /// </summary>
@ -132,18 +144,14 @@ namespace WebSocketSharp.Net.WebSockets {
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the WebSocket connection request is valid. /// Gets a value indicating whether the request is a WebSocket connection request.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the WebSocket connection request is valid; otherwise, <c>false</c>. /// <c>true</c> if the request is a WebSocket connection request; otherwise, <c>false</c>.
/// </value> /// </value>
public override bool IsValid { public override bool IsWebSocketRequest {
get { get {
return !_context.Request.IsWebSocketRequest return _context.Request.IsWebSocketRequest;
? false
: SecWebSocketKey.IsNullOrEmpty()
? false
: !SecWebSocketVersion.IsNullOrEmpty();
} }
} }
@ -155,7 +163,7 @@ namespace WebSocketSharp.Net.WebSockets {
/// </value> /// </value>
public override string Origin { public override string Origin {
get { get {
return Headers["Origin"]; return _context.Request.Headers ["Origin"];
} }
} }
@ -167,7 +175,7 @@ namespace WebSocketSharp.Net.WebSockets {
/// </value> /// </value>
public override string Path { public override string Path {
get { get {
return RequestUri.GetAbsolutePath(); return RequestUri.GetAbsolutePath ();
} }
} }
@ -191,7 +199,7 @@ namespace WebSocketSharp.Net.WebSockets {
/// </value> /// </value>
public override Uri RequestUri { public override Uri RequestUri {
get { get {
return _context.Request.RawUrl.ToUri(); return _context.Request.RawUrl.ToUri ();
} }
} }
@ -206,7 +214,7 @@ namespace WebSocketSharp.Net.WebSockets {
/// </value> /// </value>
public override string SecWebSocketKey { public override string SecWebSocketKey {
get { get {
return Headers["Sec-WebSocket-Key"]; return _context.Request.Headers ["Sec-WebSocket-Key"];
} }
} }
@ -221,7 +229,7 @@ namespace WebSocketSharp.Net.WebSockets {
/// </value> /// </value>
public override IEnumerable<string> SecWebSocketProtocols { public override IEnumerable<string> SecWebSocketProtocols {
get { get {
return Headers.GetValues("Sec-WebSocket-Protocol"); return _context.Request.Headers.GetValues ("Sec-WebSocket-Protocol");
} }
} }
@ -236,7 +244,7 @@ namespace WebSocketSharp.Net.WebSockets {
/// </value> /// </value>
public override string SecWebSocketVersion { public override string SecWebSocketVersion {
get { get {
return Headers["Sec-WebSocket-Version"]; return _context.Request.Headers ["Sec-WebSocket-Version"];
} }
} }
@ -246,7 +254,7 @@ namespace WebSocketSharp.Net.WebSockets {
/// <value> /// <value>
/// A <see cref="System.Net.IPEndPoint"/> that contains the server endpoint. /// A <see cref="System.Net.IPEndPoint"/> that contains the server endpoint.
/// </value> /// </value>
public virtual System.Net.IPEndPoint ServerEndPoint { public override System.Net.IPEndPoint ServerEndPoint {
get { get {
return _context.Connection.LocalEndPoint; return _context.Connection.LocalEndPoint;
} }
@ -270,7 +278,7 @@ namespace WebSocketSharp.Net.WebSockets {
/// <value> /// <value>
/// A <see cref="System.Net.IPEndPoint"/> that contains the client endpoint. /// A <see cref="System.Net.IPEndPoint"/> that contains the client endpoint.
/// </value> /// </value>
public virtual System.Net.IPEndPoint UserEndPoint { public override System.Net.IPEndPoint UserEndPoint {
get { get {
return _context.Connection.RemoteEndPoint; return _context.Connection.RemoteEndPoint;
} }
@ -292,9 +300,24 @@ namespace WebSocketSharp.Net.WebSockets {
#region Internal Methods #region Internal Methods
internal void Close() internal void Close ()
{ {
_context.Connection.Close(true); _context.Connection.Close (true);
}
#endregion
#region Public Methods
/// <summary>
/// Returns a <see cref="string"/> that represents the current <see cref="HttpListenerWebSocketContext"/>.
/// </summary>
/// <returns>
/// A <see cref="string"/> that represents the current <see cref="HttpListenerWebSocketContext"/>.
/// </returns>
public override string ToString ()
{
return _context.Request.ToString ();
} }
#endregion #endregion

View File

@ -105,6 +105,18 @@ namespace WebSocketSharp.Net.WebSockets
} }
} }
/// <summary>
/// Gets the value of the Host header field used in the WebSocket opening handshake.
/// </summary>
/// <value>
/// A <see cref="string"/> that contains the value of the Host header field.
/// </value>
public override string Host {
get {
return _request.Headers ["Host"];
}
}
/// <summary> /// <summary>
/// Gets a value indicating whether the client is authenticated. /// Gets a value indicating whether the client is authenticated.
/// </summary> /// </summary>
@ -145,18 +157,14 @@ namespace WebSocketSharp.Net.WebSockets
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the WebSocket connection request is valid. /// Gets a value indicating whether the request is a WebSocket connection request.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the WebSocket connection request is valid; otherwise, <c>false</c>. /// <c>true</c> if the request is a WebSocket connection request; otherwise, <c>false</c>.
/// </value> /// </value>
public override bool IsValid { public override bool IsWebSocketRequest {
get { get {
return !_request.IsWebSocketRequest return _request.IsWebSocketRequest;
? false
: SecWebSocketKey.IsNullOrEmpty ()
? false
: !SecWebSocketVersion.IsNullOrEmpty ();
} }
} }
@ -168,7 +176,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override string Origin { public override string Origin {
get { get {
return Headers ["Origin"]; return _request.Headers ["Origin"];
} }
} }
@ -219,7 +227,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override string SecWebSocketKey { public override string SecWebSocketKey {
get { get {
return Headers ["Sec-WebSocket-Key"]; return _request.Headers ["Sec-WebSocket-Key"];
} }
} }
@ -227,14 +235,14 @@ namespace WebSocketSharp.Net.WebSockets
/// Gets the values of the Sec-WebSocket-Protocol header field used in the WebSocket opening handshake. /// Gets the values of the Sec-WebSocket-Protocol header field used in the WebSocket opening handshake.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The SecWebSocketProtocols property indicates the subprotocols of the WebSocket connection. /// This property indicates the subprotocols of the WebSocket connection.
/// </remarks> /// </remarks>
/// <value> /// <value>
/// An IEnumerable&lt;string&gt; that contains the values of the Sec-WebSocket-Protocol header field. /// An IEnumerable&lt;string&gt; that contains the values of the Sec-WebSocket-Protocol header field.
/// </value> /// </value>
public override IEnumerable<string> SecWebSocketProtocols { public override IEnumerable<string> SecWebSocketProtocols {
get { get {
return Headers.GetValues ("Sec-WebSocket-Protocol"); return _request.Headers.GetValues ("Sec-WebSocket-Protocol");
} }
} }
@ -249,7 +257,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value> /// </value>
public override string SecWebSocketVersion { public override string SecWebSocketVersion {
get { get {
return Headers ["Sec-WebSocket-Version"]; return _request.Headers ["Sec-WebSocket-Version"];
} }
} }
@ -259,7 +267,7 @@ namespace WebSocketSharp.Net.WebSockets
/// <value> /// <value>
/// A <see cref="System.Net.IPEndPoint"/> that contains the server endpoint. /// A <see cref="System.Net.IPEndPoint"/> that contains the server endpoint.
/// </value> /// </value>
public virtual System.Net.IPEndPoint ServerEndPoint { public override System.Net.IPEndPoint ServerEndPoint {
get { get {
return (System.Net.IPEndPoint) _client.Client.LocalEndPoint; return (System.Net.IPEndPoint) _client.Client.LocalEndPoint;
} }
@ -286,7 +294,7 @@ namespace WebSocketSharp.Net.WebSockets
/// <value> /// <value>
/// A <see cref="System.Net.IPEndPoint"/> that contains the client endpoint. /// A <see cref="System.Net.IPEndPoint"/> that contains the client endpoint.
/// </value> /// </value>
public virtual System.Net.IPEndPoint UserEndPoint { public override System.Net.IPEndPoint UserEndPoint {
get { get {
return (System.Net.IPEndPoint) _client.Client.RemoteEndPoint; return (System.Net.IPEndPoint) _client.Client.RemoteEndPoint;
} }
@ -315,5 +323,20 @@ namespace WebSocketSharp.Net.WebSockets
} }
#endregion #endregion
#region Public Methods
/// <summary>
/// Returns a <see cref="string"/> that represents the current <see cref="TcpListenerWebSocketContext"/>.
/// </summary>
/// <returns>
/// A <see cref="string"/> that represents the current <see cref="TcpListenerWebSocketContext"/>.
/// </returns>
public override string ToString ()
{
return _request.ToString ();
}
#endregion
} }
} }

View File

@ -31,22 +31,22 @@ using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Security.Principal; using System.Security.Principal;
namespace WebSocketSharp.Net.WebSockets { namespace WebSocketSharp.Net.WebSockets
{
/// <summary> /// <summary>
/// Provides access to the WebSocket connection request objects. /// Provides access to the WebSocket connection request objects.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The WebSocketContext class is an abstract class. /// The WebSocketContext class is an abstract class.
/// </remarks> /// </remarks>
public abstract class WebSocketContext { public abstract class WebSocketContext
{
#region Protected Constructors #region Protected Constructors
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebSocketSharp.Net.WebSockets.WebSocketContext"/> class. /// Initializes a new instance of the <see cref="WebSocketSharp.Net.WebSockets.WebSocketContext"/> class.
/// </summary> /// </summary>
protected WebSocketContext() protected WebSocketContext ()
{ {
} }
@ -70,6 +70,14 @@ namespace WebSocketSharp.Net.WebSockets {
/// </value> /// </value>
public abstract NameValueCollection Headers { get; } public abstract NameValueCollection Headers { get; }
/// <summary>
/// Gets the value of the Host header field used in the WebSocket opening handshake.
/// </summary>
/// <value>
/// A <see cref="string"/> that contains the value of the Host header field.
/// </value>
public abstract string Host { get; }
/// <summary> /// <summary>
/// Gets a value indicating whether the client is authenticated. /// Gets a value indicating whether the client is authenticated.
/// </summary> /// </summary>
@ -95,12 +103,12 @@ namespace WebSocketSharp.Net.WebSockets {
public abstract bool IsSecureConnection { get; } public abstract bool IsSecureConnection { get; }
/// <summary> /// <summary>
/// Gets a value indicating whether the WebSocket connection request is valid. /// Gets a value indicating whether the request is a WebSocket connection request.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the WebSocket connection request is valid; otherwise, <c>false</c>. /// <c>true</c> if the request is a WebSocket connection request; otherwise, <c>false</c>.
/// </value> /// </value>
public abstract bool IsValid { get; } public abstract bool IsWebSocketRequest { get; }
/// <summary> /// <summary>
/// Gets the value of the Origin header field used in the WebSocket opening handshake. /// Gets the value of the Origin header field used in the WebSocket opening handshake.
@ -167,6 +175,14 @@ namespace WebSocketSharp.Net.WebSockets {
/// </value> /// </value>
public abstract string SecWebSocketVersion { get; } public abstract string SecWebSocketVersion { get; }
/// <summary>
/// Gets the server endpoint as an IP address and a port number.
/// </summary>
/// <value>
/// A <see cref="System.Net.IPEndPoint"/> that contains the server endpoint.
/// </value>
public abstract System.Net.IPEndPoint ServerEndPoint { get; }
/// <summary> /// <summary>
/// Gets the client information (identity, authentication information and security roles). /// Gets the client information (identity, authentication information and security roles).
/// </summary> /// </summary>
@ -175,6 +191,14 @@ namespace WebSocketSharp.Net.WebSockets {
/// </value> /// </value>
public abstract IPrincipal User { get; } public abstract IPrincipal User { get; }
/// <summary>
/// Gets the client endpoint as an IP address and a port number.
/// </summary>
/// <value>
/// A <see cref="System.Net.IPEndPoint"/> that contains the client endpoint.
/// </value>
public abstract System.Net.IPEndPoint UserEndPoint { get; }
/// <summary> /// <summary>
/// Gets the WebSocket instance used for two-way communication between client and server. /// Gets the WebSocket instance used for two-way communication between client and server.
/// </summary> /// </summary>

File diff suppressed because it is too large Load Diff