2 // System.Net.ChunkStream
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.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.
31 using System.Collections;
32 using System.Globalization;
51 public Chunk (byte [] chunk)
56 public int Read (byte [] buffer, int offset, int size)
58 int nread = (size > Bytes.Length - Offset) ? Bytes.Length - Offset : size;
59 Buffer.BlockCopy (Bytes, Offset, buffer, offset, nread);
65 WebHeaderCollection headers;
75 public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
77 this.headers = headers;
78 saved = new StringBuilder ();
79 chunks = new ArrayList ();
81 Write (buffer, offset, size);
84 public void ResetBuffer ()
91 public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
93 if (offset + read > 0)
94 Write (buffer, offset, offset+read);
95 read = Read (buffer, offset, size);
98 public int Read (byte [] buffer, int offset, int size)
100 return ReadFromChunks (buffer, offset, size);
103 int ReadFromChunks (byte [] buffer, int offset, int size)
105 int count = chunks.Count;
107 for (int i = 0; i < count; i++) {
108 Chunk chunk = (Chunk) chunks [i];
112 if (chunk.Offset == chunk.Bytes.Length) {
117 nread += chunk.Read (buffer, offset + nread, size - nread);
125 public void Write (byte [] buffer, int offset, int size)
127 InternalWrite (buffer, ref offset, size);
130 void InternalWrite (byte [] buffer, ref int offset, int size)
132 if (state == State.None) {
133 state = GetChunkSize (buffer, ref offset, size);
134 if (state == State.None)
142 if (state == State.Body && offset < size) {
143 state = ReadBody (buffer, ref offset, size);
144 if (state == State.Body)
148 if (state == State.BodyFinished && offset < size) {
149 state = ReadCRLF (buffer, ref offset, size);
150 if (state == State.BodyFinished)
156 if (state == State.Trailer && offset < size) {
157 state = ReadTrailer (buffer, ref offset, size);
158 if (state == State.Trailer)
167 InternalWrite (buffer, ref offset, size);
170 public bool WantMore {
171 get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
174 public int ChunkLeft {
175 get { return chunkSize - chunkRead; }
178 State ReadBody (byte [] buffer, ref int offset, int size)
181 return State.BodyFinished;
183 int diff = size - offset;
184 if (diff + chunkRead > chunkSize)
185 diff = chunkSize - chunkRead;
187 byte [] chunk = new byte [diff];
188 Buffer.BlockCopy (buffer, offset, chunk, 0, diff);
189 chunks.Add (new Chunk (chunk));
192 return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
196 State GetChunkSize (byte [] buffer, ref int offset, int size)
199 while (offset < size) {
200 c = (char) buffer [offset++];
203 ThrowProtocolViolation ("2 CR found");
209 if (sawCR && c == '\n')
218 if (saved.Length > 20)
219 ThrowProtocolViolation ("chunk size too long.");
222 if (!sawCR || c != '\n') {
224 ThrowProtocolViolation ("Missing \\n");
227 if (saved.Length > 0)
228 chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
229 } catch (Exception) {
230 ThrowProtocolViolation ("Cannot parse chunk size.");
238 chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
239 } catch (Exception) {
240 ThrowProtocolViolation ("Cannot parse chunk size.");
244 return State.Trailer;
249 State ReadCRLF (byte [] buffer, ref int offset, int size)
252 if ((char) buffer [offset++] != '\r')
253 ThrowProtocolViolation ("Expecting \\r");
257 return State.BodyFinished;
260 if (sawCR && (char) buffer [offset++] != '\n')
261 ThrowProtocolViolation ("Expecting \\n");
266 State ReadTrailer (byte [] buffer, ref int offset, int size)
271 if ((char) buffer [offset] == '\r') {
273 if ((char) buffer [offset] == '\n') {
281 string stString = "\r\n\r";
282 while (offset < size && st < 4) {
283 c = (char) buffer [offset++];
284 if ((st == 0 || st == 2) && c == '\r') {
289 if ((st == 1 || st == 3) && c == '\n') {
295 saved.Append (stString.Substring (0, st));
302 ThrowProtocolViolation ("Error reading trailer.");
304 return State.Trailer;
307 StringReader reader = new StringReader (saved.ToString ());
309 while ((line = reader.ReadLine ()) != null && line != "")
315 static void ThrowProtocolViolation (string message)
317 WebException we = new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);