42212e15ae3339d1e67adf86d04da228abc8aebf
[mono.git] / mcs / class / System.Runtime.Remoting / MonoHttp / RequestStream.cs
1 #define EMBEDDED_IN_1_0
2
3 //
4 // System.Net.RequestStream
5 //
6 // Author:
7 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
8 //
9 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 #if EMBEDDED_IN_1_0
31 using System.IO;
32 using System.Net.Sockets;
33 using System.Runtime.InteropServices;
34 using System; using System.Net; namespace MonoHttp {
35         class RequestStream : Stream
36         {
37                 byte [] buffer;
38                 int offset;
39                 int length;
40                 long remaining_body;
41                 bool disposed;
42                 Stream stream;
43
44                 internal RequestStream (Stream stream, byte [] buffer, int offset, int length)
45                         : this (stream, buffer, offset, length, -1)
46                 {
47                 }
48
49                 internal RequestStream (Stream stream, byte [] buffer, int offset, int length, long contentlength)
50                 {
51                         this.stream = stream;
52                         this.buffer = buffer;
53                         this.offset = offset;
54                         this.length = length;
55                         this.remaining_body = contentlength;
56                 }
57
58                 public override bool CanRead {
59                         get { return true; }
60                 }
61
62                 public override bool CanSeek {
63                         get { return false; }
64                 }
65
66                 public override bool CanWrite {
67                         get { return false; }
68                 }
69
70                 public override long Length {
71                         get { throw new NotSupportedException (); }
72                 }
73
74                 public override long Position {
75                         get { throw new NotSupportedException (); }
76                         set { throw new NotSupportedException (); }
77                 }
78
79
80                 public override void Close ()
81                 {
82                         disposed = true;
83                 }
84
85                 public override void Flush ()
86                 {
87                 }
88
89                 
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)
94                 {
95                         if (buffer == null)
96                                 throw new ArgumentNullException ("buffer");
97                         if (off < 0)
98                                 throw new ArgumentOutOfRangeException ("offset", "< 0");
99                         if (count < 0)
100                                 throw new ArgumentOutOfRangeException ("count", "< 0");
101                         int len = buffer.Length;
102                         if (off > len)
103                                 throw new ArgumentException ("destination offset is beyond array size");
104                         if (off > len - count)
105                                 throw new ArgumentException ("Reading would overrun buffer");
106
107                         if (this.remaining_body == 0)
108                                 return -1;
109
110                         if (this.length == 0)
111                                 return 0;
112
113                         int size = Math.Min (this.length, count);
114                         if (this.remaining_body > 0)
115                                 size = (int) Math.Min (size, this.remaining_body);
116
117                         if (this.offset > this.buffer.Length - size) {
118                                 size = Math.Min (size, this.buffer.Length - this.offset);
119                         }
120                         if (size == 0)
121                                 return 0;
122
123                         Buffer.BlockCopy (this.buffer, this.offset, buffer, off, size);
124                         this.offset += size;
125                         this.length -= size;
126                         if (this.remaining_body > 0)
127                                 remaining_body -= size;
128                         return size;
129                 }
130
131                 public override int Read ([In,Out] byte[] buffer, int offset, int count)
132                 {
133                         if (disposed)
134                                 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
135
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)
139                                 return 0;
140                         } else if (nread > 0) {
141                                 return nread;
142                         }
143
144                         nread = stream.Read (buffer, offset, count);
145                         if (nread > 0 && remaining_body > 0)
146                                 remaining_body -= nread;
147                         return nread;
148                 }
149
150                 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
151                                                         AsyncCallback cback, object state)
152                 {
153                         if (disposed)
154                                 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
155
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;
161                                 ares.Count = count;
162                                 ares.Callback = cback;
163                                 ares.State = state;
164                                 ares.SynchRead = nread;
165                                 ares.Complete ();
166                                 return ares;
167                         }
168
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);
174                 }
175
176                 public override int EndRead (IAsyncResult ares)
177                 {
178                         if (disposed)
179                                 throw new ObjectDisposedException (typeof (RequestStream).ToString ());
180
181                         if (ares == null)
182                                 throw new ArgumentNullException ("async_result");
183
184                         if (ares is HttpStreamAsyncResult) {
185                                 HttpStreamAsyncResult r = (HttpStreamAsyncResult) ares;
186                                 if (!ares.IsCompleted)
187                                         ares.AsyncWaitHandle.WaitOne ();
188                                 return r.SynchRead;
189                         }
190
191                         // Close on exception?
192                         int nread = stream.EndRead (ares);
193                         if (remaining_body > 0 && nread > 0)
194                                 remaining_body -= nread;
195                         return nread;
196                 }
197
198                 public override long Seek (long offset, SeekOrigin origin)
199                 {
200                         throw new NotSupportedException ();
201                 }
202
203                 public override void SetLength (long value)
204                 {
205                         throw new NotSupportedException ();
206                 }
207
208                 public override void Write (byte[] buffer, int offset, int count)
209                 {
210                         throw new NotSupportedException ();
211                 }
212
213                 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
214                                                         AsyncCallback cback, object state)
215                 {
216                         throw new NotSupportedException ();
217                 }
218
219                 public override void EndWrite (IAsyncResult async_result)
220                 {
221                         throw new NotSupportedException ();
222                 }
223         }
224 }
225 #endif
226