2 // System.Net.RequestStream
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
7 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Net.Sockets;
30 using System.Runtime.InteropServices;
31 namespace System.Net {
32 class RequestStream : Stream
41 internal RequestStream (Stream stream, byte [] buffer, int offset, int length)
42 : this (stream, buffer, offset, length, -1)
46 internal RequestStream (Stream stream, byte [] buffer, int offset, int length, long contentlength)
52 this.remaining_body = contentlength;
55 public override bool CanRead {
59 public override bool CanSeek {
63 public override bool CanWrite {
67 public override long Length {
68 get { throw new NotSupportedException (); }
71 public override long Position {
72 get { throw new NotSupportedException (); }
73 set { throw new NotSupportedException (); }
77 public override void Close ()
82 public override void Flush ()
87 // Returns 0 if we can keep reading from the base stream,
88 // > 0 if we read something from the buffer.
89 // -1 if we had a content length set and we finished reading that many bytes.
90 int FillFromBuffer (byte [] buffer, int off, int count)
93 throw new ArgumentNullException ("buffer");
95 throw new ArgumentOutOfRangeException ("offset", "< 0");
97 throw new ArgumentOutOfRangeException ("count", "< 0");
98 int len = buffer.Length;
100 throw new ArgumentException ("destination offset is beyond array size");
101 if (off > len - count)
102 throw new ArgumentException ("Reading would overrun buffer");
104 if (this.remaining_body == 0)
107 if (this.length == 0)
110 int size = Math.Min (this.length, count);
111 if (this.remaining_body > 0)
112 size = (int) Math.Min (size, this.remaining_body);
114 if (this.offset > this.buffer.Length - size) {
115 size = Math.Min (size, this.buffer.Length - this.offset);
120 Buffer.BlockCopy (this.buffer, this.offset, buffer, off, size);
123 if (this.remaining_body > 0)
124 remaining_body -= size;
128 public override int Read ([In,Out] byte[] buffer, int offset, int count)
131 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
133 // Call FillFromBuffer to check for buffer boundaries even when remaining_body is 0
134 int nread = FillFromBuffer (buffer, offset, count);
135 if (nread == -1) { // No more bytes available (Content-Length)
137 } else if (nread > 0) {
141 nread = stream.Read (buffer, offset, count);
142 if (nread > 0 && remaining_body > 0)
143 remaining_body -= nread;
147 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
148 AsyncCallback cback, object state)
151 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
153 int nread = FillFromBuffer (buffer, offset, count);
154 if (nread > 0 || nread == -1) {
155 HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
156 ares.Buffer = buffer;
157 ares.Offset = offset;
159 ares.Callback = cback;
161 ares.SynchRead = Math.Max (0, nread);
166 // Avoid reading past the end of the request to allow
167 // for HTTP pipelining
168 if (remaining_body >= 0 && count > remaining_body)
169 count = (int) Math.Min (Int32.MaxValue, remaining_body);
170 return stream.BeginRead (buffer, offset, count, cback, state);
173 public override int EndRead (IAsyncResult ares)
176 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
179 throw new ArgumentNullException ("async_result");
181 if (ares is HttpStreamAsyncResult) {
182 HttpStreamAsyncResult r = (HttpStreamAsyncResult) ares;
183 if (!ares.IsCompleted)
184 ares.AsyncWaitHandle.WaitOne ();
188 // Close on exception?
189 int nread = stream.EndRead (ares);
190 if (remaining_body > 0 && nread > 0)
191 remaining_body -= nread;
195 public override long Seek (long offset, SeekOrigin origin)
197 throw new NotSupportedException ();
200 public override void SetLength (long value)
202 throw new NotSupportedException ();
205 public override void Write (byte[] buffer, int offset, int count)
207 throw new NotSupportedException ();
210 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
211 AsyncCallback cback, object state)
213 throw new NotSupportedException ();
216 public override void EndWrite (IAsyncResult async_result)
218 throw new NotSupportedException ();