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 @@ -962,7 +962,10 @@ private async Task StartRequestAsync(WinHttpRequestState state)
// Since the headers have been read, set the "receive" timeout to be based on each read
// call of the response body data. WINHTTP_OPTION_RECEIVE_TIMEOUT sets a timeout on each
// lower layer winsock read.
uint optionData = unchecked((uint)_receiveDataTimeout.TotalMilliseconds);
// Timeout.InfiniteTimeSpan will be converted to uint.MaxValue milliseconds (~ 50 days).
// The result a of double->uint cast is unspecified for -1 and may differ on ARM, returning 0 instead of uint.MaxValue.
// To handle Timeout.InfiniteTimespan correctly, we need to cast to int first.
uint optionData = (uint)(int)_receiveDataTimeout.TotalMilliseconds;
SetWinHttpOption(state.RequestHandle, Interop.WinHttp.WINHTTP_OPTION_RECEIVE_TIMEOUT, ref optionData);

HttpResponseMessage responseMessage =
Expand Down Expand Up @@ -1007,8 +1010,10 @@ private unsafe void SetTcpKeepalive(SafeWinHttpHandle sessionHandle)
onoff = 1,

// Timeout.InfiniteTimeSpan will be converted to uint.MaxValue milliseconds (~ 50 days)
keepaliveinterval = (uint)_tcpKeepAliveInterval.TotalMilliseconds,
keepalivetime = (uint)_tcpKeepAliveTime.TotalMilliseconds
// The result a of double->uint cast is unspecified for -1 and may differ on ARM, returning 0 instead of uint.MaxValue.
// To handle Timeout.InfiniteTimespan correctly, we need to cast to int first.
keepaliveinterval = (uint)(int)_tcpKeepAliveInterval.TotalMilliseconds,
keepalivetime = (uint)(int)_tcpKeepAliveTime.TotalMilliseconds
};

SetWinHttpOption(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static ProxyInfo RequestProxySettings

public static int? WinHttpOptionSendTimeout { get; set; }

public static int? WinHttpOptionReceiveTimeout { get; set; }
public static uint? WinHttpOptionReceiveTimeout { get; set; }

public static List<IntPtr> WinHttpOptionClientCertContext { get { return winHttpOptionClientCertContextList; } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@ public static bool WinHttpSetOption(
{
APICallHistory.WinHttpOptionRedirectPolicy = optionData;
}
else if (option == Interop.WinHttp.WINHTTP_OPTION_RECEIVE_TIMEOUT)
{
APICallHistory.WinHttpOptionReceiveTimeout = optionData;
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,16 @@ public void MaxConnectionsPerServer_SetPositiveValue_Success()
handler.MaxConnectionsPerServer = 1;
}

[Fact]
public void ReceiveDataTimeout_SetValidValue_ForwardsCorrectNativeOptionToWinHttp()
{
var handler = new WinHttpHandler();

SendRequestHelper.Send(handler, () => handler.ReceiveDataTimeout = TimeSpan.FromSeconds(13));

Assert.Equal(13_000u, APICallHistory.WinHttpOptionReceiveTimeout.Value);
}

[Fact]
public void ReceiveDataTimeout_SetNegativeValue_ThrowsArgumentOutOfRangeException()
{
Expand Down Expand Up @@ -490,11 +500,13 @@ public void ReceiveDataTimeout_SetZeroValue_ThrowsArgumentOutOfRangeException()
}

[Fact]
public void ReceiveDataTimeout_SetInfiniteValue_NoExceptionThrown()
public void ReceiveDataTimeout_SetInfiniteTimeSpan_TranslatesToUInt32MaxValue()
{
var handler = new WinHttpHandler();

handler.ReceiveDataTimeout = Timeout.InfiniteTimeSpan;
SendRequestHelper.Send(handler, () => handler.ReceiveDataTimeout = Timeout.InfiniteTimeSpan);

Assert.Equal(uint.MaxValue, APICallHistory.WinHttpOptionReceiveTimeout.Value);
}

[Fact]
Expand Down