Commit 412b50b8 authored by Honfika's avatar Honfika

reuse buffers, missing dispose added

parent 81d32f79
...@@ -522,6 +522,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -522,6 +522,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
WebSession.Dispose();
} }
} }
} }
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
...@@ -18,10 +19,15 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -18,10 +19,15 @@ namespace Titanium.Web.Proxy.Helpers
private readonly byte[] staticBuffer; private readonly byte[] staticBuffer;
private readonly Encoding encoding; private readonly Encoding encoding;
private static readonly ConcurrentQueue<byte[]> buffers = new ConcurrentQueue<byte[]>();
internal CustomBinaryReader(CustomBufferedStream stream, int bufferSize) internal CustomBinaryReader(CustomBufferedStream stream, int bufferSize)
{ {
this.stream = stream; this.stream = stream;
staticBuffer = new byte[bufferSize]; if (!buffers.TryDequeue(out staticBuffer) || staticBuffer.Length != bufferSize)
{
staticBuffer = new byte[bufferSize];
}
this.bufferSize = bufferSize; this.bufferSize = bufferSize;
...@@ -148,6 +154,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -148,6 +154,7 @@ namespace Titanium.Web.Proxy.Helpers
public void Dispose() public void Dispose()
{ {
buffers.Enqueue(staticBuffer);
} }
private void ResizeBuffer(ref byte[] buffer, long size) private void ResizeBuffer(ref byte[] buffer, long size)
......
...@@ -11,7 +11,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -11,7 +11,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary> /// <summary>
/// Used to communicate with the server over HTTP(S) /// Used to communicate with the server over HTTP(S)
/// </summary> /// </summary>
public class HttpWebClient public class HttpWebClient : IDisposable
{ {
/// <summary> /// <summary>
/// Connection to server /// Connection to server
...@@ -208,5 +208,13 @@ namespace Titanium.Web.Proxy.Http ...@@ -208,5 +208,13 @@ namespace Titanium.Web.Proxy.Http
//Read the response headers in to unique and non-unique header collections //Read the response headers in to unique and non-unique header collections
await HeaderParser.ReadHeaders(ServerConnection.StreamReader, Response.NonUniqueResponseHeaders, Response.ResponseHeaders); await HeaderParser.ReadHeaders(ServerConnection.StreamReader, Response.NonUniqueResponseHeaders, Response.ResponseHeaders);
} }
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
ServerConnection.Dispose();
}
} }
} }
...@@ -44,7 +44,6 @@ namespace Titanium.Web.Proxy ...@@ -44,7 +44,6 @@ namespace Titanium.Web.Proxy
if (string.IsNullOrEmpty(httpCmd)) if (string.IsNullOrEmpty(httpCmd))
{ {
Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
return; return;
} }
...@@ -99,7 +98,6 @@ namespace Titanium.Web.Proxy ...@@ -99,7 +98,6 @@ namespace Titanium.Web.Proxy
if (await CheckAuthorization(clientStreamWriter, connectRequestHeaders) == false) if (await CheckAuthorization(clientStreamWriter, connectRequestHeaders) == false)
{ {
Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
return; return;
} }
...@@ -111,7 +109,8 @@ namespace Titanium.Web.Proxy ...@@ -111,7 +109,8 @@ namespace Titanium.Web.Proxy
{ {
sslStream = new SslStream(clientStream, true); sslStream = new SslStream(clientStream, true);
var certificate = endPoint.GenericCertificate ?? CertificateManager.CreateCertificate(httpRemoteUri.Host, false); var certificate = endPoint.GenericCertificate ??
CertificateManager.CreateCertificate(httpRemoteUri.Host, false);
//Successfully managed to authenticate the client using the fake certificate //Successfully managed to authenticate the client using the fake certificate
await sslStream.AuthenticateAsServerAsync(certificate, false, await sslStream.AuthenticateAsServerAsync(certificate, false,
...@@ -119,14 +118,13 @@ namespace Titanium.Web.Proxy ...@@ -119,14 +118,13 @@ namespace Titanium.Web.Proxy
//HTTPS server created - we can now decrypt the client's traffic //HTTPS server created - we can now decrypt the client's traffic
clientStream = new CustomBufferedStream(sslStream, BufferSize); clientStream = new CustomBufferedStream(sslStream, BufferSize);
clientStreamReader.Dispose();
clientStreamReader = new CustomBinaryReader(clientStream, BufferSize); clientStreamReader = new CustomBinaryReader(clientStream, BufferSize);
clientStreamWriter = new StreamWriter(clientStream) { NewLine = ProxyConstants.NewLine }; clientStreamWriter = new StreamWriter(clientStream) {NewLine = ProxyConstants.NewLine};
} }
catch catch
{ {
sslStream?.Dispose(); sslStream?.Dispose();
Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
return; return;
} }
...@@ -141,24 +139,25 @@ namespace Titanium.Web.Proxy ...@@ -141,24 +139,25 @@ namespace Titanium.Web.Proxy
//write back successfull CONNECT response //write back successfull CONNECT response
await WriteConnectResponse(clientStreamWriter, version); await WriteConnectResponse(clientStreamWriter, version);
await TcpHelper.SendRaw(this, await TcpHelper.SendRaw(this,
httpRemoteUri.Host, httpRemoteUri.Port, httpRemoteUri.Host, httpRemoteUri.Port,
null, version, null, null, version, null,
false, false,
clientStream, tcpConnectionFactory); clientStream, tcpConnectionFactory);
Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
return; return;
} }
//Now create the request //Now create the request
await HandleHttpSessionRequest(tcpClient, httpCmd, clientStream, clientStreamReader, clientStreamWriter, await HandleHttpSessionRequest(tcpClient, httpCmd, clientStream, clientStreamReader, clientStreamWriter,
httpRemoteUri.Scheme == Uri.UriSchemeHttps ? httpRemoteUri.Host : null, endPoint, connectRequestHeaders); httpRemoteUri.Scheme == Uri.UriSchemeHttps ? httpRemoteUri.Host : null, endPoint,
connectRequestHeaders);
} }
catch (Exception) catch (Exception)
{ {
Dispose(clientStream, }
clientStreamReader, finally
clientStreamWriter, null); {
Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
} }
} }
...@@ -544,7 +543,6 @@ namespace Titanium.Web.Proxy ...@@ -544,7 +543,6 @@ namespace Titanium.Web.Proxy
break; break;
} }
} }
} }
/// <summary> /// <summary>
......
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