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 // 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
;
49 internal ResponseStream (Stream stream
, HttpListenerResponse response
, bool ignore_errors
)
51 this.response
= response
;
52 this.ignore_errors
= ignore_errors
;
56 public override bool CanRead
{
60 public override bool CanSeek
{
64 public override bool CanWrite
{
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) {
82 if (response
.HeadersSent
== false)
83 response
.SendHeaders (true);
85 if (response
.SendChunked
&& !trailer_sent
) {
86 WriteChunkSize (0, true);
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
)
109 stream
.Write (buffer
, offset
, count
);
112 stream
.Write (buffer
, offset
, count
);
116 public override void Write (byte [] buffer
, int offset
, int count
)
119 throw new ObjectDisposedException (GetType ().ToString ());
121 if (response
.HeadersSent
== false)
122 response
.SendHeaders (false);
124 bool chunked
= response
.SendChunked
;
127 WriteChunkSize (count
, false);
129 InternalWrite (buffer
, offset
, count
);
132 stream
.Write (crlf
, 0, 2);
136 public override IAsyncResult
BeginWrite (byte [] buffer
, int offset
, int count
,
137 AsyncCallback cback
, object state
)
140 throw new ObjectDisposedException (GetType ().ToString ());
142 if (response
.HeadersSent
== false)
143 response
.SendHeaders (false);
146 if (response
.SendChunked
)
147 WriteChunkSize (count
, false);
149 return stream
.BeginWrite (buffer
, offset
, count
, cback
, state
);
152 public override void EndWrite (IAsyncResult ares
)
155 throw new ObjectDisposedException (GetType ().ToString ());
159 stream
.EndWrite (ares
);
160 if (response
.SendChunked
)
161 stream
.Write (crlf
, 0, 2);
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 ();