System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System / System.Net / ResponseStream.cs
1 //
2 // System.Net.ResponseStream
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
29 #if NET_2_0 && SECURITY_DEP
30
31 using System.IO;
32 using System.Net.Sockets;
33 using System.Text;
34 using System.Runtime.InteropServices;
35 namespace System.Net {
36         // FIXME: Does this buffer the response until Close?
37         // Update: we send a single packet for the first non-chunked Write
38         // What happens when we set content-length to X and write X-1 bytes then close?
39         // what if we don't set content-length at all?
40         class ResponseStream : Stream
41         {
42                 HttpListenerResponse response;
43                 bool ignore_errors;
44                 bool disposed;
45                 bool trailer_sent;
46                 Stream stream;
47
48                 internal ResponseStream (Stream stream, HttpListenerResponse response, bool ignore_errors)
49                 {
50                         this.response = response;
51                         this.ignore_errors = ignore_errors;
52                         this.stream = stream;
53                 }
54
55                 public override bool CanRead {
56                         get { return false; }
57                 }
58
59                 public override bool CanSeek {
60                         get { return false; }
61                 }
62
63                 public override bool CanWrite {
64                         get { return true; }
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                         if (disposed == false) {
80                                 disposed = true;
81                                 byte [] bytes = null;
82                                 MemoryStream ms = GetHeaders (true);
83                                 bool chunked = response.SendChunked;
84                                 if (ms != null) {
85                                         long start = ms.Position;
86                                         if (chunked && !trailer_sent) {
87                                                 bytes = GetChunkSizeBytes (0, true);
88                                                 ms.Position = ms.Length;
89                                                 ms.Write (bytes, 0, bytes.Length);
90                                         }
91                                         InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start));
92                                         trailer_sent = true;
93                                 } else if (chunked && !trailer_sent) {
94                                         bytes = GetChunkSizeBytes (0, true);
95                                         InternalWrite (bytes, 0, bytes.Length);
96                                         trailer_sent = true;
97                                 }
98                                 response.Close ();
99                         }
100                 }
101
102                 MemoryStream GetHeaders (bool closing)
103                 {
104                         if (response.HeadersSent)
105                                 return null;
106                         MemoryStream ms = new MemoryStream ();
107                         response.SendHeaders (closing, ms);
108                         return ms;
109                 }
110
111                 public override void Flush ()
112                 {
113                 }
114
115                 static byte [] crlf = new byte [] { 13, 10 };
116                 static byte [] GetChunkSizeBytes (int size, bool final)
117                 {
118                         string str = String.Format ("{0:x}\r\n{1}", size, final ? "\r\n" : "");
119                         return Encoding.ASCII.GetBytes (str);
120                 }
121
122                 internal void InternalWrite (byte [] buffer, int offset, int count)
123                 {
124                         if (ignore_errors) {
125                                 try {
126                                         stream.Write (buffer, offset, count);
127                                 } catch { }
128                         } else {
129                                 stream.Write (buffer, offset, count);
130                         }
131                 }
132
133                 public override void Write (byte [] buffer, int offset, int count)
134                 {
135                         if (disposed)
136                                 throw new ObjectDisposedException (GetType ().ToString ());
137
138                         byte [] bytes = null;
139                         MemoryStream ms = GetHeaders (false);
140                         bool chunked = response.SendChunked;
141                         if (ms != null) {
142                                 long start = ms.Position; // After the possible preamble for the encoding
143                                 ms.Position = ms.Length;
144                                 if (chunked) {
145                                         bytes = GetChunkSizeBytes (count, false);
146                                         ms.Write (bytes, 0, bytes.Length);
147                                 }
148
149                                 int new_count = Math.Min (count, 16384 - (int) ms.Position + (int) start);
150                                 ms.Write (buffer, offset, new_count);
151                                 count -= new_count;
152                                 offset += new_count;
153                                 InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start));
154                                 ms.SetLength (0);
155                                 ms.Capacity = 0; // 'dispose' the buffer in ms.
156                         } else if (chunked) {
157                                 bytes = GetChunkSizeBytes (count, false);
158                                 InternalWrite (bytes, 0, bytes.Length);
159                         }
160
161                         if (count > 0)
162                                 InternalWrite (buffer, offset, count);
163                         if (chunked)
164                                 InternalWrite (crlf, 0, 2);
165                 }
166
167                 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
168                                                         AsyncCallback cback, object state)
169                 {
170                         if (disposed)
171                                 throw new ObjectDisposedException (GetType ().ToString ());
172
173                         byte [] bytes = null;
174                         MemoryStream ms = GetHeaders (false);
175                         bool chunked = response.SendChunked;
176                         if (ms != null) {
177                                 long start = ms.Position;
178                                 ms.Position = ms.Length;
179                                 if (chunked) {
180                                         bytes = GetChunkSizeBytes (count, false);
181                                         ms.Write (bytes, 0, bytes.Length);
182                                 }
183                                 ms.Write (buffer, offset, count);
184                                 buffer = ms.GetBuffer ();
185                                 offset = (int) start;
186                                 count = (int) (ms.Position - start);
187                         } else if (chunked) {
188                                 bytes = GetChunkSizeBytes (count, false);
189                                 InternalWrite (bytes, 0, bytes.Length);
190                         }
191
192                         return stream.BeginWrite (buffer, offset, count, cback, state);
193                 }
194
195                 public override void EndWrite (IAsyncResult ares)
196                 {
197                         if (disposed)
198                                 throw new ObjectDisposedException (GetType ().ToString ());
199
200                         if (ignore_errors) {
201                                 try {
202                                         stream.EndWrite (ares);
203                                         if (response.SendChunked)
204                                                 stream.Write (crlf, 0, 2);
205                                 } catch { }
206                         } else {
207                                 stream.EndWrite (ares);
208                                 if (response.SendChunked)
209                                         stream.Write (crlf, 0, 2);
210                         }
211                 }
212
213                 public override int Read ([In,Out] byte[] buffer, int offset, int count)
214                 {
215                         throw new NotSupportedException ();
216                 }
217
218                 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
219                                                         AsyncCallback cback, object state)
220                 {
221                         throw new NotSupportedException ();
222                 }
223
224                 public override int EndRead (IAsyncResult ares)
225                 {
226                         throw new NotSupportedException ();
227                 }
228
229                 public override long Seek (long offset, SeekOrigin origin)
230                 {
231                         throw new NotSupportedException ();
232                 }
233
234                 public override void SetLength (long value)
235                 {
236                         throw new NotSupportedException ();
237                 }
238         }
239 }
240 #endif
241