Skip to content

Commit 0b4dcf2

Browse files
committed
React to HttpRequestError API changes
1 parent 420b40f commit 0b4dcf2

File tree

11 files changed

+51
-52
lines changed

11 files changed

+51
-52
lines changed

src/libraries/System.Net.Http/ref/System.Net.Http.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ public HttpRequestException() { }
272272
public HttpRequestException(string? message) { }
273273
public HttpRequestException(string? message, System.Exception? inner) { }
274274
public HttpRequestException(string? message, System.Exception? inner, System.Net.HttpStatusCode? statusCode) { }
275-
public HttpRequestException(string? message, System.Exception? inner = null, System.Net.HttpStatusCode? statusCode = null, System.Net.Http.HttpRequestError? httpRequestError = null) { }
276-
public System.Net.Http.HttpRequestError? HttpRequestError { get { throw null; } }
275+
public HttpRequestException(System.Net.Http.HttpRequestError httpRequestError, string? message = null, System.Exception? inner = null, System.Net.HttpStatusCode? statusCode = null) { }
276+
public System.Net.Http.HttpRequestError HttpRequestError { get { throw null; } }
277277
public System.Net.HttpStatusCode? StatusCode { get { throw null; } }
278278
}
279279
public partial class HttpRequestMessage : System.IDisposable

src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Buffers;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Globalization;
78
using System.IO;
89
using System.Net.Http.Headers;
910
using System.Text;
@@ -608,7 +609,7 @@ private bool CreateTemporaryBuffer(long maxBufferSize, out MemoryStream? tempBuf
608609
// This should only be hit when called directly; HttpClient/HttpClientHandler
609610
// will not exceed this limit.
610611
throw new ArgumentOutOfRangeException(nameof(maxBufferSize), maxBufferSize,
611-
SR.Format(System.Globalization.CultureInfo.InvariantCulture,
612+
SR.Format(CultureInfo.InvariantCulture,
612613
SR.net_http_content_buffersize_limit, HttpContent.MaxBufferSize));
613614
}
614615

@@ -720,7 +721,7 @@ internal static Exception WrapStreamCopyException(Exception e)
720721
{
721722
Debug.Assert(StreamCopyExceptionNeedsWrapping(e));
722723
HttpRequestError error = e is HttpIOException ioEx ? ioEx.HttpRequestError : HttpRequestError.Unknown;
723-
return new HttpRequestException(SR.net_http_content_stream_copy_error, e, httpRequestError: error);
724+
return new HttpRequestException(error, SR.net_http_content_stream_copy_error, e);
724725
}
725726

726727
private static int GetPreambleLength(ArraySegment<byte> buffer, Encoding encoding)
@@ -835,7 +836,7 @@ private static async Task<TResult> WaitAndReturnAsync<TState, TResult>(Task wait
835836

836837
private static HttpRequestException CreateOverCapacityException(long maxBufferSize)
837838
{
838-
return new HttpRequestException(SR.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_content_buffersize_exceeded, maxBufferSize), httpRequestError: HttpRequestError.ConfigurationLimitExceeded);
839+
return new HttpRequestException(HttpRequestError.ConfigurationLimitExceeded, SR.Format(CultureInfo.InvariantCulture, SR.net_http_content_buffersize_exceeded, maxBufferSize));
839840
}
840841

841842
internal sealed class LimitMemoryStream : MemoryStream

src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestException.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public HttpRequestException(string? message, Exception? inner, HttpStatusCode? s
3838
/// <summary>
3939
/// Initializes a new instance of the <see cref="HttpRequestException" /> class with a specific message an inner exception, and an HTTP status code and an <see cref="HttpRequestError"/>.
4040
/// </summary>
41+
/// <param name="httpRequestError">The <see cref="HttpRequestError"/> that caused the exception.</param>
4142
/// <param name="message">A message that describes the current exception.</param>
4243
/// <param name="inner">The inner exception.</param>
4344
/// <param name="statusCode">The HTTP status code.</param>
44-
/// <param name="httpRequestError">The <see cref="HttpRequestError"/> that caused the exception.</param>
45-
public HttpRequestException(string? message, Exception? inner = null, HttpStatusCode? statusCode = null, HttpRequestError? httpRequestError = null)
45+
public HttpRequestException(HttpRequestError httpRequestError, string? message = null, Exception? inner = null, HttpStatusCode? statusCode = null)
4646
: this(message, inner, statusCode)
4747
{
4848
HttpRequestError = httpRequestError;
@@ -51,10 +51,7 @@ public HttpRequestException(string? message, Exception? inner = null, HttpStatus
5151
/// <summary>
5252
/// Gets the <see cref="Http.HttpRequestError"/> that caused the exception.
5353
/// </summary>
54-
/// <value>
55-
/// The <see cref="Http.HttpRequestError"/> or <see langword="null"/> if the underlying <see cref="HttpMessageHandler"/> did not provide it.
56-
/// </value>
57-
public HttpRequestError? HttpRequestError { get; }
54+
public HttpRequestError HttpRequestError { get; }
5855

5956
/// <summary>
6057
/// Gets the HTTP status code to be returned with the exception.
@@ -66,8 +63,8 @@ public HttpRequestException(string? message, Exception? inner = null, HttpStatus
6663

6764
// This constructor is used internally to indicate that a request was not successfully sent due to an IOException,
6865
// and the exception occurred early enough so that the request may be retried on another connection.
69-
internal HttpRequestException(string? message, Exception? inner, RequestRetryType allowRetry, HttpRequestError? httpRequestError = null)
70-
: this(message, inner, httpRequestError: httpRequestError)
66+
internal HttpRequestException(HttpRequestError httpRequestError, string? message, Exception? inner, RequestRetryType allowRetry)
67+
: this(httpRequestError, message, inner)
7168
{
7269
AllowRetry = allowRetry;
7370
}

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.NtAuth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private static async Task<HttpResponseMessage> SendWithNtAuthAsync(HttpRequestMe
209209
{
210210
isNewConnection = false;
211211
connection.Dispose();
212-
throw new HttpRequestException(SR.Format(SR.net_http_authvalidationfailure, statusCode), null, HttpStatusCode.Unauthorized, HttpRequestError.UserAuthenticationError);
212+
throw new HttpRequestException(HttpRequestError.UserAuthenticationError, SR.Format(SR.net_http_authvalidationfailure, statusCode), statusCode: HttpStatusCode.Unauthorized);
213213
}
214214
break;
215215
}

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static async ValueTask<SslStream> EstablishSslConnectionAsync(SslClientAu
8989
throw CancellationHelper.CreateOperationCanceledException(e, cancellationToken);
9090
}
9191

92-
HttpRequestException ex = new HttpRequestException(SR.net_http_ssl_connection_failed, e, httpRequestError: HttpRequestError.SecureConnectionError);
92+
HttpRequestException ex = new HttpRequestException(HttpRequestError.SecureConnectionError, SR.net_http_ssl_connection_failed, e);
9393
if (request.IsExtendedConnectRequest)
9494
{
9595
// Extended connect request is negotiating strictly for ALPN = "h2" because HttpClient is unaware of a possible downgrade.
@@ -139,7 +139,7 @@ internal static Exception CreateWrappedException(Exception exception, string hos
139139
{
140140
return CancellationHelper.ShouldWrapInOperationCanceledException(exception, cancellationToken) ?
141141
CancellationHelper.CreateOperationCanceledException(exception, cancellationToken) :
142-
new HttpRequestException($"{exception.Message} ({host}:{port})", exception, RequestRetryType.RetryOnNextProxy, DeduceError(exception));
142+
new HttpRequestException(DeduceError(exception), $"{exception.Message} ({host}:{port})", exception, RequestRetryType.RetryOnNextProxy);
143143

144144
static HttpRequestError DeduceError(Exception exception)
145145
{

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,11 +2104,11 @@ await Task.WhenAny(requestBodyTask, responseHeadersTask).ConfigureAwait(false) =
21042104
}
21052105
catch (HttpIOException e)
21062106
{
2107-
throw new HttpRequestException(e.Message, e, httpRequestError: e.HttpRequestError);
2107+
throw new HttpRequestException(e.HttpRequestError, e.Message, e);
21082108
}
21092109
catch (Exception e) when (e is IOException || e is ObjectDisposedException || e is InvalidOperationException)
21102110
{
2111-
throw new HttpRequestException(SR.net_http_client_execution_error, e, httpRequestError: HttpRequestError.Unknown);
2111+
throw new HttpRequestException(HttpRequestError.Unknown, SR.net_http_client_execution_error, e);
21122112
}
21132113
}
21142114

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ void IHttpStreamHeadersHandler.OnStaticIndexedHeader(int index)
540540
if (index <= LastHPackRequestPseudoHeaderId)
541541
{
542542
if (NetEventSource.Log.IsEnabled()) Trace($"Invalid request pseudo-header ID {index}.");
543-
throw new HttpRequestException(SR.net_http_invalid_response, httpRequestError: HttpRequestError.InvalidResponse);
543+
throw new HttpRequestException(HttpRequestError.InvalidResponse, SR.net_http_invalid_response);
544544
}
545545
else if (index <= LastHPackStatusPseudoHeaderId)
546546
{
@@ -563,7 +563,7 @@ void IHttpStreamHeadersHandler.OnStaticIndexedHeader(int index, ReadOnlySpan<byt
563563
if (index <= LastHPackRequestPseudoHeaderId)
564564
{
565565
if (NetEventSource.Log.IsEnabled()) Trace($"Invalid request pseudo-header ID {index}.");
566-
throw new HttpRequestException(SR.net_http_invalid_response, httpRequestError: HttpRequestError.InvalidResponse);
566+
throw new HttpRequestException(HttpRequestError.InvalidResponse, SR.net_http_invalid_response);
567567
}
568568
else if (index <= LastHPackStatusPseudoHeaderId)
569569
{
@@ -589,7 +589,7 @@ private void AdjustHeaderBudget(int amount)
589589
_headerBudgetRemaining -= amount;
590590
if (_headerBudgetRemaining < 0)
591591
{
592-
throw new HttpRequestException(SR.Format(SR.net_http_response_headers_exceeded_length, _connection._pool.Settings.MaxResponseHeadersByteLength), httpRequestError: HttpRequestError.ConfigurationLimitExceeded);
592+
throw new HttpRequestException(HttpRequestError.ConfigurationLimitExceeded, SR.Format(SR.net_http_response_headers_exceeded_length, _connection._pool.Settings.MaxResponseHeadersByteLength));
593593
}
594594
}
595595

@@ -610,15 +610,16 @@ private void OnStatus(int statusCode)
610610

611611
if (_responseProtocolState == ResponseProtocolState.ExpectingHeaders)
612612
{
613+
// TODO: Fix
613614
if (NetEventSource.Log.IsEnabled()) Trace("Received extra status header.");
614-
throw new HttpRequestException(SR.net_http_invalid_response_multiple_status_codes, httpRequestError: HttpRequestError.ConfigurationLimitExceeded);
615+
throw new HttpRequestException(HttpRequestError.ConfigurationLimitExceeded, SR.net_http_invalid_response_multiple_status_codes);
615616
}
616617

617618
if (_responseProtocolState != ResponseProtocolState.ExpectingStatus)
618619
{
619620
// Pseudo-headers are allowed only in header block
620621
if (NetEventSource.Log.IsEnabled()) Trace($"Status pseudo-header received in {_responseProtocolState} state.");
621-
throw new HttpRequestException(SR.net_http_invalid_response_pseudo_header_in_trailer, httpRequestError: HttpRequestError.InvalidResponse);
622+
throw new HttpRequestException(HttpRequestError.InvalidResponse, SR.net_http_invalid_response_pseudo_header_in_trailer);
622623
}
623624

624625
Debug.Assert(_response != null);
@@ -681,7 +682,7 @@ private void OnHeader(HeaderDescriptor descriptor, ReadOnlySpan<byte> value)
681682
if (_responseProtocolState != ResponseProtocolState.ExpectingHeaders && _responseProtocolState != ResponseProtocolState.ExpectingTrailingHeaders)
682683
{
683684
if (NetEventSource.Log.IsEnabled()) Trace("Received header before status.");
684-
throw new HttpRequestException(SR.net_http_invalid_response, httpRequestError: HttpRequestError.InvalidResponse);
685+
throw new HttpRequestException(HttpRequestError.InvalidResponse, SR.net_http_invalid_response);
685686
}
686687

687688
Encoding? valueEncoding = _connection._pool.Settings._responseHeaderEncodingSelector?.Invoke(descriptor.Name, _request);
@@ -725,7 +726,7 @@ public void OnHeader(ReadOnlySpan<byte> name, ReadOnlySpan<byte> value)
725726
else
726727
{
727728
if (NetEventSource.Log.IsEnabled()) Trace($"Invalid response pseudo-header '{Encoding.ASCII.GetString(name)}'.");
728-
throw new HttpRequestException(SR.net_http_invalid_response, httpRequestError: HttpRequestError.InvalidResponse);
729+
throw new HttpRequestException(HttpRequestError.InvalidResponse, SR.net_http_invalid_response);
729730
}
730731
}
731732
else
@@ -734,7 +735,7 @@ public void OnHeader(ReadOnlySpan<byte> name, ReadOnlySpan<byte> value)
734735
if (!HeaderDescriptor.TryGet(name, out HeaderDescriptor descriptor))
735736
{
736737
// Invalid header name
737-
throw new HttpRequestException(SR.Format(SR.net_http_invalid_response_header_name, Encoding.ASCII.GetString(name)), httpRequestError: HttpRequestError.InvalidResponse);
738+
throw new HttpRequestException(HttpRequestError.InvalidResponse, SR.Format(SR.net_http_invalid_response_header_name, Encoding.ASCII.GetString(name)));
738739
}
739740

740741
OnHeader(descriptor, value);

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3RequestStream.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,13 @@ await Task.WhenAny(sendContentTask, readResponseTask).ConfigureAwait(false) == s
257257
case Http3ErrorCode.RequestRejected:
258258
// The server is rejecting the request without processing it, retry it on a different connection.
259259
HttpProtocolException rejectedException = HttpProtocolException.CreateHttp3StreamException(code, ex);
260-
throw new HttpRequestException(SR.net_http_request_aborted, rejectedException, RequestRetryType.RetryOnConnectionFailure, httpRequestError: HttpRequestError.HttpProtocolError);
260+
throw new HttpRequestException(HttpRequestError.HttpProtocolError, SR.net_http_request_aborted, rejectedException, RequestRetryType.RetryOnConnectionFailure);
261261

262262
default:
263263
// Our stream was reset.
264264
Exception innerException = _connection.AbortException ?? HttpProtocolException.CreateHttp3StreamException(code, ex);
265265
HttpRequestError httpRequestError = innerException is HttpProtocolException ? HttpRequestError.HttpProtocolError : HttpRequestError.Unknown;
266-
throw new HttpRequestException(SR.net_http_client_execution_error, innerException, httpRequestError: httpRequestError);
266+
throw new HttpRequestException(httpRequestError, SR.net_http_client_execution_error, innerException);
267267
}
268268
}
269269
catch (QuicException ex) when (ex.QuicError == QuicError.ConnectionAborted)
@@ -273,12 +273,12 @@ await Task.WhenAny(sendContentTask, readResponseTask).ConfigureAwait(false) == s
273273
Http3ErrorCode code = (Http3ErrorCode)ex.ApplicationErrorCode.Value;
274274

275275
Exception abortException = _connection.Abort(HttpProtocolException.CreateHttp3ConnectionException(code, SR.net_http_http3_connection_close));
276-
throw new HttpRequestException(SR.net_http_client_execution_error, abortException, httpRequestError: HttpRequestError.HttpProtocolError);
276+
throw new HttpRequestException(HttpRequestError.HttpProtocolError, SR.net_http_client_execution_error);
277277
}
278278
catch (QuicException ex) when (ex.QuicError == QuicError.OperationAborted && _connection.AbortException != null)
279279
{
280280
// we close the connection, propagate the AbortException
281-
throw new HttpRequestException(SR.net_http_client_execution_error, _connection.AbortException, httpRequestError: HttpRequestError.Unknown);
281+
throw new HttpRequestException(HttpRequestError.Unknown, SR.net_http_client_execution_error, _connection.AbortException);
282282
}
283283
// It is possible for user's Content code to throw an unexpected OperationCanceledException.
284284
catch (OperationCanceledException ex) when (ex.CancellationToken == _requestBodyCancellationSource.Token || ex.CancellationToken == cancellationToken)
@@ -292,13 +292,13 @@ await Task.WhenAny(sendContentTask, readResponseTask).ConfigureAwait(false) == s
292292
else
293293
{
294294
Debug.Assert(_requestBodyCancellationSource.IsCancellationRequested);
295-
throw new HttpRequestException(SR.net_http_request_aborted, ex, RequestRetryType.RetryOnConnectionFailure, httpRequestError: HttpRequestError.Unknown);
295+
throw new HttpRequestException(HttpRequestError.Unknown, SR.net_http_request_aborted, ex, RequestRetryType.RetryOnConnectionFailure);
296296
}
297297
}
298298
catch (HttpIOException ex)
299299
{
300300
_connection.Abort(ex);
301-
throw new HttpRequestException(SR.net_http_client_execution_error, ex, httpRequestError: ex.HttpRequestError);
301+
throw new HttpRequestException(ex.HttpRequestError, SR.net_http_client_execution_error, ex);
302302
}
303303
catch (Exception ex)
304304
{
@@ -307,7 +307,7 @@ await Task.WhenAny(sendContentTask, readResponseTask).ConfigureAwait(false) == s
307307
{
308308
throw;
309309
}
310-
throw new HttpRequestException(SR.net_http_client_execution_error, ex, httpRequestError: HttpRequestError.Unknown);
310+
throw new HttpRequestException(HttpRequestError.Unknown, SR.net_http_client_execution_error, ex);
311311
}
312312
finally
313313
{
@@ -870,7 +870,7 @@ private async ValueTask ReadHeadersAsync(long headersLength, CancellationToken c
870870
if (headersLength > _headerBudgetRemaining)
871871
{
872872
_stream.Abort(QuicAbortDirection.Read, (long)Http3ErrorCode.ExcessiveLoad);
873-
throw new HttpRequestException(SR.Format(SR.net_http_response_headers_exceeded_length, _connection.Pool.Settings.MaxResponseHeadersByteLength), httpRequestError: HttpRequestError.ConfigurationLimitExceeded);
873+
throw new HttpRequestException(HttpRequestError.ConfigurationLimitExceeded, SR.Format(SR.net_http_response_headers_exceeded_length, _connection.Pool.Settings.MaxResponseHeadersByteLength));
874874
}
875875

876876
_headerBudgetRemaining -= (int)headersLength;
@@ -911,7 +911,7 @@ void IHttpStreamHeadersHandler.OnHeader(ReadOnlySpan<byte> name, ReadOnlySpan<by
911911
if (!HeaderDescriptor.TryGet(name, out HeaderDescriptor descriptor))
912912
{
913913
// Invalid header name
914-
throw new HttpRequestException(SR.Format(SR.net_http_invalid_response_header_name, Encoding.ASCII.GetString(name)), httpRequestError: HttpRequestError.InvalidResponse);
914+
throw new HttpRequestException(HttpRequestError.InvalidResponse, SR.Format(SR.net_http_invalid_response_header_name, Encoding.ASCII.GetString(name)));
915915
}
916916
OnHeader(staticIndex: null, descriptor, staticValue: default, literalValue: value);
917917
}

0 commit comments

Comments
 (0)