|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.IO.Pipelines; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using AustinHarris.JsonRpc.Serialization; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | + |
| 9 | +namespace AustinHarris.JsonRpc.AspNetCore |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// The HTTP request handler: reads the body through <see cref="PipeReader"/>, runs the processor on the |
| 13 | + /// <see cref="ReadOnlySequence{T}"/> it yields, and writes the response straight into |
| 14 | + /// <see cref="HttpResponse.BodyWriter"/>. No strings, no intermediate byte arrays. |
| 15 | + /// </summary> |
| 16 | + public static class JsonRpcEndpoint |
| 17 | + { |
| 18 | + public static async Task HandleAsync(HttpContext http, JsonRpcOptions options) |
| 19 | + { |
| 20 | + options ??= new JsonRpcOptions(); |
| 21 | + if (!HttpMethods.IsPost(http.Request.Method)) |
| 22 | + { |
| 23 | + http.Response.StatusCode = StatusCodes.Status405MethodNotAllowed; |
| 24 | + http.Response.Headers.Allow = "POST"; |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + var reader = http.Request.BodyReader; |
| 29 | + var ct = http.RequestAborted; |
| 30 | + ReadResult result; |
| 31 | + while (true) |
| 32 | + { |
| 33 | + result = await reader.ReadAsync(ct).ConfigureAwait(false); |
| 34 | + if (result.IsCompleted || result.IsCanceled) break; |
| 35 | + if (result.Buffer.Length > options.MaxRequestBytes) |
| 36 | + { |
| 37 | + reader.AdvanceTo(result.Buffer.Start, result.Buffer.End); |
| 38 | + http.Response.StatusCode = StatusCodes.Status413PayloadTooLarge; |
| 39 | + return; |
| 40 | + } |
| 41 | + // nothing consumed, everything examined: ReadAsync waits for more bytes |
| 42 | + reader.AdvanceTo(result.Buffer.Start, result.Buffer.End); |
| 43 | + } |
| 44 | + |
| 45 | + var buffer = result.Buffer; |
| 46 | + try |
| 47 | + { |
| 48 | + if (buffer.Length > options.MaxRequestBytes) |
| 49 | + { |
| 50 | + http.Response.StatusCode = StatusCodes.Status413PayloadTooLarge; |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + string session = options.SessionSelector?.Invoke(http) ?? options.SessionId ?? Handler.DefaultSessionId(); |
| 55 | + object context = options.ContextFactory != null ? options.ContextFactory(http) : http; |
| 56 | + |
| 57 | + http.Response.ContentType = options.ResponseContentType; |
| 58 | + var counting = new CountingBufferWriter(http.Response.BodyWriter); |
| 59 | + JsonRpcProcessor.Process(session, in buffer, counting, context, options.Serializer); |
| 60 | + |
| 61 | + if (counting.Written == 0) |
| 62 | + { |
| 63 | + if (options.NoContentForNotifications) |
| 64 | + { |
| 65 | + http.Response.ContentType = null; |
| 66 | + http.Response.StatusCode = StatusCodes.Status204NoContent; |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + http.Response.ContentLength = 0; |
| 71 | + } |
| 72 | + return; |
| 73 | + } |
| 74 | + await http.Response.BodyWriter.FlushAsync(ct).ConfigureAwait(false); |
| 75 | + } |
| 76 | + finally |
| 77 | + { |
| 78 | + reader.AdvanceTo(buffer.End); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary>Tracks how many bytes a processor call advanced, so an empty (notification) response can be detected before headers are sent.</summary> |
| 83 | + internal sealed class CountingBufferWriter : IBufferWriter<byte> |
| 84 | + { |
| 85 | + private readonly IBufferWriter<byte> _inner; |
| 86 | + public long Written; |
| 87 | + |
| 88 | + public CountingBufferWriter(IBufferWriter<byte> inner) { _inner = inner; } |
| 89 | + |
| 90 | + public void Advance(int count) { Written += count; _inner.Advance(count); } |
| 91 | + public Memory<byte> GetMemory(int sizeHint = 0) => _inner.GetMemory(sizeHint); |
| 92 | + public Span<byte> GetSpan(int sizeHint = 0) => _inner.GetSpan(sizeHint); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments