| | | 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.Net.WebSockets; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | using CoreWCF.Security; |
| | | 10 | | using Microsoft.AspNetCore.Http; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Channels |
| | | 14 | | { |
| | | 15 | | internal class ServerWebSocketTransportDuplexSessionChannel : WebSocketTransportDuplexSessionChannel |
| | | 16 | | { |
| | | 17 | | private WebSocketContext _webSocketContext; |
| | | 18 | | private readonly HttpContext _httpContext; |
| | | 19 | | private readonly IHttpTransportFactorySettings _transportSettings; |
| | | 20 | | private readonly IServiceProvider _serviceProvider; |
| | | 21 | | private WebSocketMessageSource _webSocketMessageSource; |
| | | 22 | | private SessionOpenNotification _sessionOpenNotification; |
| | | 23 | | |
| | | 24 | | public ServerWebSocketTransportDuplexSessionChannel(HttpContext httpContext, WebSocketContext webSocketContext, |
| | 11 | 25 | | : base(settings, new EndpointAddress(localVia), localVia) |
| | | 26 | | { |
| | 11 | 27 | | _httpContext = httpContext; |
| | 11 | 28 | | _webSocketContext = webSocketContext; |
| | 11 | 29 | | _transportSettings = settings; |
| | 11 | 30 | | _serviceProvider = serviceProvider; |
| | 11 | 31 | | } |
| | | 32 | | |
| | | 33 | | protected override bool IsStreamedOutput |
| | | 34 | | { |
| | 15 | 35 | | get { return TransferModeHelper.IsResponseStreamed(TransferMode); } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public override T GetProperty<T>() |
| | | 39 | | { |
| | 45 | 40 | | if (typeof(T) == typeof(SessionOpenNotification)) |
| | | 41 | | { |
| | 11 | 42 | | if (_sessionOpenNotification == null) |
| | | 43 | | { |
| | 11 | 44 | | _sessionOpenNotification = new SessionOpenNotificationHelper(this); |
| | | 45 | | } |
| | | 46 | | |
| | 11 | 47 | | return (T)(object)_sessionOpenNotification; |
| | | 48 | | } |
| | | 49 | | |
| | 34 | 50 | | T service = _serviceProvider.GetService<T>(); |
| | 34 | 51 | | if (service == null) |
| | | 52 | | { |
| | 12 | 53 | | service = base.GetProperty<T>(); |
| | | 54 | | } |
| | | 55 | | |
| | 34 | 56 | | return service; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | internal void SetWebSocketInfo(WebSocketContext webSocketContext, RemoteEndpointMessageProperty remoteEndpointMe |
| | | 60 | | { |
| | | 61 | | Fx.Assert(webSocketContext != null, "webSocketContext should not be null."); |
| | 11 | 62 | | ShouldDisposeWebSocketAfterClosed = shouldDisposeWebSocketAfterClosed; |
| | 11 | 63 | | _webSocketContext = webSocketContext; |
| | 11 | 64 | | WebSocket = webSocketContext.WebSocket; |
| | | 65 | | |
| | 11 | 66 | | if (handshakeSecurityMessageProperty != null) |
| | | 67 | | { |
| | 0 | 68 | | RemoteSecurity = handshakeSecurityMessageProperty; |
| | | 69 | | } |
| | | 70 | | |
| | 11 | 71 | | bool inputUseStreaming = TransferModeHelper.IsRequestStreamed(TransferMode); |
| | 11 | 72 | | _webSocketMessageSource = new WebSocketMessageSource( |
| | 11 | 73 | | this, |
| | 11 | 74 | | _webSocketContext, |
| | 11 | 75 | | inputUseStreaming, |
| | 11 | 76 | | remoteEndpointMessageProperty, |
| | 11 | 77 | | this, |
| | 11 | 78 | | httpContext); |
| | | 79 | | |
| | 11 | 80 | | SetMessageSource(_webSocketMessageSource); |
| | 11 | 81 | | } |
| | | 82 | | |
| | | 83 | | protected override void OnClosed() |
| | | 84 | | { |
| | 11 | 85 | | base.OnClosed(); |
| | 11 | 86 | | } |
| | | 87 | | |
| | | 88 | | protected override Task OnOpenAsync(CancellationToken token) |
| | | 89 | | { |
| | 11 | 90 | | RemoteEndpointMessageProperty remoteEndpointMessageProperty = null; |
| | 11 | 91 | | if (_httpContext.Connection.RemoteIpAddress != null) |
| | | 92 | | { |
| | 11 | 93 | | remoteEndpointMessageProperty = new RemoteEndpointMessageProperty(_httpContext.Connection.RemoteIpAddres |
| | | 94 | | } |
| | | 95 | | |
| | 11 | 96 | | SetWebSocketInfo(_webSocketContext, remoteEndpointMessageProperty, ProcessAuthentication(), true, _httpConte |
| | | 97 | | //if (TD.WebSocketConnectionAcceptedIsEnabled()) |
| | | 98 | | //{ |
| | | 99 | | // TD.WebSocketConnectionAccepted( |
| | | 100 | | // this._httpContext.EventTraceActivity, |
| | | 101 | | // this.WebSocket != null ? this.WebSocket.GetHashCode() : -1); |
| | | 102 | | //} |
| | | 103 | | |
| | 11 | 104 | | return Task.CompletedTask; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | private SecurityMessageProperty ProcessAuthentication() |
| | | 108 | | { |
| | 11 | 109 | | if (ShouldProcessAuthentication()) |
| | | 110 | | { |
| | | 111 | | // TODO: Create SecurityMessageProperty further up stack |
| | 11 | 112 | | return null; |
| | | 113 | | //return this.ProcessRequiredAuthentication(); |
| | | 114 | | } |
| | | 115 | | else |
| | | 116 | | { |
| | | 117 | | return null; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | private bool ShouldProcessAuthentication() |
| | | 122 | | { |
| | | 123 | | Fx.Assert(_transportSettings != null, "IsAuthenticated should only be called if _transportSettings != null") |
| | | 124 | | Fx.Assert(_httpContext != null, "IsAuthenticated should only be called if _httpContext != null"); |
| | 11 | 125 | | return _transportSettings.IsAuthenticationRequired || (_transportSettings.IsAuthenticationSupported && _http |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | private class SessionOpenNotificationHelper : SessionOpenNotification |
| | | 129 | | { |
| | | 130 | | private readonly ServerWebSocketTransportDuplexSessionChannel _channel; |
| | | 131 | | |
| | 11 | 132 | | public SessionOpenNotificationHelper(ServerWebSocketTransportDuplexSessionChannel channel) |
| | | 133 | | { |
| | 11 | 134 | | _channel = channel; |
| | 11 | 135 | | } |
| | | 136 | | |
| | | 137 | | public override bool IsEnabled |
| | | 138 | | { |
| | | 139 | | get |
| | | 140 | | { |
| | 11 | 141 | | return _channel.WebSocketSettings.CreateNotificationOnConnection; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | public override void UpdateMessageProperties(MessageProperties inboundMessageProperties) |
| | | 146 | | { |
| | 1 | 147 | | _channel._webSocketMessageSource.UpdateOpenNotificationMessageProperties(inboundMessageProperties); |
| | 1 | 148 | | } |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | } |