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.
30 using System.Net.Sockets;
31 using System.Runtime.InteropServices;
32 namespace System.Net {
33 class RequestStream : NetworkStream
41 internal RequestStream (Socket sock, byte [] buffer, int offset, int length) :
50 internal RequestStream (Socket sock, byte [] buffer, int offset, int length, long contentlength) :
56 this.available = contentlength;
59 public override bool CanRead {
63 public override bool CanSeek {
67 public override bool CanWrite {
71 public override long Length {
72 get { throw new NotSupportedException (); }
75 public override long Position {
76 get { throw new NotSupportedException (); }
77 set { throw new NotSupportedException (); }
81 public override void Close ()
83 // TODO: What do we close?
86 public override void Flush ()
91 // Returns 0 if we can keep reading from the base stream,
92 // > 0 if we read something from the buffer.
93 // -1 if we had a content length set and we finished reading that many bytes.
94 int FillFromBuffer (byte [] buffer, int off, int count)
97 throw new ArgumentNullException ("buffer");
99 throw new ArgumentOutOfRangeException ("offset", "< 0");
101 throw new ArgumentOutOfRangeException ("count", "< 0");
102 int len = buffer.Length;
104 throw new ArgumentException ("destination offset is beyond array size");
105 if (off > len - count)
106 throw new ArgumentException ("Reading would overrun buffer");
108 if (this.available == 0)
111 if (this.length == 0)
114 int size = Math.Min (this.length, count);
115 if (this.available > 0)
116 size = (int) Math.Min (size, this.available);
117 Buffer.BlockCopy (this.buffer, this.offset, buffer, off, size);
120 if (this.available > 0)
125 public override int Read ([In,Out] byte[] buffer, int offset, int count)
128 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
130 // Avoid reading past the end of the request to allow
131 // for HTTP pipelining
132 int nread = FillFromBuffer (buffer, offset, count);
133 if (nread == -1) // No more bytes available (Content-Length)
138 nread = base.Read (buffer, offset, count);
142 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
143 AsyncCallback cback, object state)
146 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
148 int nread = FillFromBuffer (buffer, offset, count);
149 if (nread > 0 || nread == -1) {
150 HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
151 ares.Buffer = buffer;
152 ares.Offset = offset;
154 ares.Callback = cback;
156 ares.SynchRead = nread;
161 // Avoid reading past the end of the request to allow
162 // for HTTP pipelining
163 if (available != -1 && count > available)
164 count = (int) Math.Min (Int32.MaxValue, available);
165 return base.BeginRead (buffer, offset, count, cback, state);
168 public override int EndRead (IAsyncResult ares)
171 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
174 throw new ArgumentNullException ("async_result");
176 if (ares is HttpStreamAsyncResult) {
177 HttpStreamAsyncResult r = (HttpStreamAsyncResult) ares;
178 if (!ares.IsCompleted)
179 ares.AsyncWaitHandle.WaitOne ();
183 // Close on exception?
184 int nread = base.EndRead (ares);
190 public override long Seek (long offset, SeekOrigin origin)
192 throw new NotSupportedException ();
195 public override void SetLength (long value)
197 throw new NotSupportedException ();
200 public override void Write (byte[] buffer, int offset, int count)
202 throw new NotSupportedException ();
205 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
206 AsyncCallback cback, object state)
208 throw new NotSupportedException ();
211 public override void EndWrite (IAsyncResult async_result)
213 throw new NotSupportedException ();