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