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 internal WebHeaderCollection headers;
76 public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
79 Write (buffer, offset, size);
82 public ChunkStream (WebHeaderCollection headers)
84 this.headers = headers;
85 saved = new StringBuilder ();
86 chunks = new ArrayList ();
90 public void ResetBuffer ()
97 public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
99 if (offset + read > 0)
100 Write (buffer, offset, offset+read);
101 read = Read (buffer, offset, size);
104 public int Read (byte [] buffer, int offset, int size)
106 return ReadFromChunks (buffer, offset, size);
109 int ReadFromChunks (byte [] buffer, int offset, int size)
111 int count = chunks.Count;
113 for (int i = 0; i < count; i++) {
114 Chunk chunk = (Chunk) chunks [i];
118 if (chunk.Offset == chunk.Bytes.Length) {
123 nread += chunk.Read (buffer, offset + nread, size - nread);
131 public void Write (byte [] buffer, int offset, int size)
133 InternalWrite (buffer, ref offset, size);
136 void InternalWrite (byte [] buffer, ref int offset, int size)
138 if (state == State.None) {
139 state = GetChunkSize (buffer, ref offset, size);
140 if (state == State.None)
148 if (state == State.Body && offset < size) {
149 state = ReadBody (buffer, ref offset, size);
150 if (state == State.Body)
154 if (state == State.BodyFinished && offset < size) {
155 state = ReadCRLF (buffer, ref offset, size);
156 if (state == State.BodyFinished)
162 if (state == State.Trailer && offset < size) {
163 state = ReadTrailer (buffer, ref offset, size);
164 if (state == State.Trailer)
173 InternalWrite (buffer, ref offset, size);
176 public bool WantMore {
177 get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
180 public int ChunkLeft {
181 get { return chunkSize - chunkRead; }
184 State ReadBody (byte [] buffer, ref int offset, int size)
187 return State.BodyFinished;
189 int diff = size - offset;
190 if (diff + chunkRead > chunkSize)
191 diff = chunkSize - chunkRead;
193 byte [] chunk = new byte [diff];
194 Buffer.BlockCopy (buffer, offset, chunk, 0, diff);
195 chunks.Add (new Chunk (chunk));
198 return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
202 State GetChunkSize (byte [] buffer, ref int offset, int size)
205 while (offset < size) {
206 c = (char) buffer [offset++];
209 ThrowProtocolViolation ("2 CR found");
215 if (sawCR && c == '\n')
224 if (saved.Length > 20)
225 ThrowProtocolViolation ("chunk size too long.");
228 if (!sawCR || c != '\n') {
230 ThrowProtocolViolation ("Missing \\n");
233 if (saved.Length > 0) {
234 chunkSize = Int32.Parse (RemoveChunkExtension (saved.ToString ()), NumberStyles.HexNumber);
236 } catch (Exception) {
237 ThrowProtocolViolation ("Cannot parse chunk size.");
245 chunkSize = Int32.Parse (RemoveChunkExtension (saved.ToString ()), NumberStyles.HexNumber);
246 } catch (Exception) {
247 ThrowProtocolViolation ("Cannot parse chunk size.");
250 if (chunkSize == 0) {
252 return State.Trailer;
258 static string RemoveChunkExtension (string input)
260 int idx = input.IndexOf (';');
263 return input.Substring (0, idx);
266 State ReadCRLF (byte [] buffer, ref int offset, int size)
269 if ((char) buffer [offset++] != '\r')
270 ThrowProtocolViolation ("Expecting \\r");
274 return State.BodyFinished;
277 if (sawCR && (char) buffer [offset++] != '\n')
278 ThrowProtocolViolation ("Expecting \\n");
283 State ReadTrailer (byte [] buffer, ref int offset, int size)
288 if (trailerState == 2 && (char) buffer [offset] == '\r' && saved.Length == 0) {
290 if (offset < size && (char) buffer [offset] == '\n') {
297 int st = trailerState;
298 string stString = "\r\n\r";
299 while (offset < size && st < 4) {
300 c = (char) buffer [offset++];
301 if ((st == 0 || st == 2) && c == '\r') {
306 if ((st == 1 || st == 3) && c == '\n') {
312 saved.Append (stString.Substring (0, saved.Length == 0? st-2: st));
314 if (saved.Length > 4196)
315 ThrowProtocolViolation ("Error reading trailer (too long).");
322 ThrowProtocolViolation ("Error reading trailer.");
324 return State.Trailer;
327 StringReader reader = new StringReader (saved.ToString ());
329 while ((line = reader.ReadLine ()) != null && line != "")
335 static void ThrowProtocolViolation (string message)
337 WebException we = new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);