1 #define EMBEDDED_IN_1_0
4 // System.Net.ChunkedInputStream
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.
33 using System.Net.Sockets;
34 using System.Runtime.InteropServices;
35 using System; using System.Net; namespace MonoHttp {
36 class ChunkedInputStream : RequestStream {
39 HttpListenerContext context;
42 class ReadBufferState {
43 public byte [] Buffer;
46 public int InitialCount;
47 public HttpStreamAsyncResult Ares;
48 public ReadBufferState (byte [] buffer, int offset, int count,
49 HttpStreamAsyncResult ares)
59 public ChunkedInputStream (HttpListenerContext context, Stream stream,
60 byte [] buffer, int offset, int length)
61 : base (stream, buffer, offset, length)
63 this.context = context;
64 WebHeaderCollection coll = (WebHeaderCollection) context.Request.Headers;
65 decoder = new ChunkStream (coll);
68 public ChunkStream Decoder {
69 get { return decoder; }
70 set { decoder = value; }
73 public override int Read ([In,Out] byte [] buffer, int offset, int count)
75 IAsyncResult ares = BeginRead (buffer, offset, count, null, null);
76 return EndRead (ares);
79 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
80 AsyncCallback cback, object state)
83 throw new ObjectDisposedException (GetType ().ToString ());
86 throw new ArgumentNullException ("buffer");
88 int len = buffer.Length;
89 if (offset < 0 || offset > len)
90 throw new ArgumentOutOfRangeException ("offset exceeds the size of buffer");
92 if (count < 0 || offset > len - count)
93 throw new ArgumentOutOfRangeException ("offset+size exceeds the size of buffer");
95 HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
96 ares.Callback = cback;
102 int nread = decoder.Read (buffer, offset, count);
106 // got all we wanted, no need to bother the decoder yet
111 if (!decoder.WantMore) {
112 no_more_data = nread == 0;
117 ares.Buffer = new byte [8192];
120 ReadBufferState rb = new ReadBufferState (buffer, offset, count, ares);
121 rb.InitialCount += nread;
122 base.BeginRead (ares.Buffer, ares.Offset, ares.Count, OnRead, rb);
126 void OnRead (IAsyncResult base_ares)
128 ReadBufferState rb = (ReadBufferState) base_ares.AsyncState;
129 HttpStreamAsyncResult ares = rb.Ares;
131 int nread = base.EndRead (base_ares);
132 decoder.Write (ares.Buffer, ares.Offset, nread);
133 nread = decoder.Read (rb.Buffer, rb.Offset, rb.Count);
136 if (rb.Count == 0 || !decoder.WantMore || nread == 0) {
137 no_more_data = !decoder.WantMore && nread == 0;
138 ares.Count = rb.InitialCount - rb.Count;
143 ares.Count = Math.Min (8192, decoder.ChunkLeft + 6);
144 base.BeginRead (ares.Buffer, ares.Offset, ares.Count, OnRead, rb);
145 } catch (Exception e) {
146 context.Connection.SendError (e.Message, 400);
151 public override int EndRead (IAsyncResult ares)
154 throw new ObjectDisposedException (GetType ().ToString ());
156 HttpStreamAsyncResult my_ares = ares as HttpStreamAsyncResult;
158 throw new ArgumentException ("Invalid IAsyncResult", "ares");
160 if (!ares.IsCompleted)
161 ares.AsyncWaitHandle.WaitOne ();
163 if (my_ares.Error != null)
164 throw new HttpListenerException (400, "I/O operation aborted.");
166 return my_ares.Count;
169 public override void Close ()