Add MonoHttp: a scripted copy of the HttpListener core that works in 1.0 for the...
[mono-project.git] / mcs / class / System.Runtime.Remoting / MonoHttp / ResponseStream.cs
blob68ba0e990610d46fc1d0513235f8fed74551b77a
1 #define EMBEDDED_IN_1_0
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)
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.
31 #if EMBEDDED_IN_1_0
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
43 HttpListenerResponse response;
44 bool ignore_errors;
45 bool disposed;
46 bool trailer_sent;
47 Stream stream;
49 internal ResponseStream (Stream stream, HttpListenerResponse response, bool ignore_errors)
51 this.response = response;
52 this.ignore_errors = ignore_errors;
53 this.stream = stream;
56 public override bool CanRead {
57 get { return false; }
60 public override bool CanSeek {
61 get { return false; }
64 public override bool CanWrite {
65 get { return true; }
68 public override long Length {
69 get { throw new NotSupportedException (); }
72 public override long Position {
73 get { throw new NotSupportedException (); }
74 set { throw new NotSupportedException (); }
78 public override void Close ()
80 if (disposed == false) {
81 disposed = true;
82 if (response.HeadersSent == false)
83 response.SendHeaders (true);
85 if (response.SendChunked && !trailer_sent) {
86 WriteChunkSize (0, true);
87 trailer_sent = true;
89 response.Close ();
93 public override void Flush ()
97 static byte [] crlf = new byte [] { 13, 10 };
98 void WriteChunkSize (int size, bool final)
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);
105 internal void InternalWrite (byte [] buffer, int offset, int count)
107 if (ignore_errors) {
108 try {
109 stream.Write (buffer, offset, count);
110 } catch { }
111 } else {
112 stream.Write (buffer, offset, count);
116 public override void Write (byte [] buffer, int offset, int count)
118 if (disposed)
119 throw new ObjectDisposedException (GetType ().ToString ());
121 if (response.HeadersSent == false)
122 response.SendHeaders (false);
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 { }
136 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
137 AsyncCallback cback, object state)
139 if (disposed)
140 throw new ObjectDisposedException (GetType ().ToString ());
142 if (response.HeadersSent == false)
143 response.SendHeaders (false);
145 try {
146 if (response.SendChunked)
147 WriteChunkSize (count, false);
148 } catch { }
149 return stream.BeginWrite (buffer, offset, count, cback, state);
152 public override void EndWrite (IAsyncResult ares)
154 if (disposed)
155 throw new ObjectDisposedException (GetType ().ToString ());
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);
170 public override int Read ([In,Out] byte[] buffer, int offset, int count)
172 throw new NotSupportedException ();
175 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
176 AsyncCallback cback, object state)
178 throw new NotSupportedException ();
181 public override int EndRead (IAsyncResult ares)
183 throw new NotSupportedException ();
186 public override long Seek (long offset, SeekOrigin origin)
188 throw new NotSupportedException ();
191 public override void SetLength (long value)
193 throw new NotSupportedException ();
197 #endif