/mcs/class/System.Runtime.Remoting/MonoHttp/RequestStream.cs

https://github.com/gustavo-melo/mono · C# · 226 lines · 158 code · 34 blank · 34 comment · 35 complexity · 49d7d465ff698d48da9ecb9bd4497110 MD5 · raw file

  1. #define EMBEDDED_IN_1_0
  2. //
  3. // System.Net.RequestStream
  4. //
  5. // Author:
  6. // Gonzalo Paniagua Javier (gonzalo@novell.com)
  7. //
  8. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if EMBEDDED_IN_1_0
  30. using System.IO;
  31. using System.Net.Sockets;
  32. using System.Runtime.InteropServices;
  33. using System; using System.Net; namespace MonoHttp {
  34. class RequestStream : Stream
  35. {
  36. byte [] buffer;
  37. int offset;
  38. int length;
  39. long remaining_body;
  40. bool disposed;
  41. Stream stream;
  42. internal RequestStream (Stream stream, byte [] buffer, int offset, int length)
  43. : this (stream, buffer, offset, length, -1)
  44. {
  45. }
  46. internal RequestStream (Stream stream, byte [] buffer, int offset, int length, long contentlength)
  47. {
  48. this.stream = stream;
  49. this.buffer = buffer;
  50. this.offset = offset;
  51. this.length = length;
  52. this.remaining_body = contentlength;
  53. }
  54. public override bool CanRead {
  55. get { return true; }
  56. }
  57. public override bool CanSeek {
  58. get { return false; }
  59. }
  60. public override bool CanWrite {
  61. get { return false; }
  62. }
  63. public override long Length {
  64. get { throw new NotSupportedException (); }
  65. }
  66. public override long Position {
  67. get { throw new NotSupportedException (); }
  68. set { throw new NotSupportedException (); }
  69. }
  70. public override void Close ()
  71. {
  72. disposed = true;
  73. }
  74. public override void Flush ()
  75. {
  76. }
  77. // Returns 0 if we can keep reading from the base stream,
  78. // > 0 if we read something from the buffer.
  79. // -1 if we had a content length set and we finished reading that many bytes.
  80. int FillFromBuffer (byte [] buffer, int off, int count)
  81. {
  82. if (buffer == null)
  83. throw new ArgumentNullException ("buffer");
  84. if (off < 0)
  85. throw new ArgumentOutOfRangeException ("offset", "< 0");
  86. if (count < 0)
  87. throw new ArgumentOutOfRangeException ("count", "< 0");
  88. int len = buffer.Length;
  89. if (off > len)
  90. throw new ArgumentException ("destination offset is beyond array size");
  91. if (off > len - count)
  92. throw new ArgumentException ("Reading would overrun buffer");
  93. if (this.remaining_body == 0)
  94. return -1;
  95. if (this.length == 0)
  96. return 0;
  97. int size = Math.Min (this.length, count);
  98. if (this.remaining_body > 0)
  99. size = (int) Math.Min (size, this.remaining_body);
  100. if (this.offset > this.buffer.Length - size) {
  101. size = Math.Min (size, this.buffer.Length - this.offset);
  102. }
  103. if (size == 0)
  104. return 0;
  105. Buffer.BlockCopy (this.buffer, this.offset, buffer, off, size);
  106. this.offset += size;
  107. this.length -= size;
  108. if (this.remaining_body > 0)
  109. remaining_body -= size;
  110. return size;
  111. }
  112. public override int Read ([In,Out] byte[] buffer, int offset, int count)
  113. {
  114. if (disposed)
  115. throw new ObjectDisposedException (typeof (RequestStream).ToString ());
  116. // Call FillFromBuffer to check for buffer boundaries even when remaining_body is 0
  117. int nread = FillFromBuffer (buffer, offset, count);
  118. if (nread == -1) { // No more bytes available (Content-Length)
  119. return 0;
  120. } else if (nread > 0) {
  121. return nread;
  122. }
  123. nread = stream.Read (buffer, offset, count);
  124. if (nread > 0 && remaining_body > 0)
  125. remaining_body -= nread;
  126. return nread;
  127. }
  128. public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
  129. AsyncCallback cback, object state)
  130. {
  131. if (disposed)
  132. throw new ObjectDisposedException (typeof (RequestStream).ToString ());
  133. int nread = FillFromBuffer (buffer, offset, count);
  134. if (nread > 0 || nread == -1) {
  135. HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
  136. ares.Buffer = buffer;
  137. ares.Offset = offset;
  138. ares.Count = count;
  139. ares.Callback = cback;
  140. ares.State = state;
  141. ares.SynchRead = nread;
  142. ares.Complete ();
  143. return ares;
  144. }
  145. // Avoid reading past the end of the request to allow
  146. // for HTTP pipelining
  147. if (remaining_body >= 0 && count > remaining_body)
  148. count = (int) Math.Min (Int32.MaxValue, remaining_body);
  149. return stream.BeginRead (buffer, offset, count, cback, state);
  150. }
  151. public override int EndRead (IAsyncResult ares)
  152. {
  153. if (disposed)
  154. throw new ObjectDisposedException (typeof (RequestStream).ToString ());
  155. if (ares == null)
  156. throw new ArgumentNullException ("async_result");
  157. if (ares is HttpStreamAsyncResult) {
  158. HttpStreamAsyncResult r = (HttpStreamAsyncResult) ares;
  159. if (!ares.IsCompleted)
  160. ares.AsyncWaitHandle.WaitOne ();
  161. return r.SynchRead;
  162. }
  163. // Close on exception?
  164. int nread = stream.EndRead (ares);
  165. if (remaining_body > 0 && nread > 0)
  166. remaining_body -= nread;
  167. return nread;
  168. }
  169. public override long Seek (long offset, SeekOrigin origin)
  170. {
  171. throw new NotSupportedException ();
  172. }
  173. public override void SetLength (long value)
  174. {
  175. throw new NotSupportedException ();
  176. }
  177. public override void Write (byte[] buffer, int offset, int count)
  178. {
  179. throw new NotSupportedException ();
  180. }
  181. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
  182. AsyncCallback cback, object state)
  183. {
  184. throw new NotSupportedException ();
  185. }
  186. public override void EndWrite (IAsyncResult async_result)
  187. {
  188. throw new NotSupportedException ();
  189. }
  190. }
  191. }
  192. #endif