HttpRequestStream.Managed.cs 6.8 KB

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