THRIFT-4217 HttpClient should support gzip and deflate
Client: C#
Patch: Jens Geyer
diff --git a/lib/csharp/src/Net35/ExtensionsNet35.cs b/lib/csharp/src/Net35/ExtensionsNet35.cs
new file mode 100644
index 0000000..73a4232
--- /dev/null
+++ b/lib/csharp/src/Net35/ExtensionsNet35.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+
+#if (!NET45)
+namespace Thrift
+{
+    static class StreamExtensionsNet35
+    {
+        // CopyTo() has been added in 4.0
+        public static long CopyTo(this Stream source, Stream target)
+        {
+            byte[] buffer = new byte[8192];  // multiple of 4096
+            long nTotal = 0;
+            while (true)
+            {
+                int nRead = source.Read(buffer, 0, buffer.Length);
+                if (nRead <= 0)  // done?
+                    return nTotal;
+
+                target.Write(buffer, 0, nRead);
+                nTotal += nRead;
+            }
+        }
+    }
+
+}
+#endif
+
diff --git a/lib/csharp/src/Thrift.csproj b/lib/csharp/src/Thrift.csproj
index e3022a4..83bc4f7 100644
--- a/lib/csharp/src/Thrift.csproj
+++ b/lib/csharp/src/Thrift.csproj
@@ -79,6 +79,7 @@
   <ItemGroup>
     <Compile Include="Collections\TCollections.cs" />
     <Compile Include="Collections\THashSet.cs" />
+    <Compile Include="Net35\ExtensionsNet35.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Protocol\TAbstractBase.cs" />
     <Compile Include="Protocol\TBase.cs" />
diff --git a/lib/csharp/src/Transport/THttpClient.cs b/lib/csharp/src/Transport/THttpClient.cs
index e68d33d..f678d1e 100644
--- a/lib/csharp/src/Transport/THttpClient.cs
+++ b/lib/csharp/src/Transport/THttpClient.cs
@@ -195,18 +195,22 @@
                             inputStream.Seek(0, 0);
                         }
 
-                        foreach( var encoding in response.Headers.GetValues("Content-Encoding"))
+                        var encodings = response.Headers.GetValues("Content-Encoding");
+                        if (encodings != null)
                         {
-                            switch(encoding)
+                            foreach (var encoding in encodings)
                             {
-                                case "gzip":
-                                    DecompressGZipped(ref inputStream);
-                                    break;
-                                case "deflate":
-                                    DecompressDeflated(ref inputStream);
-                                    break;
-                                default:
-                                    break;
+                                switch (encoding)
+                                {
+                                    case "gzip":
+                                        DecompressGZipped(ref inputStream);
+                                        break;
+                                    case "deflate":
+                                        DecompressDeflated(ref inputStream);
+                                        break;
+                                    default:
+                                        break;
+                                }
                             }
                         }
                     }