[System]: Remove 'SECURITY_DEP' conditional from HttpListener and related classes...
[mono.git] / mcs / class / System / System.Net / ChunkedInputStream.cs
index ec70bbc455fd34d6695401f4da5bff0ecce07c96..04aa77310a62ee2f7181e3f65854e06d81fe4c6e 100644 (file)
 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-#if NET_2_0
+
+using System.IO;
 using System.Net.Sockets;
 using System.Runtime.InteropServices;
 namespace System.Net {
        class ChunkedInputStream : RequestStream {
                bool disposed;
-               ChunkStream decoder;
+               MonoChunkStream decoder;
                HttpListenerContext context;
                bool no_more_data;
 
@@ -51,16 +52,16 @@ namespace System.Net {
                        }
                }
 
-               public ChunkedInputStream (HttpListenerContext context, Socket sock,
+               public ChunkedInputStream (HttpListenerContext context, Stream stream,
                                                byte [] buffer, int offset, int length)
-                                       : base (sock, buffer, offset, length)
+                                       : base (stream, buffer, offset, length)
                {
                        this.context = context;
                        WebHeaderCollection coll = (WebHeaderCollection) context.Request.Headers;
-                       decoder = new ChunkStream (coll);
+                       decoder = new MonoChunkStream (coll);
                }
 
-               public ChunkStream Decoder {
+               public MonoChunkStream Decoder {
                        get { return decoder; }
                        set { decoder = value; }
                }
@@ -94,10 +95,26 @@ namespace System.Net {
                                ares.Complete ();
                                return ares;
                        }
+                       int nread = decoder.Read (buffer, offset, count);
+                       offset += nread;
+                       count -= nread;
+                       if (count == 0) {
+                               // got all we wanted, no need to bother the decoder yet
+                               ares.Count = nread;
+                               ares.Complete ();
+                               return ares;
+                       }
+                       if (!decoder.WantMore) {
+                               no_more_data = nread == 0;
+                               ares.Count = nread;
+                               ares.Complete ();
+                               return ares;
+                       }
                        ares.Buffer = new byte [8192];
                        ares.Offset = 0;
                        ares.Count = 8192;
                        ReadBufferState rb = new ReadBufferState (buffer, offset, count, ares);
+                       rb.InitialCount += nread;
                        base.BeginRead (ares.Buffer, ares.Offset, ares.Count, OnRead, rb);
                        return ares;
                }
@@ -112,8 +129,8 @@ namespace System.Net {
                                nread = decoder.Read (rb.Buffer, rb.Offset, rb.Count);
                                rb.Offset += nread;
                                rb.Count -= nread;
-                               if (rb.Count == 0 || !decoder.WantMore) {
-                                       no_more_data = !decoder.WantMore;
+                               if (rb.Count == 0 || !decoder.WantMore || nread == 0) {
+                                       no_more_data = !decoder.WantMore && nread == 0;
                                        ares.Count = rb.InitialCount - rb.Count;
                                        ares.Complete ();
                                        return;
@@ -140,7 +157,7 @@ namespace System.Net {
                                ares.AsyncWaitHandle.WaitOne ();
 
                        if (my_ares.Error != null)
-                               throw new HttpListenerException (400, "I/O operation aborted.");
+                               throw new HttpListenerException (400, "I/O operation aborted: " + my_ares.Error.Message);
 
                        return my_ares.Count;
                }
@@ -154,4 +171,3 @@ namespace System.Net {
                }
        }
 }
-#endif