tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
LimitedStreamResultExecutor.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Threading.Tasks;
4
5using Microsoft.AspNetCore.Http.Extensions;
6using Microsoft.AspNetCore.Mvc;
7using Microsoft.AspNetCore.Mvc.Infrastructure;
8using Microsoft.Extensions.Logging;
9
11{
16 {
21 public LimitedStreamResultExecutor(ILogger<LimitedStreamResultExecutor> logger)
22 : base(logger)
23 {
24 }
25
27 public async Task ExecuteAsync(ActionContext context, LimitedStreamResult result)
28 {
29 ArgumentNullException.ThrowIfNull(context);
30
31 ArgumentNullException.ThrowIfNull(result);
32
33 await using (result)
34 {
35 var cancellationToken = context.HttpContext.RequestAborted;
36 var stream = await result.GetResult(cancellationToken);
37 var contentLength = stream.Length;
38 var (range, rangeLength, serveBody) = SetHeadersAndLog(context, result, contentLength, result.EnableRangeProcessing);
39 if (!serveBody)
40 return;
41
42 try
43 {
44 var outputStream = context.HttpContext.Response.Body;
45 if (range == null)
46 {
47 await StreamCopyOperation.CopyToAsync(
48 stream,
49 outputStream,
50 contentLength,
51 BufferSize,
52 cancellationToken);
53 }
54 else
55 {
56 stream.Seek(range.From ?? 0, SeekOrigin.Begin);
57 await StreamCopyOperation.CopyToAsync(
58 stream,
59 outputStream,
60 rangeLength,
61 BufferSize,
62 cancellationToken);
63 }
64 }
65 catch (OperationCanceledException)
66 {
67 // Don't throw this exception, it's most likely caused by the client disconnecting.
68 // However, if it was cancelled for any other reason we need to prevent empty responses.
69 context.HttpContext.Abort();
70 }
71 }
72 }
73 }
74}
LimitedStreamResultExecutor(ILogger< LimitedStreamResultExecutor > logger)
Initializes a new instance of the LimitedStreamResultExecutor class.
async Task ExecuteAsync(ActionContext context, LimitedStreamResult result)
Very similar to FileStreamResult except it's IActionResultExecutor<TResult> contains a fix for https:...
ValueTask< Stream > GetResult(CancellationToken cancellationToken)
Gets the provided Stream. May be called multiple times, though cancelling any may cause all calls to ...