Commit ce0a71fb authored by titanium007's avatar titanium007

clean up code

parent d88558ca
......@@ -16,6 +16,7 @@ namespace Titanium.Web.Proxy.Extensions
output.Write(bytes,0, bytes.Length);
CopyToAsync(input, output, bufferSize);
}
//http://stackoverflow.com/questions/1540658/net-asynchronous-stream-read-write
public static void CopyToAsync(this Stream input, Stream output, int bufferSize)
{
......
......@@ -11,7 +11,6 @@ namespace Titanium.Web.Proxy.Helpers
{
private static readonly int BUFFER_SIZE = 8192;
public static string DecompressGzip(Stream input, Encoding e)
{
using (System.IO.Compression.GZipStream decompressor = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress))
......@@ -28,9 +27,7 @@ namespace Titanium.Web.Proxy.Helpers
}
return e.GetString(output.ToArray());
}
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
......@@ -41,15 +38,12 @@ namespace Titanium.Web.Proxy.Helpers
using (MemoryStream ms = new MemoryStream())
{
using (Ionic.Zlib.ZlibStream zip = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, true))
{
zip.Write(bytes, 0, bytes.Length);
}
return ms.ToArray();
}
}
......@@ -60,14 +54,12 @@ namespace Titanium.Web.Proxy.Helpers
using (MemoryStream ms = new MemoryStream())
{
using (Ionic.Zlib.DeflateStream zip = new Ionic.Zlib.DeflateStream(ms, Ionic.Zlib.CompressionMode.Compress, true))
{
zip.Write(bytes, 0, bytes.Length);
}
return ms.ToArray();
}
}
......@@ -84,15 +76,11 @@ namespace Titanium.Web.Proxy.Helpers
}
return ms.ToArray();
}
}
public static string DecompressDeflate(Stream input, Encoding e)
{
using (Ionic.Zlib.DeflateStream decompressor = new Ionic.Zlib.DeflateStream(input, Ionic.Zlib.CompressionMode.Decompress))
{
int read = 0;
......@@ -106,15 +94,12 @@ namespace Titanium.Web.Proxy.Helpers
}
return e.GetString(output.ToArray());
}
}
}
public static string DecompressZlib(Stream input, Encoding e)
{
using (Ionic.Zlib.ZlibStream decompressor = new Ionic.Zlib.ZlibStream(input, Ionic.Zlib.CompressionMode.Decompress))
{
int read = 0;
var buffer = new byte[BUFFER_SIZE];
......@@ -127,7 +112,6 @@ namespace Titanium.Web.Proxy.Helpers
return e.GetString(output.ToArray());
}
}
}
}
}
......@@ -21,7 +21,6 @@ namespace Titanium.Web.Proxy
/// </summary>
public partial class ProxyServer
{
private static readonly int BUFFER_SIZE = 8192;
private static readonly char[] semiSplit = new char[] { ';' };
......@@ -59,7 +58,6 @@ namespace Titanium.Web.Proxy
ListeningPort = 0;
Initialize();
}
public static void Initialize()
......@@ -102,19 +100,18 @@ namespace Titanium.Web.Proxy
client.Close();
}
}
}
public static bool Start()
{
listener = new TcpListener(ListeningIpAddress, ListeningPort);
listener.Start();
listenerThread = new Thread(new ParameterizedThreadStart(Listen));
listenerThread.IsBackground = true;
ShouldListen = true;
listenerThread.Start(listener);
ListeningPort = ((IPEndPoint)listener.LocalEndpoint).Port;
......@@ -135,15 +132,12 @@ namespace Titanium.Web.Proxy
{
SystemProxyHelper.EnableProxyHTTPS("localhost", ListeningPort);
}
}
}
return true;
}
public static void Stop()
{
if (SetAsSystemProxy)
......@@ -157,13 +151,6 @@ namespace Titanium.Web.Proxy
listenerThread.Interrupt();
CertManager.Dispose();
}
}
}
\ No newline at end of file
......@@ -19,10 +19,8 @@ using System.Text.RegularExpressions;
namespace Titanium.Web.Proxy
{
partial class ProxyServer
{
private static void HandleClient(TcpClient client)
{
Stream clientStream = client.GetStream();
......@@ -51,10 +49,10 @@ namespace Titanium.Web.Proxy
httpRemoteUri = new Uri(httpCmdSplit[1]);
var httpVersion = httpCmdSplit[2];
//Client wants to create a secure tcp tunnel (its a HTTPS request)
var excluded = ExcludedHttpsHostNameRegex.Any(x => Regex.IsMatch(httpRemoteUri.Host, x));
if (httpVerb.ToUpper() == "CONNECT" && !excluded && httpRemoteUri.Port == 443)
{
......@@ -265,23 +263,7 @@ namespace Titanium.Web.Proxy
}
private static void Dispose(TcpClient client, Stream clientStream, CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, SessionEventArgs args)
{
if (args != null)
args.Dispose();
if (clientStreamReader != null)
clientStreamReader.Dispose();
if (clientStreamWriter != null)
clientStreamWriter.Dispose();
if (clientStream != null)
clientStream.Dispose();
if (client != null)
client.Close();
}
private static void SetClientRequestHeaders(List<string> requestLines, HttpWebRequest webRequest)
{
......@@ -370,10 +352,8 @@ namespace Titanium.Web.Proxy
break;
}
}
}
//This is called when the request is PUT/POST to read the body
private static void SendClientRequestBody(SessionEventArgs args)
......@@ -503,14 +483,24 @@ namespace Titanium.Web.Proxy
}
}
private static void Dispose(TcpClient client, Stream clientStream, CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, SessionEventArgs args)
{
if (args != null)
args.Dispose();
if (clientStreamReader != null)
clientStreamReader.Dispose();
if (clientStreamWriter != null)
clientStreamWriter.Dispose();
if (clientStream != null)
clientStream.Dispose();
public static bool isHttps { get; set; }
if (client != null)
client.Close();
}
}
}
\ No newline at end of file
......@@ -94,7 +94,6 @@ namespace Titanium.Web.Proxy
}
}
catch
{
......@@ -107,7 +106,7 @@ namespace Titanium.Web.Proxy
}
}
static List<Tuple<String, String>> ProcessResponse(HttpWebResponse response)
private static List<Tuple<String, String>> ProcessResponse(HttpWebResponse response)
{
String value = null;
String header = null;
......@@ -135,31 +134,27 @@ namespace Titanium.Web.Proxy
return returnHeaders;
}
static void WriteResponseStatus(Version version, HttpStatusCode code, String description, StreamWriter responseWriter)
private static void WriteResponseStatus(Version version, HttpStatusCode code, String description, StreamWriter responseWriter)
{
String s = String.Format("HTTP/{0}.{1} {2} {3}", version.Major, version.Minor, (Int32)code, description);
responseWriter.WriteLine(s);
}
static void WriteResponseHeaders(StreamWriter responseWriter, List<Tuple<String, String>> headers)
private static void WriteResponseHeaders(StreamWriter responseWriter, List<Tuple<String, String>> headers)
{
if (headers != null)
{
foreach (Tuple<String, String> header in headers)
{
responseWriter.WriteLine(String.Format("{0}: {1}", header.Item1, header.Item2));
}
}
responseWriter.WriteLine();
responseWriter.Flush();
}
static void WriteResponseHeaders(StreamWriter responseWriter, List<Tuple<String, String>> headers, int length)
private static void WriteResponseHeaders(StreamWriter responseWriter, List<Tuple<String, String>> headers, int length)
{
if (headers != null)
{
......@@ -170,16 +165,14 @@ namespace Titanium.Web.Proxy
responseWriter.WriteLine(String.Format("{0}: {1}", header.Item1, header.Item2));
else
responseWriter.WriteLine(String.Format("{0}: {1}", "content-length", length.ToString()));
}
}
responseWriter.WriteLine();
responseWriter.Flush();
}
static void SendNormal(Stream inStream, Stream outStream)
private static void SendNormal(Stream inStream, Stream outStream)
{
Byte[] buffer = new Byte[BUFFER_SIZE];
......@@ -187,14 +180,12 @@ namespace Titanium.Web.Proxy
int bytesRead;
while ((bytesRead = inStream.Read(buffer, 0, buffer.Length)) > 0)
{
outStream.Write(buffer, 0, bytesRead);
}
}
//Send chunked response
static void SendChunked(Stream inStream, Stream outStream)
private static void SendChunked(Stream inStream, Stream outStream)
{
Byte[] buffer = new Byte[BUFFER_SIZE];
......@@ -204,7 +195,6 @@ namespace Titanium.Web.Proxy
int bytesRead;
while ((bytesRead = inStream.Read(buffer, 0, buffer.Length)) > 0)
{
var chunkHead = Encoding.ASCII.GetBytes(bytesRead.ToString("x2"));
outStream.Write(chunkHead, 0, chunkHead.Length);
outStream.Write(chunkTrail, 0, chunkTrail.Length);
......@@ -216,7 +206,7 @@ namespace Titanium.Web.Proxy
outStream.Write(ChunkEnd, 0, ChunkEnd.Length);
}
static void SendChunked(byte[] data, Stream outStream)
private static void SendChunked(byte[] data, Stream outStream)
{
Byte[] buffer = new Byte[BUFFER_SIZE];
......@@ -229,20 +219,17 @@ namespace Titanium.Web.Proxy
outStream.Write(data, 0, data.Length);
outStream.Write(chunkTrail, 0, chunkTrail.Length);
var ChunkEnd = Encoding.ASCII.GetBytes(0.ToString("x2") + Environment.NewLine + Environment.NewLine);
outStream.Write(ChunkEnd, 0, ChunkEnd.Length);
}
static byte[] EncodeData(string responseData, Encoding e)
private static byte[] EncodeData(string responseData, Encoding e)
{
return e.GetBytes(responseData);
}
static void SendData(Stream outStream, byte[] data, bool isChunked)
private static void SendData(Stream outStream, byte[] data, bool isChunked)
{
if (!isChunked)
{
......@@ -252,8 +239,5 @@ namespace Titanium.Web.Proxy
SendChunked(data, outStream);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment