PageRenderTime 840ms CodeModel.GetById 25ms RepoModel.GetById 4ms app.codeStats 0ms

/mcs/class/System/System.Net/WebConnectionStream.cs

https://github.com/grendello/mono
C# | 264 lines | 188 code | 45 blank | 31 comment | 38 complexity | e5b67367a6c43a78cd68c1d7ff3cf474 MD5 | raw file
  1. //
  2. // System.Net.WebConnectionStream
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier (gonzalo@ximian.com)
  6. // Martin Baulig <mabaul@microsoft.com>
  7. //
  8. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  9. // (C) 2004 Novell, Inc (http://www.novell.com)
  10. // Copyright (c) 2017 Xamarin Inc. (http://www.xamarin.com)
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.IO;
  33. using System.Text;
  34. using System.Threading;
  35. using System.Threading.Tasks;
  36. using System.Runtime.ExceptionServices;
  37. using System.Net.Sockets;
  38. namespace System.Net
  39. {
  40. abstract class WebConnectionStream : Stream
  41. {
  42. protected bool closed;
  43. bool disposed;
  44. object locker = new object ();
  45. int read_timeout;
  46. int write_timeout;
  47. internal bool IgnoreIOErrors;
  48. protected WebConnectionStream (WebConnection cnc, WebOperation operation)
  49. {
  50. Connection = cnc;
  51. Operation = operation;
  52. Request = operation.Request;
  53. read_timeout = Request.ReadWriteTimeout;
  54. write_timeout = read_timeout;
  55. }
  56. internal HttpWebRequest Request {
  57. get;
  58. }
  59. internal WebConnection Connection {
  60. get;
  61. }
  62. internal WebOperation Operation {
  63. get;
  64. }
  65. internal ServicePoint ServicePoint => Connection.ServicePoint;
  66. public override bool CanTimeout {
  67. get { return true; }
  68. }
  69. public override int ReadTimeout {
  70. get {
  71. return read_timeout;
  72. }
  73. set {
  74. if (value < -1)
  75. throw new ArgumentOutOfRangeException ("value");
  76. read_timeout = value;
  77. }
  78. }
  79. public override int WriteTimeout {
  80. get {
  81. return write_timeout;
  82. }
  83. set {
  84. if (value < -1)
  85. throw new ArgumentOutOfRangeException ("value");
  86. write_timeout = value;
  87. }
  88. }
  89. protected Exception GetException (Exception e)
  90. {
  91. e = HttpWebRequest.FlattenException (e);
  92. if (e is WebException)
  93. return e;
  94. if (Operation.Aborted || e is OperationCanceledException || e is ObjectDisposedException)
  95. return HttpWebRequest.CreateRequestAbortedException ();
  96. return e;
  97. }
  98. public override int Read (byte[] buffer, int offset, int count)
  99. {
  100. if (!CanRead)
  101. throw new NotSupportedException (SR.net_writeonlystream);
  102. Operation.ThrowIfClosedOrDisposed ();
  103. if (buffer == null)
  104. throw new ArgumentNullException (nameof (buffer));
  105. int length = buffer.Length;
  106. if (offset < 0 || length < offset)
  107. throw new ArgumentOutOfRangeException (nameof (offset));
  108. if (count < 0 || (length - offset) < count)
  109. throw new ArgumentOutOfRangeException (nameof (count));
  110. try {
  111. return ReadAsync (buffer, offset, count, CancellationToken.None).Result;
  112. } catch (Exception e) {
  113. throw GetException (e);
  114. }
  115. }
  116. public override IAsyncResult BeginRead (byte[] buffer, int offset, int count,
  117. AsyncCallback cb, object state)
  118. {
  119. if (!CanRead)
  120. throw new NotSupportedException (SR.net_writeonlystream);
  121. Operation.ThrowIfClosedOrDisposed ();
  122. if (buffer == null)
  123. throw new ArgumentNullException (nameof (buffer));
  124. int length = buffer.Length;
  125. if (offset < 0 || length < offset)
  126. throw new ArgumentOutOfRangeException (nameof (offset));
  127. if (count < 0 || (length - offset) < count)
  128. throw new ArgumentOutOfRangeException (nameof (count));
  129. var task = ReadAsync (buffer, offset, count, CancellationToken.None);
  130. return TaskToApm.Begin (task, cb, state);
  131. }
  132. public override int EndRead (IAsyncResult r)
  133. {
  134. if (r == null)
  135. throw new ArgumentNullException (nameof (r));
  136. try {
  137. return TaskToApm.End<int> (r);
  138. } catch (Exception e) {
  139. throw GetException (e);
  140. }
  141. }
  142. public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count,
  143. AsyncCallback cb, object state)
  144. {
  145. if (buffer == null)
  146. throw new ArgumentNullException (nameof (buffer));
  147. int length = buffer.Length;
  148. if (offset < 0 || length < offset)
  149. throw new ArgumentOutOfRangeException (nameof (offset));
  150. if (count < 0 || (length - offset) < count)
  151. throw new ArgumentOutOfRangeException (nameof (count));
  152. if (!CanWrite)
  153. throw new NotSupportedException (SR.net_readonlystream);
  154. Operation.ThrowIfClosedOrDisposed ();
  155. var task = WriteAsync (buffer, offset, count, CancellationToken.None);
  156. return TaskToApm.Begin (task, cb, state);
  157. }
  158. public override void EndWrite (IAsyncResult r)
  159. {
  160. if (r == null)
  161. throw new ArgumentNullException (nameof (r));
  162. try {
  163. TaskToApm.End (r);
  164. } catch (Exception e) {
  165. throw GetException (e);
  166. }
  167. }
  168. public override void Write (byte[] buffer, int offset, int count)
  169. {
  170. if (buffer == null)
  171. throw new ArgumentNullException (nameof (buffer));
  172. int length = buffer.Length;
  173. if (offset < 0 || length < offset)
  174. throw new ArgumentOutOfRangeException (nameof (offset));
  175. if (count < 0 || (length - offset) < count)
  176. throw new ArgumentOutOfRangeException (nameof (count));
  177. if (!CanWrite)
  178. throw new NotSupportedException (SR.net_readonlystream);
  179. Operation.ThrowIfClosedOrDisposed ();
  180. try {
  181. WriteAsync (buffer, offset, count).Wait ();
  182. } catch (Exception e) {
  183. throw GetException (e);
  184. }
  185. }
  186. public override void Flush ()
  187. {
  188. }
  189. public override Task FlushAsync (CancellationToken cancellationToken)
  190. {
  191. return cancellationToken.IsCancellationRequested ?
  192. Task.FromCancellation (cancellationToken) :
  193. Task.CompletedTask;
  194. }
  195. internal void InternalClose ()
  196. {
  197. disposed = true;
  198. }
  199. protected abstract void Close_internal (ref bool disposed);
  200. public override void Close ()
  201. {
  202. Close_internal (ref disposed);
  203. }
  204. public override long Seek (long a, SeekOrigin b)
  205. {
  206. throw new NotSupportedException (SR.net_noseek);
  207. }
  208. public override void SetLength (long a)
  209. {
  210. throw new NotSupportedException (SR.net_noseek);
  211. }
  212. public override bool CanSeek => false;
  213. public override long Length => throw new NotSupportedException (SR.net_noseek);
  214. public override long Position {
  215. get { throw new NotSupportedException (SR.net_noseek); }
  216. set { throw new NotSupportedException (SR.net_noseek); }
  217. }
  218. }
  219. }