1 #define EMBEDDED_IN_1_0
4 // System.Net.ResponseStream
7 // Gonzalo Paniagua Javier (gonzalo@novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
34 using System.Net.Sockets;
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
44 HttpListenerResponse response;
50 internal ResponseStream (Stream stream, HttpListenerResponse response, bool ignore_errors)
52 this.response = response;
53 this.ignore_errors = ignore_errors;
57 public override bool CanRead {
61 public override bool CanSeek {
65 public override bool CanWrite {
69 public override long Length {
70 get { throw new NotSupportedException (); }
73 public override long Position {
74 get { throw new NotSupportedException (); }
75 set { throw new NotSupportedException (); }
79 public override void Close ()
81 if (disposed == false) {
84 MemoryStream ms = GetHeaders (true);
85 bool chunked = response.SendChunked;
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);
93 InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start));
95 } else if (chunked && !trailer_sent) {
96 bytes = GetChunkSizeBytes (0, true);
97 InternalWrite (bytes, 0, bytes.Length);
104 MemoryStream GetHeaders (bool closing)
106 if (response.HeadersSent)
108 MemoryStream ms = new MemoryStream ();
109 response.SendHeaders (closing, ms);
113 public override void Flush ()
117 static byte [] crlf = new byte [] { 13, 10 };
118 static byte [] GetChunkSizeBytes (int size, bool final)
120 string str = String.Format ("{0:x}\r\n{1}", size, final ? "\r\n" : "");
121 return Encoding.ASCII.GetBytes (str);
124 internal void InternalWrite (byte [] buffer, int offset, int count)
128 stream.Write (buffer, offset, count);
131 stream.Write (buffer, offset, count);
135 public override void Write (byte [] buffer, int offset, int count)
138 throw new ObjectDisposedException (GetType ().ToString ());
140 byte [] bytes = null;
141 MemoryStream ms = GetHeaders (false);
142 bool chunked = response.SendChunked;
144 long start = ms.Position; // After the possible preamble for the encoding
145 ms.Position = ms.Length;
147 bytes = GetChunkSizeBytes (count, false);
148 ms.Write (bytes, 0, bytes.Length);
151 int new_count = Math.Min (count, 16384 - (int) ms.Position + (int) start);
152 ms.Write (buffer, offset, new_count);
155 InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start));
157 ms.Capacity = 0; // 'dispose' the buffer in ms.
158 } else if (chunked) {
159 bytes = GetChunkSizeBytes (count, false);
160 InternalWrite (bytes, 0, bytes.Length);
164 InternalWrite (buffer, offset, count);
166 InternalWrite (crlf, 0, 2);
169 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
170 AsyncCallback cback, object state)
173 throw new ObjectDisposedException (GetType ().ToString ());
175 byte [] bytes = null;
176 MemoryStream ms = GetHeaders (false);
177 bool chunked = response.SendChunked;
179 long start = ms.Position;
180 ms.Position = ms.Length;
182 bytes = GetChunkSizeBytes (count, false);
183 ms.Write (bytes, 0, bytes.Length);
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);
194 return stream.BeginWrite (buffer, offset, count, cback, state);
197 public override void EndWrite (IAsyncResult ares)
200 throw new ObjectDisposedException (GetType ().ToString ());
204 stream.EndWrite (ares);
205 if (response.SendChunked)
206 stream.Write (crlf, 0, 2);
209 stream.EndWrite (ares);
210 if (response.SendChunked)
211 stream.Write (crlf, 0, 2);
215 public override int Read ([In,Out] byte[] buffer, int offset, int count)
217 throw new NotSupportedException ();
220 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
221 AsyncCallback cback, object state)
223 throw new NotSupportedException ();
226 public override int EndRead (IAsyncResult ares)
228 throw new NotSupportedException ();
231 public override long Seek (long offset, SeekOrigin origin)
233 throw new NotSupportedException ();
236 public override void SetLength (long value)
238 throw new NotSupportedException ();