forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp3LoopbackConnection.cs
More file actions
228 lines (186 loc) · 8.49 KB
/
Http3LoopbackConnection.cs
File metadata and controls
228 lines (186 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Net.Quic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Net.Http.Functional.Tests;
namespace System.Net.Test.Common
{
internal sealed class Http3LoopbackConnection : GenericLoopbackConnection
{
public const long H3_NO_ERROR = 0x100;
public const long H3_GENERAL_PROTOCOL_ERROR = 0x101;
public const long H3_INTERNAL_ERROR = 0x102;
public const long H3_STREAM_CREATION_ERROR = 0x103;
public const long H3_CLOSED_CRITICAL_STREAM = 0x104;
public const long H3_FRAME_UNEXPECTED = 0x105;
public const long H3_FRAME_ERROR = 0x106;
public const long H3_EXCESSIVE_LOAD = 0x107;
public const long H3_ID_ERROR = 0x108;
public const long H3_SETTINGS_ERROR = 0x109;
public const long H3_MISSING_SETTINGS = 0x10a;
public const long H3_REQUEST_REJECTED = 0x10b;
public const long H3_REQUEST_CANCELLED = 0x10c;
public const long H3_REQUEST_INCOMPLETE = 0x10d;
public const long H3_CONNECT_ERROR = 0x10f;
public const long H3_VERSION_FALLBACK = 0x110;
private readonly QuicConnection _connection;
private readonly Dictionary<int, Http3LoopbackStream> _openStreams = new Dictionary<int, Http3LoopbackStream>();
private Http3LoopbackStream _controlStream; // Our outbound control stream
private Http3LoopbackStream _currentStream;
private bool _closed;
public Http3LoopbackConnection(QuicConnection connection)
{
_connection = connection;
}
public override void Dispose()
{
foreach (Http3LoopbackStream stream in _openStreams.Values)
{
stream.Dispose();
}
if (!_closed)
{
// CloseAsync(H3_INTERNAL_ERROR).GetAwaiter().GetResult();
}
//_connection.Dispose();
}
public async Task CloseAsync(long errorCode)
{
await _connection.CloseAsync(errorCode).ConfigureAwait(false);
_closed = true;
}
public Http3LoopbackStream OpenUnidirectionalStream()
{
return new Http3LoopbackStream(_connection.OpenUnidirectionalStream());
}
public Http3LoopbackStream OpenBidirectionalStream()
{
return new Http3LoopbackStream(_connection.OpenBidirectionalStream());
}
public static int GetRequestId(QuicStream stream)
{
Debug.Assert(stream.CanRead && stream.CanWrite, "Stream must be a request stream.");
// TODO: QUIC streams can have IDs larger than int.MaxValue; update all our tests to use long rather than int.
return checked((int)stream.StreamId + 1);
}
public Http3LoopbackStream GetOpenRequest(int requestId = 0)
{
return requestId == 0 ? _currentStream : _openStreams[requestId - 1];
}
public override Task InitializeConnectionAsync()
{
throw new NotImplementedException();
}
public async Task<Http3LoopbackStream> AcceptStreamAsync()
{
QuicStream quicStream = await _connection.AcceptStreamAsync().ConfigureAwait(false);
var stream = new Http3LoopbackStream(quicStream);
_openStreams.Add(checked((int)quicStream.StreamId), stream);
_currentStream = stream;
return stream;
}
public async Task<Http3LoopbackStream> AcceptRequestStreamAsync()
{
Http3LoopbackStream stream;
do
{
stream = await AcceptStreamAsync().ConfigureAwait(false);
}
while (!stream.CanWrite); // skip control stream.
return stream;
}
public async Task<(Http3LoopbackStream clientControlStream, Http3LoopbackStream requestStream)> AcceptControlAndRequestStreamAsync()
{
Http3LoopbackStream streamA = null, streamB = null;
try
{
streamA = await AcceptStreamAsync();
streamB = await AcceptStreamAsync();
return (streamA.CanWrite, streamB.CanWrite) switch
{
(false, true) => (streamA, streamB),
(true, false) => (streamB, streamA),
_ => throw new Exception("Expected one unidirectional and one bidirectional stream; received something else.")
};
}
catch
{
streamA?.Dispose();
streamB?.Dispose();
throw;
}
}
public async Task EstablishControlStreamAsync()
{
_controlStream = OpenUnidirectionalStream();
await _controlStream.SendUnidirectionalStreamTypeAsync(Http3LoopbackStream.ControlStream);
await _controlStream.SendSettingsFrameAsync();
}
public override async Task<byte[]> ReadRequestBodyAsync()
{
return await _currentStream.ReadRequestBodyAsync().ConfigureAwait(false);
}
public override async Task<HttpRequestData> ReadRequestDataAsync(bool readBody = true)
{
Http3LoopbackStream stream = await AcceptRequestStreamAsync().ConfigureAwait(false);
return await stream.ReadRequestDataAsync(readBody).ConfigureAwait(false);
}
public override Task SendResponseAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null, string content = "", bool isFinal = true, int requestId = 0)
{
return GetOpenRequest(requestId).SendResponseAsync(statusCode, headers, content, isFinal);
}
public override Task SendResponseBodyAsync(byte[] content, bool isFinal = true, int requestId = 0)
{
return GetOpenRequest(requestId).SendResponseBodyAsync(content, isFinal);
}
public override Task SendResponseHeadersAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null, int requestId = 0)
{
return GetOpenRequest(requestId).SendResponseHeadersAsync(statusCode, headers);
}
public override Task SendPartialResponseHeadersAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null, int requestId = 0)
{
return GetOpenRequest(requestId).SendPartialResponseHeadersAsync(statusCode, headers);
}
public override async Task<HttpRequestData> HandleRequestAsync(HttpStatusCode statusCode = HttpStatusCode.OK, IList<HttpHeaderData> headers = null, string content = "")
{
Http3LoopbackStream stream = await AcceptRequestStreamAsync().ConfigureAwait(false);
HttpRequestData request = await stream.ReadRequestDataAsync().ConfigureAwait(false);
// We are about to close the connection, after we send the response.
// So, send a GOAWAY frame now so the client won't inadvertantly try to reuse the connection.
await _controlStream.SendGoAwayFrameAsync(stream.StreamId + 4);
await stream.SendResponseAsync(statusCode, headers, content).ConfigureAwait(false);
await WaitForClientDisconnectAsync();
return request;
}
// Wait for the client to close the connection, e.g. after we send a GOAWAY, or after the HttpClient is disposed.
public async Task WaitForClientDisconnectAsync()
{
while (true)
{
Http3LoopbackStream stream;
try
{
stream = await AcceptRequestStreamAsync().ConfigureAwait(false);
}
catch (QuicConnectionAbortedException abortException) when (abortException.ErrorCode == H3_NO_ERROR)
{
break;
}
using (stream)
{
await stream.AbortAndWaitForShutdownAsync(H3_REQUEST_REJECTED);
}
}
await CloseAsync(H3_NO_ERROR);
}
public override async Task WaitForCancellationAsync(bool ignoreIncomingData = true, int requestId = 0)
{
await GetOpenRequest(requestId).WaitForCancellationAsync(ignoreIncomingData).ConfigureAwait(false);
}
}
}