Files
dotnet-runtime/src/libraries/System.Net.Http/tests/FunctionalTests/CustomContent.cs
T
Stephen Toub 3d6037f313 Change how request content is read in libcurl
libcurl wants to be able to call back to CurlHandler at arbitrary times to ask it to seek in the request content stream.  This can happen for redirects, for auth challenges, etc.  Handling this has been a challenge due to the way HttpContent is designed.

HttpContent provides two modes for accessing the data via Streams:
- Push: You call CopyToAsync(Stream, TransportContext) to have the data from the HttpContent copied into the target stream.
- Pull: You call ReadAsStreamAsync to get a stream that represents the data in the HttpContent, and you can read from that.

The pull model is ideal for libcurl.  It allows us to get a stream which we can jump around in if it's seekable, and we can request a new stream if it's not seekable. The push model is challenging, as we're forced to put in plcae an intermediary, a push-pull adapter that let's us effectively read from a type that's trying to write to us.

There is however an issue with the pull model.  With the push model, StreamContent's CopyToAsync tracks whether the stream has already been accessed, and if it has and if it's non-seekable, trying to call CopyToAsync again throws to alert you that rewinding simply isn't possible.  The pull StreamContent's ReadAsStreamAsync, however, does not.  This means that if you get a non-seekable stream and you call ReadAsStreamAsync again, you get back the same stream (or, rather, a read-only wrapper around the same stream) that is at the existing position rather than the original position.  Then resending that content will end up sending less data than it should have, resulting in failure, just not abruptly.

That issue caused us to go down the path of the more challenging push model and trying to use CopyToAsync.  However, recently that's been shown to have even worse effects.  We don't have the ability to cancel CopyToAsync, which means that if the first copy is still in-flight when we try to seek, we have no choice but to fail, even if the stream is actually seekable, as most are (e.g. memory streams, file streams, etc.)  This can happen deterministically in cases where libcurl decides to redirect before it's fully sent the request content, but it's also prone to race conditions even for cases where the request content is fully sent but the copy hasn't been officially marked as complete by the time libcurl tries to redirect.   ASP.NET recently began hitting this frequently on some machines in their lab runs.

While there's no perfect solution, the best solution is to go with the one that's the simplest code, the fastest code, and the deterministic outcome: the pull model.  This commit deletes a whole bunch of code that was there in support of the push model, adds just a bit of code to enable the pull model, and fixes up a couple of tests that were written to expect the push model.


Commit migrated from https://github.com/dotnet/corefx/commit/74cd3d619b3d3b1e963ca8bd33749af3aec99c05
2016-06-09 10:18:19 -04:00

161 lines
4.4 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Text;
namespace System.Net.Http.Functional.Tests
{
public sealed class CustomContent : StreamContent
{
private long _length;
public static CustomContent Create(string data, bool rewindable)
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
Stream stream = new CustomStream(bytes, rewindable);
return new CustomContent(stream, bytes.Length);
}
private CustomContent(Stream stream, long length) : base(stream)
{
_length = length;
}
public long Length
{
get
{
return _length;
}
}
private class CustomStream : Stream
{
private byte[] _buffer;
private long _position;
private bool _rewindable;
public CustomStream(byte[] buffer, bool rewindable)
{
_buffer = buffer;
_position = 0;
_rewindable = rewindable;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return _rewindable; }
}
public override bool CanWrite
{
get { return false; }
}
public override void Flush()
{
throw new NotSupportedException("CustomStream.Flush");
}
public override long Length
{
get
{
if (_rewindable)
{
return (long)_buffer.Length;
}
else
{
throw new NotSupportedException("CustomStream.Length");
}
}
}
public override long Position
{
get
{
return _position;
}
set
{
if (_rewindable)
{
_position = value;
}
else
{
throw new NotSupportedException("CustomStream.Position");
}
}
}
public override int Read(byte[] buffer, int offset, int count)
{
int bytesRead = 0;
for (int i = 0; i < count; i++)
{
if (_position >= _buffer.Length)
{
break;
}
buffer[offset] = _buffer[_position];
bytesRead++;
offset++;
_position++;
}
return bytesRead;
}
public override long Seek(long offset, SeekOrigin origin)
{
if (_rewindable)
{
switch (origin)
{
case SeekOrigin.Begin:
Position = offset;
break;
case SeekOrigin.Current:
Position += offset;
break;
case SeekOrigin.End:
Position = _buffer.Length + offset;
break;
default:
throw new NotImplementedException("CustomStream.Seek");
}
return Position;
}
else
{
throw new NotImplementedException("CustomStream.Seek");
}
}
public override void SetLength(long value)
{
throw new NotSupportedException("CustomStream.SetLength");
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException("CustomStream.Write");
}
}
}
}