2014-08-13 10:39:27 +01:00
|
|
|
//
|
|
|
|
// System.Net.WebConnectionStream
|
|
|
|
//
|
|
|
|
// Authors:
|
|
|
|
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
|
2018-04-24 09:31:23 +00:00
|
|
|
// Martin Baulig <mabaul@microsoft.com>
|
2014-08-13 10:39:27 +01:00
|
|
|
//
|
|
|
|
// (C) 2003 Ximian, Inc (http://www.ximian.com)
|
|
|
|
// (C) 2004 Novell, Inc (http://www.novell.com)
|
2018-04-24 09:31:23 +00:00
|
|
|
// Copyright (c) 2017 Xamarin Inc. (http://www.xamarin.com)
|
2014-08-13 10:39:27 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
2018-04-24 09:31:23 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Runtime.ExceptionServices;
|
|
|
|
using System.Net.Sockets;
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
namespace System.Net
|
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
abstract class WebConnectionStream : Stream
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
protected bool closed;
|
2014-08-13 10:39:27 +01:00
|
|
|
bool disposed;
|
|
|
|
object locker = new object ();
|
|
|
|
int read_timeout;
|
|
|
|
int write_timeout;
|
|
|
|
internal bool IgnoreIOErrors;
|
|
|
|
|
2018-08-07 15:19:03 +00:00
|
|
|
protected WebConnectionStream (WebConnection cnc, WebOperation operation)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
Connection = cnc;
|
|
|
|
Operation = operation;
|
|
|
|
Request = operation.Request;
|
|
|
|
|
|
|
|
read_timeout = Request.ReadWriteTimeout;
|
2014-08-13 10:39:27 +01:00
|
|
|
write_timeout = read_timeout;
|
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
internal HttpWebRequest Request {
|
|
|
|
get;
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
internal WebConnection Connection {
|
|
|
|
get;
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
internal WebOperation Operation {
|
|
|
|
get;
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
internal ServicePoint ServicePoint => Connection.ServicePoint;
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
public override bool CanTimeout {
|
|
|
|
get { return true; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int ReadTimeout {
|
|
|
|
get {
|
|
|
|
return read_timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
|
|
|
if (value < -1)
|
|
|
|
throw new ArgumentOutOfRangeException ("value");
|
|
|
|
read_timeout = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int WriteTimeout {
|
|
|
|
get {
|
|
|
|
return write_timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
|
|
|
if (value < -1)
|
|
|
|
throw new ArgumentOutOfRangeException ("value");
|
|
|
|
write_timeout = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
protected Exception GetException (Exception e)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
e = HttpWebRequest.FlattenException (e);
|
|
|
|
if (e is WebException)
|
|
|
|
return e;
|
|
|
|
if (Operation.Aborted || e is OperationCanceledException || e is ObjectDisposedException)
|
|
|
|
return HttpWebRequest.CreateRequestAbortedException ();
|
|
|
|
return e;
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
public override int Read (byte[] buffer, int offset, int count)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
if (!CanRead)
|
|
|
|
throw new NotSupportedException (SR.net_writeonlystream);
|
|
|
|
Operation.ThrowIfClosedOrDisposed ();
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
if (buffer == null)
|
|
|
|
throw new ArgumentNullException (nameof (buffer));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
int length = buffer.Length;
|
|
|
|
if (offset < 0 || length < offset)
|
|
|
|
throw new ArgumentOutOfRangeException (nameof (offset));
|
|
|
|
if (count < 0 || (length - offset) < count)
|
|
|
|
throw new ArgumentOutOfRangeException (nameof (count));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
try {
|
|
|
|
return ReadAsync (buffer, offset, count, CancellationToken.None).Result;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw GetException (e);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
public override IAsyncResult BeginRead (byte[] buffer, int offset, int count,
|
2014-08-13 10:39:27 +01:00
|
|
|
AsyncCallback cb, object state)
|
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
if (!CanRead)
|
|
|
|
throw new NotSupportedException (SR.net_writeonlystream);
|
|
|
|
Operation.ThrowIfClosedOrDisposed ();
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
if (buffer == null)
|
2018-04-24 09:31:23 +00:00
|
|
|
throw new ArgumentNullException (nameof (buffer));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
int length = buffer.Length;
|
|
|
|
if (offset < 0 || length < offset)
|
2018-04-24 09:31:23 +00:00
|
|
|
throw new ArgumentOutOfRangeException (nameof (offset));
|
|
|
|
if (count < 0 || (length - offset) < count)
|
|
|
|
throw new ArgumentOutOfRangeException (nameof (count));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
var task = ReadAsync (buffer, offset, count, CancellationToken.None);
|
|
|
|
return TaskToApm.Begin (task, cb, state);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int EndRead (IAsyncResult r)
|
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
if (r == null)
|
|
|
|
throw new ArgumentNullException (nameof (r));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
try {
|
2018-04-24 09:31:23 +00:00
|
|
|
return TaskToApm.End<int> (r);
|
2014-08-13 10:39:27 +01:00
|
|
|
} catch (Exception e) {
|
2018-04-24 09:31:23 +00:00
|
|
|
throw GetException (e);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count,
|
|
|
|
AsyncCallback cb, object state)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
|
|
|
if (buffer == null)
|
2018-04-24 09:31:23 +00:00
|
|
|
throw new ArgumentNullException (nameof (buffer));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
int length = buffer.Length;
|
|
|
|
if (offset < 0 || length < offset)
|
2018-04-24 09:31:23 +00:00
|
|
|
throw new ArgumentOutOfRangeException (nameof (offset));
|
|
|
|
if (count < 0 || (length - offset) < count)
|
|
|
|
throw new ArgumentOutOfRangeException (nameof (count));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
if (!CanWrite)
|
|
|
|
throw new NotSupportedException (SR.net_readonlystream);
|
|
|
|
Operation.ThrowIfClosedOrDisposed ();
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
var task = WriteAsync (buffer, offset, count, CancellationToken.None);
|
|
|
|
return TaskToApm.Begin (task, cb, state);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void EndWrite (IAsyncResult r)
|
|
|
|
{
|
|
|
|
if (r == null)
|
2018-04-24 09:31:23 +00:00
|
|
|
throw new ArgumentNullException (nameof (r));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
try {
|
|
|
|
TaskToApm.End (r);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw GetException (e);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
public override void Write (byte[] buffer, int offset, int count)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
if (buffer == null)
|
|
|
|
throw new ArgumentNullException (nameof (buffer));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
int length = buffer.Length;
|
|
|
|
if (offset < 0 || length < offset)
|
|
|
|
throw new ArgumentOutOfRangeException (nameof (offset));
|
|
|
|
if (count < 0 || (length - offset) < count)
|
|
|
|
throw new ArgumentOutOfRangeException (nameof (count));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
if (!CanWrite)
|
|
|
|
throw new NotSupportedException (SR.net_readonlystream);
|
|
|
|
Operation.ThrowIfClosedOrDisposed ();
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
try {
|
2018-04-24 09:31:23 +00:00
|
|
|
WriteAsync (buffer, offset, count).Wait ();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw GetException (e);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
public override void Flush ()
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
}
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
public override Task FlushAsync (CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return cancellationToken.IsCancellationRequested ?
|
|
|
|
Task.FromCancellation (cancellationToken) :
|
|
|
|
Task.CompletedTask;
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void InternalClose ()
|
|
|
|
{
|
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
|
2018-04-24 09:31:23 +00:00
|
|
|
protected abstract void Close_internal (ref bool disposed);
|
2014-10-04 11:27:48 +01:00
|
|
|
|
2014-08-13 10:39:27 +01:00
|
|
|
public override void Close ()
|
|
|
|
{
|
2018-04-24 09:31:23 +00:00
|
|
|
Close_internal (ref disposed);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override long Seek (long a, SeekOrigin b)
|
|
|
|
{
|
2018-08-07 15:19:03 +00:00
|
|
|
throw new NotSupportedException (SR.net_noseek);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
2018-04-24 09:31:23 +00:00
|
|
|
|
2014-08-13 10:39:27 +01:00
|
|
|
public override void SetLength (long a)
|
|
|
|
{
|
2018-08-07 15:19:03 +00:00
|
|
|
throw new NotSupportedException (SR.net_noseek);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 15:19:03 +00:00
|
|
|
public override bool CanSeek => false;
|
|
|
|
|
|
|
|
public override long Length => throw new NotSupportedException (SR.net_noseek);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
public override long Position {
|
2018-08-07 15:19:03 +00:00
|
|
|
get { throw new NotSupportedException (SR.net_noseek); }
|
|
|
|
set { throw new NotSupportedException (SR.net_noseek); }
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|