2012-08-31 16:41:15 +08:00
|
|
|
#region MIT License
|
2013-01-11 19:32:38 +08:00
|
|
|
/*
|
2012-08-31 16:41:15 +08:00
|
|
|
* ResponseHandshake.cs
|
|
|
|
*
|
|
|
|
* The MIT License
|
|
|
|
*
|
2013-02-26 16:31:15 +08:00
|
|
|
* Copyright (c) 2012-2013 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
|
|
namespace WebSocketSharp {
|
|
|
|
|
2013-01-11 19:32:38 +08:00
|
|
|
internal class ResponseHandshake : Handshake
|
2012-08-31 16:41:15 +08:00
|
|
|
{
|
2012-10-01 14:26:31 +08:00
|
|
|
#region Constructor
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2012-08-31 16:41:15 +08:00
|
|
|
public ResponseHandshake()
|
2012-10-01 14:26:31 +08:00
|
|
|
: this(HttpStatusCode.SwitchingProtocols)
|
2012-08-31 16:41:15 +08:00
|
|
|
{
|
|
|
|
AddHeader("Upgrade", "websocket");
|
|
|
|
AddHeader("Connection", "Upgrade");
|
|
|
|
}
|
|
|
|
|
2012-10-01 14:26:31 +08:00
|
|
|
public ResponseHandshake(HttpStatusCode code)
|
|
|
|
{
|
|
|
|
StatusCode = ((int)code).ToString();
|
|
|
|
Reason = code.GetDescription();
|
|
|
|
}
|
|
|
|
|
2012-09-10 00:36:22 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
2012-08-31 16:41:15 +08:00
|
|
|
public bool IsWebSocketResponse {
|
|
|
|
get {
|
2013-02-28 13:37:27 +08:00
|
|
|
return ProtocolVersion < HttpVersion.Version11
|
2013-02-26 16:31:15 +08:00
|
|
|
? false
|
|
|
|
: StatusCode != "101"
|
|
|
|
? false
|
|
|
|
: !HeaderExists("Upgrade", "websocket")
|
|
|
|
? false
|
|
|
|
: !HeaderExists("Connection", "Upgrade")
|
|
|
|
? false
|
|
|
|
: HeaderExists("Sec-WebSocket-Accept");
|
2012-08-31 16:41:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 14:26:31 +08:00
|
|
|
public string Reason { get; internal set; }
|
|
|
|
|
2012-09-13 22:44:35 +08:00
|
|
|
public string StatusCode { get; internal set; }
|
2012-09-10 00:36:22 +08:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2012-10-01 14:26:31 +08:00
|
|
|
#region Methods
|
|
|
|
|
|
|
|
public static ResponseHandshake CreateCloseResponse(HttpStatusCode code)
|
|
|
|
{
|
|
|
|
var res = new ResponseHandshake(code);
|
|
|
|
res.AddHeader("Connection", "close");
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2012-08-31 16:41:15 +08:00
|
|
|
|
|
|
|
public static ResponseHandshake Parse(string[] response)
|
|
|
|
{
|
|
|
|
var statusLine = response[0].Split(' ');
|
|
|
|
if (statusLine.Length < 3)
|
|
|
|
throw new ArgumentException("Invalid status line.");
|
|
|
|
|
|
|
|
var reason = new StringBuilder(statusLine[2]);
|
|
|
|
for (int i = 3; i < statusLine.Length; i++)
|
|
|
|
reason.AppendFormat(" {0}", statusLine[i]);
|
|
|
|
|
|
|
|
var headers = new WebHeaderCollection();
|
|
|
|
for (int i = 1; i < response.Length; i++)
|
2013-03-19 18:45:50 +08:00
|
|
|
headers.SetInternal (response[i], true);
|
2012-08-31 16:41:15 +08:00
|
|
|
|
|
|
|
return new ResponseHandshake {
|
2012-09-10 00:36:22 +08:00
|
|
|
Headers = headers,
|
|
|
|
Reason = reason.ToString(),
|
|
|
|
StatusCode = statusLine[1],
|
|
|
|
ProtocolVersion = new Version(statusLine[0].Substring(5))
|
2012-08-31 16:41:15 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
var buffer = new StringBuilder();
|
2012-09-10 00:36:22 +08:00
|
|
|
buffer.AppendFormat("HTTP/{0} {1} {2}{3}", ProtocolVersion, StatusCode, Reason, _crlf);
|
2012-08-31 16:41:15 +08:00
|
|
|
foreach (string key in Headers.AllKeys)
|
|
|
|
buffer.AppendFormat("{0}: {1}{2}", key, Headers[key], _crlf);
|
|
|
|
|
|
|
|
buffer.Append(_crlf);
|
|
|
|
return buffer.ToString();
|
|
|
|
}
|
2012-09-10 00:36:22 +08:00
|
|
|
|
|
|
|
#endregion
|
2012-08-31 16:41:15 +08:00
|
|
|
}
|
|
|
|
}
|