| | | 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.Buffers.Binary; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Net.Http.Headers; |
| | | 10 | | using System.Net.Http.HPack; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | using System.Runtime.ExceptionServices; |
| | | 13 | | using System.Text; |
| | | 14 | | using System.Threading; |
| | | 15 | | using System.Threading.Channels; |
| | | 16 | | using System.Threading.Tasks; |
| | | 17 | | |
| | | 18 | | namespace System.Net.Http |
| | | 19 | | { |
| | | 20 | | internal sealed partial class Http2Connection : HttpConnectionBase |
| | | 21 | | { |
| | | 22 | | // Equivalent to the bytes returned from HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingNewNameToAllocatedA |
| | 0 | 23 | | private static ReadOnlySpan<byte> ProtocolLiteralHeaderBytes => [0x0, 0x9, 0x3a, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0 |
| | | 24 | | |
| | 0 | 25 | | private static readonly TaskCompletionSourceWithCancellation<bool> s_settingsReceivedSingleton = CreateSuccessfu |
| | | 26 | | |
| | | 27 | | private TaskCompletionSourceWithCancellation<bool>? _initialSettingsReceived; |
| | | 28 | | |
| | | 29 | | private readonly Stream _stream; |
| | | 30 | | |
| | | 31 | | // NOTE: These are mutable structs; do not make these readonly. |
| | | 32 | | // ProcessIncomingFramesAsync and ProcessOutgoingFramesAsync are responsible for disposing/returning their respe |
| | | 33 | | private ArrayBuffer _incomingBuffer; |
| | | 34 | | private ArrayBuffer _outgoingBuffer; |
| | | 35 | | |
| | | 36 | | /// <summary>Reusable array used to get the values for each header being written to the wire.</summary> |
| | | 37 | | [ThreadStatic] |
| | | 38 | | private static string[]? t_headerValues; |
| | | 39 | | |
| | | 40 | | private readonly HPackDecoder _hpackDecoder; |
| | | 41 | | |
| | | 42 | | private readonly Dictionary<int, Http2Stream> _httpStreams; |
| | | 43 | | |
| | | 44 | | private readonly CreditManager _connectionWindow; |
| | | 45 | | private RttEstimator _rttEstimator; |
| | | 46 | | |
| | | 47 | | private int _nextStream; |
| | | 48 | | private bool _receivedSettingsAck; |
| | | 49 | | private int _initialServerStreamWindowSize; |
| | | 50 | | private int _pendingWindowUpdate; |
| | | 51 | | |
| | | 52 | | private uint _maxConcurrentStreams; |
| | | 53 | | private uint _streamsInUse; |
| | | 54 | | private TaskCompletionSource<bool>? _availableStreamsWaiter; |
| | | 55 | | |
| | | 56 | | private readonly Channel<WriteQueueEntry> _writeChannel; |
| | | 57 | | private bool _lastPendingWriterShouldFlush; |
| | | 58 | | |
| | | 59 | | // Server-advertised SETTINGS_MAX_HEADER_LIST_SIZE |
| | | 60 | | // https://www.rfc-editor.org/rfc/rfc9113.html#section-6.5.2-2.12.1 |
| | 0 | 61 | | private uint _maxHeaderListSize = uint.MaxValue; // Defaults to infinite |
| | | 62 | | |
| | | 63 | | // This flag indicates that the connection is shutting down and cannot accept new requests, because of one of th |
| | | 64 | | // (1) We received a GOAWAY frame from the server |
| | | 65 | | // (2) We have exhaustead StreamIds (i.e. _nextStream == MaxStreamId) |
| | | 66 | | // (3) A connection-level error occurred, in which case _abortException below is set. |
| | | 67 | | // (4) The connection is being disposed. |
| | | 68 | | // Requests currently in flight will continue to be processed. |
| | | 69 | | // When all requests have completed, the connection will be torn down. |
| | | 70 | | private bool _shutdown; |
| | | 71 | | |
| | | 72 | | // If this is set, the connection is aborting due to an IO failure (IOException) or a protocol violation (Http2P |
| | | 73 | | // _shutdown above is true, and requests in flight have been (or are being) failed. |
| | | 74 | | private Exception? _abortException; |
| | | 75 | | |
| | | 76 | | private Http2ProtocolErrorCode? _goAwayErrorCode; |
| | | 77 | | |
| | | 78 | | private const int MaxStreamId = int.MaxValue; |
| | | 79 | | |
| | | 80 | | // Temporary workaround for request burst handling on connection start. |
| | | 81 | | private const int InitialMaxConcurrentStreams = 100; |
| | | 82 | | |
| | 0 | 83 | | private static ReadOnlySpan<byte> Http2ConnectionPreface => "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"u8; |
| | | 84 | | |
| | | 85 | | #if DEBUG |
| | | 86 | | // In debug builds, start with a very small buffer to induce buffer growing logic. |
| | | 87 | | private const int InitialConnectionBufferSize = FrameHeader.Size; |
| | | 88 | | #else |
| | | 89 | | // Rent enough space to receive a full data frame in one read call. |
| | | 90 | | private const int InitialConnectionBufferSize = FrameHeader.Size + FrameHeader.MaxPayloadLength; |
| | | 91 | | #endif |
| | | 92 | | |
| | | 93 | | // The default initial window size for streams and connections according to the RFC: |
| | | 94 | | // https://datatracker.ietf.org/doc/html/rfc7540#section-5.2.1 |
| | | 95 | | // Unlike HttpHandlerDefaults.DefaultInitialHttp2StreamWindowSize, this value should never be changed. |
| | | 96 | | internal const int DefaultInitialWindowSize = 65535; |
| | | 97 | | |
| | | 98 | | // We don't really care about limiting control flow at the connection level. |
| | | 99 | | // We limit it per stream, and the user controls how many streams are created. |
| | | 100 | | // So set the connection window size to a large value. |
| | | 101 | | private const int ConnectionWindowSize = 64 * 1024 * 1024; |
| | | 102 | | |
| | | 103 | | // We hold off on sending WINDOW_UPDATE until we hit the minimum threshold. |
| | | 104 | | // This value is somewhat arbitrary; the intent is to ensure it is much smaller than |
| | | 105 | | // the window size itself, or we risk stalling the server because it runs out of window space. |
| | | 106 | | // If we want to further reduce the frequency of WINDOW_UPDATEs, it's probably better to |
| | | 107 | | // increase the window size (and thus increase the threshold proportionally) |
| | | 108 | | // rather than just increase the threshold. |
| | | 109 | | private const int ConnectionWindowUpdateRatio = 8; |
| | | 110 | | private const int ConnectionWindowThreshold = ConnectionWindowSize / ConnectionWindowUpdateRatio; |
| | | 111 | | |
| | | 112 | | // When buffering outgoing writes, we will automatically buffer up to this number of bytes. |
| | | 113 | | // Single writes that are larger than the buffer can cause the buffer to expand beyond |
| | | 114 | | // this value, so this is not a hard maximum size. |
| | | 115 | | private const int UnflushedOutgoingBufferSize = 32 * 1024; |
| | | 116 | | |
| | | 117 | | // Channel options for creating _writeChannel |
| | 0 | 118 | | private static readonly UnboundedChannelOptions s_channelOptions = new UnboundedChannelOptions() { SingleReader |
| | | 119 | | |
| | | 120 | | internal enum KeepAliveState |
| | | 121 | | { |
| | | 122 | | None, |
| | | 123 | | PingSent |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private readonly long _keepAlivePingDelay; |
| | | 127 | | private readonly long _keepAlivePingTimeout; |
| | | 128 | | private readonly HttpKeepAlivePingPolicy _keepAlivePingPolicy; |
| | | 129 | | private long _keepAlivePingPayload; |
| | | 130 | | private long _nextPingRequestTimestamp; |
| | | 131 | | private long _keepAlivePingTimeoutTimestamp; |
| | | 132 | | private volatile KeepAliveState _keepAliveState; |
| | | 133 | | |
| | | 134 | | public Http2Connection(HttpConnectionPool pool, Stream stream, Activity? connectionSetupActivity, IPEndPoint? re |
| | 0 | 135 | | : base(pool, connectionSetupActivity, remoteEndPoint) |
| | 0 | 136 | | { |
| | 0 | 137 | | _stream = stream; |
| | | 138 | | |
| | 0 | 139 | | _incomingBuffer = new ArrayBuffer(initialSize: 0, usePool: true); |
| | 0 | 140 | | _outgoingBuffer = new ArrayBuffer(initialSize: 0, usePool: true); |
| | | 141 | | |
| | 0 | 142 | | _hpackDecoder = new HPackDecoder(maxHeadersLength: pool.Settings.MaxResponseHeadersByteLength); |
| | | 143 | | |
| | 0 | 144 | | _httpStreams = new Dictionary<int, Http2Stream>(); |
| | | 145 | | |
| | 0 | 146 | | _connectionWindow = new CreditManager(this, nameof(_connectionWindow), DefaultInitialWindowSize); |
| | | 147 | | |
| | 0 | 148 | | _rttEstimator = RttEstimator.Create(); |
| | | 149 | | |
| | 0 | 150 | | _writeChannel = Channel.CreateUnbounded<WriteQueueEntry>(s_channelOptions); |
| | | 151 | | |
| | 0 | 152 | | _nextStream = 1; |
| | 0 | 153 | | _initialServerStreamWindowSize = DefaultInitialWindowSize; |
| | | 154 | | |
| | 0 | 155 | | _maxConcurrentStreams = InitialMaxConcurrentStreams; |
| | 0 | 156 | | _streamsInUse = 0; |
| | | 157 | | |
| | 0 | 158 | | _pendingWindowUpdate = 0; |
| | | 159 | | |
| | 0 | 160 | | _keepAlivePingDelay = TimeSpanToMs(_pool.Settings._keepAlivePingDelay); |
| | 0 | 161 | | _keepAlivePingTimeout = TimeSpanToMs(_pool.Settings._keepAlivePingTimeout); |
| | 0 | 162 | | _nextPingRequestTimestamp = Environment.TickCount64 + _keepAlivePingDelay; |
| | 0 | 163 | | _keepAlivePingPolicy = _pool.Settings._keepAlivePingPolicy; |
| | | 164 | | |
| | 0 | 165 | | uint maxHeaderListSize = _pool._lastSeenHttp2MaxHeaderListSize; |
| | 0 | 166 | | if (maxHeaderListSize > 0) |
| | 0 | 167 | | { |
| | | 168 | | // Previous connections to the same host advertised a limit. |
| | | 169 | | // Use this as an initial value before we receive the SETTINGS frame. |
| | 0 | 170 | | _maxHeaderListSize = maxHeaderListSize; |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | if (NetEventSource.Log.IsEnabled()) TraceConnection(_stream); |
| | | 174 | | |
| | | 175 | | static long TimeSpanToMs(TimeSpan value) |
| | 0 | 176 | | { |
| | 0 | 177 | | double milliseconds = value.TotalMilliseconds; |
| | 0 | 178 | | return (long)(milliseconds > int.MaxValue ? int.MaxValue : milliseconds); |
| | 0 | 179 | | } |
| | 0 | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | ~Http2Connection() => Dispose(); |
| | | 183 | | |
| | 0 | 184 | | private object SyncObject => _httpStreams; |
| | | 185 | | |
| | | 186 | | internal TaskCompletionSourceWithCancellation<bool> InitialSettingsReceived => |
| | 0 | 187 | | _initialSettingsReceived ?? |
| | 0 | 188 | | Interlocked.CompareExchange(ref _initialSettingsReceived, new(), null) ?? |
| | 0 | 189 | | _initialSettingsReceived; |
| | | 190 | | |
| | 0 | 191 | | internal bool IsConnectEnabled { get; private set; } |
| | | 192 | | |
| | | 193 | | public async ValueTask SetupAsync(CancellationToken cancellationToken) |
| | 0 | 194 | | { |
| | | 195 | | try |
| | 0 | 196 | | { |
| | 0 | 197 | | int requiredSpace = Http2ConnectionPreface.Length + |
| | 0 | 198 | | FrameHeader.Size + (2 * FrameHeader.SettingLength) + |
| | 0 | 199 | | FrameHeader.Size + FrameHeader.WindowUpdateLength; |
| | | 200 | | |
| | 0 | 201 | | _outgoingBuffer.EnsureAvailableSpace(requiredSpace); |
| | | 202 | | |
| | | 203 | | // Send connection preface |
| | 0 | 204 | | Http2ConnectionPreface.CopyTo(_outgoingBuffer.AvailableSpan); |
| | 0 | 205 | | _outgoingBuffer.Commit(Http2ConnectionPreface.Length); |
| | | 206 | | |
| | | 207 | | // Send SETTINGS frame. Disable push promise & set initial window size. |
| | 0 | 208 | | FrameHeader.WriteTo(_outgoingBuffer.AvailableSpan, 2 * FrameHeader.SettingLength, FrameType.Settings, Fr |
| | 0 | 209 | | _outgoingBuffer.Commit(FrameHeader.Size); |
| | 0 | 210 | | BinaryPrimitives.WriteUInt16BigEndian(_outgoingBuffer.AvailableSpan, (ushort)SettingId.EnablePush); |
| | 0 | 211 | | _outgoingBuffer.Commit(2); |
| | 0 | 212 | | BinaryPrimitives.WriteUInt32BigEndian(_outgoingBuffer.AvailableSpan, 0); |
| | 0 | 213 | | _outgoingBuffer.Commit(4); |
| | 0 | 214 | | BinaryPrimitives.WriteUInt16BigEndian(_outgoingBuffer.AvailableSpan, (ushort)SettingId.InitialWindowSize |
| | 0 | 215 | | _outgoingBuffer.Commit(2); |
| | 0 | 216 | | BinaryPrimitives.WriteUInt32BigEndian(_outgoingBuffer.AvailableSpan, (uint)_pool.Settings._initialHttp2S |
| | 0 | 217 | | _outgoingBuffer.Commit(4); |
| | | 218 | | |
| | | 219 | | // The connection-level window size can not be initialized by SETTINGS frames: |
| | | 220 | | // https://datatracker.ietf.org/doc/html/rfc7540#section-6.9.2 |
| | | 221 | | // Send an initial connection-level WINDOW_UPDATE to setup the desired ConnectionWindowSize: |
| | 0 | 222 | | uint windowUpdateAmount = ConnectionWindowSize - DefaultInitialWindowSize; |
| | 0 | 223 | | if (NetEventSource.Log.IsEnabled()) Trace($"Initial connection-level WINDOW_UPDATE, windowUpdateAmount={ |
| | 0 | 224 | | FrameHeader.WriteTo(_outgoingBuffer.AvailableSpan, FrameHeader.WindowUpdateLength, FrameType.WindowUpdat |
| | 0 | 225 | | _outgoingBuffer.Commit(FrameHeader.Size); |
| | 0 | 226 | | BinaryPrimitives.WriteUInt32BigEndian(_outgoingBuffer.AvailableSpan, windowUpdateAmount); |
| | 0 | 227 | | _outgoingBuffer.Commit(4); |
| | | 228 | | |
| | 0 | 229 | | Debug.Assert(requiredSpace == _outgoingBuffer.ActiveLength); |
| | | 230 | | |
| | | 231 | | // Processing the incoming frames before sending the client preface and SETTINGS is necessary when using |
| | | 232 | | // If the preface and SETTINGS coming from the server are not read first the below WriteAsync and the Pr |
| | | 233 | | // Avoid capturing the initial request's ExecutionContext for the entire lifetime of the new connection. |
| | 0 | 234 | | using (ExecutionContext.SuppressFlow()) |
| | 0 | 235 | | { |
| | 0 | 236 | | _ = ProcessIncomingFramesAsync(); |
| | 0 | 237 | | } |
| | | 238 | | |
| | 0 | 239 | | await _stream.WriteAsync(_outgoingBuffer.ActiveMemory, cancellationToken).ConfigureAwait(false); |
| | 0 | 240 | | _rttEstimator.OnInitialSettingsSent(); |
| | 0 | 241 | | _outgoingBuffer.ClearAndReturnBuffer(); |
| | 0 | 242 | | } |
| | 0 | 243 | | catch (Exception e) |
| | 0 | 244 | | { |
| | | 245 | | // ProcessIncomingFramesAsync and ProcessOutgoingFramesAsync are responsible for disposing/returning the |
| | | 246 | | // SetupAsync is the exception as it's responsible for starting the ProcessOutgoingFramesAsync loop. |
| | | 247 | | // As we're about to throw and ProcessOutgoingFramesAsync will never be called, we must return the buffe |
| | 0 | 248 | | _outgoingBuffer.Dispose(); |
| | | 249 | | |
| | 0 | 250 | | Dispose(); |
| | | 251 | | |
| | 0 | 252 | | if (e is OperationCanceledException oce && oce.CancellationToken == cancellationToken) |
| | 0 | 253 | | { |
| | | 254 | | // Note, AddHttp2ConnectionAsync handles this OCE separately so don't wrap it. |
| | 0 | 255 | | throw; |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | // TODO: Review this case! |
| | 0 | 259 | | throw new IOException(SR.net_http_http2_connection_not_established, e); |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | // Avoid capturing the initial request's ExecutionContext for the entire lifetime of the new connection. |
| | 0 | 263 | | using (ExecutionContext.SuppressFlow()) |
| | 0 | 264 | | { |
| | 0 | 265 | | _ = ProcessOutgoingFramesAsync(); |
| | 0 | 266 | | } |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | private void Shutdown() |
| | 0 | 270 | | { |
| | 0 | 271 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(_shutdown)}={_shutdown}, {nameof(_abortException)}={_abo |
| | | 272 | | |
| | 0 | 273 | | Debug.Assert(Monitor.IsEntered(SyncObject)); |
| | 0 | 274 | | Debug.Assert(!_pool.HasSyncObjLock); |
| | | 275 | | |
| | 0 | 276 | | if (!_shutdown) |
| | 0 | 277 | | { |
| | | 278 | | // InvalidateHttp2Connection may call back into Shutdown, |
| | | 279 | | // so we set the flag early to prevent executing FinalTeardown twice. |
| | 0 | 280 | | _shutdown = true; |
| | | 281 | | |
| | 0 | 282 | | _pool.InvalidateHttp2Connection(this); |
| | 0 | 283 | | SignalAvailableStreamsWaiter(false); |
| | | 284 | | |
| | 0 | 285 | | if (_streamsInUse == 0) |
| | 0 | 286 | | { |
| | 0 | 287 | | FinalTeardown(); |
| | 0 | 288 | | } |
| | 0 | 289 | | } |
| | 0 | 290 | | } |
| | | 291 | | |
| | | 292 | | public bool TryReserveStream() |
| | 0 | 293 | | { |
| | 0 | 294 | | Debug.Assert(!_pool.HasSyncObjLock); |
| | | 295 | | |
| | 0 | 296 | | lock (SyncObject) |
| | 0 | 297 | | { |
| | 0 | 298 | | if (_shutdown) |
| | 0 | 299 | | { |
| | 0 | 300 | | return false; |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | if (_streamsInUse < _maxConcurrentStreams) |
| | 0 | 304 | | { |
| | 0 | 305 | | _streamsInUse++; |
| | 0 | 306 | | return true; |
| | | 307 | | } |
| | 0 | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | return false; |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | // Can be called by the HttpConnectionPool after TryReserveStream if the stream doesn't end up being used. |
| | | 314 | | // Otherwise, will be called when the request is complete and stream is closed. |
| | | 315 | | public void ReleaseStream() |
| | 0 | 316 | | { |
| | 0 | 317 | | Debug.Assert(!_pool.HasSyncObjLock); |
| | | 318 | | |
| | 0 | 319 | | lock (SyncObject) |
| | 0 | 320 | | { |
| | 0 | 321 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(_streamsInUse)}={_streamsInUse}"); |
| | | 322 | | |
| | 0 | 323 | | Debug.Assert(_availableStreamsWaiter is null || _streamsInUse >= _maxConcurrentStreams); |
| | | 324 | | |
| | 0 | 325 | | _streamsInUse--; |
| | | 326 | | |
| | 0 | 327 | | Debug.Assert(_streamsInUse >= _httpStreams.Count); |
| | | 328 | | |
| | 0 | 329 | | if (_streamsInUse < _maxConcurrentStreams) |
| | 0 | 330 | | { |
| | 0 | 331 | | SignalAvailableStreamsWaiter(true); |
| | 0 | 332 | | } |
| | | 333 | | |
| | 0 | 334 | | if (_streamsInUse == 0) |
| | 0 | 335 | | { |
| | 0 | 336 | | if (_shutdown) |
| | 0 | 337 | | { |
| | 0 | 338 | | FinalTeardown(); |
| | 0 | 339 | | } |
| | 0 | 340 | | } |
| | 0 | 341 | | } |
| | 0 | 342 | | } |
| | | 343 | | |
| | | 344 | | // Returns true to indicate at least one stream is available |
| | | 345 | | // Returns false to indicate that the connection is shutting down and cannot be used anymore |
| | | 346 | | public Task<bool> WaitForAvailableStreamsAsync() |
| | 0 | 347 | | { |
| | 0 | 348 | | Debug.Assert(!_pool.HasSyncObjLock); |
| | | 349 | | |
| | 0 | 350 | | lock (SyncObject) |
| | 0 | 351 | | { |
| | 0 | 352 | | Debug.Assert(_availableStreamsWaiter is null, "As used currently, shouldn't already have a waiter"); |
| | | 353 | | |
| | 0 | 354 | | if (_shutdown) |
| | 0 | 355 | | { |
| | 0 | 356 | | return Task.FromResult(false); |
| | | 357 | | } |
| | | 358 | | |
| | 0 | 359 | | if (_streamsInUse < _maxConcurrentStreams) |
| | 0 | 360 | | { |
| | 0 | 361 | | return Task.FromResult(true); |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | // Need to wait for streams to become available. |
| | 0 | 365 | | _availableStreamsWaiter = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronou |
| | 0 | 366 | | return _availableStreamsWaiter.Task; |
| | | 367 | | } |
| | 0 | 368 | | } |
| | | 369 | | |
| | | 370 | | private void SignalAvailableStreamsWaiter(bool result) |
| | 0 | 371 | | { |
| | 0 | 372 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(result)}={result}, {nameof(_availableStreamsWaiter)}?={_ |
| | | 373 | | |
| | 0 | 374 | | Debug.Assert(Monitor.IsEntered(SyncObject)); |
| | | 375 | | |
| | 0 | 376 | | if (_availableStreamsWaiter is not null) |
| | 0 | 377 | | { |
| | 0 | 378 | | Debug.Assert(_shutdown != result); |
| | 0 | 379 | | _availableStreamsWaiter.SetResult(result); |
| | 0 | 380 | | _availableStreamsWaiter = null; |
| | 0 | 381 | | } |
| | 0 | 382 | | } |
| | | 383 | | |
| | | 384 | | private async Task FlushOutgoingBytesAsync() |
| | 0 | 385 | | { |
| | 0 | 386 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(_outgoingBuffer.ActiveLength)}={_outgoingBuffer.ActiveLe |
| | | 387 | | |
| | 0 | 388 | | if (_outgoingBuffer.ActiveLength > 0) |
| | 0 | 389 | | { |
| | | 390 | | try |
| | 0 | 391 | | { |
| | 0 | 392 | | await _stream.WriteAsync(_outgoingBuffer.ActiveMemory).ConfigureAwait(false); |
| | 0 | 393 | | } |
| | 0 | 394 | | catch (Exception e) |
| | 0 | 395 | | { |
| | 0 | 396 | | Abort(e); |
| | 0 | 397 | | } |
| | | 398 | | |
| | 0 | 399 | | _lastPendingWriterShouldFlush = false; |
| | 0 | 400 | | _outgoingBuffer.Discard(_outgoingBuffer.ActiveLength); |
| | 0 | 401 | | } |
| | 0 | 402 | | } |
| | | 403 | | |
| | | 404 | | private async ValueTask<FrameHeader> ReadFrameAsync(bool initialFrame = false) |
| | 0 | 405 | | { |
| | 0 | 406 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(initialFrame)}={initialFrame}"); |
| | | 407 | | |
| | | 408 | | // Ensure we've read enough data for the frame header. |
| | 0 | 409 | | if (_incomingBuffer.ActiveLength < FrameHeader.Size) |
| | 0 | 410 | | { |
| | | 411 | | do |
| | 0 | 412 | | { |
| | | 413 | | // Issue a zero-byte read to avoid potentially pinning the buffer while waiting for more data. |
| | 0 | 414 | | await _stream.ReadAsync(Memory<byte>.Empty).ConfigureAwait(false); |
| | | 415 | | |
| | 0 | 416 | | _incomingBuffer.EnsureAvailableSpace(FrameHeader.Size); |
| | | 417 | | |
| | 0 | 418 | | int bytesRead = await _stream.ReadAsync(_incomingBuffer.AvailableMemory).ConfigureAwait(false); |
| | 0 | 419 | | _incomingBuffer.Commit(bytesRead); |
| | 0 | 420 | | if (bytesRead == 0) |
| | 0 | 421 | | { |
| | 0 | 422 | | if (_goAwayErrorCode is not null) |
| | 0 | 423 | | { |
| | 0 | 424 | | ThrowProtocolError(_goAwayErrorCode.Value, SR.net_http_http2_connection_close); |
| | | 425 | | } |
| | 0 | 426 | | else if (_incomingBuffer.ActiveLength == 0) |
| | 0 | 427 | | { |
| | 0 | 428 | | ThrowMissingFrame(); |
| | 0 | 429 | | } |
| | | 430 | | else |
| | 0 | 431 | | { |
| | 0 | 432 | | ThrowPrematureEOF(FrameHeader.Size); |
| | 0 | 433 | | } |
| | 0 | 434 | | } |
| | 0 | 435 | | } |
| | 0 | 436 | | while (_incomingBuffer.ActiveLength < FrameHeader.Size); |
| | 0 | 437 | | } |
| | | 438 | | |
| | | 439 | | // Parse the frame header from our read buffer and validate it. |
| | 0 | 440 | | FrameHeader frameHeader = FrameHeader.ReadFrom(_incomingBuffer.ActiveSpan); |
| | 0 | 441 | | if (frameHeader.PayloadLength > FrameHeader.MaxPayloadLength) |
| | 0 | 442 | | { |
| | 0 | 443 | | if (initialFrame && NetEventSource.Log.IsEnabled()) |
| | 0 | 444 | | { |
| | 0 | 445 | | string response = Encoding.ASCII.GetString(_incomingBuffer.ActiveSpan.Slice(0, Math.Min(20, _incomin |
| | 0 | 446 | | Trace($"HTTP/2 handshake failed. Server returned {response}"); |
| | 0 | 447 | | } |
| | | 448 | | |
| | 0 | 449 | | _incomingBuffer.Discard(FrameHeader.Size); |
| | 0 | 450 | | ThrowProtocolError(initialFrame ? Http2ProtocolErrorCode.ProtocolError : Http2ProtocolErrorCode.FrameSiz |
| | | 451 | | } |
| | 0 | 452 | | _incomingBuffer.Discard(FrameHeader.Size); |
| | | 453 | | |
| | | 454 | | // Ensure we've read the frame contents into our buffer. |
| | 0 | 455 | | if (_incomingBuffer.ActiveLength < frameHeader.PayloadLength) |
| | 0 | 456 | | { |
| | 0 | 457 | | _incomingBuffer.EnsureAvailableSpace(frameHeader.PayloadLength - _incomingBuffer.ActiveLength); |
| | | 458 | | do |
| | 0 | 459 | | { |
| | | 460 | | // Issue a zero-byte read to avoid potentially pinning the buffer while waiting for more data. |
| | 0 | 461 | | await _stream.ReadAsync(Memory<byte>.Empty).ConfigureAwait(false); |
| | | 462 | | |
| | 0 | 463 | | int bytesRead = await _stream.ReadAsync(_incomingBuffer.AvailableMemory).ConfigureAwait(false); |
| | 0 | 464 | | _incomingBuffer.Commit(bytesRead); |
| | 0 | 465 | | if (bytesRead == 0) ThrowPrematureEOF(frameHeader.PayloadLength); |
| | 0 | 466 | | } |
| | 0 | 467 | | while (_incomingBuffer.ActiveLength < frameHeader.PayloadLength); |
| | 0 | 468 | | } |
| | | 469 | | |
| | | 470 | | // Return the read frame header. |
| | 0 | 471 | | return frameHeader; |
| | | 472 | | |
| | | 473 | | void ThrowPrematureEOF(int requiredBytes) => |
| | 0 | 474 | | throw new HttpIOException(HttpRequestError.ResponseEnded, SR.Format(SR.net_http_invalid_response_prematu |
| | | 475 | | |
| | | 476 | | void ThrowMissingFrame() => |
| | 0 | 477 | | throw new HttpIOException(HttpRequestError.ResponseEnded, SR.net_http_invalid_response_missing_frame); |
| | 0 | 478 | | } |
| | | 479 | | |
| | | 480 | | private async Task ProcessIncomingFramesAsync() |
| | 0 | 481 | | { |
| | | 482 | | try |
| | 0 | 483 | | { |
| | | 484 | | FrameHeader frameHeader; |
| | | 485 | | try |
| | 0 | 486 | | { |
| | | 487 | | // Read the initial settings frame. |
| | 0 | 488 | | frameHeader = await ReadFrameAsync(initialFrame: true).ConfigureAwait(false); |
| | 0 | 489 | | if (frameHeader.Type != FrameType.Settings || frameHeader.AckFlag) |
| | 0 | 490 | | { |
| | 0 | 491 | | if (frameHeader.Type == FrameType.GoAway) |
| | 0 | 492 | | { |
| | 0 | 493 | | var (_, errorCode) = ReadGoAwayFrame(frameHeader); |
| | 0 | 494 | | ThrowProtocolError(errorCode, SR.net_http_http2_connection_close); |
| | | 495 | | } |
| | | 496 | | else |
| | 0 | 497 | | { |
| | 0 | 498 | | ThrowProtocolError(); |
| | | 499 | | } |
| | | 500 | | } |
| | | 501 | | |
| | 0 | 502 | | if (NetEventSource.Log.IsEnabled()) Trace($"Frame 0: {frameHeader}."); |
| | | 503 | | |
| | | 504 | | // Process the initial SETTINGS frame. This will send an ACK. |
| | 0 | 505 | | ProcessSettingsFrame(frameHeader, initialFrame: true); |
| | | 506 | | |
| | 0 | 507 | | Debug.Assert(InitialSettingsReceived.Task.IsCompleted); |
| | 0 | 508 | | } |
| | 0 | 509 | | catch (HttpProtocolException e) |
| | 0 | 510 | | { |
| | 0 | 511 | | InitialSettingsReceived.TrySetException(e); |
| | 0 | 512 | | LogExceptions(InitialSettingsReceived.Task); |
| | 0 | 513 | | throw; |
| | | 514 | | } |
| | 0 | 515 | | catch (Exception e) |
| | 0 | 516 | | { |
| | 0 | 517 | | InitialSettingsReceived.TrySetException(new HttpIOException(HttpRequestError.InvalidResponse, SR.net |
| | 0 | 518 | | LogExceptions(InitialSettingsReceived.Task); |
| | 0 | 519 | | throw new HttpIOException(HttpRequestError.InvalidResponse, SR.net_http_http2_connection_not_establi |
| | | 520 | | } |
| | | 521 | | |
| | | 522 | | // Keep processing frames as they arrive. |
| | 0 | 523 | | for (long frameNum = 1; ; frameNum++) |
| | 0 | 524 | | { |
| | | 525 | | // We could just call ReadFrameAsync here, but we add this code |
| | | 526 | | // to avoid another state machine allocation in the relatively common case where we |
| | | 527 | | // currently don't have enough data buffered and issuing a read for the frame header |
| | | 528 | | // completes asynchronously, but that read ends up also reading enough data to fulfill |
| | | 529 | | // the entire frame's needs (not just the header). |
| | 0 | 530 | | if (_incomingBuffer.ActiveLength < FrameHeader.Size) |
| | 0 | 531 | | { |
| | | 532 | | do |
| | 0 | 533 | | { |
| | | 534 | | // Issue a zero-byte read to avoid potentially pinning the buffer while waiting for more dat |
| | 0 | 535 | | ValueTask<int> zeroByteReadTask = _stream.ReadAsync(Memory<byte>.Empty); |
| | 0 | 536 | | if (!zeroByteReadTask.IsCompletedSuccessfully && _incomingBuffer.ActiveLength == 0) |
| | 0 | 537 | | { |
| | | 538 | | // No data is available yet. Return the receive buffer back to the pool while we wait. |
| | 0 | 539 | | _incomingBuffer.ClearAndReturnBuffer(); |
| | 0 | 540 | | } |
| | 0 | 541 | | await zeroByteReadTask.ConfigureAwait(false); |
| | | 542 | | |
| | | 543 | | // While we only need FrameHeader.Size bytes to complete this read, it's better if we rent m |
| | | 544 | | // to avoid multiple ReadAsync calls and resizes once we start copying the content. |
| | 0 | 545 | | _incomingBuffer.EnsureAvailableSpace(InitialConnectionBufferSize); |
| | | 546 | | |
| | 0 | 547 | | int bytesRead = await _stream.ReadAsync(_incomingBuffer.AvailableMemory).ConfigureAwait(fals |
| | 0 | 548 | | Debug.Assert(bytesRead >= 0); |
| | 0 | 549 | | _incomingBuffer.Commit(bytesRead); |
| | 0 | 550 | | if (bytesRead == 0) |
| | 0 | 551 | | { |
| | | 552 | | // ReadFrameAsync below will detect that the frame is incomplete and throw the appropria |
| | 0 | 553 | | break; |
| | | 554 | | } |
| | 0 | 555 | | } |
| | 0 | 556 | | while (_incomingBuffer.ActiveLength < FrameHeader.Size); |
| | 0 | 557 | | } |
| | | 558 | | |
| | | 559 | | // Read the frame. |
| | 0 | 560 | | frameHeader = await ReadFrameAsync().ConfigureAwait(false); |
| | 0 | 561 | | if (NetEventSource.Log.IsEnabled()) Trace($"Frame {frameNum}: {frameHeader}."); |
| | | 562 | | |
| | 0 | 563 | | RefreshPingTimestamp(); |
| | | 564 | | |
| | | 565 | | // Process the frame. |
| | 0 | 566 | | switch (frameHeader.Type) |
| | | 567 | | { |
| | | 568 | | case FrameType.Headers: |
| | 0 | 569 | | await ProcessHeadersFrame(frameHeader).ConfigureAwait(false); |
| | 0 | 570 | | break; |
| | | 571 | | |
| | | 572 | | case FrameType.Data: |
| | 0 | 573 | | ProcessDataFrame(frameHeader); |
| | 0 | 574 | | break; |
| | | 575 | | |
| | | 576 | | case FrameType.Settings: |
| | 0 | 577 | | ProcessSettingsFrame(frameHeader); |
| | 0 | 578 | | break; |
| | | 579 | | |
| | | 580 | | case FrameType.Priority: |
| | 0 | 581 | | ProcessPriorityFrame(frameHeader); |
| | 0 | 582 | | break; |
| | | 583 | | |
| | | 584 | | case FrameType.Ping: |
| | 0 | 585 | | ProcessPingFrame(frameHeader); |
| | 0 | 586 | | break; |
| | | 587 | | |
| | | 588 | | case FrameType.WindowUpdate: |
| | 0 | 589 | | ProcessWindowUpdateFrame(frameHeader); |
| | 0 | 590 | | break; |
| | | 591 | | |
| | | 592 | | case FrameType.RstStream: |
| | 0 | 593 | | ProcessRstStreamFrame(frameHeader); |
| | 0 | 594 | | break; |
| | | 595 | | |
| | | 596 | | case FrameType.GoAway: |
| | 0 | 597 | | ProcessGoAwayFrame(frameHeader); |
| | 0 | 598 | | break; |
| | | 599 | | |
| | | 600 | | case FrameType.AltSvc: |
| | 0 | 601 | | ProcessAltSvcFrame(frameHeader); |
| | 0 | 602 | | break; |
| | | 603 | | |
| | | 604 | | case FrameType.PushPromise: // Should not happen, since we disable this in our initial SETTI |
| | | 605 | | case FrameType.Continuation: // Should only be received while processing headers in ProcessHe |
| | | 606 | | default: |
| | 0 | 607 | | ThrowProtocolError(); |
| | | 608 | | break; |
| | | 609 | | } |
| | 0 | 610 | | } |
| | | 611 | | } |
| | 0 | 612 | | catch (Exception e) |
| | 0 | 613 | | { |
| | 0 | 614 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(ProcessIncomingFramesAsync)}: {e.Message}"); |
| | | 615 | | |
| | 0 | 616 | | Abort(e); |
| | 0 | 617 | | } |
| | | 618 | | finally |
| | 0 | 619 | | { |
| | 0 | 620 | | _incomingBuffer.Dispose(); |
| | 0 | 621 | | } |
| | 0 | 622 | | } |
| | | 623 | | |
| | | 624 | | // Note, this will return null for a streamId that's no longer in use. |
| | | 625 | | // Callers must check for this and send a RST_STREAM or ignore as appropriate. |
| | | 626 | | // If the streamId is invalid or the stream is idle, calling this function |
| | | 627 | | // will result in a connection level error. |
| | | 628 | | private Http2Stream? GetStream(int streamId) |
| | 0 | 629 | | { |
| | 0 | 630 | | if (streamId <= 0 || streamId >= _nextStream) |
| | 0 | 631 | | { |
| | 0 | 632 | | ThrowProtocolError(); |
| | | 633 | | } |
| | | 634 | | |
| | 0 | 635 | | lock (SyncObject) |
| | 0 | 636 | | { |
| | 0 | 637 | | if (!_httpStreams.TryGetValue(streamId, out Http2Stream? http2Stream)) |
| | 0 | 638 | | { |
| | 0 | 639 | | return null; |
| | | 640 | | } |
| | | 641 | | |
| | 0 | 642 | | return http2Stream; |
| | | 643 | | } |
| | 0 | 644 | | } |
| | | 645 | | |
| | | 646 | | private async ValueTask ProcessHeadersFrame(FrameHeader frameHeader) |
| | 0 | 647 | | { |
| | 0 | 648 | | if (NetEventSource.Log.IsEnabled()) Trace($"{frameHeader}"); |
| | 0 | 649 | | Debug.Assert(frameHeader.Type == FrameType.Headers); |
| | | 650 | | |
| | 0 | 651 | | bool endStream = frameHeader.EndStreamFlag; |
| | | 652 | | |
| | 0 | 653 | | int streamId = frameHeader.StreamId; |
| | 0 | 654 | | Http2Stream? http2Stream = GetStream(streamId); |
| | | 655 | | |
| | | 656 | | IHttpStreamHeadersHandler headersHandler; |
| | 0 | 657 | | if (http2Stream != null) |
| | 0 | 658 | | { |
| | 0 | 659 | | http2Stream.OnHeadersStart(); |
| | 0 | 660 | | _rttEstimator.OnDataOrHeadersReceived(this, sendWindowUpdateBeforePing: true); |
| | 0 | 661 | | headersHandler = http2Stream; |
| | 0 | 662 | | } |
| | | 663 | | else |
| | 0 | 664 | | { |
| | | 665 | | // http2Stream will be null if this is a closed stream. We will still process the headers, |
| | | 666 | | // to ensure the header decoding state is up-to-date, but we will discard the decoded headers. |
| | 0 | 667 | | headersHandler = NopHeadersHandler.Instance; |
| | 0 | 668 | | } |
| | | 669 | | |
| | 0 | 670 | | _hpackDecoder.Decode( |
| | 0 | 671 | | GetFrameData(_incomingBuffer.ActiveSpan.Slice(0, frameHeader.PayloadLength), frameHeader.PaddedFlag, fra |
| | 0 | 672 | | frameHeader.EndHeadersFlag, |
| | 0 | 673 | | headersHandler); |
| | 0 | 674 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | | 675 | | |
| | 0 | 676 | | while (!frameHeader.EndHeadersFlag) |
| | 0 | 677 | | { |
| | 0 | 678 | | frameHeader = await ReadFrameAsync().ConfigureAwait(false); |
| | | 679 | | |
| | 0 | 680 | | if (frameHeader.Type != FrameType.Continuation || |
| | 0 | 681 | | frameHeader.StreamId != streamId) |
| | 0 | 682 | | { |
| | 0 | 683 | | ThrowProtocolError(); |
| | | 684 | | } |
| | | 685 | | |
| | 0 | 686 | | _hpackDecoder.Decode( |
| | 0 | 687 | | _incomingBuffer.ActiveSpan.Slice(0, frameHeader.PayloadLength), |
| | 0 | 688 | | frameHeader.EndHeadersFlag, |
| | 0 | 689 | | headersHandler); |
| | 0 | 690 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | 0 | 691 | | } |
| | | 692 | | |
| | 0 | 693 | | _hpackDecoder.CompleteDecode(); |
| | | 694 | | |
| | 0 | 695 | | http2Stream?.OnHeadersComplete(endStream); |
| | 0 | 696 | | } |
| | | 697 | | |
| | | 698 | | /// <summary>Nop implementation of <see cref="IHttpStreamHeadersHandler"/> used by <see cref="ProcessHeadersFram |
| | | 699 | | private sealed class NopHeadersHandler : IHttpStreamHeadersHandler |
| | | 700 | | { |
| | 0 | 701 | | public static readonly NopHeadersHandler Instance = new NopHeadersHandler(); |
| | 0 | 702 | | void IHttpStreamHeadersHandler.OnHeader(ReadOnlySpan<byte> name, ReadOnlySpan<byte> value) { } |
| | 0 | 703 | | void IHttpStreamHeadersHandler.OnHeadersComplete(bool endStream) { } |
| | 0 | 704 | | void IHttpStreamHeadersHandler.OnStaticIndexedHeader(int index) { } |
| | 0 | 705 | | void IHttpStreamHeadersHandler.OnStaticIndexedHeader(int index, ReadOnlySpan<byte> value) { } |
| | 0 | 706 | | void IHttpStreamHeadersHandler.OnDynamicIndexedHeader(int? index, ReadOnlySpan<byte> name, ReadOnlySpan<byte |
| | | 707 | | } |
| | | 708 | | |
| | | 709 | | private static ReadOnlySpan<byte> GetFrameData(ReadOnlySpan<byte> frameData, bool hasPad, bool hasPriority) |
| | 0 | 710 | | { |
| | 0 | 711 | | if (hasPad) |
| | 0 | 712 | | { |
| | 0 | 713 | | if (frameData.Length == 0) |
| | 0 | 714 | | { |
| | 0 | 715 | | ThrowProtocolError(); |
| | | 716 | | } |
| | | 717 | | |
| | 0 | 718 | | int padLength = frameData[0]; |
| | 0 | 719 | | frameData = frameData.Slice(1); |
| | | 720 | | |
| | 0 | 721 | | if (frameData.Length < padLength) |
| | 0 | 722 | | { |
| | 0 | 723 | | ThrowProtocolError(); |
| | | 724 | | } |
| | | 725 | | |
| | 0 | 726 | | frameData = frameData.Slice(0, frameData.Length - padLength); |
| | 0 | 727 | | } |
| | | 728 | | |
| | 0 | 729 | | if (hasPriority) |
| | 0 | 730 | | { |
| | 0 | 731 | | if (frameData.Length < FrameHeader.PriorityInfoLength) |
| | 0 | 732 | | { |
| | 0 | 733 | | ThrowProtocolError(); |
| | | 734 | | } |
| | | 735 | | |
| | | 736 | | // We ignore priority info. |
| | 0 | 737 | | frameData = frameData.Slice(FrameHeader.PriorityInfoLength); |
| | 0 | 738 | | } |
| | | 739 | | |
| | 0 | 740 | | return frameData; |
| | 0 | 741 | | } |
| | | 742 | | |
| | | 743 | | /// <summary> |
| | | 744 | | /// Parses an ALTSVC frame, defined by RFC 7838 Section 4. |
| | | 745 | | /// </summary> |
| | | 746 | | /// <remarks> |
| | | 747 | | /// The RFC states that any parse errors should result in ignoring the frame. |
| | | 748 | | /// </remarks> |
| | | 749 | | private void ProcessAltSvcFrame(FrameHeader frameHeader) |
| | 0 | 750 | | { |
| | 0 | 751 | | if (NetEventSource.Log.IsEnabled()) Trace($"{frameHeader}"); |
| | 0 | 752 | | Debug.Assert(frameHeader.Type == FrameType.AltSvc); |
| | 0 | 753 | | Debug.Assert(!Monitor.IsEntered(SyncObject)); |
| | | 754 | | |
| | 0 | 755 | | ReadOnlySpan<byte> span = _incomingBuffer.ActiveSpan.Slice(0, frameHeader.PayloadLength); |
| | | 756 | | |
| | 0 | 757 | | if (BinaryPrimitives.TryReadUInt16BigEndian(span, out ushort originLength)) |
| | 0 | 758 | | { |
| | 0 | 759 | | span = span.Slice(2); |
| | | 760 | | |
| | | 761 | | // Check that this ALTSVC frame is valid for our pool's origin. ALTSVC frames can come in one of two way |
| | | 762 | | // - On stream 0, the origin will be specified. HTTP/2 can service multiple origins per connection, and |
| | | 763 | | // - Otherwise, the origin is implicitly defined by the request stream and must be of length 0. |
| | | 764 | | |
| | 0 | 765 | | if ((frameHeader.StreamId != 0 && originLength == 0) || (frameHeader.StreamId == 0 && span.Length >= ori |
| | 0 | 766 | | { |
| | 0 | 767 | | span = span.Slice(originLength); |
| | | 768 | | |
| | | 769 | | // The span now contains a string with the same format as Alt-Svc headers. |
| | | 770 | | |
| | 0 | 771 | | string altSvcHeaderValue = Encoding.ASCII.GetString(span); |
| | 0 | 772 | | _pool.HandleAltSvc(new[] { altSvcHeaderValue }, null); |
| | 0 | 773 | | } |
| | 0 | 774 | | } |
| | | 775 | | |
| | 0 | 776 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | 0 | 777 | | } |
| | | 778 | | |
| | | 779 | | private void ProcessDataFrame(FrameHeader frameHeader) |
| | 0 | 780 | | { |
| | 0 | 781 | | Debug.Assert(frameHeader.Type == FrameType.Data); |
| | | 782 | | |
| | 0 | 783 | | Http2Stream? http2Stream = GetStream(frameHeader.StreamId); |
| | | 784 | | |
| | | 785 | | // Note, http2Stream will be null if this is a closed stream. |
| | | 786 | | // Just ignore the frame in this case. |
| | | 787 | | |
| | 0 | 788 | | ReadOnlySpan<byte> frameData = GetFrameData(_incomingBuffer.ActiveSpan.Slice(0, frameHeader.PayloadLength), |
| | 0 | 789 | | bool endStream = frameHeader.EndStreamFlag; |
| | | 790 | | |
| | 0 | 791 | | if (frameData.Length > 0 || endStream) |
| | 0 | 792 | | { |
| | 0 | 793 | | http2Stream?.OnResponseData(frameData, endStream); |
| | 0 | 794 | | } |
| | | 795 | | |
| | 0 | 796 | | if (frameData.Length > 0) |
| | 0 | 797 | | { |
| | 0 | 798 | | bool windowUpdateSent = ExtendWindow(frameData.Length); |
| | 0 | 799 | | if (http2Stream is not null && !endStream) |
| | 0 | 800 | | { |
| | 0 | 801 | | _rttEstimator.OnDataOrHeadersReceived(this, sendWindowUpdateBeforePing: !windowUpdateSent); |
| | 0 | 802 | | } |
| | 0 | 803 | | } |
| | | 804 | | |
| | 0 | 805 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | 0 | 806 | | } |
| | | 807 | | |
| | | 808 | | private void ProcessSettingsFrame(FrameHeader frameHeader, bool initialFrame = false) |
| | 0 | 809 | | { |
| | 0 | 810 | | Debug.Assert(frameHeader.Type == FrameType.Settings); |
| | | 811 | | |
| | 0 | 812 | | if (frameHeader.StreamId != 0) |
| | 0 | 813 | | { |
| | 0 | 814 | | ThrowProtocolError(); |
| | | 815 | | } |
| | | 816 | | |
| | 0 | 817 | | if (frameHeader.AckFlag) |
| | 0 | 818 | | { |
| | 0 | 819 | | if (frameHeader.PayloadLength != 0) |
| | 0 | 820 | | { |
| | 0 | 821 | | ThrowProtocolError(Http2ProtocolErrorCode.FrameSizeError); |
| | | 822 | | } |
| | | 823 | | |
| | 0 | 824 | | if (_receivedSettingsAck) |
| | 0 | 825 | | { |
| | 0 | 826 | | ThrowProtocolError(); |
| | | 827 | | } |
| | | 828 | | |
| | | 829 | | // We only send SETTINGS once initially, so we don't need to do anything in response to the ACK. |
| | | 830 | | // Just remember that we received one and we won't be expecting any more. |
| | 0 | 831 | | _receivedSettingsAck = true; |
| | 0 | 832 | | _rttEstimator.OnInitialSettingsAckReceived(this); |
| | 0 | 833 | | } |
| | | 834 | | else |
| | 0 | 835 | | { |
| | 0 | 836 | | if ((frameHeader.PayloadLength % 6) != 0) |
| | 0 | 837 | | { |
| | 0 | 838 | | ThrowProtocolError(Http2ProtocolErrorCode.FrameSizeError); |
| | | 839 | | } |
| | | 840 | | |
| | | 841 | | // Parse settings and process the ones we care about. |
| | 0 | 842 | | ReadOnlySpan<byte> settings = _incomingBuffer.ActiveSpan.Slice(0, frameHeader.PayloadLength); |
| | 0 | 843 | | bool maxConcurrentStreamsReceived = false; |
| | 0 | 844 | | while (settings.Length > 0) |
| | 0 | 845 | | { |
| | 0 | 846 | | Debug.Assert((settings.Length % 6) == 0); |
| | | 847 | | |
| | 0 | 848 | | ushort settingId = BinaryPrimitives.ReadUInt16BigEndian(settings); |
| | 0 | 849 | | settings = settings.Slice(2); |
| | 0 | 850 | | uint settingValue = BinaryPrimitives.ReadUInt32BigEndian(settings); |
| | 0 | 851 | | settings = settings.Slice(4); |
| | | 852 | | |
| | 0 | 853 | | if (NetEventSource.Log.IsEnabled()) Trace($"Applying setting {(SettingId)settingId}={settingValue}") |
| | | 854 | | |
| | 0 | 855 | | switch ((SettingId)settingId) |
| | | 856 | | { |
| | | 857 | | case SettingId.MaxConcurrentStreams: |
| | 0 | 858 | | ChangeMaxConcurrentStreams(settingValue); |
| | 0 | 859 | | maxConcurrentStreamsReceived = true; |
| | 0 | 860 | | break; |
| | | 861 | | |
| | | 862 | | case SettingId.InitialWindowSize: |
| | 0 | 863 | | if (settingValue > 0x7FFFFFFF) |
| | 0 | 864 | | { |
| | 0 | 865 | | ThrowProtocolError(Http2ProtocolErrorCode.FlowControlError); |
| | | 866 | | } |
| | | 867 | | |
| | 0 | 868 | | ChangeInitialWindowSize((int)settingValue); |
| | 0 | 869 | | break; |
| | | 870 | | |
| | | 871 | | case SettingId.MaxFrameSize: |
| | 0 | 872 | | if (settingValue < 16384 || settingValue > 16777215) |
| | 0 | 873 | | { |
| | 0 | 874 | | ThrowProtocolError(); |
| | | 875 | | } |
| | | 876 | | |
| | | 877 | | // We don't actually store this value; we always send frames of the minimum size (16K). |
| | 0 | 878 | | break; |
| | | 879 | | |
| | | 880 | | case SettingId.EnableConnect: |
| | 0 | 881 | | if (settingValue == 1) |
| | 0 | 882 | | { |
| | 0 | 883 | | IsConnectEnabled = true; |
| | 0 | 884 | | } |
| | 0 | 885 | | else if (settingValue == 0 && IsConnectEnabled) |
| | 0 | 886 | | { |
| | | 887 | | // Accroding to RFC: a sender MUST NOT send a SETTINGS_ENABLE_CONNECT_PROTOCOL parameter |
| | | 888 | | // with the value of 0 after previously sending a value of 1. |
| | | 889 | | // https://datatracker.ietf.org/doc/html/rfc8441#section-3 |
| | 0 | 890 | | ThrowProtocolError(); |
| | | 891 | | } |
| | 0 | 892 | | break; |
| | | 893 | | |
| | | 894 | | case SettingId.MaxHeaderListSize: |
| | 0 | 895 | | _maxHeaderListSize = settingValue; |
| | 0 | 896 | | _pool._lastSeenHttp2MaxHeaderListSize = _maxHeaderListSize; |
| | 0 | 897 | | break; |
| | | 898 | | |
| | | 899 | | default: |
| | | 900 | | // All others are ignored because we don't care about them. |
| | | 901 | | // Note, per RFC, unknown settings IDs should be ignored. |
| | 0 | 902 | | break; |
| | | 903 | | } |
| | 0 | 904 | | } |
| | | 905 | | |
| | 0 | 906 | | if (initialFrame) |
| | 0 | 907 | | { |
| | 0 | 908 | | if (!maxConcurrentStreamsReceived) |
| | 0 | 909 | | { |
| | | 910 | | // Set to 'infinite' because MaxConcurrentStreams was not set on the initial SETTINGS frame. |
| | 0 | 911 | | ChangeMaxConcurrentStreams(int.MaxValue); |
| | 0 | 912 | | } |
| | | 913 | | |
| | 0 | 914 | | if (_initialSettingsReceived is null) |
| | 0 | 915 | | { |
| | 0 | 916 | | Interlocked.CompareExchange(ref _initialSettingsReceived, s_settingsReceivedSingleton, null); |
| | 0 | 917 | | } |
| | | 918 | | // Set result in case if CompareExchange lost the race |
| | 0 | 919 | | InitialSettingsReceived.TrySetResult(true); |
| | 0 | 920 | | } |
| | | 921 | | |
| | 0 | 922 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | | 923 | | |
| | | 924 | | // Send acknowledgement |
| | | 925 | | // Don't wait for completion, which could happen asynchronously. |
| | 0 | 926 | | LogExceptions(SendSettingsAckAsync()); |
| | 0 | 927 | | } |
| | 0 | 928 | | } |
| | | 929 | | |
| | | 930 | | private void ChangeMaxConcurrentStreams(uint newValue) |
| | 0 | 931 | | { |
| | 0 | 932 | | lock (SyncObject) |
| | 0 | 933 | | { |
| | 0 | 934 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(newValue)}={newValue}, {nameof(_streamsInUse)}={_str |
| | | 935 | | |
| | 0 | 936 | | Debug.Assert(_availableStreamsWaiter is null || _streamsInUse >= _maxConcurrentStreams); |
| | | 937 | | |
| | 0 | 938 | | _maxConcurrentStreams = newValue; |
| | 0 | 939 | | if (_streamsInUse < _maxConcurrentStreams) |
| | 0 | 940 | | { |
| | 0 | 941 | | SignalAvailableStreamsWaiter(true); |
| | 0 | 942 | | } |
| | 0 | 943 | | } |
| | 0 | 944 | | } |
| | | 945 | | |
| | | 946 | | private void ChangeInitialWindowSize(int newSize) |
| | 0 | 947 | | { |
| | 0 | 948 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(newSize)}={newSize}"); |
| | 0 | 949 | | Debug.Assert(newSize >= 0); |
| | | 950 | | |
| | 0 | 951 | | lock (SyncObject) |
| | 0 | 952 | | { |
| | 0 | 953 | | int delta = newSize - _initialServerStreamWindowSize; |
| | 0 | 954 | | _initialServerStreamWindowSize = newSize; |
| | | 955 | | |
| | | 956 | | // Adjust existing streams |
| | 0 | 957 | | foreach (KeyValuePair<int, Http2Stream> kvp in _httpStreams) |
| | 0 | 958 | | { |
| | 0 | 959 | | kvp.Value.OnWindowUpdate(delta); |
| | 0 | 960 | | } |
| | 0 | 961 | | } |
| | 0 | 962 | | } |
| | | 963 | | |
| | | 964 | | private void ProcessPriorityFrame(FrameHeader frameHeader) |
| | 0 | 965 | | { |
| | 0 | 966 | | Debug.Assert(frameHeader.Type == FrameType.Priority); |
| | | 967 | | |
| | 0 | 968 | | if (frameHeader.StreamId == 0 || frameHeader.PayloadLength != FrameHeader.PriorityInfoLength) |
| | 0 | 969 | | { |
| | 0 | 970 | | ThrowProtocolError(); |
| | | 971 | | } |
| | | 972 | | |
| | | 973 | | // Ignore priority info. |
| | | 974 | | |
| | 0 | 975 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | 0 | 976 | | } |
| | | 977 | | |
| | | 978 | | private void ProcessPingFrame(FrameHeader frameHeader) |
| | 0 | 979 | | { |
| | 0 | 980 | | Debug.Assert(frameHeader.Type == FrameType.Ping); |
| | | 981 | | |
| | 0 | 982 | | if (frameHeader.StreamId != 0) |
| | 0 | 983 | | { |
| | 0 | 984 | | ThrowProtocolError(); |
| | | 985 | | } |
| | | 986 | | |
| | 0 | 987 | | if (frameHeader.PayloadLength != FrameHeader.PingLength) |
| | 0 | 988 | | { |
| | 0 | 989 | | ThrowProtocolError(Http2ProtocolErrorCode.FrameSizeError); |
| | | 990 | | } |
| | | 991 | | |
| | | 992 | | // We don't wait for SendPingAckAsync to complete before discarding |
| | | 993 | | // the incoming buffer, so we need to take a copy of the data. Read |
| | | 994 | | // it as a big-endian integer here to avoid allocating an array. |
| | 0 | 995 | | Debug.Assert(sizeof(long) == FrameHeader.PingLength); |
| | 0 | 996 | | ReadOnlySpan<byte> pingContent = _incomingBuffer.ActiveSpan.Slice(0, FrameHeader.PingLength); |
| | 0 | 997 | | long pingContentLong = BinaryPrimitives.ReadInt64BigEndian(pingContent); |
| | | 998 | | |
| | 0 | 999 | | if (NetEventSource.Log.IsEnabled()) Trace($"Received PING frame, content:{pingContentLong} ack: {frameHeader |
| | | 1000 | | |
| | 0 | 1001 | | if (frameHeader.AckFlag) |
| | 0 | 1002 | | { |
| | 0 | 1003 | | ProcessPingAck(pingContentLong); |
| | 0 | 1004 | | } |
| | | 1005 | | else |
| | 0 | 1006 | | { |
| | 0 | 1007 | | LogExceptions(SendPingAsync(pingContentLong, isAck: true)); |
| | 0 | 1008 | | } |
| | 0 | 1009 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | 0 | 1010 | | } |
| | | 1011 | | |
| | | 1012 | | private void ProcessWindowUpdateFrame(FrameHeader frameHeader) |
| | 0 | 1013 | | { |
| | 0 | 1014 | | Debug.Assert(frameHeader.Type == FrameType.WindowUpdate); |
| | | 1015 | | |
| | 0 | 1016 | | if (frameHeader.PayloadLength != FrameHeader.WindowUpdateLength) |
| | 0 | 1017 | | { |
| | 0 | 1018 | | ThrowProtocolError(Http2ProtocolErrorCode.FrameSizeError); |
| | | 1019 | | } |
| | | 1020 | | |
| | 0 | 1021 | | int amount = BinaryPrimitives.ReadInt32BigEndian(_incomingBuffer.ActiveSpan) & 0x7FFFFFFF; |
| | 0 | 1022 | | if (NetEventSource.Log.IsEnabled()) Trace($"{frameHeader}. {nameof(amount)}={amount}"); |
| | | 1023 | | |
| | 0 | 1024 | | Debug.Assert(amount >= 0); |
| | 0 | 1025 | | if (amount == 0) |
| | 0 | 1026 | | { |
| | 0 | 1027 | | ThrowProtocolError(); |
| | | 1028 | | } |
| | | 1029 | | |
| | 0 | 1030 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | | 1031 | | |
| | 0 | 1032 | | if (frameHeader.StreamId == 0) |
| | 0 | 1033 | | { |
| | 0 | 1034 | | _connectionWindow.AdjustCredit(amount); |
| | 0 | 1035 | | } |
| | | 1036 | | else |
| | 0 | 1037 | | { |
| | 0 | 1038 | | Http2Stream? http2Stream = GetStream(frameHeader.StreamId); |
| | 0 | 1039 | | if (http2Stream == null) |
| | 0 | 1040 | | { |
| | | 1041 | | // Ignore invalid stream ID, as per RFC |
| | 0 | 1042 | | return; |
| | | 1043 | | } |
| | | 1044 | | |
| | 0 | 1045 | | http2Stream.OnWindowUpdate(amount); |
| | 0 | 1046 | | } |
| | 0 | 1047 | | } |
| | | 1048 | | |
| | | 1049 | | private void ProcessRstStreamFrame(FrameHeader frameHeader) |
| | 0 | 1050 | | { |
| | 0 | 1051 | | Debug.Assert(frameHeader.Type == FrameType.RstStream); |
| | | 1052 | | |
| | 0 | 1053 | | if (frameHeader.PayloadLength != FrameHeader.RstStreamLength) |
| | 0 | 1054 | | { |
| | 0 | 1055 | | ThrowProtocolError(Http2ProtocolErrorCode.FrameSizeError); |
| | | 1056 | | } |
| | | 1057 | | |
| | 0 | 1058 | | if (frameHeader.StreamId == 0) |
| | 0 | 1059 | | { |
| | 0 | 1060 | | ThrowProtocolError(); |
| | | 1061 | | } |
| | | 1062 | | |
| | 0 | 1063 | | Http2Stream? http2Stream = GetStream(frameHeader.StreamId); |
| | 0 | 1064 | | if (http2Stream == null) |
| | 0 | 1065 | | { |
| | | 1066 | | // Ignore invalid stream ID, as per RFC |
| | 0 | 1067 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | 0 | 1068 | | return; |
| | | 1069 | | } |
| | | 1070 | | |
| | 0 | 1071 | | var protocolError = (Http2ProtocolErrorCode)BinaryPrimitives.ReadInt32BigEndian(_incomingBuffer.ActiveSpan); |
| | 0 | 1072 | | if (NetEventSource.Log.IsEnabled()) Trace(frameHeader.StreamId, $"{nameof(protocolError)}={protocolError}"); |
| | | 1073 | | |
| | 0 | 1074 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | | 1075 | | |
| | 0 | 1076 | | bool canRetry = protocolError == Http2ProtocolErrorCode.RefusedStream; |
| | 0 | 1077 | | http2Stream.OnReset(HttpProtocolException.CreateHttp2StreamException(protocolError), resetStreamErrorCode: p |
| | 0 | 1078 | | } |
| | | 1079 | | |
| | | 1080 | | private void ProcessGoAwayFrame(FrameHeader frameHeader) |
| | 0 | 1081 | | { |
| | 0 | 1082 | | var (lastStreamId, errorCode) = ReadGoAwayFrame(frameHeader); |
| | | 1083 | | |
| | 0 | 1084 | | Debug.Assert(lastStreamId >= 0); |
| | 0 | 1085 | | Exception resetException = HttpProtocolException.CreateHttp2ConnectionException(errorCode, SR.net_http_http2 |
| | 0 | 1086 | | _goAwayErrorCode = errorCode; |
| | | 1087 | | |
| | | 1088 | | // There is no point sending more PING frames for RTT estimation: |
| | 0 | 1089 | | _rttEstimator.OnGoAwayReceived(); |
| | | 1090 | | |
| | 0 | 1091 | | List<Http2Stream> streamsToAbort = new List<Http2Stream>(); |
| | 0 | 1092 | | lock (SyncObject) |
| | 0 | 1093 | | { |
| | 0 | 1094 | | Shutdown(); |
| | | 1095 | | |
| | 0 | 1096 | | foreach (KeyValuePair<int, Http2Stream> kvp in _httpStreams) |
| | 0 | 1097 | | { |
| | 0 | 1098 | | int streamId = kvp.Key; |
| | 0 | 1099 | | Debug.Assert(streamId == kvp.Value.StreamId); |
| | | 1100 | | |
| | 0 | 1101 | | if (streamId > lastStreamId) |
| | 0 | 1102 | | { |
| | 0 | 1103 | | streamsToAbort.Add(kvp.Value); |
| | 0 | 1104 | | } |
| | 0 | 1105 | | } |
| | 0 | 1106 | | } |
| | | 1107 | | |
| | | 1108 | | // Avoid calling OnReset under the lock, as it may cause the Http2Stream to call back in to RemoveStream |
| | 0 | 1109 | | foreach (Http2Stream s in streamsToAbort) |
| | 0 | 1110 | | { |
| | 0 | 1111 | | s.OnReset(resetException, canRetry: true); |
| | 0 | 1112 | | } |
| | 0 | 1113 | | } |
| | | 1114 | | |
| | | 1115 | | private (int lastStreamId, Http2ProtocolErrorCode errorCode) ReadGoAwayFrame(FrameHeader frameHeader) |
| | 0 | 1116 | | { |
| | 0 | 1117 | | Debug.Assert(frameHeader.Type == FrameType.GoAway); |
| | | 1118 | | |
| | 0 | 1119 | | if (frameHeader.PayloadLength < FrameHeader.GoAwayMinLength) |
| | 0 | 1120 | | { |
| | 0 | 1121 | | ThrowProtocolError(Http2ProtocolErrorCode.FrameSizeError); |
| | | 1122 | | } |
| | | 1123 | | |
| | 0 | 1124 | | if (frameHeader.StreamId != 0) |
| | 0 | 1125 | | { |
| | 0 | 1126 | | ThrowProtocolError(); |
| | | 1127 | | } |
| | | 1128 | | |
| | 0 | 1129 | | int lastStreamId = (int)(BinaryPrimitives.ReadUInt32BigEndian(_incomingBuffer.ActiveSpan) & 0x7FFFFFFF); |
| | 0 | 1130 | | Http2ProtocolErrorCode errorCode = (Http2ProtocolErrorCode)BinaryPrimitives.ReadInt32BigEndian(_incomingBuff |
| | 0 | 1131 | | if (NetEventSource.Log.IsEnabled()) Trace(frameHeader.StreamId, $"{nameof(lastStreamId)}={lastStreamId}, {na |
| | | 1132 | | |
| | 0 | 1133 | | _incomingBuffer.Discard(frameHeader.PayloadLength); |
| | | 1134 | | |
| | 0 | 1135 | | return (lastStreamId, errorCode); |
| | 0 | 1136 | | } |
| | | 1137 | | |
| | | 1138 | | internal Task FlushAsync(CancellationToken cancellationToken) => |
| | 0 | 1139 | | PerformWriteAsync(0, 0, static (_, __) => true, cancellationToken); |
| | | 1140 | | |
| | | 1141 | | private abstract class WriteQueueEntry : TaskCompletionSource |
| | | 1142 | | { |
| | | 1143 | | private readonly CancellationTokenRegistration _cancellationRegistration; |
| | | 1144 | | |
| | | 1145 | | public WriteQueueEntry(int writeBytes, CancellationToken cancellationToken) |
| | 0 | 1146 | | : base(TaskCreationOptions.RunContinuationsAsynchronously) |
| | 0 | 1147 | | { |
| | 0 | 1148 | | WriteBytes = writeBytes; |
| | | 1149 | | |
| | 0 | 1150 | | _cancellationRegistration = cancellationToken.UnsafeRegister(static (s, cancellationToken) => |
| | 0 | 1151 | | { |
| | 0 | 1152 | | bool canceled = ((WriteQueueEntry)s!).TrySetCanceled(cancellationToken); |
| | 0 | 1153 | | Debug.Assert(canceled, "Callback should have been unregistered if the operation was completing succe |
| | 0 | 1154 | | }, this); |
| | 0 | 1155 | | } |
| | | 1156 | | |
| | 0 | 1157 | | public int WriteBytes { get; } |
| | | 1158 | | |
| | | 1159 | | public bool TryDisableCancellation() |
| | 0 | 1160 | | { |
| | 0 | 1161 | | _cancellationRegistration.Dispose(); |
| | 0 | 1162 | | return !Task.IsCanceled; |
| | 0 | 1163 | | } |
| | | 1164 | | |
| | | 1165 | | public abstract bool InvokeWriteAction(Memory<byte> writeBuffer); |
| | | 1166 | | } |
| | | 1167 | | |
| | | 1168 | | private sealed class WriteQueueEntry<T> : WriteQueueEntry |
| | | 1169 | | { |
| | | 1170 | | private readonly T _state; |
| | | 1171 | | private readonly Func<T, Memory<byte>, bool> _writeAction; |
| | | 1172 | | |
| | | 1173 | | public WriteQueueEntry(int writeBytes, T state, Func<T, Memory<byte>, bool> writeAction, CancellationToken c |
| | 0 | 1174 | | : base(writeBytes, cancellationToken) |
| | 0 | 1175 | | { |
| | 0 | 1176 | | _state = state; |
| | 0 | 1177 | | _writeAction = writeAction; |
| | 0 | 1178 | | } |
| | | 1179 | | |
| | | 1180 | | public override bool InvokeWriteAction(Memory<byte> writeBuffer) |
| | 0 | 1181 | | { |
| | 0 | 1182 | | return _writeAction(_state, writeBuffer); |
| | 0 | 1183 | | } |
| | | 1184 | | } |
| | | 1185 | | |
| | | 1186 | | private Task PerformWriteAsync<T>(int writeBytes, T state, Func<T, Memory<byte>, bool> writeAction, Cancellation |
| | 0 | 1187 | | { |
| | 0 | 1188 | | WriteQueueEntry writeEntry = new WriteQueueEntry<T>(writeBytes, state, writeAction, cancellationToken); |
| | | 1189 | | |
| | 0 | 1190 | | if (!_writeChannel.Writer.TryWrite(writeEntry)) |
| | 0 | 1191 | | { |
| | 0 | 1192 | | if (_abortException is not null) |
| | 0 | 1193 | | { |
| | 0 | 1194 | | return Task.FromException(GetRequestAbortedException(_abortException)); |
| | | 1195 | | } |
| | | 1196 | | |
| | | 1197 | | // We must be trying to send something asynchronously (like RST_STREAM or a PING or a SETTINGS ACK) and |
| | | 1198 | | // As such, it should not matter that we were not able to actually send the frame. |
| | | 1199 | | // But just in case, throw ObjectDisposedException. Asynchronous callers will ignore the failure. |
| | 0 | 1200 | | Debug.Assert(_shutdown && _streamsInUse == 0); |
| | 0 | 1201 | | return Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof( |
| | | 1202 | | } |
| | | 1203 | | |
| | 0 | 1204 | | return writeEntry.Task; |
| | 0 | 1205 | | } |
| | | 1206 | | |
| | | 1207 | | private async Task ProcessOutgoingFramesAsync() |
| | 0 | 1208 | | { |
| | | 1209 | | try |
| | 0 | 1210 | | { |
| | 0 | 1211 | | while (await _writeChannel.Reader.WaitToReadAsync().ConfigureAwait(false)) |
| | 0 | 1212 | | { |
| | 0 | 1213 | | while (_writeChannel.Reader.TryRead(out WriteQueueEntry? writeEntry)) |
| | 0 | 1214 | | { |
| | 0 | 1215 | | if (_abortException is not null) |
| | 0 | 1216 | | { |
| | 0 | 1217 | | if (writeEntry.TryDisableCancellation()) |
| | 0 | 1218 | | { |
| | 0 | 1219 | | writeEntry.SetException(_abortException); |
| | 0 | 1220 | | } |
| | 0 | 1221 | | } |
| | | 1222 | | else |
| | 0 | 1223 | | { |
| | 0 | 1224 | | int writeBytes = writeEntry.WriteBytes; |
| | | 1225 | | |
| | | 1226 | | // If the buffer has already grown to 32k, does not have room for the next request, |
| | | 1227 | | // and is non-empty, flush the current contents to the wire. |
| | 0 | 1228 | | int totalBufferLength = _outgoingBuffer.Capacity; |
| | 0 | 1229 | | if (totalBufferLength >= UnflushedOutgoingBufferSize) |
| | 0 | 1230 | | { |
| | 0 | 1231 | | int activeBufferLength = _outgoingBuffer.ActiveLength; |
| | 0 | 1232 | | if (writeBytes >= totalBufferLength - activeBufferLength) |
| | 0 | 1233 | | { |
| | 0 | 1234 | | await FlushOutgoingBytesAsync().ConfigureAwait(false); |
| | 0 | 1235 | | } |
| | 0 | 1236 | | } |
| | | 1237 | | |
| | | 1238 | | // We are ready to process the write, so disable write cancellation now. |
| | 0 | 1239 | | if (writeEntry.TryDisableCancellation()) |
| | 0 | 1240 | | { |
| | 0 | 1241 | | _outgoingBuffer.EnsureAvailableSpace(writeBytes); |
| | | 1242 | | |
| | | 1243 | | try |
| | 0 | 1244 | | { |
| | 0 | 1245 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(writeBytes)}={writeBytes}"); |
| | | 1246 | | |
| | | 1247 | | // Invoke the callback with the supplied state and the target write buffer. |
| | 0 | 1248 | | bool flush = writeEntry.InvokeWriteAction(_outgoingBuffer.AvailableMemorySliced(writ |
| | | 1249 | | |
| | 0 | 1250 | | writeEntry.SetResult(); |
| | | 1251 | | |
| | 0 | 1252 | | _outgoingBuffer.Commit(writeBytes); |
| | 0 | 1253 | | _lastPendingWriterShouldFlush |= flush; |
| | 0 | 1254 | | } |
| | 0 | 1255 | | catch (Exception e) |
| | 0 | 1256 | | { |
| | 0 | 1257 | | writeEntry.SetException(e); |
| | 0 | 1258 | | } |
| | 0 | 1259 | | } |
| | 0 | 1260 | | } |
| | 0 | 1261 | | } |
| | | 1262 | | |
| | | 1263 | | // Nothing left in the queue to process. |
| | | 1264 | | // Flush the write buffer if we need to. |
| | 0 | 1265 | | if (_lastPendingWriterShouldFlush) |
| | 0 | 1266 | | { |
| | 0 | 1267 | | await FlushOutgoingBytesAsync().ConfigureAwait(false); |
| | 0 | 1268 | | } |
| | | 1269 | | |
| | 0 | 1270 | | if (_outgoingBuffer.ActiveLength == 0) |
| | 0 | 1271 | | { |
| | 0 | 1272 | | _outgoingBuffer.ClearAndReturnBuffer(); |
| | 0 | 1273 | | } |
| | 0 | 1274 | | } |
| | 0 | 1275 | | } |
| | 0 | 1276 | | catch (Exception e) |
| | 0 | 1277 | | { |
| | 0 | 1278 | | if (NetEventSource.Log.IsEnabled()) Trace($"Unexpected exception in {nameof(ProcessOutgoingFramesAsync)} |
| | | 1279 | | |
| | 0 | 1280 | | Debug.Fail($"Unexpected exception in {nameof(ProcessOutgoingFramesAsync)}: {e}"); |
| | | 1281 | | } |
| | | 1282 | | finally |
| | 0 | 1283 | | { |
| | 0 | 1284 | | _outgoingBuffer.Dispose(); |
| | 0 | 1285 | | } |
| | 0 | 1286 | | } |
| | | 1287 | | |
| | | 1288 | | private Task SendSettingsAckAsync() => |
| | 0 | 1289 | | PerformWriteAsync(FrameHeader.Size, this, static (thisRef, writeBuffer) => |
| | 0 | 1290 | | { |
| | 0 | 1291 | | if (NetEventSource.Log.IsEnabled()) thisRef.Trace("Started writing."); |
| | 0 | 1292 | | |
| | 0 | 1293 | | FrameHeader.WriteTo(writeBuffer.Span, 0, FrameType.Settings, FrameFlags.Ack, streamId: 0); |
| | 0 | 1294 | | |
| | 0 | 1295 | | return true; |
| | 0 | 1296 | | }); |
| | | 1297 | | |
| | | 1298 | | /// <param name="pingContent">The 8-byte ping content to send, read as a big-endian integer.</param> |
| | | 1299 | | /// <param name="isAck">Determine whether the frame is ping or ping ack.</param> |
| | | 1300 | | private Task SendPingAsync(long pingContent, bool isAck = false) => |
| | 0 | 1301 | | PerformWriteAsync(FrameHeader.Size + FrameHeader.PingLength, (thisRef: this, pingContent, isAck), static (st |
| | 0 | 1302 | | { |
| | 0 | 1303 | | if (NetEventSource.Log.IsEnabled()) state.thisRef.Trace($"Started writing. {nameof(pingContent)}={state. |
| | 0 | 1304 | | |
| | 0 | 1305 | | Debug.Assert(sizeof(long) == FrameHeader.PingLength); |
| | 0 | 1306 | | |
| | 0 | 1307 | | Span<byte> span = writeBuffer.Span; |
| | 0 | 1308 | | FrameHeader.WriteTo(span, FrameHeader.PingLength, FrameType.Ping, state.isAck ? FrameFlags.Ack : FrameFl |
| | 0 | 1309 | | BinaryPrimitives.WriteInt64BigEndian(span.Slice(FrameHeader.Size), state.pingContent); |
| | 0 | 1310 | | |
| | 0 | 1311 | | return true; |
| | 0 | 1312 | | }); |
| | | 1313 | | |
| | | 1314 | | private Task SendRstStreamAsync(int streamId, Http2ProtocolErrorCode errorCode) => |
| | 0 | 1315 | | PerformWriteAsync(FrameHeader.Size + FrameHeader.RstStreamLength, (thisRef: this, streamId, errorCode), stat |
| | 0 | 1316 | | { |
| | 0 | 1317 | | if (NetEventSource.Log.IsEnabled()) s.thisRef.Trace(s.streamId, $"Started writing. {nameof(s.errorCode)} |
| | 0 | 1318 | | |
| | 0 | 1319 | | Span<byte> span = writeBuffer.Span; |
| | 0 | 1320 | | FrameHeader.WriteTo(span, FrameHeader.RstStreamLength, FrameType.RstStream, FrameFlags.None, s.streamId) |
| | 0 | 1321 | | BinaryPrimitives.WriteInt32BigEndian(span.Slice(FrameHeader.Size), (int)s.errorCode); |
| | 0 | 1322 | | |
| | 0 | 1323 | | return true; |
| | 0 | 1324 | | }); |
| | | 1325 | | |
| | | 1326 | | |
| | | 1327 | | internal void HeartBeat() |
| | 0 | 1328 | | { |
| | 0 | 1329 | | Debug.Assert(!_pool.HasSyncObjLock); |
| | | 1330 | | |
| | 0 | 1331 | | if (_shutdown) |
| | 0 | 1332 | | return; |
| | | 1333 | | |
| | | 1334 | | try |
| | 0 | 1335 | | { |
| | 0 | 1336 | | VerifyKeepAlive(); |
| | 0 | 1337 | | } |
| | 0 | 1338 | | catch (Exception e) |
| | 0 | 1339 | | { |
| | 0 | 1340 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(HeartBeat)}: {e.Message}"); |
| | | 1341 | | |
| | 0 | 1342 | | Abort(e); |
| | 0 | 1343 | | } |
| | 0 | 1344 | | } |
| | | 1345 | | |
| | | 1346 | | private static (ReadOnlyMemory<byte> first, ReadOnlyMemory<byte> rest) SplitBuffer(ReadOnlyMemory<byte> buffer, |
| | 0 | 1347 | | buffer.Length > maxSize ? |
| | 0 | 1348 | | (buffer.Slice(0, maxSize), buffer.Slice(maxSize)) : |
| | 0 | 1349 | | (buffer, Memory<byte>.Empty); |
| | | 1350 | | |
| | | 1351 | | private void WriteIndexedHeader(int index, ref ArrayBuffer headerBuffer) |
| | 0 | 1352 | | { |
| | 0 | 1353 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(index)}={index}"); |
| | | 1354 | | |
| | | 1355 | | int bytesWritten; |
| | 0 | 1356 | | while (!HPackEncoder.EncodeIndexedHeaderField(index, headerBuffer.AvailableSpan, out bytesWritten)) |
| | 0 | 1357 | | { |
| | 0 | 1358 | | headerBuffer.Grow(); |
| | 0 | 1359 | | } |
| | | 1360 | | |
| | 0 | 1361 | | headerBuffer.Commit(bytesWritten); |
| | 0 | 1362 | | } |
| | | 1363 | | |
| | | 1364 | | private void WriteIndexedHeader(int index, string value, ref ArrayBuffer headerBuffer) |
| | 0 | 1365 | | { |
| | 0 | 1366 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(index)}={index}, {nameof(value)}={value}"); |
| | | 1367 | | |
| | | 1368 | | int bytesWritten; |
| | 0 | 1369 | | while (!HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexing(index, value, valueEncoding: null, headerBuffer |
| | 0 | 1370 | | { |
| | 0 | 1371 | | headerBuffer.Grow(); |
| | 0 | 1372 | | } |
| | | 1373 | | |
| | 0 | 1374 | | headerBuffer.Commit(bytesWritten); |
| | 0 | 1375 | | } |
| | | 1376 | | |
| | | 1377 | | private void WriteLiteralHeader(string name, ReadOnlySpan<string> values, Encoding? valueEncoding, ref ArrayBuff |
| | 0 | 1378 | | { |
| | 0 | 1379 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(name)}={name}, {nameof(values)}={string.Join(", ", value |
| | | 1380 | | |
| | | 1381 | | int bytesWritten; |
| | 0 | 1382 | | while (!HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingNewName(name, values, HttpHeaderParser.DefaultSe |
| | 0 | 1383 | | { |
| | 0 | 1384 | | headerBuffer.Grow(); |
| | 0 | 1385 | | } |
| | | 1386 | | |
| | 0 | 1387 | | headerBuffer.Commit(bytesWritten); |
| | 0 | 1388 | | } |
| | | 1389 | | |
| | | 1390 | | private void WriteLiteralHeaderValues(ReadOnlySpan<string> values, byte[]? separator, Encoding? valueEncoding, r |
| | 0 | 1391 | | { |
| | 0 | 1392 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(values)}={string.Join(Encoding.ASCII.GetString(separator |
| | | 1393 | | |
| | | 1394 | | int bytesWritten; |
| | 0 | 1395 | | while (!HPackEncoder.EncodeStringLiterals(values, separator, valueEncoding, headerBuffer.AvailableSpan, out |
| | 0 | 1396 | | { |
| | 0 | 1397 | | headerBuffer.Grow(); |
| | 0 | 1398 | | } |
| | | 1399 | | |
| | 0 | 1400 | | headerBuffer.Commit(bytesWritten); |
| | 0 | 1401 | | } |
| | | 1402 | | |
| | | 1403 | | private void WriteLiteralHeaderValue(string value, Encoding? valueEncoding, ref ArrayBuffer headerBuffer) |
| | 0 | 1404 | | { |
| | 0 | 1405 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(value)}={value}"); |
| | | 1406 | | |
| | | 1407 | | int bytesWritten; |
| | 0 | 1408 | | while (!HPackEncoder.EncodeStringLiteral(value, valueEncoding, headerBuffer.AvailableSpan, out bytesWritten) |
| | 0 | 1409 | | { |
| | 0 | 1410 | | headerBuffer.Grow(); |
| | 0 | 1411 | | } |
| | | 1412 | | |
| | 0 | 1413 | | headerBuffer.Commit(bytesWritten); |
| | 0 | 1414 | | } |
| | | 1415 | | |
| | | 1416 | | private void WriteBytes(ReadOnlySpan<byte> bytes, ref ArrayBuffer headerBuffer) |
| | 0 | 1417 | | { |
| | 0 | 1418 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(bytes.Length)}={bytes.Length}"); |
| | | 1419 | | |
| | 0 | 1420 | | headerBuffer.EnsureAvailableSpace(bytes.Length); |
| | 0 | 1421 | | bytes.CopyTo(headerBuffer.AvailableSpan); |
| | 0 | 1422 | | headerBuffer.Commit(bytes.Length); |
| | 0 | 1423 | | } |
| | | 1424 | | |
| | | 1425 | | private int WriteHeaderCollection(HttpRequestMessage request, HttpHeaders headers, ref ArrayBuffer headerBuffer) |
| | 0 | 1426 | | { |
| | 0 | 1427 | | if (NetEventSource.Log.IsEnabled()) Trace(""); |
| | | 1428 | | |
| | 0 | 1429 | | HeaderEncodingSelector<HttpRequestMessage>? encodingSelector = _pool.Settings._requestHeaderEncodingSelector |
| | | 1430 | | |
| | 0 | 1431 | | ref string[]? tmpHeaderValuesArray = ref t_headerValues; |
| | | 1432 | | |
| | 0 | 1433 | | ReadOnlySpan<HeaderEntry> entries = headers.GetEntries(); |
| | 0 | 1434 | | int headerListSize = entries.Length * HeaderField.RfcOverhead; |
| | | 1435 | | |
| | 0 | 1436 | | foreach (HeaderEntry header in entries) |
| | 0 | 1437 | | { |
| | 0 | 1438 | | int headerValuesCount = HttpHeaders.GetStoreValuesIntoStringArray(header.Key, header.Value, ref tmpHeade |
| | 0 | 1439 | | Debug.Assert(headerValuesCount > 0, "No values for header??"); |
| | 0 | 1440 | | ReadOnlySpan<string> headerValues = tmpHeaderValuesArray.AsSpan(0, headerValuesCount); |
| | | 1441 | | |
| | 0 | 1442 | | Encoding? valueEncoding = encodingSelector?.Invoke(header.Key.Name, request); |
| | | 1443 | | |
| | 0 | 1444 | | KnownHeader? knownHeader = header.Key.KnownHeader; |
| | 0 | 1445 | | if (knownHeader != null) |
| | 0 | 1446 | | { |
| | | 1447 | | // The Host header is not sent for HTTP2 because we send the ":authority" pseudo-header instead |
| | | 1448 | | // (see pseudo-header handling below in WriteHeaders). |
| | | 1449 | | // The Connection, Upgrade and ProxyConnection headers are also not supported in HTTP2. |
| | 0 | 1450 | | if (knownHeader != KnownHeaders.Host && knownHeader != KnownHeaders.Connection && knownHeader != Kno |
| | 0 | 1451 | | { |
| | | 1452 | | // The length of the encoded name may be shorter than the actual name. |
| | | 1453 | | // Ensure that headerListSize is always >= of the actual size. |
| | 0 | 1454 | | headerListSize += knownHeader.Name.Length; |
| | | 1455 | | |
| | 0 | 1456 | | if (knownHeader == KnownHeaders.TE) |
| | 0 | 1457 | | { |
| | | 1458 | | // HTTP/2 allows only 'trailers' TE header. rfc7540 8.1.2.2 |
| | 0 | 1459 | | foreach (string value in headerValues) |
| | 0 | 1460 | | { |
| | 0 | 1461 | | if (string.Equals(value, "trailers", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 1462 | | { |
| | 0 | 1463 | | WriteBytes(knownHeader.Http2EncodedName, ref headerBuffer); |
| | 0 | 1464 | | WriteLiteralHeaderValue(value, valueEncoding, ref headerBuffer); |
| | 0 | 1465 | | break; |
| | | 1466 | | } |
| | 0 | 1467 | | } |
| | 0 | 1468 | | continue; |
| | | 1469 | | } |
| | | 1470 | | |
| | | 1471 | | // Extended connect requests will use the response content stream for bidirectional communicatio |
| | | 1472 | | // We will ignore any content set for such requests in Http2Stream.SendRequestBodyAsync, as it h |
| | | 1473 | | // Drop the Content-Length header as well in the unlikely case it was set. |
| | 0 | 1474 | | if (knownHeader == KnownHeaders.ContentLength && request.IsExtendedConnectRequest) |
| | 0 | 1475 | | { |
| | 0 | 1476 | | continue; |
| | | 1477 | | } |
| | | 1478 | | |
| | | 1479 | | // For all other known headers, send them via their pre-encoded name and the associated value. |
| | 0 | 1480 | | WriteBytes(knownHeader.Http2EncodedName, ref headerBuffer); |
| | | 1481 | | |
| | 0 | 1482 | | byte[]? separator = headerValues.Length > 1 ? header.Key.SeparatorBytes : null; |
| | | 1483 | | |
| | 0 | 1484 | | WriteLiteralHeaderValues(headerValues, separator, valueEncoding, ref headerBuffer); |
| | 0 | 1485 | | } |
| | 0 | 1486 | | } |
| | | 1487 | | else |
| | 0 | 1488 | | { |
| | | 1489 | | // The header is not known: fall back to just encoding the header name and value(s). |
| | 0 | 1490 | | WriteLiteralHeader(header.Key.Name, headerValues, valueEncoding, ref headerBuffer); |
| | 0 | 1491 | | } |
| | 0 | 1492 | | } |
| | | 1493 | | |
| | 0 | 1494 | | return headerListSize; |
| | 0 | 1495 | | } |
| | | 1496 | | |
| | | 1497 | | private void WriteHeaders(HttpRequestMessage request, ref ArrayBuffer headerBuffer) |
| | 0 | 1498 | | { |
| | 0 | 1499 | | if (NetEventSource.Log.IsEnabled()) Trace(""); |
| | | 1500 | | |
| | 0 | 1501 | | WriteBytes(request.Method.Http2EncodedBytes, ref headerBuffer); |
| | | 1502 | | |
| | 0 | 1503 | | WriteIndexedHeader(_pool.IsSecure ? H2StaticTable.SchemeHttps : H2StaticTable.SchemeHttp, ref headerBuffer); |
| | | 1504 | | |
| | 0 | 1505 | | if (request.HasHeaders && request.Headers.Host is string host) |
| | 0 | 1506 | | { |
| | 0 | 1507 | | WriteIndexedHeader(H2StaticTable.Authority, host, ref headerBuffer); |
| | 0 | 1508 | | } |
| | | 1509 | | else |
| | 0 | 1510 | | { |
| | 0 | 1511 | | WriteBytes(_pool._http2EncodedAuthorityHostHeader, ref headerBuffer); |
| | 0 | 1512 | | } |
| | | 1513 | | |
| | 0 | 1514 | | Debug.Assert(request.RequestUri != null); |
| | 0 | 1515 | | string pathAndQuery = request.RequestUri.PathAndQuery; |
| | 0 | 1516 | | if (pathAndQuery == "/") |
| | 0 | 1517 | | { |
| | 0 | 1518 | | WriteIndexedHeader(H2StaticTable.PathSlash, ref headerBuffer); |
| | 0 | 1519 | | } |
| | | 1520 | | else |
| | 0 | 1521 | | { |
| | 0 | 1522 | | WriteIndexedHeader(H2StaticTable.PathSlash, pathAndQuery, ref headerBuffer); |
| | 0 | 1523 | | } |
| | | 1524 | | |
| | 0 | 1525 | | int headerListSize = 3 * HeaderField.RfcOverhead; // Method, Authority, Path |
| | | 1526 | | |
| | 0 | 1527 | | if (request.HasHeaders) |
| | 0 | 1528 | | { |
| | | 1529 | | // HTTP2 does not support Transfer-Encoding: chunked, so disable this on the request. |
| | 0 | 1530 | | if (request.Headers.TransferEncodingChunked == true) |
| | 0 | 1531 | | { |
| | 0 | 1532 | | request.Headers.TransferEncodingChunked = false; |
| | 0 | 1533 | | } |
| | | 1534 | | |
| | 0 | 1535 | | if (request.Headers.Protocol is string protocol) |
| | 0 | 1536 | | { |
| | 0 | 1537 | | WriteBytes(ProtocolLiteralHeaderBytes, ref headerBuffer); |
| | 0 | 1538 | | Encoding? protocolEncoding = _pool.Settings._requestHeaderEncodingSelector?.Invoke(":protocol", requ |
| | 0 | 1539 | | WriteLiteralHeaderValue(protocol, protocolEncoding, ref headerBuffer); |
| | 0 | 1540 | | headerListSize += HeaderField.RfcOverhead; |
| | 0 | 1541 | | } |
| | | 1542 | | |
| | 0 | 1543 | | headerListSize += WriteHeaderCollection(request, request.Headers, ref headerBuffer); |
| | 0 | 1544 | | } |
| | | 1545 | | |
| | | 1546 | | // Determine cookies to send. |
| | 0 | 1547 | | if (_pool.Settings._useCookies) |
| | 0 | 1548 | | { |
| | 0 | 1549 | | string cookiesFromContainer = _pool.Settings._cookieContainer!.GetCookieHeader(request.RequestUri); |
| | 0 | 1550 | | if (cookiesFromContainer != string.Empty) |
| | 0 | 1551 | | { |
| | 0 | 1552 | | WriteBytes(KnownHeaders.Cookie.Http2EncodedName, ref headerBuffer); |
| | 0 | 1553 | | Encoding? cookieEncoding = _pool.Settings._requestHeaderEncodingSelector?.Invoke(KnownHeaders.Cookie |
| | 0 | 1554 | | WriteLiteralHeaderValue(cookiesFromContainer, cookieEncoding, ref headerBuffer); |
| | 0 | 1555 | | headerListSize += HttpKnownHeaderNames.Cookie.Length + HeaderField.RfcOverhead; |
| | 0 | 1556 | | } |
| | 0 | 1557 | | } |
| | | 1558 | | |
| | 0 | 1559 | | if (request.Content == null) |
| | 0 | 1560 | | { |
| | | 1561 | | // Write out Content-Length: 0 header to indicate no body, |
| | | 1562 | | // unless this is a method that never has a body. |
| | 0 | 1563 | | if (request.Method.MustHaveRequestBody) |
| | 0 | 1564 | | { |
| | 0 | 1565 | | WriteBytes(KnownHeaders.ContentLength.Http2EncodedName, ref headerBuffer); |
| | 0 | 1566 | | WriteLiteralHeaderValue("0", valueEncoding: null, ref headerBuffer); |
| | 0 | 1567 | | headerListSize += HttpKnownHeaderNames.ContentLength.Length + HeaderField.RfcOverhead; |
| | 0 | 1568 | | } |
| | 0 | 1569 | | } |
| | | 1570 | | else |
| | 0 | 1571 | | { |
| | 0 | 1572 | | headerListSize += WriteHeaderCollection(request, request.Content.Headers, ref headerBuffer); |
| | 0 | 1573 | | } |
| | | 1574 | | |
| | | 1575 | | // The headerListSize is an approximation of the total header length. |
| | | 1576 | | // This is acceptable as long as the value is always >= the actual length. |
| | | 1577 | | // We must avoid ever sending more than the server allowed. |
| | | 1578 | | // This approach must be revisited if we ever support the dynamic table or compression when sending requests |
| | 0 | 1579 | | headerListSize += headerBuffer.ActiveLength; |
| | | 1580 | | |
| | 0 | 1581 | | uint maxHeaderListSize = _maxHeaderListSize; |
| | 0 | 1582 | | if ((uint)headerListSize > maxHeaderListSize) |
| | 0 | 1583 | | { |
| | 0 | 1584 | | throw new HttpRequestException(SR.Format(SR.net_http_request_headers_exceeded_length, maxHeaderListSize) |
| | | 1585 | | } |
| | 0 | 1586 | | } |
| | | 1587 | | |
| | | 1588 | | private void AddStream(Http2Stream http2Stream) |
| | 0 | 1589 | | { |
| | 0 | 1590 | | lock (SyncObject) |
| | 0 | 1591 | | { |
| | 0 | 1592 | | if (_nextStream == MaxStreamId) |
| | 0 | 1593 | | { |
| | | 1594 | | // We have exhausted StreamIds. Shut down the connection. |
| | 0 | 1595 | | Shutdown(); |
| | 0 | 1596 | | } |
| | | 1597 | | |
| | 0 | 1598 | | if (_abortException is not null) |
| | 0 | 1599 | | { |
| | 0 | 1600 | | throw GetRequestAbortedException(_abortException); |
| | | 1601 | | } |
| | | 1602 | | |
| | 0 | 1603 | | if (_shutdown) |
| | 0 | 1604 | | { |
| | | 1605 | | // The connection has shut down. Throw a retryable exception so that this request will be handled on |
| | 0 | 1606 | | ThrowRetry(SR.net_http_server_shutdown); |
| | | 1607 | | } |
| | | 1608 | | |
| | 0 | 1609 | | if (_streamsInUse > _maxConcurrentStreams) |
| | 0 | 1610 | | { |
| | | 1611 | | // The server must have sent a downward adjustment to SETTINGS_MAX_CONCURRENT_STREAMS, so our previo |
| | | 1612 | | // We might want a better exception message here, but in general the user shouldn't see this anyway |
| | 0 | 1613 | | ThrowRetry(SR.net_http_request_aborted); |
| | | 1614 | | } |
| | | 1615 | | |
| | 0 | 1616 | | if (_httpStreams.Count == 0) |
| | 0 | 1617 | | { |
| | 0 | 1618 | | MarkConnectionAsNotIdle(); |
| | 0 | 1619 | | } |
| | | 1620 | | |
| | | 1621 | | // Now that we're holding the lock, configure the stream. The lock must be held while |
| | | 1622 | | // assigning the stream ID to ensure only one stream gets an ID, and it must be held |
| | | 1623 | | // across setting the initial window size (available credit) and storing the stream into |
| | | 1624 | | // collection such that window size updates are able to atomically affect all known streams. |
| | 0 | 1625 | | http2Stream.Initialize(_nextStream, _initialServerStreamWindowSize); |
| | | 1626 | | |
| | | 1627 | | // Client-initiated streams are always odd-numbered, so increase by 2. |
| | 0 | 1628 | | _nextStream += 2; |
| | | 1629 | | |
| | 0 | 1630 | | _httpStreams.Add(http2Stream.StreamId, http2Stream); |
| | 0 | 1631 | | } |
| | 0 | 1632 | | } |
| | | 1633 | | |
| | | 1634 | | private async ValueTask<Http2Stream> SendHeadersAsync(HttpRequestMessage request, CancellationToken cancellation |
| | 0 | 1635 | | { |
| | 0 | 1636 | | ArrayBuffer headerBuffer = default; |
| | | 1637 | | try |
| | 0 | 1638 | | { |
| | 0 | 1639 | | if (HttpTelemetry.Log.IsEnabled()) HttpTelemetry.Log.RequestHeadersStart(Id); |
| | | 1640 | | |
| | | 1641 | | // Serialize headers to a temporary buffer, and do as much work to prepare to send the headers as we can |
| | | 1642 | | // before taking the write lock. |
| | 0 | 1643 | | headerBuffer = new ArrayBuffer(InitialConnectionBufferSize, usePool: true); |
| | 0 | 1644 | | WriteHeaders(request, ref headerBuffer); |
| | 0 | 1645 | | ReadOnlyMemory<byte> headerBytes = headerBuffer.ActiveMemory; |
| | 0 | 1646 | | Debug.Assert(headerBytes.Length > 0); |
| | | 1647 | | |
| | | 1648 | | // Calculate the total number of bytes we're going to use (content + headers). |
| | 0 | 1649 | | int frameCount = ((headerBytes.Length - 1) / FrameHeader.MaxPayloadLength) + 1; |
| | 0 | 1650 | | int totalSize = headerBytes.Length + (frameCount * FrameHeader.Size); |
| | | 1651 | | |
| | | 1652 | | // Construct and initialize the new Http2Stream instance. It's stream ID must be set below |
| | | 1653 | | // before the instance is used and stored into the dictionary. However, we construct it here |
| | | 1654 | | // so as to avoid the allocation and initialization expense while holding multiple locks. |
| | 0 | 1655 | | var http2Stream = new Http2Stream(request, this); |
| | | 1656 | | |
| | | 1657 | | // Start the write. This serializes access to write to the connection, and ensures that HEADERS |
| | | 1658 | | // and CONTINUATION frames stay together, as they must do. We use the lock as well to ensure new |
| | | 1659 | | // streams are created and started in order. |
| | 0 | 1660 | | await PerformWriteAsync(totalSize, (thisRef: this, http2Stream, headerBytes, endStream: (request.Content |
| | | 1661 | | { |
| | | 1662 | | s.thisRef.AddStream(s.http2Stream); |
| | 0 | 1663 | | |
| | | 1664 | | if (NetEventSource.Log.IsEnabled()) s.thisRef.Trace(s.http2Stream.StreamId, $"Started writing. Total |
| | 0 | 1665 | | |
| | | 1666 | | Span<byte> span = writeBuffer.Span; |
| | 0 | 1667 | | |
| | 0 | 1668 | | // Copy the HEADERS frame. |
| | 0 | 1669 | | ReadOnlyMemory<byte> current, remaining; |
| | | 1670 | | (current, remaining) = SplitBuffer(s.headerBytes, FrameHeader.MaxPayloadLength); |
| | | 1671 | | FrameFlags flags = (remaining.Length == 0 ? FrameFlags.EndHeaders : FrameFlags.None); |
| | | 1672 | | flags |= (s.endStream ? FrameFlags.EndStream : FrameFlags.None); |
| | | 1673 | | FrameHeader.WriteTo(span, current.Length, FrameType.Headers, flags, s.http2Stream.StreamId); |
| | | 1674 | | span = span.Slice(FrameHeader.Size); |
| | | 1675 | | current.Span.CopyTo(span); |
| | | 1676 | | span = span.Slice(current.Length); |
| | | 1677 | | if (NetEventSource.Log.IsEnabled()) s.thisRef.Trace(s.http2Stream.StreamId, $"Wrote HEADERS frame. L |
| | 0 | 1678 | | |
| | 0 | 1679 | | // Copy CONTINUATION frames, if any. |
| | | 1680 | | while (remaining.Length > 0) |
| | | 1681 | | { |
| | | 1682 | | (current, remaining) = SplitBuffer(remaining, FrameHeader.MaxPayloadLength); |
| | | 1683 | | flags = remaining.Length == 0 ? FrameFlags.EndHeaders : FrameFlags.None; |
| | 0 | 1684 | | |
| | | 1685 | | FrameHeader.WriteTo(span, current.Length, FrameType.Continuation, flags, s.http2Stream.StreamId) |
| | | 1686 | | span = span.Slice(FrameHeader.Size); |
| | | 1687 | | current.Span.CopyTo(span); |
| | | 1688 | | span = span.Slice(current.Length); |
| | | 1689 | | if (NetEventSource.Log.IsEnabled()) s.thisRef.Trace(s.http2Stream.StreamId, $"Wrote CONTINUATION |
| | | 1690 | | } |
| | 0 | 1691 | | |
| | | 1692 | | Debug.Assert(span.Length == 0); |
| | 0 | 1693 | | |
| | | 1694 | | return s.mustFlush || s.endStream; |
| | | 1695 | | }, cancellationToken).ConfigureAwait(false); |
| | | 1696 | | |
| | 0 | 1697 | | if (HttpTelemetry.Log.IsEnabled()) HttpTelemetry.Log.RequestHeadersStop(); |
| | | 1698 | | |
| | 0 | 1699 | | return http2Stream; |
| | | 1700 | | } |
| | 0 | 1701 | | catch |
| | 0 | 1702 | | { |
| | 0 | 1703 | | ReleaseStream(); |
| | 0 | 1704 | | throw; |
| | | 1705 | | } |
| | | 1706 | | finally |
| | 0 | 1707 | | { |
| | 0 | 1708 | | headerBuffer.Dispose(); |
| | 0 | 1709 | | } |
| | 0 | 1710 | | } |
| | | 1711 | | |
| | | 1712 | | private async Task SendStreamDataAsync(int streamId, ReadOnlyMemory<byte> buffer, bool finalFlush, CancellationT |
| | 0 | 1713 | | { |
| | 0 | 1714 | | ReadOnlyMemory<byte> remaining = buffer; |
| | | 1715 | | |
| | 0 | 1716 | | while (remaining.Length > 0) |
| | 0 | 1717 | | { |
| | | 1718 | | // Once credit had been granted, we want to actually consume those bytes. |
| | 0 | 1719 | | int frameSize = Math.Min(remaining.Length, FrameHeader.MaxPayloadLength); |
| | 0 | 1720 | | frameSize = await _connectionWindow.RequestCreditAsync(frameSize, cancellationToken).ConfigureAwait(fals |
| | | 1721 | | |
| | | 1722 | | ReadOnlyMemory<byte> current; |
| | 0 | 1723 | | (current, remaining) = SplitBuffer(remaining, frameSize); |
| | | 1724 | | |
| | 0 | 1725 | | bool flush = false; |
| | 0 | 1726 | | if (finalFlush && remaining.Length == 0) |
| | 0 | 1727 | | { |
| | 0 | 1728 | | flush = true; |
| | 0 | 1729 | | } |
| | | 1730 | | |
| | | 1731 | | // Force a flush if we are out of credit, because we don't know that we will be sending more data any ti |
| | 0 | 1732 | | if (!_connectionWindow.IsCreditAvailable) |
| | 0 | 1733 | | { |
| | 0 | 1734 | | flush = true; |
| | 0 | 1735 | | } |
| | | 1736 | | |
| | | 1737 | | try |
| | 0 | 1738 | | { |
| | 0 | 1739 | | await PerformWriteAsync(FrameHeader.Size + current.Length, (thisRef: this, streamId, current, flush) |
| | | 1740 | | { |
| | 0 | 1741 | | // Invoked while holding the lock: |
| | | 1742 | | if (NetEventSource.Log.IsEnabled()) s.thisRef.Trace(s.streamId, $"Started writing. {nameof(write |
| | 0 | 1743 | | |
| | | 1744 | | FrameHeader.WriteTo(writeBuffer.Span, s.current.Length, FrameType.Data, FrameFlags.None, s.strea |
| | | 1745 | | s.current.CopyTo(writeBuffer.Slice(FrameHeader.Size)); |
| | 0 | 1746 | | |
| | | 1747 | | return s.flush; |
| | | 1748 | | }, cancellationToken).ConfigureAwait(false); |
| | 0 | 1749 | | } |
| | 0 | 1750 | | catch |
| | 0 | 1751 | | { |
| | | 1752 | | // Invoked if waiting for the lock is canceled (in that case, we need to return the credit that we h |
| | 0 | 1753 | | _connectionWindow.AdjustCredit(frameSize); |
| | 0 | 1754 | | throw; |
| | | 1755 | | } |
| | 0 | 1756 | | } |
| | 0 | 1757 | | } |
| | | 1758 | | |
| | | 1759 | | private Task SendEndStreamAsync(int streamId) => |
| | 0 | 1760 | | PerformWriteAsync(FrameHeader.Size, (thisRef: this, streamId), static (s, writeBuffer) => |
| | 0 | 1761 | | { |
| | 0 | 1762 | | if (NetEventSource.Log.IsEnabled()) s.thisRef.Trace(s.streamId, "Started writing."); |
| | 0 | 1763 | | |
| | 0 | 1764 | | FrameHeader.WriteTo(writeBuffer.Span, 0, FrameType.Data, FrameFlags.EndStream, s.streamId); |
| | 0 | 1765 | | |
| | 0 | 1766 | | return true; // finished sending request body, so flush soon (but ok to wait for pending packets) |
| | 0 | 1767 | | }); |
| | | 1768 | | |
| | | 1769 | | private Task SendWindowUpdateAsync(int streamId, int amount) |
| | 0 | 1770 | | { |
| | | 1771 | | // We update both the connection-level and stream-level windows at the same time |
| | 0 | 1772 | | Debug.Assert(amount > 0); |
| | 0 | 1773 | | return PerformWriteAsync(FrameHeader.Size + FrameHeader.WindowUpdateLength, (thisRef: this, streamId, amount |
| | 0 | 1774 | | { |
| | 0 | 1775 | | if (NetEventSource.Log.IsEnabled()) s.thisRef.Trace(s.streamId, $"Started writing. {nameof(s.amount)}={s |
| | 0 | 1776 | | |
| | 0 | 1777 | | Span<byte> span = writeBuffer.Span; |
| | 0 | 1778 | | FrameHeader.WriteTo(span, FrameHeader.WindowUpdateLength, FrameType.WindowUpdate, FrameFlags.None, s.str |
| | 0 | 1779 | | BinaryPrimitives.WriteInt32BigEndian(span.Slice(FrameHeader.Size), s.amount); |
| | 0 | 1780 | | |
| | 0 | 1781 | | return true; |
| | 0 | 1782 | | }); |
| | 0 | 1783 | | } |
| | | 1784 | | |
| | | 1785 | | private bool ExtendWindow(int amount) |
| | 0 | 1786 | | { |
| | 0 | 1787 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(amount)}={amount}"); |
| | 0 | 1788 | | Debug.Assert(amount > 0); |
| | 0 | 1789 | | Debug.Assert(_pendingWindowUpdate < ConnectionWindowThreshold); |
| | | 1790 | | |
| | 0 | 1791 | | _pendingWindowUpdate += amount; |
| | 0 | 1792 | | if (_pendingWindowUpdate < ConnectionWindowThreshold) |
| | 0 | 1793 | | { |
| | 0 | 1794 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(_pendingWindowUpdate)} {_pendingWindowUpdate} < {Con |
| | 0 | 1795 | | return false; |
| | | 1796 | | } |
| | | 1797 | | |
| | 0 | 1798 | | int windowUpdateSize = _pendingWindowUpdate; |
| | 0 | 1799 | | _pendingWindowUpdate = 0; |
| | | 1800 | | |
| | 0 | 1801 | | LogExceptions(SendWindowUpdateAsync(0, windowUpdateSize)); |
| | 0 | 1802 | | return true; |
| | 0 | 1803 | | } |
| | | 1804 | | |
| | | 1805 | | private bool ForceSendConnectionWindowUpdate() |
| | 0 | 1806 | | { |
| | 0 | 1807 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(_pendingWindowUpdate)}={_pendingWindowUpdate}"); |
| | 0 | 1808 | | if (_pendingWindowUpdate == 0) return false; |
| | | 1809 | | |
| | 0 | 1810 | | LogExceptions(SendWindowUpdateAsync(0, _pendingWindowUpdate)); |
| | 0 | 1811 | | _pendingWindowUpdate = 0; |
| | 0 | 1812 | | return true; |
| | 0 | 1813 | | } |
| | | 1814 | | |
| | | 1815 | | /// <summary>Abort all streams and cause further processing to fail.</summary> |
| | | 1816 | | /// <param name="abortException">Exception causing Abort to be called.</param> |
| | | 1817 | | private void Abort(Exception abortException) |
| | 0 | 1818 | | { |
| | 0 | 1819 | | if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(abortException)}=={abortException}"); |
| | | 1820 | | |
| | | 1821 | | // The connection has failed, e.g. failed IO or a connection-level protocol error. |
| | 0 | 1822 | | List<Http2Stream> streamsToAbort = new List<Http2Stream>(); |
| | 0 | 1823 | | lock (SyncObject) |
| | 0 | 1824 | | { |
| | 0 | 1825 | | if (_abortException is not null) |
| | 0 | 1826 | | { |
| | 0 | 1827 | | if (NetEventSource.Log.IsEnabled()) Trace($"Abort called while already aborting. {nameof(abortExcept |
| | 0 | 1828 | | return; |
| | | 1829 | | } |
| | | 1830 | | |
| | 0 | 1831 | | _abortException = abortException; |
| | | 1832 | | |
| | 0 | 1833 | | Shutdown(); |
| | | 1834 | | |
| | 0 | 1835 | | foreach (KeyValuePair<int, Http2Stream> kvp in _httpStreams) |
| | 0 | 1836 | | { |
| | 0 | 1837 | | int streamId = kvp.Key; |
| | 0 | 1838 | | Debug.Assert(streamId == kvp.Value.StreamId); |
| | | 1839 | | |
| | 0 | 1840 | | streamsToAbort.Add(kvp.Value); |
| | 0 | 1841 | | } |
| | 0 | 1842 | | } |
| | | 1843 | | |
| | | 1844 | | // Avoid calling OnReset under the lock, as it may cause the Http2Stream to call back in to RemoveStream |
| | 0 | 1845 | | foreach (Http2Stream s in streamsToAbort) |
| | 0 | 1846 | | { |
| | 0 | 1847 | | s.OnReset(_abortException); |
| | 0 | 1848 | | } |
| | 0 | 1849 | | } |
| | | 1850 | | |
| | | 1851 | | private void FinalTeardown() |
| | 0 | 1852 | | { |
| | 0 | 1853 | | if (NetEventSource.Log.IsEnabled()) Trace(""); |
| | | 1854 | | |
| | 0 | 1855 | | Debug.Assert(_shutdown); |
| | 0 | 1856 | | Debug.Assert(_streamsInUse == 0); |
| | | 1857 | | |
| | 0 | 1858 | | GC.SuppressFinalize(this); |
| | 0 | 1859 | | _stream.Dispose(); |
| | | 1860 | | |
| | 0 | 1861 | | _connectionWindow.Dispose(); |
| | 0 | 1862 | | bool completed = _writeChannel.Writer.TryComplete(); |
| | 0 | 1863 | | Debug.Assert(completed, "FinalTeardown was called twice"); |
| | | 1864 | | |
| | | 1865 | | // We're not disposing the _incomingBuffer and _outgoingBuffer here as they may still be in use by |
| | | 1866 | | // ProcessIncomingFramesAsync and ProcessOutgoingFramesAsync respectively, and those methods are |
| | | 1867 | | // responsible for returning the buffers. |
| | | 1868 | | |
| | 0 | 1869 | | MarkConnectionAsClosed(); |
| | 0 | 1870 | | } |
| | | 1871 | | |
| | | 1872 | | public override void Dispose() |
| | 0 | 1873 | | { |
| | 0 | 1874 | | lock (SyncObject) |
| | 0 | 1875 | | { |
| | 0 | 1876 | | Shutdown(); |
| | 0 | 1877 | | } |
| | 0 | 1878 | | } |
| | | 1879 | | |
| | | 1880 | | private enum FrameType : byte |
| | | 1881 | | { |
| | | 1882 | | Data = 0, |
| | | 1883 | | Headers = 1, |
| | | 1884 | | Priority = 2, |
| | | 1885 | | RstStream = 3, |
| | | 1886 | | Settings = 4, |
| | | 1887 | | PushPromise = 5, |
| | | 1888 | | Ping = 6, |
| | | 1889 | | GoAway = 7, |
| | | 1890 | | WindowUpdate = 8, |
| | | 1891 | | Continuation = 9, |
| | | 1892 | | AltSvc = 10, |
| | | 1893 | | |
| | | 1894 | | Last = 10 |
| | | 1895 | | } |
| | | 1896 | | |
| | | 1897 | | private readonly struct FrameHeader |
| | | 1898 | | { |
| | | 1899 | | public readonly int PayloadLength; |
| | | 1900 | | public readonly FrameType Type; |
| | | 1901 | | public readonly FrameFlags Flags; |
| | | 1902 | | public readonly int StreamId; |
| | | 1903 | | |
| | | 1904 | | public const int Size = 9; |
| | | 1905 | | public const int MaxPayloadLength = 16384; |
| | | 1906 | | |
| | | 1907 | | public const int SettingLength = 6; // per setting (total SETTINGS length must be a multiple of t |
| | | 1908 | | public const int PriorityInfoLength = 5; // for both PRIORITY frame and priority info within HEADERS |
| | | 1909 | | public const int PingLength = 8; |
| | | 1910 | | public const int WindowUpdateLength = 4; |
| | | 1911 | | public const int RstStreamLength = 4; |
| | | 1912 | | public const int GoAwayMinLength = 8; |
| | | 1913 | | |
| | | 1914 | | public FrameHeader(int payloadLength, FrameType type, FrameFlags flags, int streamId) |
| | 0 | 1915 | | { |
| | 0 | 1916 | | Debug.Assert(streamId >= 0); |
| | | 1917 | | |
| | 0 | 1918 | | PayloadLength = payloadLength; |
| | 0 | 1919 | | Type = type; |
| | 0 | 1920 | | Flags = flags; |
| | 0 | 1921 | | StreamId = streamId; |
| | 0 | 1922 | | } |
| | | 1923 | | |
| | 0 | 1924 | | public bool PaddedFlag => (Flags & FrameFlags.Padded) != 0; |
| | 0 | 1925 | | public bool AckFlag => (Flags & FrameFlags.Ack) != 0; |
| | 0 | 1926 | | public bool EndHeadersFlag => (Flags & FrameFlags.EndHeaders) != 0; |
| | 0 | 1927 | | public bool EndStreamFlag => (Flags & FrameFlags.EndStream) != 0; |
| | 0 | 1928 | | public bool PriorityFlag => (Flags & FrameFlags.Priority) != 0; |
| | | 1929 | | |
| | | 1930 | | public static FrameHeader ReadFrom(ReadOnlySpan<byte> buffer) |
| | 0 | 1931 | | { |
| | 0 | 1932 | | Debug.Assert(buffer.Length >= Size); |
| | | 1933 | | |
| | 0 | 1934 | | FrameFlags flags = (FrameFlags)buffer[4]; // do first to avoid some bounds checks |
| | 0 | 1935 | | int payloadLength = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2]; |
| | 0 | 1936 | | FrameType type = (FrameType)buffer[3]; |
| | 0 | 1937 | | int streamId = (int)(BinaryPrimitives.ReadUInt32BigEndian(buffer.Slice(5)) & 0x7FFFFFFF); |
| | | 1938 | | |
| | 0 | 1939 | | return new FrameHeader(payloadLength, type, flags, streamId); |
| | 0 | 1940 | | } |
| | | 1941 | | |
| | | 1942 | | public static void WriteTo(Span<byte> destination, int payloadLength, FrameType type, FrameFlags flags, int |
| | 0 | 1943 | | { |
| | 0 | 1944 | | Debug.Assert(destination.Length >= Size); |
| | 0 | 1945 | | Debug.Assert(type <= FrameType.Last); |
| | 0 | 1946 | | Debug.Assert((flags & FrameFlags.ValidBits) == flags); |
| | 0 | 1947 | | Debug.Assert((uint)payloadLength <= MaxPayloadLength); |
| | | 1948 | | |
| | | 1949 | | // This ordering helps eliminate bounds checks. |
| | 0 | 1950 | | BinaryPrimitives.WriteInt32BigEndian(destination.Slice(5), streamId); |
| | 0 | 1951 | | destination[4] = (byte)flags; |
| | 0 | 1952 | | destination[0] = (byte)((payloadLength & 0x00FF0000) >> 16); |
| | 0 | 1953 | | destination[1] = (byte)((payloadLength & 0x0000FF00) >> 8); |
| | 0 | 1954 | | destination[2] = (byte)(payloadLength & 0x000000FF); |
| | 0 | 1955 | | destination[3] = (byte)type; |
| | 0 | 1956 | | } |
| | | 1957 | | |
| | 0 | 1958 | | public override string ToString() => $"StreamId={StreamId}; Type={Type}; Flags={Flags}; PayloadLength={Paylo |
| | | 1959 | | } |
| | | 1960 | | |
| | | 1961 | | [Flags] |
| | | 1962 | | private enum FrameFlags : byte |
| | | 1963 | | { |
| | | 1964 | | None = 0, |
| | | 1965 | | |
| | | 1966 | | // Some frame types define bits differently. Define them all here for simplicity. |
| | | 1967 | | |
| | | 1968 | | EndStream = 0b00000001, |
| | | 1969 | | Ack = 0b00000001, |
| | | 1970 | | EndHeaders = 0b00000100, |
| | | 1971 | | Padded = 0b00001000, |
| | | 1972 | | Priority = 0b00100000, |
| | | 1973 | | |
| | | 1974 | | ValidBits = 0b00101101 |
| | | 1975 | | } |
| | | 1976 | | |
| | | 1977 | | private enum SettingId : ushort |
| | | 1978 | | { |
| | | 1979 | | HeaderTableSize = 0x1, |
| | | 1980 | | EnablePush = 0x2, |
| | | 1981 | | MaxConcurrentStreams = 0x3, |
| | | 1982 | | InitialWindowSize = 0x4, |
| | | 1983 | | MaxFrameSize = 0x5, |
| | | 1984 | | MaxHeaderListSize = 0x6, |
| | | 1985 | | EnableConnect = 0x8 |
| | | 1986 | | } |
| | | 1987 | | |
| | | 1988 | | private static TaskCompletionSourceWithCancellation<bool> CreateSuccessfullyCompletedTcs() |
| | 0 | 1989 | | { |
| | 0 | 1990 | | var tcs = new TaskCompletionSourceWithCancellation<bool>(); |
| | 0 | 1991 | | tcs.TrySetResult(true); |
| | 0 | 1992 | | return tcs; |
| | 0 | 1993 | | } |
| | | 1994 | | |
| | | 1995 | | // Note that this is safe to be called concurrently by multiple threads. |
| | | 1996 | | |
| | | 1997 | | public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, CancellationToken cance |
| | 0 | 1998 | | { |
| | 0 | 1999 | | Debug.Assert(async); |
| | 0 | 2000 | | Debug.Assert(!_pool.HasSyncObjLock); |
| | 0 | 2001 | | if (NetEventSource.Log.IsEnabled()) Trace($"Sending request: {request}"); |
| | 0 | 2002 | | if (ConnectionSetupActivity is not null) ConnectionSetupDistributedTracing.AddConnectionLinkToRequestActivit |
| | | 2003 | | |
| | | 2004 | | try |
| | 0 | 2005 | | { |
| | | 2006 | | // Send request headers |
| | 0 | 2007 | | bool shouldExpectContinue = (request.Content != null && request.HasHeaders && request.Headers.ExpectCont |
| | 0 | 2008 | | Http2Stream http2Stream = await SendHeadersAsync(request, cancellationToken, mustFlush: shouldExpectCont |
| | | 2009 | | |
| | 0 | 2010 | | bool duplex = request.Content != null && request.Content.AllowDuplex; |
| | | 2011 | | |
| | | 2012 | | // If we have duplex content, then don't propagate the cancellation to the request body task. |
| | | 2013 | | // If cancellation occurs before we receive the response headers, then we will cancel the request body a |
| | 0 | 2014 | | CancellationToken requestBodyCancellationToken = duplex ? CancellationToken.None : cancellationToken; |
| | | 2015 | | |
| | | 2016 | | // Start sending request body, if any. |
| | 0 | 2017 | | Task requestBodyTask = http2Stream.SendRequestBodyAsync(requestBodyCancellationToken); |
| | | 2018 | | |
| | | 2019 | | // Start receiving the response headers. |
| | 0 | 2020 | | Task responseHeadersTask = http2Stream.ReadResponseHeadersAsync(cancellationToken); |
| | | 2021 | | |
| | | 2022 | | // Wait for either task to complete. The best and most common case is when the request body completes |
| | | 2023 | | // before the response headers, in which case we can fully process the sending of the request and then |
| | | 2024 | | // fully process the sending of the response. WhenAny is not free, so we do a fast-path check to see |
| | | 2025 | | // if the request body completed synchronously, only progressing to do the WhenAny if it didn't. Then |
| | | 2026 | | // if the WhenAny completes and either the WhenAny indicated that the request body completed or |
| | | 2027 | | // both tasks completed, we can proceed to handle the request body as if it completed first. We also |
| | | 2028 | | // check whether the request content even allows for duplex communication; if it doesn't (none of |
| | | 2029 | | // our built-in content types do), then we can just proceed to wait for the request body content to |
| | | 2030 | | // complete before worrying about response headers completing. |
| | 0 | 2031 | | if (requestBodyTask.IsCompleted || |
| | 0 | 2032 | | !duplex || |
| | 0 | 2033 | | await Task.WhenAny(requestBodyTask, responseHeadersTask).ConfigureAwait(false) == requestBodyTask || |
| | 0 | 2034 | | requestBodyTask.IsCompleted || |
| | 0 | 2035 | | http2Stream.SendRequestFinished) |
| | 0 | 2036 | | { |
| | | 2037 | | // The sending of the request body completed before receiving all of the request headers (or we're |
| | | 2038 | | // ok waiting for the request body even if it hasn't completed, e.g. because we're not doing duplex) |
| | | 2039 | | // This is the common and desirable case. |
| | | 2040 | | try |
| | 0 | 2041 | | { |
| | 0 | 2042 | | await requestBodyTask.ConfigureAwait(false); |
| | 0 | 2043 | | } |
| | 0 | 2044 | | catch (Exception e) |
| | 0 | 2045 | | { |
| | 0 | 2046 | | if (NetEventSource.Log.IsEnabled()) Trace($"Sending request content failed: {e}"); |
| | 0 | 2047 | | LogExceptions(responseHeadersTask); // Observe exception (if any) on responseHeadersTask. |
| | 0 | 2048 | | throw; |
| | | 2049 | | } |
| | 0 | 2050 | | } |
| | | 2051 | | else |
| | 0 | 2052 | | { |
| | | 2053 | | // We received the response headers but the request body hasn't yet finished; this most commonly hap |
| | | 2054 | | // when the protocol is being used to enable duplex communication. If the connection is aborted or i |
| | | 2055 | | // get RST or GOAWAY from server, exception will be stored in stream._abortException and propagated |
| | | 2056 | | // to caller if possible while processing response, but make sure that we log any exceptions from th |
| | | 2057 | | // completing asynchronously). |
| | 0 | 2058 | | LogExceptions(requestBodyTask); |
| | 0 | 2059 | | } |
| | | 2060 | | |
| | | 2061 | | // Wait for the response headers to complete if they haven't already, propagating any exceptions. |
| | 0 | 2062 | | await responseHeadersTask.ConfigureAwait(false); |
| | | 2063 | | |
| | 0 | 2064 | | return http2Stream.GetAndClearResponse(); |
| | | 2065 | | } |
| | 0 | 2066 | | catch (HttpIOException e) |
| | 0 | 2067 | | { |
| | 0 | 2068 | | throw new HttpRequestException(e.HttpRequestError, e.Message, e); |
| | | 2069 | | } |
| | 0 | 2070 | | catch (Exception e) when (e is IOException || e is ObjectDisposedException || e is InvalidOperationException |
| | 0 | 2071 | | { |
| | 0 | 2072 | | throw new HttpRequestException(HttpRequestError.Unknown, SR.net_http_client_execution_error, e); |
| | | 2073 | | } |
| | 0 | 2074 | | } |
| | | 2075 | | |
| | | 2076 | | private void RemoveStream(Http2Stream http2Stream) |
| | 0 | 2077 | | { |
| | 0 | 2078 | | if (NetEventSource.Log.IsEnabled()) Trace(http2Stream.StreamId, ""); |
| | | 2079 | | |
| | 0 | 2080 | | lock (SyncObject) |
| | 0 | 2081 | | { |
| | 0 | 2082 | | if (!_httpStreams.Remove(http2Stream.StreamId)) |
| | 0 | 2083 | | { |
| | 0 | 2084 | | Debug.Fail($"Stream {http2Stream.StreamId} not found in dictionary during RemoveStream???"); |
| | | 2085 | | return; |
| | | 2086 | | } |
| | | 2087 | | |
| | 0 | 2088 | | if (_httpStreams.Count == 0) |
| | 0 | 2089 | | { |
| | 0 | 2090 | | MarkConnectionAsIdle(); |
| | 0 | 2091 | | } |
| | 0 | 2092 | | } |
| | | 2093 | | |
| | 0 | 2094 | | ReleaseStream(); |
| | 0 | 2095 | | } |
| | | 2096 | | |
| | | 2097 | | private void RefreshPingTimestamp() |
| | 0 | 2098 | | { |
| | 0 | 2099 | | _nextPingRequestTimestamp = Environment.TickCount64 + _keepAlivePingDelay; |
| | 0 | 2100 | | } |
| | | 2101 | | |
| | | 2102 | | private void ProcessPingAck(long payload) |
| | 0 | 2103 | | { |
| | | 2104 | | // RttEstimator is using negative values in PING payloads. |
| | | 2105 | | // _keepAlivePingPayload is always non-negative. |
| | 0 | 2106 | | if (payload < 0) // RTT ping |
| | 0 | 2107 | | { |
| | 0 | 2108 | | _rttEstimator.OnPingAckReceived(payload, this); |
| | 0 | 2109 | | } |
| | | 2110 | | else // Keepalive ping |
| | 0 | 2111 | | { |
| | 0 | 2112 | | if (_keepAliveState != KeepAliveState.PingSent) |
| | 0 | 2113 | | ThrowProtocolError(); |
| | 0 | 2114 | | if (Interlocked.Read(ref _keepAlivePingPayload) != payload) |
| | 0 | 2115 | | ThrowProtocolError(); |
| | 0 | 2116 | | _keepAliveState = KeepAliveState.None; |
| | 0 | 2117 | | } |
| | 0 | 2118 | | } |
| | | 2119 | | |
| | | 2120 | | private void VerifyKeepAlive() |
| | 0 | 2121 | | { |
| | 0 | 2122 | | if (_keepAlivePingPolicy == HttpKeepAlivePingPolicy.WithActiveRequests) |
| | 0 | 2123 | | { |
| | 0 | 2124 | | lock (SyncObject) |
| | 0 | 2125 | | { |
| | 0 | 2126 | | if (_streamsInUse == 0) |
| | 0 | 2127 | | { |
| | 0 | 2128 | | return; |
| | | 2129 | | } |
| | 0 | 2130 | | } |
| | 0 | 2131 | | } |
| | | 2132 | | |
| | 0 | 2133 | | long now = Environment.TickCount64; |
| | 0 | 2134 | | switch (_keepAliveState) |
| | | 2135 | | { |
| | | 2136 | | case KeepAliveState.None: |
| | | 2137 | | // Check whether keep alive delay has passed since last frame received |
| | 0 | 2138 | | if (now > _nextPingRequestTimestamp) |
| | 0 | 2139 | | { |
| | | 2140 | | // Set the status directly to ping sent and set the timestamp |
| | 0 | 2141 | | _keepAliveState = KeepAliveState.PingSent; |
| | 0 | 2142 | | _keepAlivePingTimeoutTimestamp = now + _keepAlivePingTimeout; |
| | | 2143 | | |
| | 0 | 2144 | | long pingPayload = Interlocked.Increment(ref _keepAlivePingPayload); |
| | 0 | 2145 | | LogExceptions(SendPingAsync(pingPayload)); |
| | 0 | 2146 | | return; |
| | | 2147 | | } |
| | 0 | 2148 | | break; |
| | | 2149 | | case KeepAliveState.PingSent: |
| | 0 | 2150 | | if (now > _keepAlivePingTimeoutTimestamp) |
| | 0 | 2151 | | ThrowProtocolError(Http2ProtocolErrorCode.ProtocolError, SR.net_ping_request_timed_out); |
| | 0 | 2152 | | break; |
| | | 2153 | | default: |
| | 0 | 2154 | | Debug.Fail($"Unexpected keep alive state ({_keepAliveState})"); |
| | | 2155 | | break; |
| | | 2156 | | } |
| | 0 | 2157 | | } |
| | | 2158 | | |
| | 0 | 2159 | | public sealed override string ToString() => $"{nameof(Http2Connection)}({_pool})"; // Description for diagnostic |
| | | 2160 | | |
| | | 2161 | | public override void Trace(string message, [CallerMemberName] string? memberName = null) => |
| | 0 | 2162 | | Trace(0, message, memberName); |
| | | 2163 | | |
| | | 2164 | | internal void Trace(int streamId, string message, [CallerMemberName] string? memberName = null) => |
| | 0 | 2165 | | NetEventSource.Log.HandlerMessage( |
| | 0 | 2166 | | _pool?.GetHashCode() ?? 0, // pool ID |
| | 0 | 2167 | | GetHashCode(), // connection ID |
| | 0 | 2168 | | streamId, // stream ID |
| | 0 | 2169 | | memberName, // method name |
| | 0 | 2170 | | message); // message |
| | | 2171 | | |
| | | 2172 | | [DoesNotReturn] |
| | | 2173 | | private static void ThrowRetry(string message, Exception? innerException = null) => |
| | 0 | 2174 | | throw new HttpRequestException((innerException as HttpIOException)?.HttpRequestError ?? HttpRequestError.Unk |
| | | 2175 | | |
| | | 2176 | | private static Exception GetRequestAbortedException(Exception? innerException = null) => |
| | 0 | 2177 | | innerException as HttpIOException ?? ExceptionDispatchInfo.SetCurrentStackTrace(new IOException(SR.net_http_ |
| | | 2178 | | |
| | | 2179 | | [DoesNotReturn] |
| | | 2180 | | private static void ThrowRequestAborted(Exception? innerException = null) => |
| | 0 | 2181 | | throw GetRequestAbortedException(innerException); |
| | | 2182 | | |
| | | 2183 | | [DoesNotReturn] |
| | | 2184 | | private static void ThrowProtocolError() => |
| | 0 | 2185 | | ThrowProtocolError(Http2ProtocolErrorCode.ProtocolError); |
| | | 2186 | | |
| | | 2187 | | [DoesNotReturn] |
| | | 2188 | | private static void ThrowProtocolError(Http2ProtocolErrorCode errorCode, string? message = null) => |
| | 0 | 2189 | | throw HttpProtocolException.CreateHttp2ConnectionException(errorCode, message); |
| | | 2190 | | } |
| | | 2191 | | } |