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

Add Redirect method and refactor out compression/decompression logic #62

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Titanium.Web.Proxy/Compression/CompressionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Titanium.Web.Proxy.Compression
{
class CompressionFactory
{
public ICompression Create(string type)
{
switch (type)
{
case "gzip":
return new GZipCompression();
case "deflate":
return new DeflateCompression();
case "zlib":
return new ZlibCompression();
default:
return null;
}
}
}
}
21 changes: 21 additions & 0 deletions Titanium.Web.Proxy/Compression/DeflateCompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.IO;
using System.IO.Compression;

namespace Titanium.Web.Proxy.Compression
{
class DeflateCompression : ICompression
{
public byte[] Compress(byte[] responseBody)
{
using (var ms = new MemoryStream())
{
using (var zip = new DeflateStream(ms, CompressionMode.Compress, true))
{
zip.Write(responseBody, 0, responseBody.Length);
}

return ms.ToArray();
}
}
}
}
21 changes: 21 additions & 0 deletions Titanium.Web.Proxy/Compression/GZipCompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Ionic.Zlib;
using System.IO;

namespace Titanium.Web.Proxy.Compression
{
class GZipCompression : ICompression
{
public byte[] Compress(byte[] responseBody)
{
using (var ms = new MemoryStream())
{
using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(responseBody, 0, responseBody.Length);
}

return ms.ToArray();
}
}
}
}
7 changes: 7 additions & 0 deletions Titanium.Web.Proxy/Compression/ICompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Titanium.Web.Proxy.Compression
{
interface ICompression
{
byte[] Compress(byte[] responseBody);
}
}
21 changes: 21 additions & 0 deletions Titanium.Web.Proxy/Compression/ZlibCompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Ionic.Zlib;
using System.IO;

namespace Titanium.Web.Proxy.Compression
{
class ZlibCompression : ICompression
{
public byte[] Compress(byte[] responseBody)
{
using (var ms = new MemoryStream())
{
using (var zip = new ZlibStream(ms, CompressionMode.Compress, true))
{
zip.Write(responseBody, 0, responseBody.Length);
}

return ms.ToArray();
}
}
}
}
20 changes: 20 additions & 0 deletions Titanium.Web.Proxy/Decompression/DecompressionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Titanium.Web.Proxy.Decompression
{
class DecompressionFactory
{
public IDecompression Create(string type)
{
switch(type)
{
case "gzip":
return new GZipDecompression();
case "deflate":
return new DeflateDecompression();
case "zlib":
return new ZlibDecompression();
default:
return new DefaultDecompression();
}
}
}
}
10 changes: 10 additions & 0 deletions Titanium.Web.Proxy/Decompression/DefaultDecompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Titanium.Web.Proxy.Decompression
{
class DefaultDecompression : IDecompression
{
public byte[] Decompress(byte[] compressedArray)
{
return compressedArray;
}
}
}
29 changes: 29 additions & 0 deletions Titanium.Web.Proxy/Decompression/DeflateDecompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Ionic.Zlib;
using System.IO;

namespace Titanium.Web.Proxy.Decompression
{
class DeflateDecompression : IDecompression
{
public byte[] Decompress(byte[] compressedArray)
{
var stream = new MemoryStream(compressedArray);

using (var decompressor = new DeflateStream(stream, CompressionMode.Decompress))
{
var buffer = new byte[ProxyServer.BUFFER_SIZE];

using (var output = new MemoryStream())
{
int read;
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}

return output.ToArray();
}
}
}
}
}
25 changes: 25 additions & 0 deletions Titanium.Web.Proxy/Decompression/GZipDecompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.IO;
using System.IO.Compression;

namespace Titanium.Web.Proxy.Decompression
{
class GZipDecompression : IDecompression
{
public byte[] Decompress(byte[] compressedArray)
{
using (var decompressor = new GZipStream(new MemoryStream(compressedArray), CompressionMode.Decompress))
{
var buffer = new byte[ProxyServer.BUFFER_SIZE];
using (var output = new MemoryStream())
{
int read;
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
return output.ToArray();
}
}
}
}
}
9 changes: 9 additions & 0 deletions Titanium.Web.Proxy/Decompression/IDecompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.IO;

namespace Titanium.Web.Proxy.Decompression
{
interface IDecompression
{
byte[] Decompress(byte[] compressedArray);
}
}
28 changes: 28 additions & 0 deletions Titanium.Web.Proxy/Decompression/ZlibDecompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Ionic.Zlib;
using System.IO;

namespace Titanium.Web.Proxy.Decompression
{
class ZlibDecompression : IDecompression
{
public byte[] Decompress(byte[] compressedArray)
{
var memoryStream = new MemoryStream(compressedArray);
using (var decompressor = new ZlibStream(memoryStream, CompressionMode.Decompress))
{
var buffer = new byte[ProxyServer.BUFFER_SIZE];

using (var output = new MemoryStream())
{
int read;
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
return output.ToArray();
}
}
}
}
}

77 changes: 26 additions & 51 deletions Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Network;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Responses;
using Titanium.Web.Proxy.Decompression;

namespace Titanium.Web.Proxy.EventArguments
{

/// <summary>
/// Holds info related to a single proxy session (single request/response sequence)
/// A proxy session is bounded to a single connection from client
Expand All @@ -22,13 +18,11 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
public class SessionEventArgs : EventArgs, IDisposable
{

/// <summary>
/// Constructor to initialize the proxy
/// </summary>
internal SessionEventArgs()
{

Client = new ProxyClient();
ProxySession = new HttpWebSession();
}
Expand All @@ -38,7 +32,6 @@ internal SessionEventArgs()
/// </summary>
internal ProxyClient Client { get; set; }


/// <summary>
/// Does this session uses SSL
/// </summary>
Expand All @@ -50,7 +43,6 @@ internal SessionEventArgs()
/// </summary>
public HttpWebSession ProxySession { get; set; }


/// <summary>
/// A shortcut to get the request content length
/// </summary>
Expand Down Expand Up @@ -165,22 +157,7 @@ private void ReadRequestBody()

try
{
//decompress
switch (requestContentEncoding)
{
case "gzip":
ProxySession.Request.RequestBody = CompressionHelper.DecompressGzip(requestBodyStream.ToArray());
break;
case "deflate":
ProxySession.Request.RequestBody = CompressionHelper.DecompressDeflate(requestBodyStream);
break;
case "zlib":
ProxySession.Request.RequestBody = CompressionHelper.DecompressZlib(requestBodyStream);
break;
default:
ProxySession.Request.RequestBody = requestBodyStream.ToArray();
break;
}
ProxySession.Request.RequestBody = GetDecompressedResponseBody(requestContentEncoding, requestBodyStream.ToArray());
}
catch
{
Expand Down Expand Up @@ -235,22 +212,9 @@ private void ReadResponseBody()
var buffer = ProxySession.ProxyClient.ServerStreamReader.ReadBytes(ProxySession.Response.ContentLength);
responseBodyStream.Write(buffer, 0, buffer.Length);
}
//decompress
switch (ProxySession.Response.ContentEncoding)
{
case "gzip":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressGzip(responseBodyStream.ToArray());
break;
case "deflate":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressDeflate(responseBodyStream);
break;
case "zlib":
ProxySession.Response.ResponseBody = CompressionHelper.DecompressZlib(responseBodyStream);
break;
default:
ProxySession.Response.ResponseBody = responseBodyStream.ToArray();
break;
}

ProxySession.Response.ResponseBody = GetDecompressedResponseBody(ProxySession.Response.ContentEncoding, responseBodyStream.ToArray());

}
//set this to true for caching
ProxySession.Response.ResponseBodyRead = true;
Expand Down Expand Up @@ -379,6 +343,14 @@ public void SetResponseBodyString(string body)
SetResponseBody(bodyBytes);
}

private byte[] GetDecompressedResponseBody(string encodingType, byte[] responseBodyStream)
{
var decompressionFactory = new DecompressionFactory();
var decompressor = decompressionFactory.Create(encodingType);

return decompressor.Decompress(responseBodyStream);
}


/// <summary>
/// Before request is made to server
Expand Down Expand Up @@ -406,20 +378,22 @@ public void Ok(string html)
/// <param name="body"></param>
public void Ok(byte[] result)
{
var response = new Response();
var response = new OkResponse();

response.HttpVersion = ProxySession.Request.HttpVersion;
response.ResponseStatusCode = "200";
response.ResponseStatusDescription = "Ok";
response.ResponseBody = result;

response.ResponseHeaders.Add(new HttpHeader("Timestamp", DateTime.Now.ToString()));
Respond(response);

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

response.ResponseBody = result;
public void Redirect(string url)
{
var response = new RedirectResponse();

response.HttpVersion = ProxySession.Request.HttpVersion;
response.ResponseBody = Encoding.ASCII.GetBytes(string.Empty);

Respond(response);

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

response.ResponseLocked = true;
response.ResponseBodyRead = true;
Expand Down
Loading