ChunkedRequestStream.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #region License
  2. /*
  3. * ChunkedRequestStream.cs
  4. *
  5. * This code is derived from ChunkedInputStream.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 ChunkedRequestStream : RequestStream
  43. {
  44. #region Private Fields
  45. private static readonly int _bufferLength;
  46. private HttpListenerContext _context;
  47. private ChunkStream _decoder;
  48. private bool _disposed;
  49. private bool _noMoreData;
  50. #endregion
  51. #region Static Constructor
  52. static ChunkedRequestStream ()
  53. {
  54. _bufferLength = 8192;
  55. }
  56. #endregion
  57. #region Internal Constructors
  58. internal ChunkedRequestStream (
  59. Stream innerStream,
  60. byte[] initialBuffer,
  61. int offset,
  62. int count,
  63. HttpListenerContext context
  64. )
  65. : base (innerStream, initialBuffer, offset, count, -1)
  66. {
  67. _context = context;
  68. _decoder = new ChunkStream (
  69. (WebHeaderCollection) context.Request.Headers
  70. );
  71. }
  72. #endregion
  73. #region Internal Properties
  74. internal bool HasRemainingBuffer {
  75. get {
  76. return _decoder.Count + Count > 0;
  77. }
  78. }
  79. internal byte[] RemainingBuffer {
  80. get {
  81. using (var buff = new MemoryStream ()) {
  82. var cnt = _decoder.Count;
  83. if (cnt > 0)
  84. buff.Write (_decoder.EndBuffer, _decoder.Offset, cnt);
  85. cnt = Count;
  86. if (cnt > 0)
  87. buff.Write (InitialBuffer, Offset, cnt);
  88. buff.Close ();
  89. return buff.ToArray ();
  90. }
  91. }
  92. }
  93. #endregion
  94. #region Private Methods
  95. private void onRead (IAsyncResult asyncResult)
  96. {
  97. var rstate = (ReadBufferState) asyncResult.AsyncState;
  98. var ares = rstate.AsyncResult;
  99. try {
  100. var nread = base.EndRead (asyncResult);
  101. _decoder.Write (ares.Buffer, ares.Offset, nread);
  102. nread = _decoder.Read (rstate.Buffer, rstate.Offset, rstate.Count);
  103. rstate.Offset += nread;
  104. rstate.Count -= nread;
  105. if (rstate.Count == 0 || !_decoder.WantsMore || nread == 0) {
  106. _noMoreData = !_decoder.WantsMore && nread == 0;
  107. ares.Count = rstate.InitialCount - rstate.Count;
  108. ares.Complete ();
  109. return;
  110. }
  111. base.BeginRead (ares.Buffer, ares.Offset, ares.Count, onRead, rstate);
  112. }
  113. catch (Exception ex) {
  114. _context.ErrorMessage = "I/O operation aborted";
  115. _context.SendError ();
  116. ares.Complete (ex);
  117. }
  118. }
  119. #endregion
  120. #region Public Methods
  121. public override IAsyncResult BeginRead (
  122. byte[] buffer, int offset, int count, AsyncCallback callback, object state
  123. )
  124. {
  125. if (_disposed) {
  126. var name = GetType ().ToString ();
  127. throw new ObjectDisposedException (name);
  128. }
  129. if (buffer == null)
  130. throw new ArgumentNullException ("buffer");
  131. if (offset < 0) {
  132. var msg = "A negative value.";
  133. throw new ArgumentOutOfRangeException ("offset", msg);
  134. }
  135. if (count < 0) {
  136. var msg = "A negative value.";
  137. throw new ArgumentOutOfRangeException ("count", msg);
  138. }
  139. var len = buffer.Length;
  140. if (offset + count > len) {
  141. var msg = "The sum of 'offset' and 'count' is greater than the length of 'buffer'.";
  142. throw new ArgumentException (msg);
  143. }
  144. var ares = new HttpStreamAsyncResult (callback, state);
  145. if (_noMoreData) {
  146. ares.Complete ();
  147. return ares;
  148. }
  149. var nread = _decoder.Read (buffer, offset, count);
  150. offset += nread;
  151. count -= nread;
  152. if (count == 0) {
  153. ares.Count = nread;
  154. ares.Complete ();
  155. return ares;
  156. }
  157. if (!_decoder.WantsMore) {
  158. _noMoreData = nread == 0;
  159. ares.Count = nread;
  160. ares.Complete ();
  161. return ares;
  162. }
  163. ares.Buffer = new byte[_bufferLength];
  164. ares.Offset = 0;
  165. ares.Count = _bufferLength;
  166. var rstate = new ReadBufferState (buffer, offset, count, ares);
  167. rstate.InitialCount += nread;
  168. base.BeginRead (ares.Buffer, ares.Offset, ares.Count, onRead, rstate);
  169. return ares;
  170. }
  171. public override void Close ()
  172. {
  173. if (_disposed)
  174. return;
  175. base.Close ();
  176. _disposed = true;
  177. }
  178. public override int EndRead (IAsyncResult asyncResult)
  179. {
  180. if (_disposed) {
  181. var name = GetType ().ToString ();
  182. throw new ObjectDisposedException (name);
  183. }
  184. if (asyncResult == null)
  185. throw new ArgumentNullException ("asyncResult");
  186. var ares = asyncResult as HttpStreamAsyncResult;
  187. if (ares == null) {
  188. var msg = "A wrong IAsyncResult instance.";
  189. throw new ArgumentException (msg, "asyncResult");
  190. }
  191. if (!ares.IsCompleted)
  192. ares.AsyncWaitHandle.WaitOne ();
  193. if (ares.HasException) {
  194. var msg = "The I/O operation has been aborted.";
  195. throw new HttpListenerException (995, msg);
  196. }
  197. return ares.Count;
  198. }
  199. public override int Read (byte[] buffer, int offset, int count)
  200. {
  201. var ares = BeginRead (buffer, offset, count, null, null);
  202. return EndRead (ares);
  203. }
  204. #endregion
  205. }
  206. }