Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ SendAsyncCore()

async Task< HttpResponseMessage > System.Net.Http.HttpConnection.SendAsyncCore ( HttpRequestMessage request,
bool async,
CancellationToken cancellationToken )
inline

Definition at line 1359 of file HttpConnection.cs.

1360 {
1364 HttpMethod normalizedMethod = HttpMethod.Normalize(request.Method);
1365 _canRetry = false;
1367 if (System.Net.NetEventSource.Log.IsEnabled())
1368 {
1369 Trace($"Sending request: {request}", "SendAsyncCore");
1370 }
1372 Unsafe.SkipInit(out HttpResponseMessage result);
1373 try
1374 {
1375 if (HttpTelemetry.Log.IsEnabled())
1376 {
1377 HttpTelemetry.Log.RequestHeadersStart();
1378 }
1381 if ((object)normalizedMethod == HttpMethod.Connect)
1382 {
1383 if (!request.HasHeaders || request.Headers.Host == null)
1384 {
1385 throw new HttpRequestException(System.SR.net_http_request_no_host);
1386 }
1388 }
1389 else
1390 {
1391 if (Kind == HttpConnectionKind.Proxy)
1392 {
1394 if (request.RequestUri.HostNameType != UriHostNameType.IPv6)
1395 {
1397 }
1398 else
1399 {
1403 }
1404 if (!request.RequestUri.IsDefaultPort)
1405 {
1408 }
1409 }
1411 }
1412 bool flag = request.Version.Minor == 0 && request.Version.Major == 1;
1414 string cookiesFromContainer = null;
1416 {
1418 if (cookiesFromContainer == "")
1419 {
1420 cookiesFromContainer = null;
1421 }
1422 }
1423 if (!request.HasHeaders || request.Headers.Host == null)
1424 {
1426 }
1427 if (request.HasHeaders || cookiesFromContainer != null)
1428 {
1430 }
1431 if (request.Content != null)
1432 {
1433 await WriteHeadersAsync(request.Content.Headers, null, async).ConfigureAwait(continueOnCapturedContext: false);
1434 }
1435 else if (normalizedMethod.MustHaveRequestBody)
1436 {
1438 }
1440 if (HttpTelemetry.Log.IsEnabled())
1441 {
1442 HttpTelemetry.Log.RequestHeadersStop();
1443 }
1444 if (request.Content == null)
1445 {
1447 }
1448 else
1449 {
1450 bool flag2 = request.HasHeaders && request.Headers.ExpectContinue == true;
1451 if (System.Net.NetEventSource.Log.IsEnabled())
1452 {
1453 Trace($"Request content is not null, start processing it. hasExpectContinueHeader = {flag2}", "SendAsyncCore");
1454 }
1455 if (!flag2)
1456 {
1458 }
1459 else
1460 {
1463 Timer expect100Timer = new Timer(delegate(object s)
1464 {
1465 ((TaskCompletionSource<bool>)s).TrySetResult(result: true);
1468 }
1469 }
1470 _allowedReadLineBytes = (int)Math.Min(2147483647L, (long)_pool.Settings._maxResponseHeadersLength * 1024L);
1472 if (!valueTask.HasValue)
1473 {
1475 }
1476 else
1477 {
1478 ValueTask<int> valueOrDefault = valueTask.GetValueOrDefault();
1479 int num;
1480 if (valueOrDefault.IsCompleted)
1481 {
1482 num = valueOrDefault.Result;
1483 }
1484 else
1485 {
1486 if (!async)
1487 {
1488 Trace("Pre-emptive read completed asynchronously for a synchronous request.", "SendAsyncCore");
1489 }
1490 num = await valueOrDefault.ConfigureAwait(continueOnCapturedContext: false);
1491 }
1492 if (System.Net.NetEventSource.Log.IsEnabled())
1493 {
1494 Trace($"Received {num} bytes.", "SendAsyncCore");
1495 }
1496 _readOffset = 0;
1497 _readLength = num;
1498 }
1499 if (_readLength == 0)
1500 {
1502 {
1503 _canRetry = true;
1504 }
1506 }
1507 HttpResponseMessage response = new HttpResponseMessage
1508 {
1509 RequestMessage = request,
1510 Content = new HttpConnectionResponseContent()
1511 };
1513 if (HttpTelemetry.Log.IsEnabled())
1514 {
1515 HttpTelemetry.Log.ResponseHeadersStart();
1516 }
1517 while ((uint)(response.StatusCode - 100) <= 99u)
1518 {
1519 if (allowExpect100ToContinue != null && response.StatusCode == HttpStatusCode.Continue)
1520 {
1521 allowExpect100ToContinue.TrySetResult(result: true);
1523 }
1524 else if (response.StatusCode == HttpStatusCode.SwitchingProtocols)
1525 {
1526 break;
1527 }
1528 if (System.Net.NetEventSource.Log.IsEnabled())
1529 {
1530 Trace($"Current {response.StatusCode} response is an interim response or not expected, need to read for a final response.", "SendAsyncCore");
1531 }
1533 {
1534 }
1536 }
1537 while (true)
1538 {
1540 if (IsLineEmpty(line))
1541 {
1542 break;
1543 }
1545 }
1546 if (HttpTelemetry.Log.IsEnabled())
1547 {
1548 HttpTelemetry.Log.ResponseHeadersStop();
1549 }
1550 if (allowExpect100ToContinue != null)
1551 {
1552 if (response.StatusCode >= HttpStatusCode.MultipleChoices && request.Content != null && (!request.Content.Headers.ContentLength.HasValue || request.Content.Headers.ContentLength.GetValueOrDefault() > 1024) && !AuthenticationHelper.IsSessionAuthenticationChallenge(response))
1553 {
1554 allowExpect100ToContinue.TrySetResult(result: false);
1555 if (!allowExpect100ToContinue.Task.Result)
1556 {
1557 _connectionClose = true;
1558 }
1559 }
1560 else
1561 {
1562 allowExpect100ToContinue.TrySetResult(result: true);
1563 }
1564 }
1565 if (response.Headers.ConnectionClose.GetValueOrDefault())
1566 {
1567 _connectionClose = true;
1568 }
1569 if (sendRequestContentTask != null)
1570 {
1573 await task.ConfigureAwait(continueOnCapturedContext: false);
1574 }
1575 if (System.Net.NetEventSource.Log.IsEnabled())
1576 {
1577 Trace("Request is fully sent.", "SendAsyncCore");
1578 }
1579 cancellationRegistration.Dispose();
1580 CancellationHelper.ThrowIfCancellationRequested(cancellationToken);
1581 Stream stream;
1582 if ((object)normalizedMethod == HttpMethod.Head || response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.NotModified)
1583 {
1584 stream = EmptyReadStream.Instance;
1586 }
1587 else if ((object)normalizedMethod == HttpMethod.Connect && response.StatusCode == HttpStatusCode.OK)
1588 {
1589 stream = new RawConnectionStream(this);
1590 _connectionClose = true;
1592 _detachedFromPool = true;
1593 }
1594 else if (response.StatusCode == HttpStatusCode.SwitchingProtocols)
1595 {
1596 stream = new RawConnectionStream(this);
1597 _connectionClose = true;
1599 _detachedFromPool = true;
1600 }
1601 else if (!response.Content.Headers.ContentLength.HasValue)
1602 {
1603 stream = ((response.Headers.TransferEncodingChunked != true) ? ((HttpContentReadStream)new ConnectionCloseReadStream(this)) : ((HttpContentReadStream)new ChunkedEncodingReadStream(this, response)));
1604 }
1605 else
1606 {
1607 long valueOrDefault2 = response.Content.Headers.ContentLength.GetValueOrDefault();
1608 if (valueOrDefault2 <= 0)
1609 {
1610 stream = EmptyReadStream.Instance;
1612 }
1613 else
1614 {
1615 stream = new ContentLengthReadStream(this, (ulong)valueOrDefault2);
1616 }
1617 }
1618 ((HttpConnectionResponseContent)response.Content).SetStream(stream);
1619 if (System.Net.NetEventSource.Log.IsEnabled())
1620 {
1621 Trace($"Received response: {response}", "SendAsyncCore");
1622 }
1624 {
1625 CookieHelper.ProcessReceivedCookies(response, _pool.Settings._cookieContainer);
1626 }
1627 result = response;
1628 return result;
1629 }
1630 catch (Exception ex)
1631 {
1632 Exception error = ex;
1633 cancellationRegistration.Dispose();
1634 allowExpect100ToContinue?.TrySetResult(result: false);
1635 if (System.Net.NetEventSource.Log.IsEnabled())
1636 {
1637 Trace($"Error sending request: {error}", "SendAsyncCore");
1638 }
1639 if (sendRequestContentTask != null && !sendRequestContentTask.IsCompletedSuccessfully)
1640 {
1641 if (Volatile.Read(ref _disposed) == 1)
1642 {
1643 Exception mappedException;
1644 try
1645 {
1647 }
1649 {
1650 throw mappedException;
1651 }
1652 }
1654 }
1655 Dispose();
1657 {
1658 throw mappedException2;
1659 }
1660 if (!(ex is Exception source))
1661 {
1662 throw ex;
1663 }
1665 }
1666 return result;
1667 }
void InvalidateHttp11Connection(HttpConnection connection, bool disposing=true)
HttpContentWriteStream CreateRequestContentStream(HttpRequestMessage request)
Task WriteTwoBytesAsync(byte b1, byte b2, bool async)
Task WriteBytesAsync(byte[] bytes, bool async)
Task WriteAsciiStringAsync(string s, bool async)
async ValueTask< ReadOnlyMemory< byte > > ReadNextResponseHeaderLineAsync(bool async, bool foldedHeadersAllowed=false)
bool MapSendException(Exception exception, CancellationToken cancellationToken, out Exception mappedException)
static readonly byte[] s_spaceHttp10NewlineAsciiBytes
static readonly byte[] s_spaceHttp11NewlineAsciiBytes
async ValueTask SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, bool async, CancellationToken cancellationToken)
ValueTask FlushAsync(bool async)
async ValueTask InitialFillAsync(bool async)
Task WriteStringAsync(string s, bool async)
async Task SendRequestContentWithExpect100ContinueAsync(HttpRequestMessage request, Task< bool > allowExpect100ToContinueTask, HttpContentWriteStream stream, Timer expect100Timer, bool async, CancellationToken cancellationToken)
CancellationTokenRegistration RegisterCancellation(CancellationToken cancellationToken)
async ValueTask WriteHostHeaderAsync(Uri uri, bool async)
static void ParseHeaderNameValue(HttpConnection connection, ReadOnlySpan< byte > line, HttpResponseMessage response, bool isFromTrailer)
readonly HttpConnectionPool _pool
Task WriteByteAsync(byte b, bool async)
static void ParseStatusLine(ReadOnlySpan< byte > line, HttpResponseMessage response)
async ValueTask WriteHeadersAsync(HttpHeaders headers, string cookiesFromContainer, bool async)
override void Trace(string message, [CallerMemberName] string memberName=null)
Task WriteDecimalInt32Async(int value, bool async)
static readonly byte[] s_httpSchemeAndDelimiter
static bool IsLineEmpty(ReadOnlyMemory< byte > line)
ValueTask< int >? ConsumeReadAheadTask()
static readonly byte[] s_contentLength0NewlineAsciiBytes
static readonly System.Net.NetEventSource Log
static ExceptionDispatchInfo Capture(Exception source)
static string net_http_invalid_response_premature_eof
Definition SR.cs:94
static string net_http_request_no_host
Definition SR.cs:144
Definition SR.cs:7
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
static readonly TimeSpan InfiniteTimeSpan
Definition Timeout.cs:5
static bool Read(ref bool location)
Definition Volatile.cs:67
ConfiguredValueTaskAwaitable ConfigureAwait(bool continueOnCapturedContext)
Definition ValueTask.cs:312

References System.Net.Http.HttpConnection._allowedReadLineBytes, System.Net.Http.HttpConnection._canRetry, System.Net.Http.HttpConnection._connectionClose, System.Net.Http.HttpConnectionSettings._cookieContainer, System.Net.Http.HttpConnection._currentRequest, System.Net.Http.HttpConnection._detachedFromPool, System.Net.Http.HttpConnection._disposed, System.Net.Http.HttpConnectionSettings._expect100ContinueTimeout, System.Net.Http.HttpConnection._pool, System.Net.Http.HttpConnection._readLength, System.Net.Http.HttpConnection._readOffset, System.Net.Http.HttpConnection._startedSendingRequestBody, System.Net.Http.HttpConnectionSettings._useCookies, System.cancellationToken, System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(), System.Net.Http.HttpConnection.CompleteResponse(), System.Threading.Tasks.Task< TResult >.ConfigureAwait(), System.Threading.Tasks.ValueTask< TResult >.ConfigureAwait(), System.Net.Http.HttpMethod.Connect, System.Net.Http.HttpConnection.ConsumeReadAheadTask(), System.Net.Http.HttpConnection.CreateRequestContentStream(), System.Net.Http.HttpConnection.Dispose(), System.exception, System.Net.Http.HttpConnection.FlushAsync(), System.Net.CookieContainer.GetCookieHeader(), System.Net.Http.HttpMethod.Head, System.Threading.Timeout.InfiniteTimeSpan, System.Net.Http.HttpConnection.InitialFillAsync(), System.Net.Http.EmptyReadStream.Instance, System.Net.Http.HttpConnectionPool.InvalidateHttp11Connection(), System.Net.Http.HttpConnection.IsLineEmpty(), System.Net.Http.AuthenticationHelper.IsSessionAuthenticationChallenge(), System.Net.Http.HttpConnection.Kind, System.L, System.Net.Http.HttpTelemetry.Log, System.Net.NetEventSource.Log, System.Net.Http.HttpConnectionBase.LogExceptions(), System.Net.Http.HttpConnection.MapSendException(), System.Math.Min(), System.SR.net_http_invalid_response_premature_eof, System.SR.net_http_request_no_host, System.Net.Http.HttpMethod.Normalize(), System.Net.Http.HttpConnection.ParseHeaderNameValue(), System.Net.Http.HttpConnection.ParseStatusLine(), System.Net.Http.CookieHelper.ProcessReceivedCookies(), System.Threading.Volatile.Read(), System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(), System.Net.Http.HttpConnection.RegisterCancellation(), System.s, System.Net.Http.HttpConnection.s_contentLength0NewlineAsciiBytes, System.Net.Http.HttpConnection.s_httpSchemeAndDelimiter, System.Net.Http.HttpConnection.s_spaceHttp10NewlineAsciiBytes, System.Net.Http.HttpConnection.s_spaceHttp11NewlineAsciiBytes, System.Net.Http.HttpConnection.SendRequestContentAsync(), System.Net.Http.HttpConnection.SendRequestContentWithExpect100ContinueAsync(), System.Net.Http.HttpConnectionPool.Settings, System.source, System.stream, System.task, System.Net.Http.CancellationHelper.ThrowIfCancellationRequested(), System.Net.Http.HttpConnection.Trace(), System.Net.Http.HttpConnection.WriteAsciiStringAsync(), System.Net.Http.HttpConnection.WriteByteAsync(), System.Net.Http.HttpConnection.WriteBytesAsync(), System.Net.Http.HttpConnection.WriteDecimalInt32Async(), System.Net.Http.HttpConnection.WriteHeadersAsync(), System.Net.Http.HttpConnection.WriteHostHeaderAsync(), System.Net.Http.HttpConnection.WriteStringAsync(), and System.Net.Http.HttpConnection.WriteTwoBytesAsync().

Referenced by System.Net.Http.AuthenticationHelper.InnerSendAsync(), and System.Net.Http.HttpConnection.SendAsync().