-
Notifications
You must be signed in to change notification settings - Fork 833
Expand file tree
/
Copy pathPipeExtensions.cs
More file actions
527 lines (452 loc) · 21.7 KB
/
PipeExtensions.cs
File metadata and controls
527 lines (452 loc) · 21.7 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#region Copyright notice and license
// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System.Buffers;
using System.Buffers.Binary;
using System.Diagnostics;
using System.IO.Pipelines;
using System.Runtime.CompilerServices;
using Grpc.Core;
using Grpc.Net.Compression;
using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.Logging;
namespace Grpc.AspNetCore.Server.Internal;
internal static partial class PipeExtensions
{
private const int MessageDelimiterSize = 4; // how many bytes it takes to encode "Message-Length"
private const int HeaderSize = MessageDelimiterSize + 1; // message length + compression flag
private static readonly Status MessageCancelledStatus = new Status(StatusCode.Internal, "Incoming message cancelled.");
private static readonly Status AdditionalDataStatus = new Status(StatusCode.Internal, "Additional data after the message received.");
private static readonly Status IncompleteMessageStatus = new Status(StatusCode.Internal, "Incomplete message.");
private static readonly Status ReceivedMessageExceedsLimitStatus = new Status(StatusCode.ResourceExhausted, "Received message exceeds the maximum configured message size.");
private static readonly Status NoMessageEncodingMessageStatus = new Status(StatusCode.Internal, "Request did not include grpc-encoding value with compressed message.");
private static readonly Status IdentityMessageEncodingMessageStatus = new Status(StatusCode.Internal, "Request sent 'identity' grpc-encoding value with compressed message.");
private static Status CreateUnknownMessageEncodingMessageStatus(string unsupportedEncoding, IEnumerable<string> supportedEncodings)
{
return new Status(StatusCode.Unimplemented, $"Unsupported grpc-encoding value '{unsupportedEncoding}'. Supported encodings: {string.Join(", ", supportedEncodings)}");
}
public static async Task WriteSingleMessageAsync<TResponse>(this PipeWriter pipeWriter, TResponse response, HttpContextServerCallContext serverCallContext, Action<TResponse, SerializationContext> serializer)
where TResponse : class
{
var logger = serverCallContext.Logger;
try
{
// Must call StartAsync before the first pipeWriter.GetSpan() in WriteHeader
var httpResponse = serverCallContext.HttpContext.Response;
if (!httpResponse.HasStarted)
{
await httpResponse.StartAsync();
}
GrpcServerLog.SendingMessage(logger);
var serializationContext = serverCallContext.SerializationContext;
serializationContext.Reset();
serializationContext.ResponseBufferWriter = pipeWriter;
serializer(response, serializationContext);
GrpcServerLog.MessageSent(serverCallContext.Logger);
if (GrpcEventSource.Log.IsEnabled())
{
GrpcEventSource.Log.MessageSent();
}
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Don't write error when user cancels write
GrpcServerLog.ErrorSendingMessage(logger, ex);
throw;
}
}
public static async Task WriteStreamedMessageAsync<TResponse>(this PipeWriter pipeWriter, TResponse response, HttpContextServerCallContext serverCallContext, Action<TResponse, SerializationContext> serializer, CancellationToken cancellationToken = default)
where TResponse : class
{
var logger = serverCallContext.Logger;
try
{
// Must call StartAsync before the first pipeWriter.GetSpan() in WriteHeader
var httpResponse = serverCallContext.HttpContext.Response;
if (!httpResponse.HasStarted)
{
await httpResponse.StartAsync(cancellationToken);
}
GrpcServerLog.SendingMessage(logger);
var serializationContext = serverCallContext.SerializationContext;
serializationContext.Reset();
serializationContext.ResponseBufferWriter = pipeWriter;
serializer(response, serializationContext);
// Flush messages unless WriteOptions.Flags has BufferHint set
var flush = ((serverCallContext.WriteOptions?.Flags ?? default) & WriteFlags.BufferHint) != WriteFlags.BufferHint;
if (flush)
{
var flushResult = await pipeWriter.FlushAsync(cancellationToken);
// Workaround bug where FlushAsync doesn't return IsCanceled = true on request abort.
// https://github.com/dotnet/aspnetcore/issues/40788
// Also, sometimes the request CT isn't triggered. Also check CT passed into method.
if (!flushResult.IsCompleted &&
(serverCallContext.CancellationToken.IsCancellationRequested || cancellationToken.IsCancellationRequested))
{
throw new OperationCanceledException("Request aborted while sending the message.");
}
}
GrpcServerLog.MessageSent(serverCallContext.Logger);
if (GrpcEventSource.Log.IsEnabled())
{
GrpcEventSource.Log.MessageSent();
}
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Don't write error when user cancels write
GrpcServerLog.ErrorSendingMessage(logger, ex);
throw;
}
}
private static int DecodeMessageLength(ReadOnlySpan<byte> buffer)
{
Debug.Assert(buffer.Length >= MessageDelimiterSize, "Buffer too small to decode message length.");
var result = BinaryPrimitives.ReadUInt32BigEndian(buffer);
if (result > int.MaxValue)
{
throw new IOException("Message too large: " + result);
}
return (int)result;
}
private static bool TryReadHeader(in ReadOnlySequence<byte> buffer, out bool compressed, out int messageLength)
{
if (buffer.Length < HeaderSize)
{
compressed = false;
messageLength = 0;
return false;
}
if (buffer.First.Length >= HeaderSize)
{
var headerData = buffer.First.Span.Slice(0, HeaderSize);
compressed = ReadCompressedFlag(headerData[0]);
messageLength = DecodeMessageLength(headerData.Slice(1));
}
else
{
Span<byte> headerData = stackalloc byte[HeaderSize];
buffer.Slice(0, HeaderSize).CopyTo(headerData);
compressed = ReadCompressedFlag(headerData[0]);
messageLength = DecodeMessageLength(headerData.Slice(1));
}
return true;
}
private static bool ReadCompressedFlag(byte flag)
{
if (flag == 0)
{
return false;
}
else if (flag == 1)
{
return true;
}
else
{
throw new InvalidDataException("Unexpected compressed flag value in message header.");
}
}
/// <summary>
/// Read a single message from the pipe reader. Ensure the reader completes without additional data.
/// </summary>
/// <param name="input">The request pipe reader.</param>
/// <param name="serverCallContext">The request context.</param>
/// <param name="deserializer">Message deserializer.</param>
/// <returns>Complete message data.</returns>
public static async ValueTask<T> ReadSingleMessageAsync<T>(this PipeReader input, HttpContextServerCallContext serverCallContext, Func<DeserializationContext, T> deserializer)
where T : class
{
var logger = serverCallContext.Logger;
try
{
GrpcServerLog.ReadingMessage(logger);
T? request = null;
while (true)
{
// Check for client disconnect before reading
serverCallContext.EnsureRequestNotAborted();
var result = await input.ReadAsync();
var buffer = result.Buffer;
try
{
if (result.IsCanceled)
{
throw new RpcException(MessageCancelledStatus);
}
// Check for client disconnect during processing
serverCallContext.EnsureRequestNotAborted();
if (!buffer.IsEmpty)
{
if (request != null)
{
throw new RpcException(AdditionalDataStatus);
}
if (TryReadMessage(ref buffer, serverCallContext, out var data))
{
// Check for client disconnect before deserialization
serverCallContext.EnsureRequestNotAborted();
// Finished and the complete message has arrived
GrpcServerLog.DeserializingMessage(logger, (int)data.Length, typeof(T));
serverCallContext.DeserializationContext.SetPayload(data);
request = deserializer(serverCallContext.DeserializationContext);
serverCallContext.DeserializationContext.SetPayload(null);
GrpcServerLog.ReceivedMessage(logger);
if (GrpcEventSource.Log.IsEnabled())
{
GrpcEventSource.Log.MessageReceived();
}
// Store the request
// Need to verify the request completes with no additional data
}
}
if (result.IsCompleted)
{
if (request != null)
{
// Additional data came with message
if (buffer.Length > 0)
{
throw new RpcException(AdditionalDataStatus);
}
return request;
}
throw new RpcException(IncompleteMessageStatus);
}
}
finally
{
// The buffer was sliced up to where it was consumed, so we can just advance to the start.
if (request != null)
{
input.AdvanceTo(buffer.Start);
}
else
{
// We mark examined as buffer.End so that if we didn't receive a full frame, we'll wait for more data
// before yielding the read again.
input.AdvanceTo(buffer.Start, buffer.End);
}
}
}
}
catch (OperationCanceledException ex) when (serverCallContext.HttpContext.RequestAborted.IsCancellationRequested)
{
// Convert operation canceled due to client disconnect to proper RpcException
throw new RpcException(new Status(StatusCode.Cancelled, "Call canceled by the client.", ex));
}
catch (IOException ex) when (IsConnectionResetException(ex))
{
// Convert connection reset to proper RpcException
throw new RpcException(new Status(StatusCode.Cancelled, "Client disconnected during request.", ex));
}
catch (Exception ex) when (IsConnectionAbortedException(ex))
{
// Convert connection aborted to proper RpcException
throw new RpcException(new Status(StatusCode.Cancelled, "Connection aborted during request.", ex));
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// Don't write error when user cancels read
GrpcServerLog.ErrorReadingMessage(logger, ex);
throw;
}
}
/// <summary>
/// Read a message in a stream from the pipe reader. Additional message data is left in the reader.
/// </summary>
/// <param name="input">The request pipe reader.</param>
/// <param name="serverCallContext">The request content.</param>
/// <param name="deserializer">Message deserializer.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Complete message data or null if the stream is complete.</returns>
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
public static async ValueTask<T?> ReadStreamMessageAsync<T>(this PipeReader input, HttpContextServerCallContext serverCallContext, Func<DeserializationContext, T> deserializer, CancellationToken cancellationToken = default)
where T : class
{
var logger = serverCallContext.Logger;
try
{
GrpcServerLog.ReadingMessage(logger);
while (true)
{
var completeMessage = false;
// Check for client disconnect before reading
serverCallContext.EnsureRequestNotAborted();
var result = await input.ReadAsync(cancellationToken);
var buffer = result.Buffer;
try
{
if (result.IsCanceled)
{
throw new RpcException(MessageCancelledStatus);
}
// Check for client disconnect during processing
serverCallContext.EnsureRequestNotAborted();
if (!buffer.IsEmpty)
{
if (TryReadMessage(ref buffer, serverCallContext, out var data))
{
completeMessage = true;
// Check for client disconnect before deserialization
serverCallContext.EnsureRequestNotAborted();
GrpcServerLog.DeserializingMessage(logger, (int)data.Length, typeof(T));
serverCallContext.DeserializationContext.SetPayload(data);
var request = deserializer(serverCallContext.DeserializationContext);
serverCallContext.DeserializationContext.SetPayload(null);
GrpcServerLog.ReceivedMessage(logger);
if (GrpcEventSource.Log.IsEnabled())
{
GrpcEventSource.Log.MessageReceived();
}
return request;
}
}
if (result.IsCompleted)
{
if (buffer.Length == 0)
{
// Finished and there is no more data
GrpcServerLog.NoMessageReturned(logger);
return default;
}
throw new RpcException(IncompleteMessageStatus);
}
}
finally
{
// The buffer was sliced up to where it was consumed, so we can just advance to the start.
if (completeMessage)
{
input.AdvanceTo(buffer.Start);
}
else
{
// We mark examined as buffer.End so that if we didn't receive a full frame, we'll wait for more data
// before yielding the read again.
input.AdvanceTo(buffer.Start, buffer.End);
}
}
}
}
catch (OperationCanceledException ex) when (serverCallContext.HttpContext.RequestAborted.IsCancellationRequested)
{
// Convert operation canceled due to client disconnect to proper RpcException
throw new RpcException(new Status(StatusCode.Cancelled, "Call canceled by the client.", ex));
}
catch (IOException ex) when (IsConnectionResetException(ex))
{
// Convert connection reset to proper RpcException
throw new RpcException(new Status(StatusCode.Cancelled, "Client disconnected during request.", ex));
}
catch (Exception ex) when (IsConnectionAbortedException(ex))
{
// Convert connection aborted to proper RpcException
throw new RpcException(new Status(StatusCode.Cancelled, "Connection aborted during request.", ex));
}
catch (Exception ex) when (!(ex is OperationCanceledException && cancellationToken.IsCancellationRequested))
{
// Don't write error when user cancels read
GrpcServerLog.ErrorReadingMessage(logger, ex);
throw;
}
}
// Add helper methods for detecting connection issues
private static bool IsConnectionResetException(IOException ex)
{
return ex.Message.Contains("reset", StringComparison.OrdinalIgnoreCase) ||
ex.Message.Contains("aborted", StringComparison.OrdinalIgnoreCase) ||
ex.Message.Contains("disconnect", StringComparison.OrdinalIgnoreCase) ||
ex.Message.Contains("canceled", StringComparison.OrdinalIgnoreCase);
}
private static bool IsConnectionAbortedException(Exception ex)
{
return ex is ObjectDisposedException ||
ex is ConnectionAbortedException ||
(ex is IOException ioEx && ioEx.InnerException is ConnectionAbortedException);
}
private static bool TryReadMessage(ref ReadOnlySequence<byte> buffer, HttpContextServerCallContext context, out ReadOnlySequence<byte> message)
{
if (!TryReadHeader(buffer, out var compressed, out var messageLength))
{
message = default;
return false;
}
if (messageLength > context.Options.MaxReceiveMessageSize)
{
throw new RpcException(ReceivedMessageExceedsLimitStatus);
}
if (buffer.Length < HeaderSize + messageLength)
{
message = default;
return false;
}
// Convert message to byte array
var messageBuffer = buffer.Slice(HeaderSize, messageLength);
if (compressed)
{
var encoding = context.GetRequestGrpcEncoding();
if (encoding == null)
{
throw new RpcException(NoMessageEncodingMessageStatus);
}
if (GrpcProtocolConstants.IsGrpcEncodingIdentity(encoding))
{
throw new RpcException(IdentityMessageEncodingMessageStatus);
}
// Performance improvement would be to decompress without converting to an intermediary byte array
if (!TryDecompressMessage(context.Logger, encoding, context.Options.CompressionProviders, messageBuffer, out var decompressedMessage))
{
// https://github.com/grpc/grpc/blob/master/doc/compression.md#test-cases
// A message compressed by a client in a way not supported by its server MUST fail with status UNIMPLEMENTED,
// its associated description indicating the unsupported condition as well as the supported ones. The returned
// grpc-accept-encoding header MUST NOT contain the compression method (encoding) used.
var supportedEncodings = new List<string>();
supportedEncodings.Add(GrpcProtocolConstants.IdentityGrpcEncoding);
supportedEncodings.AddRange(context.Options.CompressionProviders.Select(p => p.Key));
if (!context.HttpContext.Response.HasStarted)
{
context.HttpContext.Response.Headers[GrpcProtocolConstants.MessageAcceptEncodingHeader] = string.Join(",", supportedEncodings);
}
throw new RpcException(CreateUnknownMessageEncodingMessageStatus(encoding, supportedEncodings));
}
context.ValidateAcceptEncodingContainsResponseEncoding();
message = decompressedMessage;
}
else
{
message = messageBuffer;
}
// Update buffer to remove message
buffer = buffer.Slice(HeaderSize + messageLength);
return true;
}
private static bool TryDecompressMessage(ILogger logger, string compressionEncoding, IReadOnlyDictionary<string, ICompressionProvider> compressionProviders, in ReadOnlySequence<byte> messageData, out ReadOnlySequence<byte> result)
{
if (compressionProviders.TryGetValue(compressionEncoding, out var compressionProvider))
{
GrpcServerLog.DecompressingMessage(logger, compressionProvider.EncodingName);
var output = new MemoryStream();
using (var compressionStream = compressionProvider.CreateDecompressionStream(new ReadOnlySequenceStream(messageData)))
{
compressionStream.CopyTo(output);
}
result = new ReadOnlySequence<byte>(output.GetBuffer(), 0, (int)output.Length);
return true;
}
result = default;
return false;
}
}