1 #define EMBEDDED_IN_1_0
4 // System.Net.RequestStream
7 // Gonzalo Paniagua Javier (gonzalo@novell.com)
9 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Net.Sockets;
33 using System.Runtime.InteropServices;
34 using System; using System.Net; namespace MonoHttp {
35 class RequestStream : Stream
44 internal RequestStream (Stream stream, byte [] buffer, int offset, int length)
45 : this (stream, buffer, offset, length, -1)
49 internal RequestStream (Stream stream, byte [] buffer, int offset, int length, long contentlength)
55 this.remaining_body = contentlength;
58 public override bool CanRead {
62 public override bool CanSeek {
66 public override bool CanWrite {
70 public override long Length {
71 get { throw new NotSupportedException (); }
74 public override long Position {
75 get { throw new NotSupportedException (); }
76 set { throw new NotSupportedException (); }
80 public override void Close ()
85 public override void Flush ()
90 // Returns 0 if we can keep reading from the base stream,
91 // > 0 if we read something from the buffer.
92 // -1 if we had a content length set and we finished reading that many bytes.
93 int FillFromBuffer (byte [] buffer, int off, int count)
96 throw new ArgumentNullException ("buffer");
98 throw new ArgumentOutOfRangeException ("offset", "< 0");
100 throw new ArgumentOutOfRangeException ("count", "< 0");
101 int len = buffer.Length;
103 throw new ArgumentException ("destination offset is beyond array size");
104 if (off > len - count)
105 throw new ArgumentException ("Reading would overrun buffer");
107 if (this.remaining_body == 0)
110 if (this.length == 0)
113 int size = Math.Min (this.length, count);
114 if (this.remaining_body > 0)
115 size = (int) Math.Min (size, this.remaining_body);
117 if (this.offset > this.buffer.Length - size) {
118 size = Math.Min (size, this.buffer.Length - this.offset);
123 Buffer.BlockCopy (this.buffer, this.offset, buffer, off, size);
126 if (this.remaining_body > 0)
127 remaining_body -= size;
131 public override int Read ([In,Out] byte[] buffer, int offset, int count)
134 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
136 // Call FillFromBuffer to check for buffer boundaries even when remaining_body is 0
137 int nread = FillFromBuffer (buffer, offset, count);
138 if (nread == -1) { // No more bytes available (Content-Length)
140 } else if (nread > 0) {
144 nread = stream.Read (buffer, offset, count);
145 if (nread > 0 && remaining_body > 0)
146 remaining_body -= nread;
150 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
151 AsyncCallback cback, object state)
154 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
156 int nread = FillFromBuffer (buffer, offset, count);
157 if (nread > 0 || nread == -1) {
158 HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
159 ares.Buffer = buffer;
160 ares.Offset = offset;
162 ares.Callback = cback;
164 ares.SynchRead = nread;
169 // Avoid reading past the end of the request to allow
170 // for HTTP pipelining
171 if (remaining_body >= 0 && count > remaining_body)
172 count = (int) Math.Min (Int32.MaxValue, remaining_body);
173 return stream.BeginRead (buffer, offset, count, cback, state);
176 public override int EndRead (IAsyncResult ares)
179 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
182 throw new ArgumentNullException ("async_result");
184 if (ares is HttpStreamAsyncResult) {
185 HttpStreamAsyncResult r = (HttpStreamAsyncResult) ares;
186 if (!ares.IsCompleted)
187 ares.AsyncWaitHandle.WaitOne ();
191 // Close on exception?
192 int nread = stream.EndRead (ares);
193 if (remaining_body > 0 && nread > 0)
194 remaining_body -= nread;
198 public override long Seek (long offset, SeekOrigin origin)
200 throw new NotSupportedException ();
203 public override void SetLength (long value)
205 throw new NotSupportedException ();
208 public override void Write (byte[] buffer, int offset, int count)
210 throw new NotSupportedException ();
213 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
214 AsyncCallback cback, object state)
216 throw new NotSupportedException ();
219 public override void EndWrite (IAsyncResult async_result)
221 throw new NotSupportedException ();