2013-04-15 10:48:51 +08:00
|
|
|
#region License
|
2013-01-11 19:32:38 +08:00
|
|
|
/*
|
2013-08-03 16:46:06 +08:00
|
|
|
* HandshakeResponse.cs
|
2012-08-31 16:41:15 +08:00
|
|
|
*
|
|
|
|
* The MIT License
|
|
|
|
*
|
2014-01-01 20:43:18 +08:00
|
|
|
* Copyright (c) 2012-2014 sta.blockhead
|
|
|
|
*
|
2012-08-31 16:41:15 +08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2014-01-01 20:43:18 +08:00
|
|
|
*
|
2012-08-31 16:41:15 +08:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
using System.Text;
|
2012-09-10 00:36:22 +08:00
|
|
|
using WebSocketSharp.Net;
|
2012-08-31 16:41:15 +08:00
|
|
|
|
2013-08-03 13:56:00 +08:00
|
|
|
namespace WebSocketSharp
|
|
|
|
{
|
2013-08-03 16:46:06 +08:00
|
|
|
internal class HandshakeResponse : HandshakeBase
|
2012-08-31 16:41:15 +08:00
|
|
|
{
|
2014-01-01 20:43:18 +08:00
|
|
|
#region Private Fields
|
|
|
|
|
|
|
|
private string _code;
|
|
|
|
private string _reason;
|
|
|
|
|
|
|
|
#endregion
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2014-01-01 20:43:18 +08:00
|
|
|
#region Private Constructors
|
|
|
|
|
|
|
|
private HandshakeResponse ()
|
2012-08-31 16:41:15 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-01 20:43:18 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public Constructors
|
|
|
|
|
2013-08-03 16:46:06 +08:00
|
|
|
public HandshakeResponse (HttpStatusCode code)
|
2012-10-01 14:26:31 +08:00
|
|
|
{
|
2014-01-01 20:43:18 +08:00
|
|
|
_code = ((int) code).ToString ();
|
|
|
|
_reason = code.GetDescription ();
|
|
|
|
|
|
|
|
var headers = Headers;
|
|
|
|
headers ["Server"] = "websocket-sharp/1.0";
|
|
|
|
if (code == HttpStatusCode.SwitchingProtocols) {
|
|
|
|
headers ["Upgrade"] = "websocket";
|
|
|
|
headers ["Connection"] = "Upgrade";
|
|
|
|
}
|
2012-10-01 14:26:31 +08:00
|
|
|
}
|
|
|
|
|
2012-09-10 00:36:22 +08:00
|
|
|
#endregion
|
|
|
|
|
2013-04-15 10:48:51 +08:00
|
|
|
#region Public Properties
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2013-06-10 16:49:44 +08:00
|
|
|
public AuthenticationChallenge AuthChallenge {
|
|
|
|
get {
|
2013-08-06 20:31:21 +08:00
|
|
|
var challenge = Headers ["WWW-Authenticate"];
|
2014-01-15 16:36:23 +08:00
|
|
|
return challenge != null && challenge.Length > 0
|
2013-08-06 20:31:21 +08:00
|
|
|
? AuthenticationChallenge.Parse (challenge)
|
2013-06-10 16:49:44 +08:00
|
|
|
: null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-06 15:21:41 +08:00
|
|
|
public CookieCollection Cookies {
|
|
|
|
get {
|
2013-08-03 13:56:00 +08:00
|
|
|
return Headers.GetCookies (true);
|
2013-04-06 15:21:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 16:49:44 +08:00
|
|
|
public bool IsUnauthorized {
|
|
|
|
get {
|
2014-01-01 20:43:18 +08:00
|
|
|
return _code == "401";
|
2013-06-10 16:49:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 16:41:15 +08:00
|
|
|
public bool IsWebSocketResponse {
|
|
|
|
get {
|
2014-01-01 20:43:18 +08:00
|
|
|
var headers = Headers;
|
2013-08-06 20:31:21 +08:00
|
|
|
return ProtocolVersion >= HttpVersion.Version11 &&
|
2014-01-01 20:43:18 +08:00
|
|
|
_code == "101" &&
|
|
|
|
headers.Contains ("Upgrade", "websocket") &&
|
|
|
|
headers.Contains ("Connection", "Upgrade");
|
2012-08-31 16:41:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-08 14:03:13 +08:00
|
|
|
public string Reason {
|
2014-01-01 20:43:18 +08:00
|
|
|
get {
|
|
|
|
return _reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
private set {
|
|
|
|
_reason = value;
|
|
|
|
}
|
2013-05-08 14:03:13 +08:00
|
|
|
}
|
2012-10-01 14:26:31 +08:00
|
|
|
|
2013-05-08 14:03:13 +08:00
|
|
|
public string StatusCode {
|
2014-01-01 20:43:18 +08:00
|
|
|
get {
|
|
|
|
return _code;
|
|
|
|
}
|
|
|
|
|
|
|
|
private set {
|
|
|
|
_code = value;
|
|
|
|
}
|
2013-05-08 14:03:13 +08:00
|
|
|
}
|
2012-09-10 00:36:22 +08:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2013-04-15 10:48:51 +08:00
|
|
|
#region Public Methods
|
2012-10-01 14:26:31 +08:00
|
|
|
|
2013-08-03 16:46:06 +08:00
|
|
|
public static HandshakeResponse CreateCloseResponse (HttpStatusCode code)
|
2012-10-01 14:26:31 +08:00
|
|
|
{
|
2013-08-03 16:46:06 +08:00
|
|
|
var res = new HandshakeResponse (code);
|
2014-01-01 20:43:18 +08:00
|
|
|
res.Headers ["Connection"] = "close";
|
2012-10-01 14:26:31 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2012-08-31 16:41:15 +08:00
|
|
|
|
2014-01-15 16:36:23 +08:00
|
|
|
public static HandshakeResponse Parse (string [] headerParts)
|
2012-08-31 16:41:15 +08:00
|
|
|
{
|
2014-01-15 16:36:23 +08:00
|
|
|
var statusLine = headerParts [0].Split (new char [] { ' ' }, 3);
|
|
|
|
if (statusLine.Length != 3)
|
|
|
|
throw new ArgumentException ("Invalid status line: " + headerParts [0]);
|
2012-08-31 16:41:15 +08:00
|
|
|
|
2013-08-03 13:56:00 +08:00
|
|
|
var headers = new WebHeaderCollection ();
|
2014-01-15 16:36:23 +08:00
|
|
|
for (int i = 1; i < headerParts.Length; i++)
|
|
|
|
headers.SetInternal (headerParts [i], true);
|
2012-08-31 16:41:15 +08:00
|
|
|
|
2013-08-03 16:46:06 +08:00
|
|
|
return new HandshakeResponse {
|
2013-05-08 14:03:13 +08:00
|
|
|
Headers = headers,
|
2014-01-15 16:36:23 +08:00
|
|
|
ProtocolVersion = new Version (statusLine [0].Substring (5)),
|
|
|
|
Reason = statusLine [2],
|
|
|
|
StatusCode = statusLine [1]
|
2012-08-31 16:41:15 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-08-03 13:56:00 +08:00
|
|
|
public void SetCookies (CookieCollection cookies)
|
2013-04-06 15:21:41 +08:00
|
|
|
{
|
2013-05-08 14:03:13 +08:00
|
|
|
if (cookies == null || cookies.Count == 0)
|
2013-04-06 15:21:41 +08:00
|
|
|
return;
|
|
|
|
|
2014-01-01 20:43:18 +08:00
|
|
|
var headers = Headers;
|
2013-04-06 15:21:41 +08:00
|
|
|
foreach (var cookie in cookies.Sorted)
|
2014-01-01 20:43:18 +08:00
|
|
|
headers.Add ("Set-Cookie", cookie.ToResponseString ());
|
2013-04-06 15:21:41 +08:00
|
|
|
}
|
|
|
|
|
2013-08-03 13:56:00 +08:00
|
|
|
public override string ToString ()
|
2012-08-31 16:41:15 +08:00
|
|
|
{
|
2013-08-03 13:56:00 +08:00
|
|
|
var buffer = new StringBuilder (64);
|
2014-01-01 20:43:18 +08:00
|
|
|
buffer.AppendFormat (
|
|
|
|
"HTTP/{0} {1} {2}{3}", ProtocolVersion, _code, _reason, CrLf);
|
|
|
|
|
|
|
|
var headers = Headers;
|
|
|
|
foreach (var key in headers.AllKeys)
|
|
|
|
buffer.AppendFormat ("{0}: {1}{2}", key, headers [key], CrLf);
|
2012-08-31 16:41:15 +08:00
|
|
|
|
2013-08-03 13:56:00 +08:00
|
|
|
buffer.Append (CrLf);
|
2014-01-14 21:42:02 +08:00
|
|
|
|
|
|
|
var entity = EntityBody;
|
|
|
|
if (entity.Length > 0)
|
|
|
|
buffer.Append (entity);
|
|
|
|
|
2013-08-03 13:56:00 +08:00
|
|
|
return buffer.ToString ();
|
2012-08-31 16:41:15 +08:00
|
|
|
}
|
2012-09-10 00:36:22 +08:00
|
|
|
|
|
|
|
#endregion
|
2012-08-31 16:41:15 +08:00
|
|
|
}
|
|
|
|
}
|