2
0

RequestStream.cs 5.7 KB

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