2d004a46e76ae8f0a9695748a8fefa0614345b34
[mono.git] / mcs / class / System.Runtime.Remoting / MonoHttp / ChunkedInputStream.cs
1 #define EMBEDDED_IN_1_0
2
3 //
4 // System.Net.ChunkedInputStream
5 //
6 // Authors:
7 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
8 //
9 // Copyright (c) 2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29
30 #if EMBEDDED_IN_1_0
31
32 using System.IO;
33 using System.Net.Sockets;
34 using System.Runtime.InteropServices;
35 using System; using System.Net; namespace MonoHttp {
36         class ChunkedInputStream : RequestStream {
37                 bool disposed;
38                 ChunkStream decoder;
39                 HttpListenerContext context;
40                 bool no_more_data;
41
42                 class ReadBufferState {
43                         public byte [] Buffer;
44                         public int Offset;
45                         public int Count;
46                         public int InitialCount;
47                         public HttpStreamAsyncResult Ares;
48                         public ReadBufferState (byte [] buffer, int offset, int count,
49                                                 HttpStreamAsyncResult ares)
50                         {
51                                 Buffer = buffer;
52                                 Offset = offset;
53                                 Count = count;
54                                 InitialCount = count;
55                                 Ares = ares;
56                         }
57                 }
58
59                 public ChunkedInputStream (HttpListenerContext context, Stream stream,
60                                                 byte [] buffer, int offset, int length)
61                                         : base (stream, buffer, offset, length)
62                 {
63                         this.context = context;
64                         WebHeaderCollection coll = (WebHeaderCollection) context.Request.Headers;
65                         decoder = new ChunkStream (coll);
66                 }
67
68                 public ChunkStream Decoder {
69                         get { return decoder; }
70                         set { decoder = value; }
71                 }
72
73                 public override int Read ([In,Out] byte [] buffer, int offset, int count)
74                 {
75                         IAsyncResult ares = BeginRead (buffer, offset, count, null, null);
76                         return EndRead (ares);
77                 }
78
79                 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
80                                                         AsyncCallback cback, object state)
81                 {
82                         if (disposed)
83                                 throw new ObjectDisposedException (GetType ().ToString ());
84
85                         if (buffer == null)
86                                 throw new ArgumentNullException ("buffer");
87
88                         int len = buffer.Length;
89                         if (offset < 0 || offset > len)
90                                 throw new ArgumentOutOfRangeException ("offset exceeds the size of buffer");
91
92                         if (count < 0 || offset > len - count)
93                                 throw new ArgumentOutOfRangeException ("offset+size exceeds the size of buffer");
94
95                         HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
96                         ares.Callback = cback;
97                         ares.State = state;
98                         if (no_more_data) {
99                                 ares.Complete ();
100                                 return ares;
101                         }
102                         int nread = decoder.Read (buffer, offset, count);
103                         offset += nread;
104                         count -= nread;
105                         if (count == 0) {
106                                 // got all we wanted, no need to bother the decoder yet
107                                 ares.Count = nread;
108                                 ares.Complete ();
109                                 return ares;
110                         }
111                         if (!decoder.WantMore) {
112                                 no_more_data = nread == 0;
113                                 ares.Count = nread;
114                                 ares.Complete ();
115                                 return ares;
116                         }
117                         ares.Buffer = new byte [8192];
118                         ares.Offset = 0;
119                         ares.Count = 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);
123                         return ares;
124                 }
125
126                 void OnRead (IAsyncResult base_ares)
127                 {
128                         ReadBufferState rb = (ReadBufferState) base_ares.AsyncState;
129                         HttpStreamAsyncResult ares = rb.Ares;
130                         try {
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);
134                                 rb.Offset += nread;
135                                 rb.Count -= nread;
136                                 if (rb.Count == 0 || !decoder.WantMore || nread == 0) {
137                                         no_more_data = !decoder.WantMore && nread == 0;
138                                         ares.Count = rb.InitialCount - rb.Count;
139                                         ares.Complete ();
140                                         return;
141                                 }
142                                 ares.Offset = 0;
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);
147                                 ares.Complete (e);
148                         }
149                 }
150
151                 public override int EndRead (IAsyncResult ares)
152                 {
153                         if (disposed)
154                                 throw new ObjectDisposedException (GetType ().ToString ());
155
156                         HttpStreamAsyncResult my_ares = ares as HttpStreamAsyncResult;
157                         if (ares == null)
158                                 throw new ArgumentException ("Invalid IAsyncResult", "ares");
159
160                         if (!ares.IsCompleted)
161                                 ares.AsyncWaitHandle.WaitOne ();
162
163                         if (my_ares.Error != null)
164                                 throw new HttpListenerException (400, "I/O operation aborted.");
165
166                         return my_ares.Count;
167                 }
168
169                 public override void Close ()
170                 {
171                         if (!disposed) {
172                                 disposed = true;
173                                 base.Close ();
174                         }
175                 }
176         }
177 }
178 #endif