ChunkedInputStream.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Runtime.InteropServices;
  5. using SocketHttpListener.Primitives;
  6. namespace SocketHttpListener.Net
  7. {
  8. // Licensed to the .NET Foundation under one or more agreements.
  9. // See the LICENSE file in the project root for more information.
  10. //
  11. // System.Net.ResponseStream
  12. //
  13. // Author:
  14. // Gonzalo Paniagua Javier (gonzalo@novell.com)
  15. //
  16. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  17. //
  18. // Permission is hereby granted, free of charge, to any person obtaining
  19. // a copy of this software and associated documentation files (the
  20. // "Software"), to deal in the Software without restriction, including
  21. // without limitation the rights to use, copy, modify, merge, publish,
  22. // distribute, sublicense, and/or sell copies of the Software, and to
  23. // permit persons to whom the Software is furnished to do so, subject to
  24. // the following conditions:
  25. //
  26. // The above copyright notice and this permission notice shall be
  27. // included in all copies or substantial portions of the Software.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  33. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  34. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  35. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. //
  37. internal sealed class ChunkedInputStream : HttpRequestStream
  38. {
  39. private ChunkStream _decoder;
  40. private readonly HttpListenerContext _context;
  41. private bool _no_more_data;
  42. private class ReadBufferState
  43. {
  44. public byte[] Buffer;
  45. public int Offset;
  46. public int Count;
  47. public int InitialCount;
  48. public HttpStreamAsyncResult Ares;
  49. public ReadBufferState(byte[] buffer, int offset, int count, HttpStreamAsyncResult ares)
  50. {
  51. Buffer = buffer;
  52. Offset = offset;
  53. Count = count;
  54. InitialCount = count;
  55. Ares = ares;
  56. }
  57. }
  58. public ChunkedInputStream(HttpListenerContext context, Stream stream, byte[] buffer, int offset, int length)
  59. : base(stream, buffer, offset, length)
  60. {
  61. _context = context;
  62. WebHeaderCollection coll = (WebHeaderCollection)context.Request.Headers;
  63. _decoder = new ChunkStream(coll);
  64. }
  65. public ChunkStream Decoder
  66. {
  67. get { return _decoder; }
  68. set { _decoder = value; }
  69. }
  70. protected override int ReadCore(byte[] buffer, int offset, int count)
  71. {
  72. IAsyncResult ares = BeginReadCore(buffer, offset, count, null, null);
  73. return EndRead(ares);
  74. }
  75. protected override IAsyncResult BeginReadCore(byte[] buffer, int offset, int size, AsyncCallback cback, object state)
  76. {
  77. HttpStreamAsyncResult ares = new HttpStreamAsyncResult(this);
  78. ares._callback = cback;
  79. ares._state = state;
  80. if (_no_more_data || size == 0 || _closed)
  81. {
  82. ares.Complete();
  83. return ares;
  84. }
  85. int nread = _decoder.Read(buffer, offset, size);
  86. offset += nread;
  87. size -= nread;
  88. if (size == 0)
  89. {
  90. // got all we wanted, no need to bother the decoder yet
  91. ares._count = nread;
  92. ares.Complete();
  93. return ares;
  94. }
  95. if (!_decoder.WantMore)
  96. {
  97. _no_more_data = nread == 0;
  98. ares._count = nread;
  99. ares.Complete();
  100. return ares;
  101. }
  102. ares._buffer = new byte[8192];
  103. ares._offset = 0;
  104. ares._count = 8192;
  105. ReadBufferState rb = new ReadBufferState(buffer, offset, size, ares);
  106. rb.InitialCount += nread;
  107. base.BeginReadCore(ares._buffer, ares._offset, ares._count, OnRead, rb);
  108. return ares;
  109. }
  110. private void OnRead(IAsyncResult base_ares)
  111. {
  112. ReadBufferState rb = (ReadBufferState)base_ares.AsyncState;
  113. HttpStreamAsyncResult ares = rb.Ares;
  114. try
  115. {
  116. int nread = base.EndRead(base_ares);
  117. if (nread == 0)
  118. {
  119. _no_more_data = true;
  120. ares._count = rb.InitialCount - rb.Count;
  121. ares.Complete();
  122. return;
  123. }
  124. _decoder.Write(ares._buffer, ares._offset, nread);
  125. nread = _decoder.Read(rb.Buffer, rb.Offset, rb.Count);
  126. rb.Offset += nread;
  127. rb.Count -= nread;
  128. if (rb.Count == 0 || !_decoder.WantMore)
  129. {
  130. _no_more_data = !_decoder.WantMore && nread == 0;
  131. ares._count = rb.InitialCount - rb.Count;
  132. ares.Complete();
  133. return;
  134. }
  135. ares._offset = 0;
  136. ares._count = Math.Min(8192, _decoder.ChunkLeft + 6);
  137. base.BeginReadCore(ares._buffer, ares._offset, ares._count, OnRead, rb);
  138. }
  139. catch (Exception e)
  140. {
  141. _context.Connection.SendError(e.Message, 400);
  142. ares.Complete(e);
  143. }
  144. }
  145. public override int EndRead(IAsyncResult asyncResult)
  146. {
  147. if (asyncResult == null)
  148. throw new ArgumentNullException(nameof(asyncResult));
  149. HttpStreamAsyncResult ares = asyncResult as HttpStreamAsyncResult;
  150. if (ares == null || !ReferenceEquals(this, ares._parent))
  151. {
  152. throw new ArgumentException("Invalid async result");
  153. }
  154. if (ares._endCalled)
  155. {
  156. throw new InvalidOperationException("Invalid end call");
  157. }
  158. ares._endCalled = true;
  159. if (!asyncResult.IsCompleted)
  160. asyncResult.AsyncWaitHandle.WaitOne();
  161. if (ares._error != null)
  162. throw new HttpListenerException((int)HttpStatusCode.BadRequest, "Operation aborted");
  163. return ares._count;
  164. }
  165. }
  166. }