[system] Redirect of put requests was converted to get. Fixes #16670
[mono.git] / mcs / class / System / System.Net / ChunkStream.cs
index 8243042b7614f9f168980602fb122a0db7e620d2..b3888fb1581c24f417e505c54c4423411b41c185 100644 (file)
@@ -70,6 +70,7 @@ namespace System.Net
                StringBuilder saved;
                bool sawCR;
                bool gotit;
+               int trailerState;
                ArrayList chunks;
                
                public ChunkStream (byte [] buffer, int offset, int size, WebHeaderCollection headers)
@@ -129,7 +130,8 @@ namespace System.Net
                
                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)
@@ -176,6 +178,20 @@ 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 ChunkLeft {
                        get { return chunkSize - chunkRead; }
                }
@@ -200,6 +216,8 @@ namespace System.Net
                
                State GetChunkSize (byte [] buffer, ref int offset, int size)
                {
+                       chunkRead = 0;
+                       chunkSize = 0;
                        char c = '\0';
                        while (offset < size) {
                                c = (char) buffer [offset++];
@@ -229,8 +247,9 @@ 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.");
                                }
@@ -240,17 +259,27 @@ namespace System.Net
 
                        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)
+
+                       if (chunkSize == 0) {
+                               trailerState = 2;
                                return State.Trailer;
+                       }
 
                        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) {
@@ -273,16 +302,16 @@ namespace System.Net
                        char c = '\0';
 
                        // short path
-                       if ((char) buffer [offset] == '\r') {
+                       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;
                                }
                                offset--;
                        }
                        
-                       int st = 0;
+                       int st = trailerState;
                        string stString = "\r\n\r";
                        while (offset < size && st < 4) {
                                c = (char) buffer [offset++];
@@ -297,13 +326,16 @@ namespace System.Net
                                }
 
                                if (st > 0) {
-                                       saved.Append (stString.Substring (0, st));
+                                       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) {
-                               if (offset <  size)
+                               trailerState = st;
+                               if (offset < size)
                                        ThrowProtocolViolation ("Error reading trailer.");
 
                                return State.Trailer;