| | | 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.Globalization; |
| | | 7 | | using System.Net.WebSockets; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels |
| | | 12 | | { |
| | | 13 | | internal static class WebSocketHelper |
| | | 14 | | { |
| | | 15 | | internal const int OperationNotStarted = 0; |
| | | 16 | | internal const int OperationFinished = 1; |
| | | 17 | | internal const string CloseOperation = "CloseOperation"; |
| | | 18 | | internal const string SendOperation = "SendOperation"; |
| | | 19 | | internal const string ReceiveOperation = "ReceiveOperation"; |
| | 2 | 20 | | internal static readonly char[] ProtocolSeparators = new char[] { ',' }; |
| | 2 | 21 | | internal static readonly HashSet<char> InvalidSeparatorSet = new HashSet<char>(new char[] { '(', ')', '<', '>', |
| | | 22 | | private const string SchemeWs = "ws"; |
| | | 23 | | private const string SchemeWss = "wss"; |
| | | 24 | | |
| | | 25 | | internal static int GetReceiveBufferSize(long maxReceivedMessageSize) |
| | | 26 | | { |
| | 33 | 27 | | int effectiveMaxReceiveBufferSize = maxReceivedMessageSize <= WebSocketDefaults.BufferSize ? (int)maxReceive |
| | 33 | 28 | | return Math.Max(WebSocketDefaults.MinReceiveBufferSize, effectiveMaxReceiveBufferSize); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | internal static bool IsSubProtocolInvalid(string protocol, out string invalidChar) |
| | | 32 | | { |
| | | 33 | | Fx.Assert(protocol != null, "protocol should not be null"); |
| | 473 | 34 | | char[] chars = protocol.ToCharArray(); |
| | 4722 | 35 | | for (int i = 0; i < chars.Length; i++) |
| | | 36 | | { |
| | 1888 | 37 | | char ch = chars[i]; |
| | 1888 | 38 | | if (ch < 0x21 || ch > 0x7e) |
| | | 39 | | { |
| | 0 | 40 | | invalidChar = string.Format(CultureInfo.InvariantCulture, "[{0}]", (int)ch); |
| | 0 | 41 | | return true; |
| | | 42 | | } |
| | | 43 | | |
| | 1888 | 44 | | if (InvalidSeparatorSet.Contains(ch)) |
| | | 45 | | { |
| | 0 | 46 | | invalidChar = ch.ToString(); |
| | 0 | 47 | | return true; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 473 | 51 | | invalidChar = null; |
| | 473 | 52 | | return false; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | internal static void ThrowCorrectException(Exception ex) |
| | | 56 | | { |
| | 0 | 57 | | throw ConvertAndTraceException(ex); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | internal static void ThrowCorrectException(Exception ex, TimeSpan timeout, string operation) |
| | | 61 | | { |
| | 0 | 62 | | throw ConvertAndTraceException(ex, timeout, operation); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | internal static Exception ConvertAndTraceException(Exception ex) |
| | | 66 | | { |
| | 0 | 67 | | return ConvertAndTraceException( |
| | 0 | 68 | | ex, |
| | 0 | 69 | | TimeSpan.MinValue, // this is a dummy since operation type is null, so the timespan value won't be u |
| | 0 | 70 | | null); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | internal static Exception ConvertAndTraceException(Exception ex, TimeSpan timeout, string operation) |
| | | 74 | | { |
| | 0 | 75 | | if (ex is ObjectDisposedException objectDisposedException) |
| | | 76 | | { |
| | 0 | 77 | | var communicationObjectAbortedException = new CommunicationObjectAbortedException(ex.Message, ex); |
| | 0 | 78 | | Fx.Exception.AsWarning(communicationObjectAbortedException); |
| | 0 | 79 | | return communicationObjectAbortedException; |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | if (ex is AggregateException aggregationException) |
| | | 83 | | { |
| | 0 | 84 | | Exception exception = Fx.Exception.AsError<OperationCanceledException>(aggregationException); |
| | 0 | 85 | | if (exception is OperationCanceledException operationCanceledException) |
| | | 86 | | { |
| | 0 | 87 | | TimeoutException timeoutException = GetTimeoutException(exception, timeout, operation); |
| | 0 | 88 | | Fx.Exception.AsWarning(timeoutException); |
| | 0 | 89 | | return timeoutException; |
| | | 90 | | } |
| | | 91 | | else |
| | | 92 | | { |
| | 0 | 93 | | Exception communicationException = ConvertAggregateExceptionToCommunicationException(aggregationExce |
| | 0 | 94 | | if (communicationException is CommunicationObjectAbortedException) |
| | | 95 | | { |
| | 0 | 96 | | Fx.Exception.AsWarning(communicationException); |
| | 0 | 97 | | return communicationException; |
| | | 98 | | } |
| | | 99 | | else |
| | | 100 | | { |
| | 0 | 101 | | return Fx.Exception.AsError(communicationException); |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | if (ex is WebSocketException webSocketException) |
| | | 107 | | { |
| | 0 | 108 | | switch (webSocketException.WebSocketErrorCode) |
| | | 109 | | { |
| | | 110 | | case WebSocketError.InvalidMessageType: |
| | | 111 | | case WebSocketError.UnsupportedProtocol: |
| | | 112 | | case WebSocketError.UnsupportedVersion: |
| | 0 | 113 | | ex = new ProtocolException(ex.Message, ex); |
| | 0 | 114 | | break; |
| | | 115 | | default: |
| | 0 | 116 | | ex = new CommunicationException(ex.Message, ex); |
| | | 117 | | break; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | return Fx.Exception.AsError(ex); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | internal static Exception ConvertAggregateExceptionToCommunicationException(AggregateException ex) |
| | | 125 | | { |
| | 0 | 126 | | Exception exception = Fx.Exception.AsError<WebSocketException>(ex); |
| | 0 | 127 | | if (exception is WebSocketException webSocketException && webSocketException.InnerException != null) |
| | | 128 | | { |
| | | 129 | | // TODO: Find out is AspNetCore has some specific exception type they throw |
| | | 130 | | //HttpListenerException httpListenerException = webSocketException.InnerException as HttpListenerExcepti |
| | | 131 | | //if (httpListenerException != null) |
| | | 132 | | //{ |
| | | 133 | | // return HttpChannelUtilities.CreateCommunicationException(httpListenerException); |
| | | 134 | | //} |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | if (exception is ObjectDisposedException objectDisposedException) |
| | | 138 | | { |
| | 0 | 139 | | return new CommunicationObjectAbortedException(exception.Message, exception); |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | return new CommunicationException(exception.Message, exception); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | internal static void ThrowExceptionOnTaskFailure(Task task, TimeSpan timeout, string operation) |
| | | 146 | | { |
| | 11 | 147 | | if (task.IsFaulted) |
| | | 148 | | { |
| | 0 | 149 | | throw Fx.Exception.AsError<CommunicationException>(task.Exception); |
| | | 150 | | } |
| | 11 | 151 | | else if (task.IsCanceled) |
| | | 152 | | { |
| | 0 | 153 | | throw Fx.Exception.AsError(GetTimeoutException(null, timeout, operation)); |
| | | 154 | | } |
| | 11 | 155 | | } |
| | | 156 | | |
| | | 157 | | internal static TimeoutException GetTimeoutException(Exception innerException, TimeSpan timeout, string operatio |
| | | 158 | | { |
| | 0 | 159 | | string errorMsg = string.Empty; |
| | 0 | 160 | | if (operation != null) |
| | | 161 | | { |
| | | 162 | | switch (operation) |
| | | 163 | | { |
| | | 164 | | case CloseOperation: |
| | 0 | 165 | | errorMsg = SR.Format(SR.CloseTimedOut, timeout); |
| | 0 | 166 | | break; |
| | | 167 | | case SendOperation: |
| | 0 | 168 | | errorMsg = SR.Format(SR.WebSocketSendTimedOut, timeout); |
| | 0 | 169 | | break; |
| | | 170 | | case ReceiveOperation: |
| | 0 | 171 | | errorMsg = SR.Format(SR.WebSocketReceiveTimedOut, timeout); |
| | 0 | 172 | | break; |
| | | 173 | | default: |
| | 0 | 174 | | errorMsg = SR.Format(SR.WebSocketOperationTimedOut, operation, timeout); |
| | | 175 | | break; |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | return innerException == null ? new TimeoutException(errorMsg) : new TimeoutException(errorMsg, innerExcepti |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | internal static bool UseWebSocketTransport(WebSocketTransportUsage transportUsage, bool isContractDuplex) |
| | | 183 | | { |
| | 71 | 184 | | return transportUsage == WebSocketTransportUsage.Always |
| | 71 | 185 | | || (transportUsage == WebSocketTransportUsage.WhenDuplex && isContractDuplex); |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | internal static Uri GetWebSocketUri(Uri httpUri) |
| | | 189 | | { |
| | | 190 | | Fx.Assert(httpUri != null, "RemoteAddress.Uri should not be null."); |
| | 0 | 191 | | UriBuilder builder = new UriBuilder(httpUri); |
| | | 192 | | |
| | 0 | 193 | | if (Uri.UriSchemeHttp.Equals(httpUri.Scheme, StringComparison.OrdinalIgnoreCase)) |
| | | 194 | | { |
| | 0 | 195 | | builder.Scheme = SchemeWs; |
| | | 196 | | } |
| | | 197 | | else |
| | | 198 | | { |
| | | 199 | | Fx.Assert( |
| | | 200 | | Uri.UriSchemeHttps.Equals(httpUri.Scheme, StringComparison.OrdinalIgnoreCase), |
| | | 201 | | "httpUri.Scheme should be http or https."); |
| | 0 | 202 | | builder.Scheme = SchemeWss; |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | return builder.Uri; |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | } |