This repository has been archived by the owner on May 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketConnectionInfo.cs
63 lines (55 loc) · 2.38 KB
/
WebSocketConnectionInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace Fleck
{
public class WebSocketConnectionInfo : IWebSocketConnectionInfo
{
const string CookiePattern = @"((;)*(\s)*(?<cookie_name>[^=]+)=(?<cookie_value>[^\;]+))+";
private static readonly Regex CookieRegex = new Regex(CookiePattern, RegexOptions.Compiled);
public static WebSocketConnectionInfo Create(WebSocketHttpRequest request, string clientIp, int clientPort, string negotiatedSubprotocol)
{
var info = new WebSocketConnectionInfo
{
Origin = request["Origin"] ?? request["Sec-WebSocket-Origin"],
Host = request["Host"],
SubProtocol = request["Sec-WebSocket-Protocol"],
Path = request.Path,
ClientIpAddress = clientIp,
ClientPort = clientPort,
NegotiatedSubProtocol = negotiatedSubprotocol,
Headers = new Dictionary<string, string>(request.Headers, System.StringComparer.InvariantCultureIgnoreCase)
};
var cookieHeader = request["Cookie"];
if (cookieHeader != null)
{
var match = CookieRegex.Match(cookieHeader);
var fields = match.Groups["cookie_name"].Captures;
var values = match.Groups["cookie_value"].Captures;
for (var i = 0; i < fields.Count; i++)
{
var name = fields[i].ToString();
var value = values[i].ToString();
info.Cookies[name] = value;
}
}
return info;
}
WebSocketConnectionInfo()
{
Cookies = new Dictionary<string, string>();
Id = Guid.NewGuid();
}
public string NegotiatedSubProtocol { get; private set; }
public string SubProtocol { get; private set; }
public string Origin { get; private set; }
public string Host { get; private set; }
public string Path { get; private set; }
public string ClientIpAddress { get; set; }
public int ClientPort { get; set; }
public Guid Id { get; set; }
public IDictionary<string, string> Cookies { get; private set; }
public IDictionary<string, string> Headers { get; private set; }
}
}