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