2 // System.Net.ResponseStream
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
7 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 #if NET_2_0 && SECURITY_DEP
32 using System.Net.Sockets;
34 using System.Runtime.InteropServices;
35 namespace System.Net {
36 // FIXME: Does this buffer the response until Close?
37 // Update: we send a single packet for the first non-chunked Write
38 // What happens when we set content-length to X and write X-1 bytes then close?
39 // what if we don't set content-length at all?
40 class ResponseStream : Stream
42 HttpListenerResponse response;
48 internal ResponseStream (Stream stream, HttpListenerResponse response, bool ignore_errors)
50 this.response = response;
51 this.ignore_errors = ignore_errors;
55 public override bool CanRead {
59 public override bool CanSeek {
63 public override bool CanWrite {
67 public override long Length {
68 get { throw new NotSupportedException (); }
71 public override long Position {
72 get { throw new NotSupportedException (); }
73 set { throw new NotSupportedException (); }
77 public override void Close ()
79 if (disposed == false) {
82 MemoryStream ms = GetHeaders (true);
83 bool chunked = response.SendChunked;
85 long start = ms.Position;
86 if (chunked && !trailer_sent) {
87 bytes = GetChunkSizeBytes (0, true);
88 ms.Position = ms.Length;
89 ms.Write (bytes, 0, bytes.Length);
91 InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start));
93 } else if (chunked && !trailer_sent) {
94 bytes = GetChunkSizeBytes (0, true);
95 InternalWrite (bytes, 0, bytes.Length);
102 MemoryStream GetHeaders (bool closing)
104 if (response.HeadersSent)
106 MemoryStream ms = new MemoryStream ();
107 response.SendHeaders (closing, ms);
111 public override void Flush ()
115 static byte [] crlf = new byte [] { 13, 10 };
116 static byte [] GetChunkSizeBytes (int size, bool final)
118 string str = String.Format ("{0:x}\r\n{1}", size, final ? "\r\n" : "");
119 return Encoding.ASCII.GetBytes (str);
122 internal void InternalWrite (byte [] buffer, int offset, int count)
126 stream.Write (buffer, offset, count);
129 stream.Write (buffer, offset, count);
133 public override void Write (byte [] buffer, int offset, int count)
136 throw new ObjectDisposedException (GetType ().ToString ());
138 byte [] bytes = null;
139 MemoryStream ms = GetHeaders (false);
140 bool chunked = response.SendChunked;
142 long start = ms.Position; // After the possible preamble for the encoding
143 ms.Position = ms.Length;
145 bytes = GetChunkSizeBytes (count, false);
146 ms.Write (bytes, 0, bytes.Length);
149 int new_count = Math.Min (count, 16384 - (int) ms.Position + (int) start);
150 ms.Write (buffer, offset, new_count);
153 InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start));
155 ms.Capacity = 0; // 'dispose' the buffer in ms.
156 } else if (chunked) {
157 bytes = GetChunkSizeBytes (count, false);
158 InternalWrite (bytes, 0, bytes.Length);
162 InternalWrite (buffer, offset, count);
164 InternalWrite (crlf, 0, 2);
167 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
168 AsyncCallback cback, object state)
171 throw new ObjectDisposedException (GetType ().ToString ());
173 byte [] bytes = null;
174 MemoryStream ms = GetHeaders (false);
175 bool chunked = response.SendChunked;
177 long start = ms.Position;
178 ms.Position = ms.Length;
180 bytes = GetChunkSizeBytes (count, false);
181 ms.Write (bytes, 0, bytes.Length);
183 ms.Write (buffer, offset, count);
184 buffer = ms.GetBuffer ();
185 offset = (int) start;
186 count = (int) (ms.Position - start);
187 } else if (chunked) {
188 bytes = GetChunkSizeBytes (count, false);
189 InternalWrite (bytes, 0, bytes.Length);
192 return stream.BeginWrite (buffer, offset, count, cback, state);
195 public override void EndWrite (IAsyncResult ares)
198 throw new ObjectDisposedException (GetType ().ToString ());
202 stream.EndWrite (ares);
203 if (response.SendChunked)
204 stream.Write (crlf, 0, 2);
207 stream.EndWrite (ares);
208 if (response.SendChunked)
209 stream.Write (crlf, 0, 2);
213 public override int Read ([In,Out] byte[] buffer, int offset, int count)
215 throw new NotSupportedException ();
218 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
219 AsyncCallback cback, object state)
221 throw new NotSupportedException ();
224 public override int EndRead (IAsyncResult ares)
226 throw new NotSupportedException ();
229 public override long Seek (long offset, SeekOrigin origin)
231 throw new NotSupportedException ();
234 public override void SetLength (long value)
236 throw new NotSupportedException ();