b414f00a5acaea08b94281c1f95ad761b1b129b5
[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         // What happens when we set content-length to X and write X-1 bytes then close?
38         // what if we don't set content-length at all?
39         class ResponseStream : Stream
40         {
41                 HttpListenerResponse response;
42                 bool ignore_errors;
43                 bool disposed;
44                 bool trailer_sent;
45                 Stream stream;
46
47                 internal ResponseStream (Stream stream, HttpListenerResponse response, bool ignore_errors)
48                 {
49                         this.response = response;
50                         this.ignore_errors = ignore_errors;
51                         this.stream = stream;
52                 }
53
54                 public override bool CanRead {
55                         get { return false; }
56                 }
57
58                 public override bool CanSeek {
59                         get { return false; }
60                 }
61
62                 public override bool CanWrite {
63                         get { return true; }
64                 }
65
66                 public override long Length {
67                         get { throw new NotSupportedException (); }
68                 }
69
70                 public override long Position {
71                         get { throw new NotSupportedException (); }
72                         set { throw new NotSupportedException (); }
73                 }
74
75
76                 public override void Close ()
77                 {
78                         if (disposed == false) {
79                                 disposed = true;
80                                 if (response.HeadersSent == false)
81                                         response.SendHeaders (true);
82
83                                 if (response.SendChunked && !trailer_sent) {
84                                         WriteChunkSize (0, true);
85                                         trailer_sent = true;
86                                 }
87                                 response.Close ();
88                         }
89                 }
90
91                 public override void Flush ()
92                 {
93                 }
94
95                 static byte [] crlf = new byte [] { 13, 10 };
96                 void WriteChunkSize (int size, bool final)
97                 {
98                         string str = String.Format ("{0:x}\r\n{1}", size, final ? "\r\n" : "");
99                         byte [] b = Encoding.ASCII.GetBytes (str);
100                         stream.Write (b, 0, b.Length);
101                 }
102
103                 internal void InternalWrite (byte [] buffer, int offset, int count)
104                 {
105                         if (ignore_errors) {
106                                 try {
107                                         stream.Write (buffer, offset, count);
108                                 } catch { }
109                         } else {
110                                 stream.Write (buffer, offset, count);
111                         }
112                 }
113
114                 public override void Write (byte [] buffer, int offset, int count)
115                 {
116                         if (disposed)
117                                 throw new ObjectDisposedException (GetType ().ToString ());
118
119                         if (response.HeadersSent == false)
120                                 response.SendHeaders (false);
121
122                         bool chunked = response.SendChunked;
123                         try {
124                                 if (chunked)
125                                         WriteChunkSize (count, false);
126                         } catch { }
127                         InternalWrite (buffer, offset, count);
128                         try {
129                                 if (chunked)
130                                         stream.Write (crlf, 0, 2);
131                         } catch { }
132                 }
133
134                 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
135                                                         AsyncCallback cback, object state)
136                 {
137                         if (disposed)
138                                 throw new ObjectDisposedException (GetType ().ToString ());
139
140                         if (response.HeadersSent == false)
141                                 response.SendHeaders (false);
142
143                         try {
144                                 if (response.SendChunked)
145                                         WriteChunkSize (count, false);
146                         } catch { }
147                         return stream.BeginWrite (buffer, offset, count, cback, state);
148                 }
149
150                 public override void EndWrite (IAsyncResult ares)
151                 {
152                         if (disposed)
153                                 throw new ObjectDisposedException (GetType ().ToString ());
154
155                         if (ignore_errors) {
156                                 try {
157                                         stream.EndWrite (ares);
158                                         if (response.SendChunked)
159                                                 stream.Write (crlf, 0, 2);
160                                 } catch { }
161                         } else {
162                                 stream.EndWrite (ares);
163                                 if (response.SendChunked)
164                                         stream.Write (crlf, 0, 2);
165                         }
166                 }
167
168                 public override int Read ([In,Out] byte[] buffer, int offset, int count)
169                 {
170                         throw new NotSupportedException ();
171                 }
172
173                 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
174                                                         AsyncCallback cback, object state)
175                 {
176                         throw new NotSupportedException ();
177                 }
178
179                 public override int EndRead (IAsyncResult ares)
180                 {
181                         throw new NotSupportedException ();
182                 }
183
184                 public override long Seek (long offset, SeekOrigin origin)
185                 {
186                         throw new NotSupportedException ();
187                 }
188
189                 public override void SetLength (long value)
190                 {
191                         throw new NotSupportedException ();
192                 }
193         }
194 }
195 #endif
196