From 197b0629935b9b240915485dd3ed631b19b74d7d Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Wed, 31 May 2017 10:35:00 +0200 Subject: [PATCH] THRIFT-4217 HttpClient should support gzip and deflate Client: C# Patch: Jens Geyer --- lib/csharp/src/Net35/ExtensionsNet35.cs | 31 +++++++++++++++++++++++++ lib/csharp/src/Thrift.csproj | 1 + lib/csharp/src/Transport/THttpClient.cs | 24 +++++++++++-------- 3 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 lib/csharp/src/Net35/ExtensionsNet35.cs diff --git a/lib/csharp/src/Net35/ExtensionsNet35.cs b/lib/csharp/src/Net35/ExtensionsNet35.cs new file mode 100644 index 000000000..73a423288 --- /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 e3022a4e8..83bc4f7d0 100644 --- a/lib/csharp/src/Thrift.csproj +++ b/lib/csharp/src/Thrift.csproj @@ -79,6 +79,7 @@ + diff --git a/lib/csharp/src/Transport/THttpClient.cs b/lib/csharp/src/Transport/THttpClient.cs index e68d33dc8..f678d1e1b 100644 --- a/lib/csharp/src/Transport/THttpClient.cs +++ b/lib/csharp/src/Transport/THttpClient.cs @@ -195,18 +195,22 @@ namespace Thrift.Transport 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; + } } } }