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)
134 InternalWrite (buffer, ref offset, size);
137 void InternalWrite (byte [] buffer, ref int offset, int size)
139 if (state == State.None) {
140 state = GetChunkSize (buffer, ref offset, size);
141 if (state == State.None)
149 if (state == State.Body && offset < size) {
150 state = ReadBody (buffer, ref offset, size);
151 if (state == State.Body)
155 if (state == State.BodyFinished && offset < size) {
156 state = ReadCRLF (buffer, ref offset, size);
157 if (state == State.BodyFinished)
163 if (state == State.Trailer && offset < size) {
164 state = ReadTrailer (buffer, ref offset, size);
165 if (state == State.Trailer)
174 InternalWrite (buffer, ref offset, size);
177 public bool WantMore {
178 get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
181 public bool DataAvailable {
183 int count = chunks.Count;
184 for (int i = 0; i < count; i++) {
185 Chunk ch = (Chunk) chunks [i];
186 if (ch == null || ch.Bytes == null)
188 if (ch.Bytes.Length > 0 && ch.Offset < ch.Bytes.Length)
189 return (state != State.Body);
195 public int ChunkLeft {
196 get { return chunkSize - chunkRead; }
199 State ReadBody (byte [] buffer, ref int offset, int size)
202 return State.BodyFinished;
204 int diff = size - offset;
205 if (diff + chunkRead > chunkSize)
206 diff = chunkSize - chunkRead;
208 byte [] chunk = new byte [diff];
209 Buffer.BlockCopy (buffer, offset, chunk, 0, diff);
210 chunks.Add (new Chunk (chunk));
213 return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
217 State GetChunkSize (byte [] buffer, ref int offset, int size)
222 while (offset < size) {
223 c = (char) buffer [offset++];
226 ThrowProtocolViolation ("2 CR found");
232 if (sawCR && c == '\n')
241 if (saved.Length > 20)
242 ThrowProtocolViolation ("chunk size too long.");
245 if (!sawCR || c != '\n') {
247 ThrowProtocolViolation ("Missing \\n");
250 if (saved.Length > 0) {
251 chunkSize = Int32.Parse (RemoveChunkExtension (saved.ToString ()), NumberStyles.HexNumber);
253 } catch (Exception) {
254 ThrowProtocolViolation ("Cannot parse chunk size.");
262 chunkSize = Int32.Parse (RemoveChunkExtension (saved.ToString ()), NumberStyles.HexNumber);
263 } catch (Exception) {
264 ThrowProtocolViolation ("Cannot parse chunk size.");
267 if (chunkSize == 0) {
269 return State.Trailer;
275 static string RemoveChunkExtension (string input)
277 int idx = input.IndexOf (';');
280 return input.Substring (0, idx);
283 State ReadCRLF (byte [] buffer, ref int offset, int size)
286 if ((char) buffer [offset++] != '\r')
287 ThrowProtocolViolation ("Expecting \\r");
291 return State.BodyFinished;
294 if (sawCR && (char) buffer [offset++] != '\n')
295 ThrowProtocolViolation ("Expecting \\n");
300 State ReadTrailer (byte [] buffer, ref int offset, int size)
305 if (trailerState == 2 && (char) buffer [offset] == '\r' && saved.Length == 0) {
307 if (offset < size && (char) buffer [offset] == '\n') {
314 int st = trailerState;
315 string stString = "\r\n\r";
316 while (offset < size && st < 4) {
317 c = (char) buffer [offset++];
318 if ((st == 0 || st == 2) && c == '\r') {
323 if ((st == 1 || st == 3) && c == '\n') {
329 saved.Append (stString.Substring (0, saved.Length == 0? st-2: st));
331 if (saved.Length > 4196)
332 ThrowProtocolViolation ("Error reading trailer (too long).");
339 ThrowProtocolViolation ("Error reading trailer.");
341 return State.Trailer;
344 StringReader reader = new StringReader (saved.ToString ());
346 while ((line = reader.ReadLine ()) != null && line != "")
352 static void ThrowProtocolViolation (string message)
354 WebException we = new WebException (message, null, WebExceptionStatus.ServerProtocolViolation, null);