| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Runtime.ExceptionServices; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | |
| | | 12 | | namespace System.Net.Http |
| | | 13 | | { |
| | | 14 | | public abstract class MessageProcessingHandler : DelegatingHandler |
| | | 15 | | { |
| | 0 | 16 | | protected MessageProcessingHandler() |
| | 0 | 17 | | { |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | protected MessageProcessingHandler(HttpMessageHandler innerHandler) |
| | 0 | 21 | | : base(innerHandler) |
| | 0 | 22 | | { |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | protected abstract HttpRequestMessage ProcessRequest(HttpRequestMessage request, |
| | | 26 | | CancellationToken cancellationToken); |
| | | 27 | | protected abstract HttpResponseMessage ProcessResponse(HttpResponseMessage response, |
| | | 28 | | CancellationToken cancellationToken); |
| | | 29 | | |
| | | 30 | | protected internal sealed override HttpResponseMessage Send(HttpRequestMessage request, |
| | | 31 | | CancellationToken cancellationToken) |
| | 0 | 32 | | { |
| | 0 | 33 | | ArgumentNullException.ThrowIfNull(request); |
| | | 34 | | |
| | | 35 | | // Since most of the SendAsync code is just Task handling, there's no reason to share the code. |
| | 0 | 36 | | HttpRequestMessage newRequestMessage = ProcessRequest(request, cancellationToken); |
| | 0 | 37 | | HttpResponseMessage response = base.Send(newRequestMessage, cancellationToken); |
| | 0 | 38 | | HttpResponseMessage newResponseMessage = ProcessResponse(response, cancellationToken); |
| | 0 | 39 | | return newResponseMessage; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | protected internal sealed override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, |
| | | 43 | | CancellationToken cancellationToken) |
| | 0 | 44 | | { |
| | 0 | 45 | | ArgumentNullException.ThrowIfNull(request); |
| | | 46 | | |
| | | 47 | | // ProcessRequest() and ProcessResponse() are supposed to be fast, so we call ProcessRequest() on the same |
| | | 48 | | // thread SendAsync() was invoked to avoid context switches. However, if ProcessRequest() throws, we have |
| | | 49 | | // to catch the exception since the caller doesn't expect exceptions when calling SendAsync(): The |
| | | 50 | | // expectation is that the returned task will get faulted on errors, but the async call to SendAsync() |
| | | 51 | | // should complete. |
| | 0 | 52 | | var tcs = new SendState(this, cancellationToken); |
| | | 53 | | try |
| | 0 | 54 | | { |
| | 0 | 55 | | HttpRequestMessage newRequestMessage = ProcessRequest(request, cancellationToken); |
| | 0 | 56 | | Task<HttpResponseMessage> sendAsyncTask = base.SendAsync(newRequestMessage, cancellationToken); |
| | | 57 | | |
| | | 58 | | // We schedule a continuation task once the inner handler completes in order to trigger the response |
| | | 59 | | // processing method. ProcessResponse() is only called if the task wasn't canceled before. |
| | 0 | 60 | | sendAsyncTask.ContinueWith(static (task, state) => |
| | 0 | 61 | | { |
| | 0 | 62 | | var sendState = (SendState)state!; |
| | 0 | 63 | | MessageProcessingHandler self = sendState._handler; |
| | 0 | 64 | | CancellationToken token = sendState._token; |
| | 0 | 65 | | |
| | 0 | 66 | | if (task.IsFaulted) |
| | 0 | 67 | | { |
| | 0 | 68 | | sendState.TrySetException(task.Exception!.GetBaseException()); |
| | 0 | 69 | | return; |
| | 0 | 70 | | } |
| | 0 | 71 | | |
| | 0 | 72 | | if (task.IsCanceled) |
| | 0 | 73 | | { |
| | 0 | 74 | | sendState.TrySetCanceled(token); |
| | 0 | 75 | | return; |
| | 0 | 76 | | } |
| | 0 | 77 | | |
| | 0 | 78 | | if (task.Result == null) |
| | 0 | 79 | | { |
| | 0 | 80 | | sendState.TrySetException(ExceptionDispatchInfo.SetCurrentStackTrace(new InvalidOperationExcepti |
| | 0 | 81 | | return; |
| | 0 | 82 | | } |
| | 0 | 83 | | |
| | 0 | 84 | | try |
| | 0 | 85 | | { |
| | 0 | 86 | | HttpResponseMessage responseMessage = self.ProcessResponse(task.Result, token); |
| | 0 | 87 | | sendState.TrySetResult(responseMessage); |
| | 0 | 88 | | } |
| | 0 | 89 | | catch (OperationCanceledException e) |
| | 0 | 90 | | { |
| | 0 | 91 | | // If ProcessResponse() throws an OperationCanceledException check whether it is related to |
| | 0 | 92 | | // the cancellation token we received from the user. If so, cancel the Task. |
| | 0 | 93 | | HandleCanceledOperations(token, sendState, e); |
| | 0 | 94 | | } |
| | 0 | 95 | | catch (Exception e) |
| | 0 | 96 | | { |
| | 0 | 97 | | sendState.TrySetException(e); |
| | 0 | 98 | | } |
| | 0 | 99 | | // We don't pass the cancellation token to the continuation task, since we want to get called even |
| | 0 | 100 | | // if the operation was canceled: We'll set the Task returned to the user to canceled. Passing the |
| | 0 | 101 | | // cancellation token here would result in the continuation task to not be called at all. I.e. we |
| | 0 | 102 | | // would never complete the task returned to the caller of SendAsync(). |
| | 0 | 103 | | }, tcs, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); |
| | 0 | 104 | | } |
| | 0 | 105 | | catch (OperationCanceledException e) |
| | 0 | 106 | | { |
| | 0 | 107 | | HandleCanceledOperations(cancellationToken, tcs, e); |
| | 0 | 108 | | } |
| | 0 | 109 | | catch (Exception e) |
| | 0 | 110 | | { |
| | 0 | 111 | | tcs.TrySetException(e); |
| | 0 | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | return tcs.Task; |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | private static void HandleCanceledOperations(CancellationToken cancellationToken, |
| | | 118 | | TaskCompletionSource<HttpResponseMessage> tcs, OperationCanceledException e) |
| | 0 | 119 | | { |
| | | 120 | | // Check if the exception was due to a cancellation. If so, check if the OperationCanceledException is |
| | | 121 | | // related to our CancellationToken. If it was indeed caused due to our cancellation token being |
| | | 122 | | // canceled, set the Task as canceled. Set it to faulted otherwise, since the OperationCanceledException |
| | | 123 | | // is not related to our cancellation token. |
| | 0 | 124 | | if (cancellationToken.IsCancellationRequested && (e.CancellationToken == cancellationToken)) |
| | 0 | 125 | | { |
| | 0 | 126 | | tcs.TrySetCanceled(cancellationToken); |
| | 0 | 127 | | } |
| | | 128 | | else |
| | 0 | 129 | | { |
| | 0 | 130 | | tcs.TrySetException(e); |
| | 0 | 131 | | } |
| | 0 | 132 | | } |
| | | 133 | | |
| | | 134 | | // Private class used to capture the SendAsync state in |
| | | 135 | | // a closure, while simultaneously avoiding a tuple allocation. |
| | | 136 | | private sealed class SendState : TaskCompletionSource<HttpResponseMessage> |
| | | 137 | | { |
| | | 138 | | internal readonly MessageProcessingHandler _handler; |
| | | 139 | | internal readonly CancellationToken _token; |
| | | 140 | | |
| | 0 | 141 | | public SendState(MessageProcessingHandler handler, CancellationToken token) |
| | 0 | 142 | | { |
| | 0 | 143 | | Debug.Assert(handler != null); |
| | | 144 | | |
| | 0 | 145 | | _handler = handler; |
| | 0 | 146 | | _token = token; |
| | 0 | 147 | | } |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | } |