HttpRequestStream.Managed.cs 6.9 KB

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