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