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;
75 public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
78 Write (buffer, offset, size);
81 public ChunkStream (WebHeaderCollection headers)
83 this.headers = headers;
84 saved = new StringBuilder ();
85 chunks = new ArrayList ();
89 public void ResetBuffer ()
96 public void WriteAndReadBack (byte [] buffer, int offset, int size, ref int read)
98 if (offset + read > 0)
99 Write (buffer, offset, offset+read);
100 read = Read (buffer, offset, size);
103 public int Read (byte [] buffer, int offset, int size)
105 return ReadFromChunks (buffer, offset, size);
108 int ReadFromChunks (byte [] buffer, int offset, int size)
110 int count = chunks.Count;
112 for (int i = 0; i < count; i++) {
113 Chunk chunk = (Chunk) chunks [i];
117 if (chunk.Offset == chunk.Bytes.Length) {
122 nread += chunk.Read (buffer, offset + nread, size - nread);
130 public void Write (byte [] buffer, int offset, int size)
132 InternalWrite (buffer, ref offset, size);
135 void InternalWrite (byte [] buffer, ref int offset, int size)
137 if (state == State.None) {
138 state = GetChunkSize (buffer, ref offset, size);
139 if (state == State.None)
147 if (state == State.Body && offset < size) {
148 state = ReadBody (buffer, ref offset, size);
149 if (state == State.Body)
153 if (state == State.BodyFinished && offset < size) {
154 state = ReadCRLF (buffer, ref offset, size);
155 if (state == State.BodyFinished)
161 if (state == State.Trailer && offset < size) {
162 state = ReadTrailer (buffer, ref offset, size);
163 if (state == State.Trailer)
172 InternalWrite (buffer, ref offset, size);
175 public bool WantMore {
176 get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
179 public int ChunkLeft {
180 get { return chunkSize - chunkRead; }
183 State ReadBody (byte [] buffer, ref int offset, int size)
186 return State.BodyFinished;
188 int diff = size - offset;
189 if (diff + chunkRead > chunkSize)
190 diff = chunkSize - chunkRead;
192 byte [] chunk = new byte [diff];
193 Buffer.BlockCopy (buffer, offset, chunk, 0, diff);
194 chunks.Add (new Chunk (chunk));
197 return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
201 State GetChunkSize (byte [] buffer, ref int offset, int size)
204 while (offset < size) {
205 c = (char) buffer [offset++];
208 ThrowProtocolViolation ("2 CR found");
214 if (sawCR && c == '\n')
223 if (saved.Length > 20)
224 ThrowProtocolViolation ("chunk size too long.");
227 if (!sawCR || c != '\n') {
229 ThrowProtocolViolation ("Missing \\n");
232 if (saved.Length > 0)
233 chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
234 } catch (Exception) {
235 ThrowProtocolViolation ("Cannot parse chunk size.");
243 chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
244 } catch (Exception) {
245 ThrowProtocolViolation ("Cannot parse chunk size.");
249 return State.Trailer;
254 State ReadCRLF (byte [] buffer, ref int offset, int size)
257 if ((char) buffer [offset++] != '\r')
258 ThrowProtocolViolation ("Expecting \\r");
262 return State.BodyFinished;
265 if (sawCR && (char) buffer [offset++] != '\n')
266 ThrowProtocolViolation ("Expecting \\n");
271 State ReadTrailer (byte [] buffer, ref int offset, int size)
276 if ((char) buffer [offset] == '\r') {
278 if ((char) buffer [offset] == '\n') {
286 string stString = "\r\n\r";
287 while (offset < size && st < 4) {
288 c = (char) buffer [offset++];
289 if ((st == 0 || st == 2) && c == '\r') {
294 if ((st == 1 || st == 3) && c == '\n') {
300 saved.Append (stString.Substring (0, st));
307 ThrowProtocolViolation ("Error reading trailer.");
309 return State.Trailer;
312 StringReader reader = new StringReader (saved.ToString ());
314 while ((line = reader.ReadLine ()) != null && line != "")
320 static void ThrowProtocolViolation (string message)
322 WebException we = new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);