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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Ionic.Zlib;
2+
using System.IO;
3+
4+
namespace Titanium.Web.Proxy.Decompression
5+
{
6+
class ZlibDecompression : IDecompression
7+
{
8+
public byte[] Decompress(byte[] compressedArray)
9+
{
10+
var memoryStream = new MemoryStream(compressedArray);
11+
using (var decompressor = new ZlibStream(memoryStream, CompressionMode.Decompress))
12+
{
13+
var buffer = new byte[ProxyServer.BUFFER_SIZE];
14+
15+
using (var output = new MemoryStream())
16+
{
17+
int read;
18+
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
19+
{
20+
output.Write(buffer, 0, read);
21+
}
22+
return output.ToArray();
23+
}
24+
}
25+
}
26+
}
27+
}
28+

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Globalization;
43
using System.IO;
54
using System.Linq;
6-
using System.Net;
7-
using System.Net.Sockets;
85
using System.Text;
96
using Titanium.Web.Proxy.Exceptions;
10-
using Titanium.Web.Proxy.Helpers;
117
using Titanium.Web.Proxy.Network;
12-
using Titanium.Web.Proxy.Models;
8+
using Titanium.Web.Proxy.Responses;
9+
using Titanium.Web.Proxy.Decompression;
1310

1411
namespace Titanium.Web.Proxy.EventArguments
1512
{
16-
1713
/// <summary>
1814
/// Holds info related to a single proxy session (single request/response sequence)
1915
/// A proxy session is bounded to a single connection from client
@@ -22,13 +18,11 @@ namespace Titanium.Web.Proxy.EventArguments
2218
/// </summary>
2319
public class SessionEventArgs : EventArgs, IDisposable
2420
{
25-
2621
/// <summary>
2722
/// Constructor to initialize the proxy
2823
/// </summary>
2924
internal SessionEventArgs()
3025
{
31-
3226
Client = new ProxyClient();
3327
ProxySession = new HttpWebSession();
3428
}
@@ -38,7 +32,6 @@ internal SessionEventArgs()
3832
/// </summary>
3933
internal ProxyClient Client { get; set; }
4034

41-
4235
/// <summary>
4336
/// Does this session uses SSL
4437
/// </summary>
@@ -50,7 +43,6 @@ internal SessionEventArgs()
5043
/// </summary>
5144
public HttpWebSession ProxySession { get; set; }
5245

53-
5446
/// <summary>
5547
/// A shortcut to get the request content length
5648
/// </summary>
@@ -165,22 +157,7 @@ private void ReadRequestBody()
165157

166158
try
167159
{
168-
//decompress
169-
switch (requestContentEncoding)
170-
{
171-
case "gzip":
172-
ProxySession.Request.RequestBody = CompressionHelper.DecompressGzip(requestBodyStream.ToArray());
173-
break;
174-
case "deflate":
175-
ProxySession.Request.RequestBody = CompressionHelper.DecompressDeflate(requestBodyStream);
176-
break;
177-
case "zlib":
178-
ProxySession.Request.RequestBody = CompressionHelper.DecompressZlib(requestBodyStream);
179-
break;
180-
default:
181-
ProxySession.Request.RequestBody = requestBodyStream.ToArray();
182-
break;
183-
}
160+
ProxySession.Request.RequestBody = GetDecompressedResponseBody(requestContentEncoding, requestBodyStream.ToArray());
184161
}
185162
catch
186163
{
@@ -235,22 +212,9 @@ private void ReadResponseBody()
235212
var buffer = ProxySession.ProxyClient.ServerStreamReader.ReadBytes(ProxySession.Response.ContentLength);
236213
responseBodyStream.Write(buffer, 0, buffer.Length);
237214
}
238-
//decompress
239-
switch (ProxySession.Response.ContentEncoding)
240-
{
241-
case "gzip":
242-
ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(responseBodyStream.ToArray());
243-
break;
244-
case "deflate":
245-
ProxySession.Response.ResponseBody = CompressionHelper.DecompressDeflate(responseBodyStream);
246-
break;
247-
case "zlib":
248-
ProxySession.Response.ResponseBody = CompressionHelper.DecompressZlib(responseBodyStream);
249-
break;
250-
default:
251-
ProxySession.Response.ResponseBody = responseBodyStream.ToArray();
252-
break;
253-
}
215+
216+
ProxySession.Response.ResponseBody = GetDecompressedResponseBody(ProxySession.Response.ContentEncoding, responseBodyStream.ToArray());
217+
254218
}
255219
//set this to true for caching
256220
ProxySession.Response.ResponseBodyRead = true;
@@ -379,6 +343,14 @@ public void SetResponseBodyString(string body)
379343
SetResponseBody(bodyBytes);
380344
}
381345

346+
private byte[] GetDecompressedResponseBody(string encodingType, byte[] responseBodyStream)
347+
{
348+
var decompressionFactory = new DecompressionFactory();
349+
var decompressor = decompressionFactory.Create(encodingType);
350+
351+
return decompressor.Decompress(responseBodyStream);
352+
}
353+
382354

383355
/// <summary>
384356
/// Before request is made to server
@@ -406,20 +378,22 @@ public void Ok(string html)
406378
/// <param name="body"></param>
407379
public void Ok(byte[] result)
408380
{
409-
var response = new Response();
381+
var response = new OkResponse();
410382

411383
response.HttpVersion = ProxySession.Request.HttpVersion;
412-
response.ResponseStatusCode = "200";
413-
response.ResponseStatusDescription = "Ok";
384+
response.ResponseBody = result;
414385

415-
response.ResponseHeaders.Add(new HttpHeader("Timestamp", DateTime.Now.ToString()));
386+
Respond(response);
416387

417-
response.ResponseHeaders.Add(new HttpHeader("content-length", DateTime.Now.ToString()));
418-
response.ResponseHeaders.Add(new HttpHeader("Cache-Control", "no-cache, no-store, must-revalidate"));
419-
response.ResponseHeaders.Add(new HttpHeader("Pragma", "no-cache"));
420-
response.ResponseHeaders.Add(new HttpHeader("Expires", "0"));
388+
ProxySession.Request.CancelRequest = true;
389+
}
421390

422-
response.ResponseBody = result;
391+
public void Redirect(string url)
392+
{
393+
var response = new RedirectResponse();
394+
395+
response.HttpVersion = ProxySession.Request.HttpVersion;
396+
response.ResponseBody = Encoding.ASCII.GetBytes(string.Empty);
423397

424398
Respond(response);
425399

@@ -430,6 +404,7 @@ public void Ok(byte[] result)
430404
public void Respond(Response response)
431405
{
432406
ProxySession.Request.RequestLocked = true;
407+
ProxySession.Response.ResponseLocked = true;
433408

434409
response.ResponseLocked = true;
435410
response.ResponseBodyRead = true;

0 commit comments

Comments
 (0)