Commit ce0a71fb authored by titanium007's avatar titanium007

clean up code

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