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