ChunkedInputStream.cs 6.4 KB

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