1 | // Licensed to the .NET Foundation under one or more agreements.
|
---|
2 | // The .NET Foundation licenses this file to you under the MIT license.
|
---|
3 |
|
---|
4 | // ReSharper disable ConditionIsAlwaysTrueOrFalse
|
---|
5 | #pragma warning disable CS8601
|
---|
6 | #pragma warning disable CS8618
|
---|
7 |
|
---|
8 | using System;
|
---|
9 | using System.Collections.Generic;
|
---|
10 | using System.Collections.Specialized;
|
---|
11 | using System.Diagnostics;
|
---|
12 | using System.Net;
|
---|
13 | using System.Net.WebSockets;
|
---|
14 | using System.Security.Principal;
|
---|
15 |
|
---|
16 | namespace SpaceWizards.HttpListener.WebSockets
|
---|
17 | {
|
---|
18 | public class HttpListenerWebSocketContext : WebSocketContext
|
---|
19 | {
|
---|
20 | private readonly Uri _requestUri;
|
---|
21 | private readonly NameValueCollection _headers;
|
---|
22 | private readonly CookieCollection _cookieCollection;
|
---|
23 | private readonly IPrincipal _user;
|
---|
24 | private readonly bool _isAuthenticated;
|
---|
25 | private readonly bool _isLocal;
|
---|
26 | private readonly bool _isSecureConnection;
|
---|
27 |
|
---|
28 | private readonly string _origin;
|
---|
29 | private readonly IEnumerable<string> _secWebSocketProtocols;
|
---|
30 | private readonly string _secWebSocketVersion;
|
---|
31 | private readonly string _secWebSocketKey;
|
---|
32 |
|
---|
33 | private readonly WebSocket _webSocket;
|
---|
34 |
|
---|
35 | internal HttpListenerWebSocketContext(
|
---|
36 | Uri requestUri,
|
---|
37 | NameValueCollection headers,
|
---|
38 | CookieCollection cookieCollection,
|
---|
39 | IPrincipal user,
|
---|
40 | bool isAuthenticated,
|
---|
41 | bool isLocal,
|
---|
42 | bool isSecureConnection,
|
---|
43 | string origin,
|
---|
44 | IEnumerable<string> secWebSocketProtocols,
|
---|
45 | string secWebSocketVersion,
|
---|
46 | string secWebSocketKey,
|
---|
47 | WebSocket webSocket)
|
---|
48 | {
|
---|
49 | Debug.Assert(requestUri != null, "requestUri shouldn't be null");
|
---|
50 | Debug.Assert(headers != null, "headers shouldn't be null");
|
---|
51 | Debug.Assert(cookieCollection != null, "cookieCollection shouldn't be null");
|
---|
52 | Debug.Assert(secWebSocketProtocols != null, "secWebSocketProtocols shouldn't be null");
|
---|
53 | Debug.Assert(webSocket != null, "webSocket shouldn't be null");
|
---|
54 |
|
---|
55 | _cookieCollection = new CookieCollection();
|
---|
56 | _cookieCollection.Add(cookieCollection);
|
---|
57 |
|
---|
58 | _headers = new NameValueCollection(headers);
|
---|
59 | _user = CopyPrincipal(user)!;
|
---|
60 |
|
---|
61 | _requestUri = requestUri;
|
---|
62 | _isAuthenticated = isAuthenticated;
|
---|
63 | _isLocal = isLocal;
|
---|
64 | _isSecureConnection = isSecureConnection;
|
---|
65 | _origin = origin;
|
---|
66 | _secWebSocketProtocols = secWebSocketProtocols;
|
---|
67 | _secWebSocketVersion = secWebSocketVersion;
|
---|
68 | _secWebSocketKey = secWebSocketKey;
|
---|
69 | _webSocket = webSocket;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override Uri RequestUri => _requestUri;
|
---|
73 |
|
---|
74 | public override NameValueCollection Headers => _headers;
|
---|
75 |
|
---|
76 | public override string Origin => _origin;
|
---|
77 |
|
---|
78 | public override IEnumerable<string> SecWebSocketProtocols => _secWebSocketProtocols;
|
---|
79 |
|
---|
80 | public override string SecWebSocketVersion => _secWebSocketVersion;
|
---|
81 |
|
---|
82 | public override string SecWebSocketKey => _secWebSocketKey;
|
---|
83 |
|
---|
84 | public override CookieCollection CookieCollection => _cookieCollection;
|
---|
85 |
|
---|
86 | public override IPrincipal User => _user;
|
---|
87 |
|
---|
88 | public override bool IsAuthenticated => _isAuthenticated;
|
---|
89 |
|
---|
90 | public override bool IsLocal => _isLocal;
|
---|
91 |
|
---|
92 | public override bool IsSecureConnection => _isSecureConnection;
|
---|
93 |
|
---|
94 | public override WebSocket WebSocket => _webSocket;
|
---|
95 |
|
---|
96 | private static IPrincipal? CopyPrincipal(IPrincipal user)
|
---|
97 | {
|
---|
98 | if (user != null)
|
---|
99 | {
|
---|
100 | /*if (!(user is WindowsPrincipal))*/
|
---|
101 | {
|
---|
102 | // AuthenticationSchemes.Basic.
|
---|
103 | if (user.Identity is HttpListenerBasicIdentity basicIdentity)
|
---|
104 | {
|
---|
105 | return new GenericPrincipal(new HttpListenerBasicIdentity(basicIdentity.Name, basicIdentity.Password), null);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | /*else
|
---|
109 | {
|
---|
110 | // AuthenticationSchemes.Digest, AuthenticationSchemes.Negotiate, AuthenticationSchemes.NTLM.
|
---|
111 | throw new PlatformNotSupportedException();
|
---|
112 | }*/
|
---|
113 | }
|
---|
114 |
|
---|
115 | return null;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|