RequestStream.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #region License
  2. /*
  3. * RequestStream.cs
  4. *
  5. * This code is derived from RequestStream.cs (System.Net) of Mono
  6. * (http://www.mono-project.com).
  7. *
  8. * The MIT License
  9. *
  10. * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  11. * Copyright (c) 2012-2022 sta.blockhead
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. #endregion
  32. #region Authors
  33. /*
  34. * Authors:
  35. * - Gonzalo Paniagua Javier <gonzalo@novell.com>
  36. */
  37. #endregion
  38. using System;
  39. using System.IO;
  40. namespace WebSocketSharp.Net
  41. {
  42. internal class RequestStream : Stream
  43. {
  44. #region Private Fields
  45. private long _bodyLeft;
  46. private int _count;
  47. private bool _disposed;
  48. private byte[] _initialBuffer;
  49. private Stream _innerStream;
  50. private int _offset;
  51. #endregion
  52. #region Internal Constructors
  53. internal RequestStream (
  54. Stream innerStream,
  55. byte[] initialBuffer,
  56. int offset,
  57. int count,
  58. long contentLength
  59. )
  60. {
  61. _innerStream = innerStream;
  62. _initialBuffer = initialBuffer;
  63. _offset = offset;
  64. _count = count;
  65. _bodyLeft = contentLength;
  66. }
  67. #endregion
  68. #region Internal Properties
  69. internal int Count {
  70. get {
  71. return _count;
  72. }
  73. }
  74. internal byte[] InitialBuffer {
  75. get {
  76. return _initialBuffer;
  77. }
  78. }
  79. internal int Offset {
  80. get {
  81. return _offset;
  82. }
  83. }
  84. #endregion
  85. #region Public Properties
  86. public override bool CanRead {
  87. get {
  88. return true;
  89. }
  90. }
  91. public override bool CanSeek {
  92. get {
  93. return false;
  94. }
  95. }
  96. public override bool CanWrite {
  97. get {
  98. return false;
  99. }
  100. }
  101. public override long Length {
  102. get {
  103. throw new NotSupportedException ();
  104. }
  105. }
  106. public override long Position {
  107. get {
  108. throw new NotSupportedException ();
  109. }
  110. set {
  111. throw new NotSupportedException ();
  112. }
  113. }
  114. #endregion
  115. #region Private Methods
  116. private int fillFromInitialBuffer (byte[] buffer, int offset, int count)
  117. {
  118. // This method returns a int:
  119. // - > 0 The number of bytes read from the initial buffer
  120. // - 0 No more bytes read from the initial buffer
  121. // - -1 No more content data
  122. if (_bodyLeft == 0)
  123. return -1;
  124. if (_count == 0)
  125. return 0;
  126. if (count > _count)
  127. count = _count;
  128. if (_bodyLeft > 0 && _bodyLeft < count)
  129. count = (int) _bodyLeft;
  130. Buffer.BlockCopy (_initialBuffer, _offset, buffer, offset, count);
  131. _offset += count;
  132. _count -= count;
  133. if (_bodyLeft > 0)
  134. _bodyLeft -= count;
  135. return count;
  136. }
  137. #endregion
  138. #region Public Methods
  139. public override IAsyncResult BeginRead (
  140. byte[] buffer, int offset, int count, AsyncCallback callback, object state
  141. )
  142. {
  143. if (_disposed) {
  144. var name = GetType ().ToString ();
  145. throw new ObjectDisposedException (name);
  146. }
  147. if (buffer == null)
  148. throw new ArgumentNullException ("buffer");
  149. if (offset < 0) {
  150. var msg = "A negative value.";
  151. throw new ArgumentOutOfRangeException ("offset", msg);
  152. }
  153. if (count < 0) {
  154. var msg = "A negative value.";
  155. throw new ArgumentOutOfRangeException ("count", msg);
  156. }
  157. var len = buffer.Length;
  158. if (offset + count > len) {
  159. var msg = "The sum of 'offset' and 'count' is greater than the length of 'buffer'.";
  160. throw new ArgumentException (msg);
  161. }
  162. if (count == 0)
  163. return _innerStream.BeginRead (buffer, offset, 0, callback, state);
  164. var nread = fillFromInitialBuffer (buffer, offset, count);
  165. if (nread != 0) {
  166. var ares = new HttpStreamAsyncResult (callback, state);
  167. ares.Buffer = buffer;
  168. ares.Offset = offset;
  169. ares.Count = count;
  170. ares.SyncRead = nread > 0 ? nread : 0;
  171. ares.Complete ();
  172. return ares;
  173. }
  174. if (_bodyLeft > 0 && _bodyLeft < count)
  175. count = (int) _bodyLeft;
  176. return _innerStream.BeginRead (buffer, offset, count, callback, state);
  177. }
  178. public override IAsyncResult BeginWrite (
  179. byte[] buffer, int offset, int count, AsyncCallback callback, object state
  180. )
  181. {
  182. throw new NotSupportedException ();
  183. }
  184. public override void Close ()
  185. {
  186. _disposed = true;
  187. }
  188. public override int EndRead (IAsyncResult asyncResult)
  189. {
  190. if (_disposed) {
  191. var name = GetType ().ToString ();
  192. throw new ObjectDisposedException (name);
  193. }
  194. if (asyncResult == null)
  195. throw new ArgumentNullException ("asyncResult");
  196. if (asyncResult is HttpStreamAsyncResult) {
  197. var ares = (HttpStreamAsyncResult) asyncResult;
  198. if (!ares.IsCompleted)
  199. ares.AsyncWaitHandle.WaitOne ();
  200. return ares.SyncRead;
  201. }
  202. var nread = _innerStream.EndRead (asyncResult);
  203. if (nread > 0 && _bodyLeft > 0)
  204. _bodyLeft -= nread;
  205. return nread;
  206. }
  207. public override void EndWrite (IAsyncResult asyncResult)
  208. {
  209. throw new NotSupportedException ();
  210. }
  211. public override void Flush ()
  212. {
  213. }
  214. public override int Read (byte[] buffer, int offset, int count)
  215. {
  216. if (_disposed) {
  217. var name = GetType ().ToString ();
  218. throw new ObjectDisposedException (name);
  219. }
  220. if (buffer == null)
  221. throw new ArgumentNullException ("buffer");
  222. if (offset < 0) {
  223. var msg = "A negative value.";
  224. throw new ArgumentOutOfRangeException ("offset", msg);
  225. }
  226. if (count < 0) {
  227. var msg = "A negative value.";
  228. throw new ArgumentOutOfRangeException ("count", msg);
  229. }
  230. var len = buffer.Length;
  231. if (offset + count > len) {
  232. var msg = "The sum of 'offset' and 'count' is greater than the length of 'buffer'.";
  233. throw new ArgumentException (msg);
  234. }
  235. if (count == 0)
  236. return 0;
  237. var nread = fillFromInitialBuffer (buffer, offset, count);
  238. if (nread == -1)
  239. return 0;
  240. if (nread > 0)
  241. return nread;
  242. if (_bodyLeft > 0 && _bodyLeft < count)
  243. count = (int) _bodyLeft;
  244. nread = _innerStream.Read (buffer, offset, count);
  245. if (nread > 0 && _bodyLeft > 0)
  246. _bodyLeft -= nread;
  247. return nread;
  248. }
  249. public override long Seek (long offset, SeekOrigin origin)
  250. {
  251. throw new NotSupportedException ();
  252. }
  253. public override void SetLength (long value)
  254. {
  255. throw new NotSupportedException ();
  256. }
  257. public override void Write (byte[] buffer, int offset, int count)
  258. {
  259. throw new NotSupportedException ();
  260. }
  261. #endregion
  262. }
  263. }