Merge pull request #2003 from esdrubal/seq_test_fix2
[mono.git] / mcs / class / System / System.Net / ChunkStream.cs
index e8ac2907d2feaa8d3f103d029a527d4f1d3eb6f2..b7cc92386f56e22d45233c602cf995a8198daf1a 100644 (file)
@@ -29,6 +29,7 @@
 //
 
 using System.Collections;
+using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
 using System.Text;
@@ -39,6 +40,7 @@ namespace System.Net
        {
                enum State {
                        None,
+                       PartialSize,
                        Body,
                        BodyFinished,
                        Trailer
@@ -65,6 +67,7 @@ namespace System.Net
                internal WebHeaderCollection headers;
                int chunkSize;
                int chunkRead;
+               int totalWritten;
                State state;
                //byte [] waitBuffer;
                StringBuilder saved;
@@ -85,12 +88,14 @@ namespace System.Net
                        saved = new StringBuilder ();
                        chunks = new ArrayList ();
                        chunkSize = -1;
+                       totalWritten = 0;
                }
 
                public void ResetBuffer ()
                {
                        chunkSize = -1;
                        chunkRead = 0;
+                       totalWritten = 0;
                        chunks.Clear ();
                }
                
@@ -110,13 +115,13 @@ namespace System.Net
                {
                        int count = chunks.Count;
                        int nread = 0;
+
+                       var chunksForRemoving = new List<Chunk>(count);
                        for (int i = 0; i < count; i++) {
                                Chunk chunk = (Chunk) chunks [i];
-                               if (chunk == null)
-                                       continue;
 
                                if (chunk.Offset == chunk.Bytes.Length) {
-                                       chunks [i] = null;
+                                       chunksForRemoving.Add(chunk);
                                        continue;
                                }
                                
@@ -125,19 +130,23 @@ namespace System.Net
                                        break;
                        }
 
+                       foreach (var chunk in chunksForRemoving)
+                               chunks.Remove(chunk);
+
                        return nread;
                }
                
                public void Write (byte [] buffer, int offset, int size)
                {
-                       InternalWrite (buffer, ref offset, size);
+                       if (offset < size)
+                               InternalWrite (buffer, ref offset, size);
                }
                
                void InternalWrite (byte [] buffer, ref int offset, int size)
                {
-                       if (state == State.None) {
+                       if (state == State.None || state == State.PartialSize) {
                                state = GetChunkSize (buffer, ref offset, size);
-                               if (state == State.None)
+                               if (state == State.PartialSize)
                                        return;
                                
                                saved.Length = 0;
@@ -177,6 +186,24 @@ namespace System.Net
                        get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
                }
 
+               public bool DataAvailable {
+                       get {
+                               int count = chunks.Count;
+                               for (int i = 0; i < count; i++) {
+                                       Chunk ch = (Chunk) chunks [i];
+                                       if (ch == null || ch.Bytes == null)
+                                               continue;
+                                       if (ch.Bytes.Length > 0 && ch.Offset < ch.Bytes.Length)
+                                               return (state != State.Body);
+                               }
+                               return false;
+                       }
+               }
+
+               public int TotalDataSize {
+                       get { return totalWritten; }
+               }
+
                public int ChunkLeft {
                        get { return chunkSize - chunkRead; }
                }
@@ -195,12 +222,15 @@ namespace System.Net
                        chunks.Add (new Chunk (chunk));
                        offset += diff;
                        chunkRead += diff;
+                       totalWritten += diff;
                        return (chunkRead == chunkSize) ? State.BodyFinished : State.Body;
                                
                }
                
                State GetChunkSize (byte [] buffer, ref int offset, int size)
                {
+                       chunkRead = 0;
+                       chunkSize = 0;
                        char c = '\0';
                        while (offset < size) {
                                c = (char) buffer [offset++];
@@ -230,22 +260,23 @@ namespace System.Net
                                        ThrowProtocolViolation ("Missing \\n");
 
                                try {
-                                       if (saved.Length > 0)
-                                               chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
+                                       if (saved.Length > 0) {
+                                               chunkSize = Int32.Parse (RemoveChunkExtension (saved.ToString ()), NumberStyles.HexNumber);
+                                       }
                                } catch (Exception) {
                                        ThrowProtocolViolation ("Cannot parse chunk size.");
                                }
 
-                               return State.None;
+                               return State.PartialSize;
                        }
 
                        chunkRead = 0;
                        try {
-                               chunkSize = Int32.Parse (saved.ToString (), NumberStyles.HexNumber);
+                               chunkSize = Int32.Parse (RemoveChunkExtension (saved.ToString ()), NumberStyles.HexNumber);
                        } catch (Exception) {
                                ThrowProtocolViolation ("Cannot parse chunk size.");
                        }
-                       
+
                        if (chunkSize == 0) {
                                trailerState = 2;
                                return State.Trailer;
@@ -254,6 +285,14 @@ namespace System.Net
                        return State.Body;
                }
 
+               static string RemoveChunkExtension (string input)
+               {
+                       int idx = input.IndexOf (';');
+                       if (idx == -1)
+                               return input;
+                       return input.Substring (0, idx);
+               }
+
                State ReadCRLF (byte [] buffer, ref int offset, int size)
                {
                        if (!sawCR) {
@@ -278,7 +317,7 @@ namespace System.Net
                        // short path
                        if (trailerState == 2 && (char) buffer [offset] == '\r' && saved.Length == 0) {
                                offset++;
-                               if ((char) buffer [offset] == '\n') {
+                               if (offset < size && (char) buffer [offset] == '\n') {
                                        offset++;
                                        return State.None;
                                }
@@ -302,12 +341,14 @@ namespace System.Net
                                if (st > 0) {
                                        saved.Append (stString.Substring (0, saved.Length == 0? st-2: st));
                                        st = 0;
+                                       if (saved.Length > 4196)
+                                               ThrowProtocolViolation ("Error reading trailer (too long).");
                                }
                        }
 
                        if (st < 4) {
                                trailerState = st;
-                               if (offset <  size)
+                               if (offset < size)
                                        ThrowProtocolViolation ("Error reading trailer.");
 
                                return State.Trailer;