216 _connection._allowedReadLineBytes = 16384;
255 _connection._allowedReadLineBytes = 16384;
260 if (
line.Length != 0)
269 _connection._allowedReadLineBytes = 16384;
283 if (!
base.IsDisposed)
381 base.BytesWritten +=
buffer.Length;
396 base.BytesWritten +=
buffer.Length;
426 if (connection ==
null ||
buffer.Length == 0)
443 if (connection ==
null ||
buffer.Length == 0)
486 if (connection ==
null)
491 if (
task.IsCompletedSuccessfully)
620 if (
task.IsCompletedSuccessfully)
723 base.BytesWritten +=
buffer.Length;
730 base.BytesWritten +=
buffer.Length;
759 if (connection !=
null)
761 return connection._disposed != 1;
810 connection.
Trace(flag ?
"Connection drain succeeded" :
$"Connection drain failed when MaxResponseDrainSize={connection._pool.Settings._maxResponseDrainSize} bytes or MaxResponseDrainTime=={connection._pool.Settings._maxResponseDrainTime} exceeded",
"DrainOnDisposeAsync");
817 connection.
Trace(
$"Connection drain failed due to exception: {value}",
"DrainOnDisposeAsync");
883 if (connection ==
null ||
buffer.Length == 0)
900 if (connection ==
null ||
buffer.Length == 0)
943 if (connection ==
null)
948 if (
task.IsCompletedSuccessfully)
984 if (connection ==
null)
1001 if (connection ==
null)
1029 if (connection ==
null)
1034 if (!
task.IsCompleted)
1038 return task.AsTask();
1165 Trace(
"Connection closing.",
"Dispose");
1214 Trace(
$"Error performing read ahead: {value}",
"PrepareForReuse");
1259 if (
headers.HeaderStore !=
null)
1263 if (
header.Key.KnownHeader ==
null)
1369 Trace(
$"Sending request: {request}",
"SendAsyncCore");
1404 if (!
request.RequestUri.IsDefaultPort)
1412 bool flag = request.Version.Minor == 0 && request.Version.Major == 1;
1450 bool flag2 = request.HasHeaders && request.Headers.ExpectContinue ==
true;
1453 Trace(
$"Request content is not null, start processing it. hasExpectContinueHeader = {flag2}",
"SendAsyncCore");
1488 Trace(
"Pre-emptive read completed asynchronously for a synchronous request.",
"SendAsyncCore");
1494 Trace(
$"Received {num} bytes.",
"SendAsyncCore");
1517 while ((uint)(response.StatusCode - 100) <= 99
u)
1530 Trace(
$"Current {response.StatusCode} response is an interim response or not expected, need to read for a final response.",
"SendAsyncCore");
1565 if (
response.Headers.ConnectionClose.GetValueOrDefault())
1577 Trace(
"Request is fully sent.",
"SendAsyncCore");
1601 else if (!
response.Content.Headers.ContentLength.HasValue)
1621 Trace(
$"Received response: {response}",
"SendAsyncCore");
1637 Trace(
$"Error sending request: {error}",
"SendAsyncCore");
1709 target.Trace(
"Cancellation requested. Disposing of the connection.",
"RegisterCancellation");
1718 return line.Length == 0;
1744 Trace(
"Finished sending request content.",
"SendRequestContentAsync");
1756 Trace(
"Sending request content for Expect: 100-continue.",
"SendRequestContentWithExpect100ContinueAsync");
1771 Trace(
"Canceling request content for Expect: 100-continue.",
"SendRequestContentWithExpect100ContinueAsync");
1777 if (
line.Length < 12 ||
line[8] != 32)
1807 if (
line.Length == 12)
1837 while (
line[i] != 58 &&
line[i] != 32)
1840 if (i ==
line.Length)
1857 connection.
Trace(
"Stripping forbidden " + descriptor.Name +
" from trailer headers.",
"ParseHeaderNameValue");
1861 while (
line[i] == 32)
1864 if (i ==
line.Length)
1869 if (
line[i++] != 58)
1873 for (; i < line.Length && (
line[i] == 32 ||
line[i] == 9); i++)
1910 if (
source.Length <= num)
1934 if (
source.Length <= num)
1960 if (
source.Length <= num)
1978 if (
source.Length <= num)
2067 foreach (
char c
in s)
2083 if (encoding ==
null)
2116 foreach (
char c
in s)
2128 foreach (
char c
in s)
2162 Trace(
$"Writing {source.Length} bytes.",
"WriteToStream");
2171 Trace(
$"Writing {source.Length} bytes.",
"WriteToStreamAsync");
2184 int num =
span.IndexOf<
byte>(10);
2198 line =
span.Slice(0, (num > 0 &&
span[num - 1] == 13) ? (num - 1) : num);
2235 if (c !=
' ' && c !=
'\t')
2286 Trace(
$"Received {_readLength} bytes.",
"InitialFillAsync");
2305 byte[]
array =
new byte[_readBuffer.Length * 2];
2315 Trace(
$"Received {num3} bytes.",
"FillAsync");
2346 Trace(
$"Received {num2} bytes.",
"Read");
2367 Trace(
$"Received {num2} bytes.",
"ReadAsync");
2389 Trace(
$"Received {num2} bytes.",
"ReadBuffered");
2424 Trace(
$"Received {num2} bytes.",
"ReadBufferedAsyncCore");
2437 Trace(
$"Copying {count} bytes to stream.",
"CopyFromBufferAsync");
2516 if (num > readBuffer.Length)
2563 Trace(
"Unexpected data on connection after response read.",
"CompleteResponse");
2594 Trace(
"Connection will not be reused.",
"ReturnConnectionToPool");
2607 if (left.Length != right.Length)
2611 for (
int i = 0; i < left.Length; i++)
2613 if (left[i] != right[i])
int IList. IndexOf(object value)
static string ToString(byte[] value, int startIndex, int length)
static ulong ToUInt64(byte[] value, int startIndex)
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
static bool TryFormat(bool value, Span< byte > destination, out int bytesWritten, StandardFormat format=default(StandardFormat))
static bool TryParse(ReadOnlySpan< byte > source, out bool value, out int bytesConsumed, char standardFormat='\0')
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
static void SuppressFinalize(object obj)
static CultureInfo InvariantCulture
static void ValidateCopyToArguments(Stream destination, int bufferSize)
Task WriteAsync(byte[] buffer, int offset, int count)
int Read(byte[] buffer, int offset, int count)
void CopyTo(Stream destination)
Task< int > ReadAsync(byte[] buffer, int offset, int count)
void Write(byte[] buffer, int offset, int count)
Task CopyToAsync(Stream destination)
static byte Min(byte val1, byte val2)
string GetCookieHeader(Uri uri)
static string Get(HttpStatusCode code)
static readonly Version Version10
static readonly Version Version11
static bool IsSessionAuthenticationChallenge(HttpResponseMessage response)
static bool ShouldWrapInOperationCanceledException(Exception exception, CancellationToken cancellationToken)
static Exception CreateOperationCanceledException(Exception innerException, CancellationToken cancellationToken)
static void ThrowIfCancellationRequested(CancellationToken cancellationToken)
static void ProcessReceivedCookies(HttpResponseMessage response, CookieContainer cookieContainer)
static EmptyReadStream Instance
string GetResponseHeaderValueWithCaching(HeaderDescriptor descriptor, ReadOnlySpan< byte > value, Encoding valueEncoding)
void TraceConnection(Stream stream)
void LogExceptions(Task task)
static void IgnoreExceptions(ValueTask< int > task)
static bool IsDigit(byte c)
void InvalidateHttp11Connection(HttpConnection connection, bool disposing=true)
void ReturnHttp11Connection(HttpConnection connection, bool isNewConnection=false)
byte[] HostHeaderValueBytes
HttpConnectionSettings Settings
HeaderEncodingSelector< HttpRequestMessage > _responseHeaderEncodingSelector
TimeSpan _maxResponseDrainTime
HeaderEncodingSelector< HttpRequestMessage > _requestHeaderEncodingSelector
TimeSpan _expect100ContinueTimeout
CookieContainer _cookieContainer
int _maxResponseHeadersLength
int _maxResponseDrainSize
ChunkedEncodingReadStream(HttpConnection connection, HttpResponseMessage response)
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken)
async Task CopyToAsyncCore(Stream destination, CancellationToken cancellationToken)
int ReadChunksFromConnectionBuffer(Span< byte > buffer, CancellationTokenRegistration cancellationRegistration)
override async ValueTask< bool > DrainAsync(int maxDrainBytes)
static void ValidateChunkExtension(ReadOnlySpan< byte > lineAfterChunkSize)
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
ulong _chunkBytesRemaining
readonly HttpResponseMessage _response
override int Read(Span< byte > buffer)
ReadOnlyMemory< byte > ReadChunkFromConnectionBuffer(int maxBytesToRead, CancellationTokenRegistration cancellationRegistration)
async ValueTask< int > ReadAsyncCore(Memory< byte > buffer, CancellationToken cancellationToken)
ChunkedEncodingWriteStream(HttpConnection connection)
static readonly byte[] s_finalChunkBytes
override void Write(ReadOnlySpan< byte > buffer)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken ignored)
override Task FinishAsync(bool async)
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
override int Read(Span< byte > buffer)
async Task CompleteCopyToAsync(Task copyTask, HttpConnection connection, CancellationToken cancellationToken)
void Finish(HttpConnection connection)
ConnectionCloseReadStream(HttpConnection connection)
override async ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken)
ContentLengthReadStream(HttpConnection connection, ulong contentLength)
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
ulong _contentBytesRemaining
override async ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken)
override async ValueTask< bool > DrainAsync(int maxDrainBytes)
override int Read(Span< byte > buffer)
ReadOnlyMemory< byte > ReadFromConnectionBuffer(int maxBytesToRead)
async Task CompleteCopyToAsync(Task copyTask, CancellationToken cancellationToken)
override Task FinishAsync(bool async)
override void Write(ReadOnlySpan< byte > buffer)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken ignored)
ContentLengthWriteStream(HttpConnection connection)
override void Write(ReadOnlySpan< byte > buffer)
bool CanReadFromConnection
virtual ValueTask< bool > DrainAsync(int maxDrainBytes)
override void Dispose(bool disposing)
async Task DrainOnDisposeAsync()
override ValueTask WriteAsync(ReadOnlyMemory< byte > destination, CancellationToken cancellationToken)
HttpContentReadStream(HttpConnection connection)
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken)
HttpContentWriteStream(HttpConnection connection)
Task FinishAsync(bool async)
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
override Task FlushAsync(CancellationToken ignored)
override int Read(Span< byte > buffer)
RawConnectionStream(HttpConnection connection)
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
async Task CompleteCopyToAsync(Task copyTask, HttpConnection connection, CancellationToken cancellationToken)
override void Write(ReadOnlySpan< byte > buffer)
override int Read(Span< byte > buffer)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken)
override Task FlushAsync(CancellationToken cancellationToken)
static async Task WaitWithConnectionCancellationAsync(ValueTask task, HttpConnection connection, CancellationToken cancellationToken)
override async ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken)
void Finish(HttpConnection connection)
HttpRequestMessage _currentRequest
readonly WeakReference< HttpConnection > _weakThisRef
int _allowedReadLineBytes
HttpContentWriteStream CreateRequestContentStream(HttpRequestMessage request)
async Task< HttpResponseMessage > SendAsyncCore(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
Task WriteTwoBytesAsync(byte b1, byte b2, bool async)
Task WriteBytesAsync(byte[] bytes, bool async)
static readonly byte[] s_http1DotBytes
override string ToString()
static bool EqualsOrdinal(string left, ReadOnlySpan< byte > right)
Task WriteAsciiStringAsync(string s, bool async)
async ValueTask WriteAsync(ReadOnlyMemory< byte > source, bool async)
void Write(ReadOnlySpan< byte > source)
bool TryReadNextLine(out ReadOnlySpan< byte > line)
ValueTask WriteToStreamAsync(ReadOnlyMemory< byte > source, bool async)
async Task CopyToContentLengthAsync(Stream destination, bool async, ulong length, int bufferSize, CancellationToken cancellationToken)
int Read(Span< byte > destination)
async ValueTask DrainResponseAsync(HttpResponseMessage response, CancellationToken cancellationToken)
readonly TransportContext _transportContext
async ValueTask< ReadOnlyMemory< byte > > ReadNextResponseHeaderLineAsync(bool async, bool foldedHeadersAllowed=false)
bool MapSendException(Exception exception, CancellationToken cancellationToken, out Exception mappedException)
Task< HttpResponseMessage > SendAsync(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
async ValueTask FillAsync(bool async)
void ReadFromBuffer(Span< byte > buffer)
async ValueTask< int > ReadAsync(Memory< byte > destination)
async Task CopyToUntilEofWithExistingBufferedDataAsync(Stream destination, bool async, int bufferSize, CancellationToken cancellationToken)
static readonly byte[] s_spaceHttp10NewlineAsciiBytes
void WriteToBuffer(ReadOnlyMemory< byte > source)
static readonly byte[] s_spaceHttp11NewlineAsciiBytes
async ValueTask SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, bool async, CancellationToken cancellationToken)
async ValueTask< int > ReadBufferedAsyncCore(Memory< byte > destination)
void WriteWithoutBuffering(ReadOnlySpan< byte > source)
async Task WriteStringWithEncodingAsyncSlow(string s, bool async, Encoding encoding)
static readonly ulong s_http10Bytes
HttpConnection(HttpConnectionPool pool, Socket socket, Stream stream, TransportContext transportContext)
ValueTask FlushAsync(bool async)
async ValueTask InitialFillAsync(bool async)
static readonly ulong s_http11Bytes
Task WriteStringAsync(string s, bool async)
async Task SendRequestContentWithExpect100ContinueAsync(HttpRequestMessage request, Task< bool > allowExpect100ToContinueTask, HttpContentWriteStream stream, Timer expect100Timer, bool async, CancellationToken cancellationToken)
ValueTask< int >? _readAheadTask
Task WriteHexInt32Async(int value, bool async)
void Dispose(bool disposing)
CancellationTokenRegistration RegisterCancellation(CancellationToken cancellationToken)
void ReturnConnectionToPool()
async ValueTask WriteHostHeaderAsync(Uri uri, bool async)
async Task WriteBytesSlowAsync(byte[] bytes, int length, bool async)
ValueTask< int > ReadBufferedAsync(Memory< byte > destination)
static void ParseHeaderNameValue(HttpConnection connection, ReadOnlySpan< byte > line, HttpResponseMessage response, bool isFromTrailer)
async Task WriteByteSlowAsync(byte b, bool async)
bool PrepareForReuse(bool async)
void WriteToStream(ReadOnlySpan< byte > source)
ReadOnlyMemory< byte > RemainingBuffer
override bool CheckUsabilityOnScavenge()
ValueTask CopyFromBufferAsync(Stream destination, bool async, int count, CancellationToken cancellationToken)
int ReadBuffered(Span< byte > destination)
readonly HttpConnectionPool _pool
ValueTask WriteWithoutBufferingAsync(ReadOnlyMemory< byte > source, bool async)
Task WriteByteAsync(byte b, bool async)
static void ParseStatusLine(ReadOnlySpan< byte > line, HttpResponseMessage response)
void WriteToBuffer(ReadOnlySpan< byte > source)
async ValueTask WriteHeadersAsync(HttpHeaders headers, string cookiesFromContainer, bool async)
Task CopyToUntilEofAsync(Stream destination, bool async, int bufferSize, CancellationToken cancellationToken)
override void Trace(string message, [CallerMemberName] string memberName=null)
readonly byte[] _writeBuffer
void ConsumeFromRemainingBuffer(int bytesToConsume)
bool _startedSendingRequestBody
override long GetIdleTicks(long nowTicks)
Task WriteStringAsync(string s, bool async, Encoding encoding)
Task WriteDecimalInt32Async(int value, bool async)
async Task WriteTwoBytesSlowAsync(byte b1, byte b2, bool async)
static readonly byte[] s_httpSchemeAndDelimiter
static bool IsLineEmpty(ReadOnlyMemory< byte > line)
async ValueTask FlushThenWriteWithoutBufferingAsync(ReadOnlyMemory< byte > source, bool async)
async Task WriteStringAsyncSlow(string s, bool async)
ValueTask< int >? ConsumeReadAheadTask()
void ThrowIfExceededAllowedReadLineBytes()
static readonly byte[] s_contentLength0NewlineAsciiBytes
HttpConnection _connection
HttpConnection GetConnectionOrThrow()
static HttpMethod Normalize(HttpMethod method)
static HttpMethod Connect
static Encoding DefaultHttpEncoding
static readonly HttpTelemetry Log
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
bool Poll(int microSeconds, SelectMode mode)
static ExceptionDispatchInfo Capture(Exception source)
static Exception SetCurrentStackTrace(Exception source)
static string net_http_invalid_response_premature_eof
static string ObjectDisposed_StreamClosed
static string net_http_invalid_response_header_line
static string net_http_invalid_response_chunk_extension_invalid
static string net_http_response_headers_exceeded_length
static string net_http_content_readonly_stream
static string Format(string resourceFormat, object p1)
static string net_http_invalid_response_status_reason
static string net_http_invalid_response_status_code
static string net_http_client_execution_error
static string net_http_invalid_response_chunk_header_invalid
static string net_http_invalid_response_status_line
static string net_http_invalid_response_chunk_terminator_invalid
static string net_http_request_no_host
static string net_http_request_invalid_char_encoding
static string net_http_invalid_response_premature_eof_bytecount
static string net_http_invalid_response_header_folder
static string net_http_authconnectionfailure
static string net_http_invalid_response_header_name
virtual byte[] GetBytes(char[] chars)
int GetMaxByteCount(int charCount)
virtual int GetByteCount(char[] chars)
static int CompareExchange(ref int location1, int value, int comparand)
static int Exchange(ref int location1, int value)
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
static Task FromCanceled(CancellationToken cancellationToken)
static Task CompletedTask
static readonly TimeSpan InfiniteTimeSpan
static bool Read(ref bool location)
static void Write(ref bool location, bool value)
static readonly string SchemeDelimiter
UriHostNameType HostNameType
static readonly string UriSchemeHttp
static ValueTask FromCanceled(CancellationToken cancellationToken)
ConfiguredValueTaskAwaitable ConfigureAwait(bool continueOnCapturedContext)
static ValueTask FromException(Exception exception)
static readonly TimeSpan Zero