Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using System.IO;
using System.Net.Http.Headers;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -695,7 +696,7 @@ internal static Exception WrapStreamCopyException(Exception e)
{
Debug.Assert(StreamCopyExceptionNeedsWrapping(e));
HttpRequestError error = e is HttpIOException ioEx ? ioEx.HttpRequestError : HttpRequestError.Unknown;
return new HttpRequestException(error, SR.net_http_content_stream_copy_error, e);
return ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(error, SR.net_http_content_stream_copy_error, e));
}

private static int GetPreambleLength(ReadOnlySpan<byte> data, Encoding encoding)
Expand Down Expand Up @@ -767,7 +768,7 @@ private static async Task<TResult> WaitAndReturnAsync<TState, TResult>(Task wait

private static HttpRequestException CreateOverCapacityException(long maxBufferSize)
{
return new HttpRequestException(HttpRequestError.ConfigurationLimitExceeded, SR.Format(CultureInfo.InvariantCulture, SR.net_http_content_buffersize_exceeded, maxBufferSize));
return (HttpRequestException)ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(HttpRequestError.ConfigurationLimitExceeded, SR.Format(CultureInfo.InvariantCulture, SR.net_http_content_buffersize_exceeded, maxBufferSize)));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -39,7 +40,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo

if (BytesWritten > _contentLength)
{
return ValueTask.FromException(new HttpRequestException(SR.net_http_content_write_larger_than_content_length));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(SR.net_http_content_write_larger_than_content_length)));
}

HttpConnection connection = GetConnectionOrThrow();
Expand All @@ -51,7 +52,7 @@ public override Task FinishAsync(bool async)
{
if (BytesWritten != _contentLength)
{
return Task.FromException(new HttpRequestException(SR.Format(SR.net_http_request_content_length_mismatch, BytesWritten, _contentLength)));
return Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(SR.Format(SR.net_http_request_content_length_mismatch, BytesWritten, _contentLength))));
}

_connection = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net.Http.Headers;
using System.Net.Http.HPack;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Channels;
Expand Down Expand Up @@ -1197,7 +1198,7 @@ private Task PerformWriteAsync<T>(int writeBytes, T state, Func<T, Memory<byte>,
// As such, it should not matter that we were not able to actually send the frame.
// But just in case, throw ObjectDisposedException. Asynchronous callers will ignore the failure.
Debug.Assert(_shutdown && _streamsInUse == 0);
return Task.FromException(new ObjectDisposedException(nameof(Http2Connection)));
return Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2Connection))));
}

return writeEntry.Task;
Expand Down Expand Up @@ -2173,7 +2174,7 @@ private static void ThrowRetry(string message, Exception? innerException = null)
throw new HttpRequestException((innerException as HttpIOException)?.HttpRequestError ?? HttpRequestError.Unknown, message, innerException, RequestRetryType.RetryOnConnectionFailure);

private static Exception GetRequestAbortedException(Exception? innerException = null) =>
innerException as HttpIOException ?? new IOException(SR.net_http_request_aborted, innerException);
innerException as HttpIOException ?? ExceptionDispatchInfo.SetCurrentStackTrace(new IOException(SR.net_http_request_aborted, innerException));

[DoesNotReturn]
private static void ThrowRequestAborted(Exception? innerException = null) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,7 @@ public Http2ReadStream(Http2Stream http2Stream) : base(http2Stream, closeRespons

public override void Write(ReadOnlySpan<byte> buffer) => throw new NotSupportedException(SR.net_http_content_readonly_stream);

public override ValueTask WriteAsync(ReadOnlyMemory<byte> destination, CancellationToken cancellationToken) => ValueTask.FromException(new NotSupportedException(SR.net_http_content_readonly_stream));
public override ValueTask WriteAsync(ReadOnlyMemory<byte> destination, CancellationToken cancellationToken) => ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_content_readonly_stream)));
}

private sealed class Http2WriteStream : Http2ReadWriteStream
Expand All @@ -1522,19 +1522,19 @@ public Http2WriteStream(Http2Stream http2Stream, long contentLength) : base(http

public override int Read(Span<byte> buffer) => throw new NotSupportedException(SR.net_http_content_writeonly_stream);

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken) => ValueTask.FromException<int>(new NotSupportedException(SR.net_http_content_writeonly_stream));
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken) => ValueTask.FromException<int>(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_content_writeonly_stream)));

public override void CopyTo(Stream destination, int bufferSize) => throw new NotSupportedException(SR.net_http_content_writeonly_stream);

public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) => Task.FromException(new NotSupportedException(SR.net_http_content_writeonly_stream));
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) => Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_content_writeonly_stream)));

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
{
BytesWritten += buffer.Length;

if ((ulong)BytesWritten > (ulong)ContentLength) // If ContentLength == -1, this will always be false
{
return ValueTask.FromException(new HttpRequestException(SR.net_http_content_write_larger_than_content_length));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(SR.net_http_content_write_larger_than_content_length)));
}

return base.WriteAsync(buffer, cancellationToken);
Expand Down Expand Up @@ -1643,7 +1643,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo

if (http2Stream == null)
{
return ValueTask.FromException(new ObjectDisposedException(nameof(Http2WriteStream)));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2WriteStream))));
}

return http2Stream.SendDataAsync(buffer, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken

if (stream is null)
{
return ValueTask.FromException<int>(new ObjectDisposedException(nameof(Http3RequestStream)));
return ValueTask.FromException<int>(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http3RequestStream))));
}

Debug.Assert(_response != null);
Expand Down Expand Up @@ -1577,7 +1577,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo

if (stream is null)
{
return ValueTask.FromException(new ObjectDisposedException(nameof(Http3WriteStream)));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http3WriteStream))));
}

return stream.WriteRequestContentAsync(buffer, cancellationToken);
Expand All @@ -1589,7 +1589,7 @@ public override Task FlushAsync(CancellationToken cancellationToken)

if (stream is null)
{
return Task.FromException(new ObjectDisposedException(nameof(Http3WriteStream)));
return Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http3WriteStream))));
}

return stream.FlushSendBufferAsync(endStream: false, cancellationToken).AsTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Net.Http.Metrics;
using System.Net.Security;
using System.Runtime.ExceptionServices;
using System.Runtime.Versioning;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -634,16 +635,16 @@ async Task<HttpResponseMessage> CreateHandlerAndSendAsync(HttpRequestMessage req
{
if (request.Version != HttpVersion.Version10 && request.Version != HttpVersion.Version11 && request.Version != HttpVersion.Version20 && request.Version != HttpVersion.Version30)
{
return new NotSupportedException(SR.net_http_unsupported_version);
return ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_unsupported_version));
}

// Add headers to define content transfer, if not present
if (request.HasHeaders && request.Headers.TransferEncodingChunked.GetValueOrDefault())
{
if (request.Content == null)
{
return new HttpRequestException(SR.net_http_client_execution_error,
new InvalidOperationException(SR.net_http_chunked_not_allowed_with_empty_content));
return ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(SR.net_http_client_execution_error,
ExceptionDispatchInfo.SetCurrentStackTrace(new InvalidOperationException(SR.net_http_chunked_not_allowed_with_empty_content))));
}

// Since the user explicitly set TransferEncodingChunked to true, we need to remove
Expand All @@ -661,7 +662,7 @@ async Task<HttpResponseMessage> CreateHandlerAndSendAsync(HttpRequestMessage req
// HTTP 1.0 does not support chunking
if (request.Headers.TransferEncodingChunked == true)
{
return new NotSupportedException(SR.net_http_unsupported_chunking);
return ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_http_unsupported_chunking));
}

// HTTP 1.0 does not support Expect: 100-continue; just disable it.
Expand All @@ -674,12 +675,12 @@ async Task<HttpResponseMessage> CreateHandlerAndSendAsync(HttpRequestMessage req
Uri? requestUri = request.RequestUri;
if (requestUri is null || !requestUri.IsAbsoluteUri)
{
return new InvalidOperationException(SR.net_http_client_invalid_requesturi);
return ExceptionDispatchInfo.SetCurrentStackTrace(new InvalidOperationException(SR.net_http_client_invalid_requesturi));
}

if (!HttpUtilities.IsSupportedScheme(requestUri.Scheme))
{
return new NotSupportedException(SR.Format(SR.net_http_unsupported_requesturi_scheme, requestUri.Scheme));
return ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.Format(SR.net_http_unsupported_requesturi_scheme, requestUri.Scheme)));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory<byte> preBuffer,

if (!IsConnectionOriented)
{
var ex = new NotSupportedException(SR.net_notconnected);
var ex = ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.net_notconnected));
return ValueTask.FromException(ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ private ValueTask SendFrameLockAcquiredNonCancelableAsync(MessageOpcode opcode,
return ValueTask.FromException(
exc is OperationCanceledException ? exc :
_state == WebSocketState.Aborted ? CreateOperationCanceledException(exc) :
new WebSocketException(WebSocketError.ConnectionClosedPrematurely, exc));
ExceptionDispatchInfo.SetCurrentStackTrace(new WebSocketException(WebSocketError.ConnectionClosedPrematurely, exc)));
}
finally
{
Expand Down Expand Up @@ -1714,10 +1714,10 @@ private void ThrowIfOperationInProgress(bool operationCompleted, [CallerMemberNa
/// <summary>Creates an OperationCanceledException instance, using a default message and the specified inner exception and token.</summary>
private static OperationCanceledException CreateOperationCanceledException(Exception innerException, CancellationToken cancellationToken = default(CancellationToken))
{
return new OperationCanceledException(
return (OperationCanceledException)ExceptionDispatchInfo.SetCurrentStackTrace(new OperationCanceledException(
new OperationCanceledException().Message,
innerException,
cancellationToken);
cancellationToken));
}

private void ThrowIfDisposed() => ThrowIfInvalidState(validStates: ManagedWebSocketStates.All);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -130,7 +131,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel

/// <inheritdoc />
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) =>
ValueTask.FromException<int>(new NotSupportedException());
ValueTask.FromException<int>(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException()));

/// <inheritdoc />
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
Expand All @@ -150,7 +151,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati

/// <inheritdoc />
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) =>
ValueTask.FromException(new NotSupportedException());
ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException()));

/// <inheritdoc />
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
Expand Down Expand Up @@ -195,12 +196,12 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo
{
if (_disposed)
{
return ValueTask.FromException(new ObjectDisposedException(GetType().FullName));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(GetType().FullName)));
}

if (!CanWrite)
{
return ValueTask.FromException(new NotSupportedException(SR.NotWriteableStream));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.NotWriteableStream)));
}

if (cancellationToken.IsCancellationRequested)
Expand Down Expand Up @@ -297,12 +298,12 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo
{
if (_disposed)
{
return ValueTask.FromException(new ObjectDisposedException(GetType().FullName));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(GetType().FullName)));
}

if (!CanWrite)
{
return ValueTask.FromException(new NotSupportedException(SR.NotWriteableStream));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new NotSupportedException(SR.NotWriteableStream)));
}

if (cancellationToken.IsCancellationRequested)
Expand Down
Loading