HttpRequestStream.Managed.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.ExceptionServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SocketHttpListener.Net
  8. {
  9. // Licensed to the .NET Foundation under one or more agreements.
  10. // See the LICENSE file in the project root for more information.
  11. //
  12. // System.Net.ResponseStream
  13. //
  14. // Author:
  15. // Gonzalo Paniagua Javier (gonzalo@novell.com)
  16. //
  17. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  18. //
  19. // Permission is hereby granted, free of charge, to any person obtaining
  20. // a copy of this software and associated documentation files (the
  21. // "Software"), to deal in the Software without restriction, including
  22. // without limitation the rights to use, copy, modify, merge, publish,
  23. // distribute, sublicense, and/or sell copies of the Software, and to
  24. // permit persons to whom the Software is furnished to do so, subject to
  25. // the following conditions:
  26. //
  27. // The above copyright notice and this permission notice shall be
  28. // included in all copies or substantial portions of the Software.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  34. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  35. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. //
  38. internal partial class HttpRequestStream : Stream
  39. {
  40. private byte[] _buffer;
  41. private int _offset;
  42. private int _length;
  43. private long _remainingBody;
  44. protected bool _closed;
  45. private Stream _stream;
  46. internal HttpRequestStream(Stream stream, byte[] buffer, int offset, int length)
  47. : this(stream, buffer, offset, length, -1)
  48. {
  49. }
  50. internal HttpRequestStream(Stream stream, byte[] buffer, int offset, int length, long contentlength)
  51. {
  52. _stream = stream;
  53. _buffer = buffer;
  54. _offset = offset;
  55. _length = length;
  56. _remainingBody = contentlength;
  57. }
  58. // Returns 0 if we can keep reading from the base stream,
  59. // > 0 if we read something from the buffer.
  60. // -1 if we had a content length set and we finished reading that many bytes.
  61. private int FillFromBuffer(byte[] buffer, int offset, int count)
  62. {
  63. if (_remainingBody == 0)
  64. return -1;
  65. if (_length == 0)
  66. return 0;
  67. int size = Math.Min(_length, count);
  68. if (_remainingBody > 0)
  69. size = (int)Math.Min(size, _remainingBody);
  70. if (_offset > _buffer.Length - size)
  71. {
  72. size = Math.Min(size, _buffer.Length - _offset);
  73. }
  74. if (size == 0)
  75. return 0;
  76. Buffer.BlockCopy(_buffer, _offset, buffer, offset, size);
  77. _offset += size;
  78. _length -= size;
  79. if (_remainingBody > 0)
  80. _remainingBody -= size;
  81. return size;
  82. }
  83. protected virtual int ReadCore(byte[] buffer, int offset, int size)
  84. {
  85. // Call FillFromBuffer to check for buffer boundaries even when remaining_body is 0
  86. int nread = FillFromBuffer(buffer, offset, size);
  87. if (nread == -1)
  88. { // No more bytes available (Content-Length)
  89. return 0;
  90. }
  91. else if (nread > 0)
  92. {
  93. return nread;
  94. }
  95. if (_remainingBody > 0)
  96. {
  97. size = (int)Math.Min(_remainingBody, (long)size);
  98. }
  99. nread = _stream.Read(buffer, offset, size);
  100. if (_remainingBody > 0)
  101. {
  102. if (nread == 0)
  103. {
  104. throw new Exception("Bad request");
  105. }
  106. //Debug.Assert(nread <= _remainingBody);
  107. _remainingBody -= nread;
  108. }
  109. return nread;
  110. }
  111. protected virtual IAsyncResult BeginReadCore(byte[] buffer, int offset, int size, AsyncCallback cback, object state)
  112. {
  113. if (size == 0 || _closed)
  114. {
  115. HttpStreamAsyncResult ares = new HttpStreamAsyncResult(this);
  116. ares._callback = cback;
  117. ares._state = state;
  118. ares.Complete();
  119. return ares;
  120. }
  121. int nread = FillFromBuffer(buffer, offset, size);
  122. if (nread > 0 || nread == -1)
  123. {
  124. HttpStreamAsyncResult ares = new HttpStreamAsyncResult(this);
  125. ares._buffer = buffer;
  126. ares._offset = offset;
  127. ares._count = size;
  128. ares._callback = cback;
  129. ares._state = state;
  130. ares._synchRead = Math.Max(0, nread);
  131. ares.Complete();
  132. return ares;
  133. }
  134. // Avoid reading past the end of the request to allow
  135. // for HTTP pipelining
  136. if (_remainingBody >= 0 && size > _remainingBody)
  137. {
  138. size = (int)Math.Min(_remainingBody, (long)size);
  139. }
  140. return _stream.BeginRead(buffer, offset, size, cback, state);
  141. }
  142. public override int EndRead(IAsyncResult asyncResult)
  143. {
  144. if (asyncResult == null)
  145. throw new ArgumentNullException(nameof(asyncResult));
  146. var r = asyncResult as HttpStreamAsyncResult;
  147. if (r != null)
  148. {
  149. if (!ReferenceEquals(this, r._parent))
  150. {
  151. throw new ArgumentException("Invalid async result");
  152. }
  153. if (r._endCalled)
  154. {
  155. throw new InvalidOperationException("invalid end call");
  156. }
  157. r._endCalled = true;
  158. if (!asyncResult.IsCompleted)
  159. {
  160. asyncResult.AsyncWaitHandle.WaitOne();
  161. }
  162. return r._synchRead;
  163. }
  164. if (_closed)
  165. return 0;
  166. int nread = 0;
  167. try
  168. {
  169. nread = _stream.EndRead(asyncResult);
  170. }
  171. catch (IOException e) when (e.InnerException is ArgumentException || e.InnerException is InvalidOperationException)
  172. {
  173. throw e.InnerException;
  174. }
  175. if (_remainingBody > 0)
  176. {
  177. if (nread == 0)
  178. {
  179. throw new Exception("Bad request");
  180. }
  181. _remainingBody -= nread;
  182. }
  183. return nread;
  184. }
  185. }
  186. }