HttpRequestStream.Managed.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. nread = _stream.Read(buffer, offset, size);
  97. if (nread > 0 && _remainingBody > 0)
  98. _remainingBody -= nread;
  99. return nread;
  100. }
  101. protected virtual IAsyncResult BeginReadCore(byte[] buffer, int offset, int size, AsyncCallback cback, object state)
  102. {
  103. if (size == 0 || _closed)
  104. {
  105. HttpStreamAsyncResult ares = new HttpStreamAsyncResult(this);
  106. ares._callback = cback;
  107. ares._state = state;
  108. ares.Complete();
  109. return ares;
  110. }
  111. int nread = FillFromBuffer(buffer, offset, size);
  112. if (nread > 0 || nread == -1)
  113. {
  114. HttpStreamAsyncResult ares = new HttpStreamAsyncResult(this);
  115. ares._buffer = buffer;
  116. ares._offset = offset;
  117. ares._count = size;
  118. ares._callback = cback;
  119. ares._state = state;
  120. ares._synchRead = Math.Max(0, nread);
  121. ares.Complete();
  122. return ares;
  123. }
  124. // Avoid reading past the end of the request to allow
  125. // for HTTP pipelining
  126. if (_remainingBody >= 0 && size > _remainingBody)
  127. {
  128. size = (int)Math.Min(int.MaxValue, _remainingBody);
  129. }
  130. return _stream.BeginRead(buffer, offset, size, cback, state);
  131. }
  132. public override int EndRead(IAsyncResult asyncResult)
  133. {
  134. if (asyncResult == null)
  135. throw new ArgumentNullException(nameof(asyncResult));
  136. var r = asyncResult as HttpStreamAsyncResult;
  137. if (r != null)
  138. {
  139. if (!ReferenceEquals(this, r._parent))
  140. {
  141. throw new ArgumentException("Invalid async result");
  142. }
  143. if (r._endCalled)
  144. {
  145. throw new InvalidOperationException("Invalid end call");
  146. }
  147. r._endCalled = true;
  148. if (!asyncResult.IsCompleted)
  149. {
  150. asyncResult.AsyncWaitHandle.WaitOne();
  151. }
  152. return r._synchRead;
  153. }
  154. if (_closed)
  155. return 0;
  156. int nread = 0;
  157. try
  158. {
  159. nread = _stream.EndRead(asyncResult);
  160. }
  161. catch (IOException e) when (e.InnerException is ArgumentException || e.InnerException is InvalidOperationException)
  162. {
  163. throw e.InnerException;
  164. }
  165. if (_remainingBody > 0 && nread > 0)
  166. {
  167. _remainingBody -= nread;
  168. }
  169. return nread;
  170. }
  171. }
  172. }