Commit 9e41d558 authored by justcoding121's avatar justcoding121 Committed by justcoding121

Improve code brevity

parent 4b302863
...@@ -56,7 +56,4 @@ Global ...@@ -56,7 +56,4 @@ Global
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
EnterpriseLibraryConfigurationToolBinariesPath = .1.505.2\lib\NET35 EnterpriseLibraryConfigurationToolBinariesPath = .1.505.2\lib\NET35
EndGlobalSection EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal EndGlobal
...@@ -99,17 +99,7 @@ namespace Titanium.Web.Proxy ...@@ -99,17 +99,7 @@ namespace Titanium.Web.Proxy
{ {
TcpHelper.SendRaw(tunnelHostName, tunnelPort, clientStreamReader.BaseStream); TcpHelper.SendRaw(tunnelHostName, tunnelPort, clientStreamReader.BaseStream);
if (clientStreamReader != null) Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null);
clientStreamReader.Dispose();
if (clientStreamWriter != null)
clientStreamWriter.Dispose();
if (clientStream != null)
clientStream.Dispose();
if (client != null)
client.Close();
return; return;
} }
...@@ -159,17 +149,7 @@ namespace Titanium.Web.Proxy ...@@ -159,17 +149,7 @@ namespace Titanium.Web.Proxy
//Hostname was a previously failed request due to certificate pinning, just relay (tunnel the request) //Hostname was a previously failed request due to certificate pinning, just relay (tunnel the request)
TcpHelper.SendRaw(tunnelHostName, tunnelPort, clientStreamReader.BaseStream); TcpHelper.SendRaw(tunnelHostName, tunnelPort, clientStreamReader.BaseStream);
if (clientStreamReader != null) Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null);
clientStreamReader.Dispose();
if (clientStreamWriter != null)
clientStreamWriter.Dispose();
if (clientStream != null)
clientStream.Dispose();
if (client != null)
client.Close();
return; return;
} }
...@@ -198,17 +178,7 @@ namespace Titanium.Web.Proxy ...@@ -198,17 +178,7 @@ namespace Titanium.Web.Proxy
} }
catch catch
{ {
if (clientStreamReader != null) Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null);
clientStreamReader.Dispose();
if (clientStreamWriter != null)
clientStreamWriter.Dispose();
if (clientStream != null)
clientStream.Dispose();
if (client != null)
client.Close();
} }
...@@ -219,17 +189,7 @@ namespace Titanium.Web.Proxy ...@@ -219,17 +189,7 @@ namespace Titanium.Web.Proxy
if (httpCmd == null) if (httpCmd == null)
{ {
if (clientStreamReader != null) Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null);
clientStreamReader.Dispose();
if (clientStreamWriter != null)
clientStreamWriter.Dispose();
if (clientStream != null)
clientStream.Dispose();
if (client != null)
client.Close();
return; return;
} }
...@@ -249,20 +209,7 @@ namespace Titanium.Web.Proxy ...@@ -249,20 +209,7 @@ namespace Titanium.Web.Proxy
{ {
TcpHelper.SendRaw(httpCmd, tunnelHostName, requestLines, args.IsSSLRequest, clientStreamReader.BaseStream); TcpHelper.SendRaw(httpCmd, tunnelHostName, requestLines, args.IsSSLRequest, clientStreamReader.BaseStream);
if (args != null) Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
args.Dispose();
if (clientStreamReader != null)
clientStreamReader.Dispose();
if (clientStreamWriter != null)
clientStreamWriter.Dispose();
if (clientStream != null)
clientStream.Dispose();
if (client != null)
client.Close();
return; return;
} }
...@@ -305,20 +252,7 @@ namespace Titanium.Web.Proxy ...@@ -305,20 +252,7 @@ namespace Titanium.Web.Proxy
TcpHelper.SendRaw(httpCmd, tunnelHostName, requestLines, args.IsSSLRequest, clientStreamReader.BaseStream); TcpHelper.SendRaw(httpCmd, tunnelHostName, requestLines, args.IsSSLRequest, clientStreamReader.BaseStream);
if (args != null) Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
args.Dispose();
if (clientStreamReader != null)
clientStreamReader.Dispose();
if (clientStreamWriter != null)
clientStreamWriter.Dispose();
if (clientStream != null)
clientStream.Dispose();
if (client != null)
client.Close();
return; return;
} }
...@@ -351,20 +285,7 @@ namespace Titanium.Web.Proxy ...@@ -351,20 +285,7 @@ namespace Titanium.Web.Proxy
string tmpLine; string tmpLine;
if (args.CancelRequest) if (args.CancelRequest)
{ {
if (args != null) Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
args.Dispose();
if (clientStreamReader != null)
clientStreamReader.Dispose();
if (clientStreamWriter != null)
clientStreamWriter.Dispose();
if (clientStream != null)
clientStream.Dispose();
if (client != null)
client.Close();
return; return;
} }
...@@ -390,17 +311,11 @@ namespace Titanium.Web.Proxy ...@@ -390,17 +311,11 @@ namespace Titanium.Web.Proxy
{ {
SendClientRequestBody(args); SendClientRequestBody(args);
//Http request body sent, now wait asynchronously for response
args.ProxyRequest.BeginGetResponse(new AsyncCallback(HandleHttpSessionResponse), args);
}
else
{
//otherwise wait for response asynchronously
args.ProxyRequest.BeginGetResponse(new AsyncCallback(HandleHttpSessionResponse), args);
} }
//Http request body sent, now wait asynchronously for response
args.ProxyRequest.BeginGetResponse(new AsyncCallback(HandleHttpSessionResponse), args);
} }
//Now read the next request (if keep-Alive is enabled, otherwise exit this thread) //Now read the next request (if keep-Alive is enabled, otherwise exit this thread)
...@@ -421,25 +336,27 @@ namespace Titanium.Web.Proxy ...@@ -421,25 +336,27 @@ namespace Titanium.Web.Proxy
} }
catch catch
{ {
if (args != null) Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
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 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)
{ {
...@@ -508,6 +425,7 @@ namespace Titanium.Web.Proxy ...@@ -508,6 +425,7 @@ namespace Titanium.Web.Proxy
case "user-agent": case "user-agent":
webRequest.UserAgent = header[1]; webRequest.UserAgent = header[1];
break; break;
//revisit this, transfer-encoding is not a request header according to spec //revisit this, transfer-encoding is not a request header according to spec
//But how to identify if client is sending chunked body for PUT/POST? //But how to identify if client is sending chunked body for PUT/POST?
case "transfer-encoding": case "transfer-encoding":
...@@ -537,7 +455,6 @@ namespace Titanium.Web.Proxy ...@@ -537,7 +455,6 @@ namespace Titanium.Web.Proxy
private static void SendClientRequestBody(SessionEventArgs args) private static void SendClientRequestBody(SessionEventArgs args)
{ {
// End the operation // End the operation
Stream postStream = args.ProxyRequest.GetRequestStream(); Stream postStream = args.ProxyRequest.GetRequestStream();
...@@ -574,12 +491,15 @@ namespace Titanium.Web.Proxy ...@@ -574,12 +491,15 @@ namespace Titanium.Web.Proxy
} }
postStream.Close(); postStream.Close();
postStream.Dispose();
} }
catch catch
{ {
if (postStream != null) if (postStream != null)
{
postStream.Close(); postStream.Close();
postStream.Dispose();
}
throw; throw;
} }
...@@ -649,7 +569,10 @@ namespace Titanium.Web.Proxy ...@@ -649,7 +569,10 @@ namespace Titanium.Web.Proxy
catch catch
{ {
if (postStream != null) if (postStream != null)
{
postStream.Close(); postStream.Close();
postStream.Dispose();
}
throw; throw;
} }
......
...@@ -97,24 +97,11 @@ namespace Titanium.Web.Proxy ...@@ -97,24 +97,11 @@ namespace Titanium.Web.Proxy
} }
} }
catch catch
{ {
if (args.ClientStreamReader != null) Dispose(args.Client, args.ClientStream, args.ClientStreamReader, args.ClientStreamWriter, args);
args.ClientStreamReader.Dispose();
if (args.ClientStreamWriter != null)
args.ClientStreamWriter.Dispose();
if (args.ClientStream != null)
args.ClientStream.Dispose();
if (args.Client != null)
args.Client.Close();
} }
finally finally
{ {
if (args != null) if (args != null)
......
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