Skip to content

Commit

Permalink
[2019-06] [System.Net.Http]: Add hack to the legacy HttpClient to pas…
Browse files Browse the repository at this point in the history
…s down Timeout property. mono#12577. (mono#15014)

* [System.Net.Http]Add hack to the legacy HttpClient to pass down Timeout property. mono#12577.

* `HttpClientHandler`, `IMonoHttpClientHandler`: add new internal `MonoSetTimeout (TimeSpan)` function.

* `HttpClient.SendAsyncWorker()`: if `handler` is `HttpClientHandler`, call the new internal
  `MonoSetTimeout()` function to pass down the timeout.

* `MonoWebRequestHandler.SendAsync()`: this is only used here to pass the timeout to the `HttpWebRequest`.

* Address feedback.

* Update HttpMessageInvoker.cs

* Make it build.

* Really make it build (forgot monotouch_watch).
  • Loading branch information
monojenkins authored and marek-safar committed Jun 14, 2019
1 parent 6752005 commit c15b300
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions mcs/class/System.Net.Http/HttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ public long MaxRequestContentBufferSize {

public IDictionary<string, object> Properties => _delegatingHandler.Properties;

// Only used in MonoWebRequestHandler and ignored by the other handlers.
internal void SetWebRequestTimeout (TimeSpan timeout)
{
_delegatingHandler.SetWebRequestTimeout (timeout);
}

protected internal override Task<HttpResponseMessage> SendAsync (HttpRequestMessage request, CancellationToken cancellationToken) =>
_delegatingHandler.SendAsync (request, cancellationToken);
}
Expand Down
3 changes: 3 additions & 0 deletions mcs/class/System.Net.Http/IMonoHttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ IDictionary<string, object> Properties {
}

Task<HttpResponseMessage> SendAsync (HttpRequestMessage request, CancellationToken cancellationToken);

// Only used by MonoWebRequestHandler and ignored by the other handlers.
void SetWebRequestTimeout (TimeSpan timeout);
}
}
9 changes: 9 additions & 0 deletions mcs/class/System.Net.Http/MonoWebRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class MonoWebRequestHandler : IMonoHttpClientHandler
bool unsafeAuthenticatedConnectionSharing;
bool sentRequest;
string connectionGroupName;
TimeSpan? timeout;
bool disposed;

internal MonoWebRequestHandler ()
Expand Down Expand Up @@ -368,6 +369,9 @@ internal virtual HttpWebRequest CreateWebRequest (HttpRequestMessage request)

wr.ServicePoint.Expect100Continue = request.Headers.ExpectContinue == true;

if (timeout != null)
wr.Timeout = (int)timeout.Value.TotalMilliseconds;

// Add request headers
var headers = wr.Headers;
foreach (var header in request.Headers) {
Expand Down Expand Up @@ -529,5 +533,10 @@ public IDictionary<string, object> Properties {
throw new NotImplementedException ();
}
}

void IMonoHttpClientHandler.SetWebRequestTimeout (TimeSpan timeout)
{
this.timeout = timeout;
}
}
}
3 changes: 3 additions & 0 deletions mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ public Task<HttpResponseMessage> SendAsync (HttpRequestMessage request, HttpComp
async Task<HttpResponseMessage> SendAsyncWorker (HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
{
using (var lcts = CancellationTokenSource.CreateLinkedTokenSource (cts.Token, cancellationToken)) {
// Hack to pass the timeout to the HttpWebRequest that's created by MonoWebRequestHandler; all other handlers ignore this.
if (handler is HttpClientHandler clientHandler)
clientHandler.SetWebRequestTimeout (timeout);
lcts.CancelAfter (timeout);

var task = base.SendAsync (request, lcts.Token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public SslProtocols SslProtocols {
set { throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); }
}

// Only used in MonoWebRequestHandler and ignored by the other handlers.
internal void SetWebRequestTimeout (TimeSpan timeout) => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE);

// NS2.1:
public static System.Func<System.Net.Http.HttpRequestMessage, System.Security.Cryptography.X509Certificates.X509Certificate2, System.Security.Cryptography.X509Certificates.X509Chain, System.Net.Security.SslPolicyErrors, bool> DangerousAcceptAnyServerCertificateValidator => throw new PlatformNotSupportedException ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace System.Net.Http
{
public class HttpMessageInvoker : IDisposable
{
HttpMessageHandler handler;
protected private HttpMessageHandler handler;
readonly bool disposeHandler;

public HttpMessageInvoker (HttpMessageHandler handler)
Expand Down
5 changes: 5 additions & 0 deletions mcs/class/System.Net.Http/corefx/SocketsHttpHandler.Mono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ long IMonoHttpClientHandler.MaxRequestContentBufferSize {
}
}

// This is only used by MonoWebRequestHandler.
void IMonoHttpClientHandler.SetWebRequestTimeout (TimeSpan timeout)
{
}

Task<HttpResponseMessage> IMonoHttpClientHandler.SendAsync (HttpRequestMessage request, CancellationToken cancellationToken) => SendAsync (request, cancellationToken);
}
}

0 comments on commit c15b300

Please sign in to comment.