Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit c16e1c2

Browse files
committed
Add Redirect method and refactor out compression/decompression logic for reuse
1 parent a4d42ab commit c16e1c2

16 files changed

+302
-62
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Titanium.Web.Proxy.Compression
2+
{
3+
class CompressionFactory
4+
{
5+
public ICompression Create(string type)
6+
{
7+
switch (type)
8+
{
9+
case "gzip":
10+
return new GZipCompression();
11+
case "deflate":
12+
return new DeflateCompression();
13+
case "zlib":
14+
return new ZlibCompression();
15+
default:
16+
return null;
17+
}
18+
}
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.IO;
2+
using System.IO.Compression;
3+
4+
namespace Titanium.Web.Proxy.Compression
5+
{
6+
class DeflateCompression : ICompression
7+
{
8+
public byte[] Compress(byte[] responseBody)
9+
{
10+
using (var ms = new MemoryStream())
11+
{
12+
using (var zip = new DeflateStream(ms, CompressionMode.Compress, true))
13+
{
14+
zip.Write(responseBody, 0, responseBody.Length);
15+
}
16+
17+
return ms.ToArray();
18+
}
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Ionic.Zlib;
2+
using System.IO;
3+
4+
namespace Titanium.Web.Proxy.Compression
5+
{
6+
class GZipCompression : ICompression
7+
{
8+
public byte[] Compress(byte[] responseBody)
9+
{
10+
using (var ms = new MemoryStream())
11+
{
12+
using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
13+
{
14+
zip.Write(responseBody, 0, responseBody.Length);
15+
}
16+
17+
return ms.ToArray();
18+
}
19+
}
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Titanium.Web.Proxy.Compression
2+
{
3+
interface ICompression
4+
{
5+
byte[] Compress(byte[] responseBody);
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Ionic.Zlib;
2+
using System.IO;
3+
4+
namespace Titanium.Web.Proxy.Compression
5+
{
6+
class ZlibCompression : ICompression
7+
{
8+
public byte[] Compress(byte[] responseBody)
9+
{
10+
using (var ms = new MemoryStream())
11+
{
12+
using (var zip = new ZlibStream(ms, CompressionMode.Compress, true))
13+
{
14+
zip.Write(responseBody, 0, responseBody.Length);
15+
}
16+
17+
return ms.ToArray();
18+
}
19+
}
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Titanium.Web.Proxy.Decompression
2+
{
3+
class DecompressionFactory
4+
{
5+
public IDecompression Create(string type)
6+
{
7+
switch(type)
8+
{
9+
case "gzip":
10+
return new GZipDecompression();
11+
case "deflate":
12+
return new DeflateDecompression();
13+
case "zlib":
14+
return new ZlibDecompression();
15+
default:
16+
return new DefaultDecompression();
17+
}
18+
}
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Titanium.Web.Proxy.Decompression
2+
{
3+
class DefaultDecompression : IDecompression
4+
{
5+
public byte[] Decompress(byte[] compressedArray)
6+
{
7+
return compressedArray;
8+
}
9+
}
10+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Ionic.Zlib;
2+
using System.IO;
3+
4+
namespace Titanium.Web.Proxy.Decompression
5+
{
6+
class DeflateDecompression : IDecompression
7+
{
8+
public byte[] Decompress(byte[] compressedArray)
9+
{
10+
var stream = new MemoryStream(compressedArray);
11+
12+
using (var decompressor = new DeflateStream(stream, CompressionMode.Decompress))
13+
{
14+
var buffer = new byte[ProxyServer.BUFFER_SIZE];
15+
16+
using (var output = new MemoryStream())
17+
{
18+
int read;
19+
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
20+
{
21+
output.Write(buffer, 0, read);
22+
}
23+
24+
return output.ToArray();
25+
}
26+
}
27+
}
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.IO;
2+
using System.IO.Compression;
3+
4+
namespace Titanium.Web.Proxy.Decompression
5+
{
6+
class GZipDecompression : IDecompression
7+
{
8+
public byte[] Decompress(byte[] compressedArray)
9+
{
10+
using (var decompressor = new GZipStream(new MemoryStream(compressedArray), CompressionMode.Decompress))
11+
{
12+
var buffer = new byte[ProxyServer.BUFFER_SIZE];
13+
using (var output = new MemoryStream())
14+
{
15+
int read;
16+
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
17+
{
18+
output.Write(buffer, 0, read);
19+
}
20+
return output.ToArray();
21+
}
22+
}
23+
}
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.IO;
2+
3+
namespace Titanium.Web.Proxy.Decompression
4+
{
5+
interface IDecompression
6+
{
7+
byte[] Decompress(byte[] compressedArray);
8+
}
9+
}

0 commit comments

Comments
 (0)