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 // What happens when we set content-length to X and write X-1 bytes then close?
38 // what if we don't set content-length at all?
39 class ResponseStream : Stream
41 HttpListenerResponse response;
47 internal ResponseStream (Stream stream, HttpListenerResponse response, bool ignore_errors)
49 this.response = response;
50 this.ignore_errors = ignore_errors;
54 public override bool CanRead {
58 public override bool CanSeek {
62 public override bool CanWrite {
66 public override long Length {
67 get { throw new NotSupportedException (); }
70 public override long Position {
71 get { throw new NotSupportedException (); }
72 set { throw new NotSupportedException (); }
76 public override void Close ()
78 if (disposed == false) {
80 if (response.HeadersSent == false)
81 response.SendHeaders (true);
83 if (response.SendChunked && !trailer_sent) {
84 WriteChunkSize (0, true);
91 public override void Flush ()
95 static byte [] crlf = new byte [] { 13, 10 };
96 void WriteChunkSize (int size, bool final)
98 string str = String.Format ("{0:x}\r\n{1}", size, final ? "\r\n" : "");
99 byte [] b = Encoding.ASCII.GetBytes (str);
100 stream.Write (b, 0, b.Length);
103 internal void InternalWrite (byte [] buffer, int offset, int count)
107 stream.Write (buffer, offset, count);
110 stream.Write (buffer, offset, count);
114 public override void Write (byte [] buffer, int offset, int count)
117 throw new ObjectDisposedException (GetType ().ToString ());
119 if (response.HeadersSent == false)
120 response.SendHeaders (false);
122 bool chunked = response.SendChunked;
125 WriteChunkSize (count, false);
127 InternalWrite (buffer, offset, count);
130 stream.Write (crlf, 0, 2);
134 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
135 AsyncCallback cback, object state)
138 throw new ObjectDisposedException (GetType ().ToString ());
140 if (response.HeadersSent == false)
141 response.SendHeaders (false);
144 if (response.SendChunked)
145 WriteChunkSize (count, false);
147 return stream.BeginWrite (buffer, offset, count, cback, state);
150 public override void EndWrite (IAsyncResult ares)
153 throw new ObjectDisposedException (GetType ().ToString ());
157 stream.EndWrite (ares);
158 if (response.SendChunked)
159 stream.Write (crlf, 0, 2);
162 stream.EndWrite (ares);
163 if (response.SendChunked)
164 stream.Write (crlf, 0, 2);
168 public override int Read ([In,Out] byte[] buffer, int offset, int count)
170 throw new NotSupportedException ();
173 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
174 AsyncCallback cback, object state)
176 throw new NotSupportedException ();
179 public override int EndRead (IAsyncResult ares)
181 throw new NotSupportedException ();
184 public override long Seek (long offset, SeekOrigin origin)
186 throw new NotSupportedException ();
189 public override void SetLength (long value)
191 throw new NotSupportedException ();