Add some 4.5 api
[mono.git] / mcs / class / System / System.Net / RequestStream.cs
1 //
2 // System.Net.RequestStream
3 //
4 // Author:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
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 : Stream
34         {
35                 byte [] buffer;
36                 int offset;
37                 int length;
38                 long remaining_body;
39                 bool disposed;
40                 Stream stream;
41
42                 internal RequestStream (Stream stream, byte [] buffer, int offset, int length)
43                         : this (stream, buffer, offset, length, -1)
44                 {
45                 }
46
47                 internal RequestStream (Stream stream, byte [] buffer, int offset, int length, long contentlength)
48                 {
49                         this.stream = stream;
50                         this.buffer = buffer;
51                         this.offset = offset;
52                         this.length = length;
53                         this.remaining_body = contentlength;
54                 }
55
56                 public override bool CanRead {
57                         get { return true; }
58                 }
59
60                 public override bool CanSeek {
61                         get { return false; }
62                 }
63
64                 public override bool CanWrite {
65                         get { return false; }
66                 }
67
68                 public override long Length {
69                         get { throw new NotSupportedException (); }
70                 }
71
72                 public override long Position {
73                         get { throw new NotSupportedException (); }
74                         set { throw new NotSupportedException (); }
75                 }
76
77
78                 public override void Close ()
79                 {
80                         disposed = true;
81                 }
82
83                 public override void Flush ()
84                 {
85                 }
86
87                 
88                 // Returns 0 if we can keep reading from the base stream,
89                 // > 0 if we read something from the buffer.
90                 // -1 if we had a content length set and we finished reading that many bytes.
91                 int FillFromBuffer (byte [] buffer, int off, int count)
92                 {
93                         if (buffer == null)
94                                 throw new ArgumentNullException ("buffer");
95                         if (off < 0)
96                                 throw new ArgumentOutOfRangeException ("offset", "< 0");
97                         if (count < 0)
98                                 throw new ArgumentOutOfRangeException ("count", "< 0");
99                         int len = buffer.Length;
100                         if (off > len)
101                                 throw new ArgumentException ("destination offset is beyond array size");
102                         if (off > len - count)
103                                 throw new ArgumentException ("Reading would overrun buffer");
104
105                         if (this.remaining_body == 0)
106                                 return -1;
107
108                         if (this.length == 0)
109                                 return 0;
110
111                         int size = Math.Min (this.length, count);
112                         if (this.remaining_body > 0)
113                                 size = (int) Math.Min (size, this.remaining_body);
114
115                         if (this.offset > this.buffer.Length - size) {
116                                 size = Math.Min (size, this.buffer.Length - this.offset);
117                         }
118                         if (size == 0)
119                                 return 0;
120
121                         Buffer.BlockCopy (this.buffer, this.offset, buffer, off, size);
122                         this.offset += size;
123                         this.length -= size;
124                         if (this.remaining_body > 0)
125                                 remaining_body -= size;
126                         return size;
127                 }
128
129                 public override int Read ([In,Out] byte[] buffer, int offset, int count)
130                 {
131                         if (disposed)
132                                 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
133
134                         // Call FillFromBuffer to check for buffer boundaries even when remaining_body is 0
135                         int nread = FillFromBuffer (buffer, offset, count);
136                         if (nread == -1) { // No more bytes available (Content-Length)
137                                 return 0;
138                         } else if (nread > 0) {
139                                 return nread;
140                         }
141
142                         nread = stream.Read (buffer, offset, count);
143                         if (nread > 0 && remaining_body > 0)
144                                 remaining_body -= nread;
145                         return nread;
146                 }
147
148                 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
149                                                         AsyncCallback cback, object state)
150                 {
151                         if (disposed)
152                                 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
153
154                         int nread = FillFromBuffer (buffer, offset, count);
155                         if (nread > 0 || nread == -1) {
156                                 HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
157                                 ares.Buffer = buffer;
158                                 ares.Offset = offset;
159                                 ares.Count = count;
160                                 ares.Callback = cback;
161                                 ares.State = state;
162                                 ares.SynchRead = nread;
163                                 ares.Complete ();
164                                 return ares;
165                         }
166
167                         // Avoid reading past the end of the request to allow
168                         // for HTTP pipelining
169                         if (remaining_body >= 0 && count > remaining_body)
170                                 count = (int) Math.Min (Int32.MaxValue, remaining_body);
171                         return stream.BeginRead (buffer, offset, count, cback, state);
172                 }
173
174                 public override int EndRead (IAsyncResult ares)
175                 {
176                         if (disposed)
177                                 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
178
179                         if (ares == null)
180                                 throw new ArgumentNullException ("async_result");
181
182                         if (ares is HttpStreamAsyncResult) {
183                                 HttpStreamAsyncResult r = (HttpStreamAsyncResult) ares;
184                                 if (!ares.IsCompleted)
185                                         ares.AsyncWaitHandle.WaitOne ();
186                                 return r.SynchRead;
187                         }
188
189                         // Close on exception?
190                         int nread = stream.EndRead (ares);
191                         if (remaining_body > 0 && nread > 0)
192                                 remaining_body -= nread;
193                         return nread;
194                 }
195
196                 public override long Seek (long offset, SeekOrigin origin)
197                 {
198                         throw new NotSupportedException ();
199                 }
200
201                 public override void SetLength (long value)
202                 {
203                         throw new NotSupportedException ();
204                 }
205
206                 public override void Write (byte[] buffer, int offset, int count)
207                 {
208                         throw new NotSupportedException ();
209                 }
210
211                 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
212                                                         AsyncCallback cback, object state)
213                 {
214                         throw new NotSupportedException ();
215                 }
216
217                 public override void EndWrite (IAsyncResult async_result)
218                 {
219                         throw new NotSupportedException ();
220                 }
221         }
222 }
223 #endif
224