Commit b2c145ec authored by honfika's avatar honfika Committed by GitHub

Merge pull request #413 from antrv/feature/optimize2

Remove unused extension class; Remove Lazy<T> instantiation when it just creates an additional object; Fixes in GetProcessIdByLocalPort method.
parents be832f30 c15ee71f
...@@ -9,17 +9,17 @@ namespace Titanium.Web.Proxy.Compression ...@@ -9,17 +9,17 @@ namespace Titanium.Web.Proxy.Compression
internal static class CompressionFactory internal static class CompressionFactory
{ {
//cache //cache
private static readonly Lazy<ICompression> gzip = new Lazy<ICompression>(() => new GZipCompression()); private static readonly ICompression gzip = new GZipCompression();
private static readonly Lazy<ICompression> deflate = new Lazy<ICompression>(() => new DeflateCompression()); private static readonly ICompression deflate = new DeflateCompression();
public static ICompression GetCompression(string type) public static ICompression GetCompression(string type)
{ {
switch (type) switch (type)
{ {
case KnownHeaders.ContentEncodingGzip: case KnownHeaders.ContentEncodingGzip:
return gzip.Value; return gzip;
case KnownHeaders.ContentEncodingDeflate: case KnownHeaders.ContentEncodingDeflate:
return deflate.Value; return deflate;
default: default:
throw new Exception($"Unsupported compression mode: {type}"); throw new Exception($"Unsupported compression mode: {type}");
} }
......
...@@ -9,19 +9,18 @@ namespace Titanium.Web.Proxy.Decompression ...@@ -9,19 +9,18 @@ namespace Titanium.Web.Proxy.Decompression
internal class DecompressionFactory internal class DecompressionFactory
{ {
//cache //cache
private static readonly Lazy<IDecompression> gzip = new Lazy<IDecompression>(() => new GZipDecompression()); private static readonly IDecompression gzip = new GZipDecompression();
private static readonly Lazy<IDecompression> deflate = private static readonly IDecompression deflate = new DeflateDecompression();
new Lazy<IDecompression>(() => new DeflateDecompression());
public static IDecompression Create(string type) public static IDecompression Create(string type)
{ {
switch (type) switch (type)
{ {
case KnownHeaders.ContentEncodingGzip: case KnownHeaders.ContentEncodingGzip:
return gzip.Value; return gzip;
case KnownHeaders.ContentEncodingDeflate: case KnownHeaders.ContentEncodingDeflate:
return deflate.Value; return deflate;
default: default:
throw new Exception($"Unsupported decompression mode: {type}"); throw new Exception($"Unsupported decompression mode: {type}");
} }
......
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
namespace Titanium.Web.Proxy.EventArguments namespace Titanium.Web.Proxy.EventArguments
{ {
public delegate Task AsyncEventHandler<TEventArgs>(object sender, TEventArgs e); public delegate Task AsyncEventHandler<in TEventArgs>(object sender, TEventArgs e);
} }
...@@ -55,7 +55,8 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -55,7 +55,8 @@ namespace Titanium.Web.Proxy.EventArguments
//If client is localhost get the process id //If client is localhost get the process id
if (NetworkHelper.IsLocalIpAddress(remoteEndPoint.Address)) if (NetworkHelper.IsLocalIpAddress(remoteEndPoint.Address))
{ {
return NetworkHelper.GetProcessIdFromPort(remoteEndPoint.Port, endPoint.IpV6Enabled); var ipVersion = endPoint.IpV6Enabled ? IpVersion.Ipv6 : IpVersion.Ipv4;
return TcpHelper.GetProcessIdByLocalPort(ipVersion, remoteEndPoint.Port);
} }
//can't access process Id of remote request from remote machine //can't access process Id of remote request from remote machine
......
using System;
namespace Titanium.Web.Proxy.Extensions
{
/// <summary>
/// Extension methods for Byte Arrays.
/// </summary>
internal static class ByteArrayExtensions
{
/// <summary>
/// Get the sub array from byte of data
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <param name="index"></param>
/// <param name="length"></param>
/// <returns></returns>
internal static T[] SubArray<T>(this T[] data, int index, int length)
{
var result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
}
}
...@@ -6,18 +6,6 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -6,18 +6,6 @@ namespace Titanium.Web.Proxy.Helpers
{ {
internal class NetworkHelper internal class NetworkHelper
{ {
internal static int GetProcessIdFromPort(int port, bool ipV6Enabled)
{
int processId = TcpHelper.GetProcessIdByLocalPort(IpVersion.Ipv4, port);
if (processId > 0 && !ipV6Enabled)
{
return processId;
}
return TcpHelper.GetProcessIdByLocalPort(IpVersion.Ipv6, port);
}
/// <summary> /// <summary>
/// Adapated from below link /// Adapated from below link
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost /// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
......
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
...@@ -27,7 +26,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -27,7 +26,7 @@ namespace Titanium.Web.Proxy.Helpers
int tcpTableLength = 0; int tcpTableLength = 0;
int ipVersionValue = ipVersion == IpVersion.Ipv4 ? NativeMethods.AfInet : NativeMethods.AfInet6; int ipVersionValue = ipVersion == IpVersion.Ipv4 ? NativeMethods.AfInet : NativeMethods.AfInet6;
int allPid = (int)NativeMethods.TcpTableType.OwnerPidAll; const int allPid = (int)NativeMethods.TcpTableType.OwnerPidAll;
if (NativeMethods.GetExtendedTcpTable(tcpTable, ref tcpTableLength, false, ipVersionValue, allPid, 0) != 0) if (NativeMethods.GetExtendedTcpTable(tcpTable, ref tcpTableLength, false, ipVersionValue, allPid, 0) != 0)
{ {
...@@ -56,7 +55,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -56,7 +55,7 @@ namespace Titanium.Web.Proxy.Helpers
} }
else else
{ {
NativeMethods.Tcp6Row* rowPtr = (NativeMethods.Tcp6Row*)tcpTable + 4; NativeMethods.Tcp6Row* rowPtr = (NativeMethods.Tcp6Row*)tcpTable;
for (int i = 0; i < rowCount; ++i) for (int i = 0; i < rowCount; ++i)
{ {
......
...@@ -29,7 +29,7 @@ namespace Titanium.Web.Proxy ...@@ -29,7 +29,7 @@ namespace Titanium.Web.Proxy
/// <summary> /// <summary>
/// An default exception log func /// An default exception log func
/// </summary> /// </summary>
private readonly Lazy<ExceptionHandler> defaultExceptionFunc = new Lazy<ExceptionHandler>(() => (e => { })); private readonly ExceptionHandler defaultExceptionFunc = e => { };
/// <summary> /// <summary>
/// Backing field for corresponding public property /// Backing field for corresponding public property
...@@ -209,7 +209,7 @@ namespace Titanium.Web.Proxy ...@@ -209,7 +209,7 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public ExceptionHandler ExceptionFunc public ExceptionHandler ExceptionFunc
{ {
get => exceptionFunc ?? defaultExceptionFunc.Value; get => exceptionFunc ?? defaultExceptionFunc;
set => exceptionFunc = value; set => exceptionFunc = value;
} }
......
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