diff --git a/src/Foundation/NSUrlSessionHandler.cs b/src/Foundation/NSUrlSessionHandler.cs index 396e11c3195f..3d348d71decf 100644 --- a/src/Foundation/NSUrlSessionHandler.cs +++ b/src/Foundation/NSUrlSessionHandler.cs @@ -84,6 +84,8 @@ public NSUrlSessionHandler () inflightRequests = new Dictionary (); } + public long MaxInputInMemory { get; set; } = long.MaxValue; + void RemoveInflightData (NSUrlSessionTask task, bool cancel = true) { InflightData inflight; @@ -151,10 +153,14 @@ async Task CreateRequest (HttpRequestMessage request) return acc; }) }; - - if (stream != Stream.Null) - nsrequest.BodyStream = new WrappedNSInputStream (stream); - + if (stream != Stream.Null) { + // HttpContent.TryComputeLength is `protected internal` :-( but it's indirectly called by headers + var length = request.Content.Headers.ContentLength; + if (length.HasValue && (length <= MaxInputInMemory)) + nsrequest.Body = NSData.FromStream (stream); + else + nsrequest.BodyStream = new WrappedNSInputStream (stream); + } return nsrequest; }