| | | 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 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.Specialized; |
| | | 7 | | using System.Net; |
| | | 8 | | using System.Net.WebSockets; |
| | | 9 | | using System.Security.Principal; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | using Microsoft.AspNetCore.Http; |
| | | 12 | | using Microsoft.AspNetCore.Http.Extensions; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Channels |
| | | 15 | | { |
| | | 16 | | internal class AspNetCoreWebSocketContext : WebSocketContext |
| | | 17 | | { |
| | | 18 | | private readonly HttpContext _httpContext; |
| | | 19 | | private readonly WebSocket _webSocket; |
| | | 20 | | private CookieCollection _cookieCollection; |
| | | 21 | | private NameValueCollection _headers; |
| | | 22 | | private bool? _isLocal; |
| | | 23 | | private Uri _requestUri; |
| | | 24 | | |
| | 11 | 25 | | internal AspNetCoreWebSocketContext(HttpContext httpContext, WebSocket webSocket) |
| | | 26 | | { |
| | | 27 | | Fx.Assert(httpContext != null, "HttpContext can't be null"); |
| | | 28 | | Fx.Assert(webSocket != null, "WebSocket can't be null"); |
| | 11 | 29 | | _httpContext = httpContext; |
| | 11 | 30 | | _webSocket = webSocket; |
| | 11 | 31 | | } |
| | | 32 | | |
| | | 33 | | public override CookieCollection CookieCollection |
| | | 34 | | { |
| | | 35 | | get |
| | | 36 | | { |
| | 0 | 37 | | if (_cookieCollection == null) |
| | | 38 | | { |
| | 0 | 39 | | var cookieContainer = new CookieContainer(); |
| | 0 | 40 | | foreach (KeyValuePair<string, string> item in _httpContext.Request.Cookies) |
| | | 41 | | { |
| | 0 | 42 | | cookieContainer.SetCookies(RequestUri, item.Value); |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | _cookieCollection = cookieContainer.GetCookies(RequestUri); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | return _cookieCollection; |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public override NameValueCollection Headers |
| | | 53 | | { |
| | | 54 | | get |
| | | 55 | | { |
| | 0 | 56 | | if (_headers == null) |
| | | 57 | | { |
| | 0 | 58 | | var headers = new NameValueCollection(); |
| | 0 | 59 | | foreach (KeyValuePair<string, Microsoft.Extensions.Primitives.StringValues> header in _httpContext.R |
| | | 60 | | { |
| | 0 | 61 | | headers.Add(header.Key, header.Value); |
| | | 62 | | } |
| | 0 | 63 | | _headers = headers; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return _headers; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | public override bool IsAuthenticated => _httpContext.User.Identity.IsAuthenticated; |
| | | 71 | | |
| | | 72 | | public override bool IsLocal |
| | | 73 | | { |
| | | 74 | | get |
| | | 75 | | { |
| | 0 | 76 | | if (!_isLocal.HasValue) |
| | | 77 | | { |
| | 0 | 78 | | ConnectionInfo connection = _httpContext.Connection; |
| | 0 | 79 | | if (connection.RemoteIpAddress != null) |
| | | 80 | | { |
| | 0 | 81 | | if (connection.RemoteIpAddress.Equals(connection.LocalIpAddress)) |
| | | 82 | | { |
| | 0 | 83 | | _isLocal = true; |
| | | 84 | | } |
| | | 85 | | else |
| | | 86 | | { |
| | 0 | 87 | | _isLocal = IPAddress.IsLoopback(connection.RemoteIpAddress); |
| | | 88 | | } |
| | | 89 | | } |
| | 0 | 90 | | else if (connection.LocalIpAddress == null) |
| | | 91 | | { |
| | 0 | 92 | | _isLocal = true; |
| | | 93 | | } |
| | | 94 | | else |
| | | 95 | | { |
| | 0 | 96 | | _isLocal = false; |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | return _isLocal.Value; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | public override bool IsSecureConnection => throw new PlatformNotSupportedException(nameof(IsSecureConnection)); |
| | | 105 | | |
| | 0 | 106 | | public override string Origin => Headers["Origin"]; |
| | | 107 | | |
| | | 108 | | public override Uri RequestUri |
| | | 109 | | { |
| | | 110 | | get |
| | | 111 | | { |
| | 0 | 112 | | if (_requestUri == null) |
| | | 113 | | { |
| | 0 | 114 | | _requestUri = new Uri(_httpContext.Request.GetEncodedUrl()); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | return _requestUri; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | public override string SecWebSocketKey => Headers["Sec-WebSocket-Key"]; |
| | | 122 | | |
| | 0 | 123 | | public override IEnumerable<string> SecWebSocketProtocols => _httpContext.WebSockets.WebSocketRequestedProtocols |
| | | 124 | | |
| | 0 | 125 | | public override string SecWebSocketVersion => Headers["Sec-WebSocket-Version"]; |
| | | 126 | | |
| | 0 | 127 | | public override IPrincipal User => _httpContext.User; |
| | | 128 | | |
| | 22 | 129 | | public override WebSocket WebSocket => _webSocket; |
| | | 130 | | } |
| | | 131 | | } |