Commit e8449211 authored by Anton Ryzhov's avatar Anton Ryzhov

Remove unused extension class;

Remove Lazy<T> instantiation when it just creates an additional object;
Fixes in GetProcessIdByLocalPort method.
parent e79264a8
......@@ -9,17 +9,17 @@ namespace Titanium.Web.Proxy.Compression
internal static class CompressionFactory
{
//cache
private static readonly Lazy<ICompression> gzip = new Lazy<ICompression>(() => new GZipCompression());
private static readonly Lazy<ICompression> deflate = new Lazy<ICompression>(() => new DeflateCompression());
private static readonly ICompression gzip = new GZipCompression();
private static readonly ICompression deflate = new DeflateCompression();
public static ICompression GetCompression(string type)
{
switch (type)
{
case KnownHeaders.ContentEncodingGzip:
return gzip.Value;
return gzip;
case KnownHeaders.ContentEncodingDeflate:
return deflate.Value;
return deflate;
default:
throw new Exception($"Unsupported compression mode: {type}");
}
......
......@@ -9,19 +9,18 @@ namespace Titanium.Web.Proxy.Decompression
internal class DecompressionFactory
{
//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 =
new Lazy<IDecompression>(() => new DeflateDecompression());
private static readonly IDecompression deflate = new DeflateDecompression();
public static IDecompression Create(string type)
{
switch (type)
{
case KnownHeaders.ContentEncodingGzip:
return gzip.Value;
return gzip;
case KnownHeaders.ContentEncodingDeflate:
return deflate.Value;
return deflate;
default:
throw new Exception($"Unsupported decompression mode: {type}");
}
......
......@@ -2,5 +2,5 @@
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
//If client is localhost get the process id
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
......
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
{
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>
/// Adapated from below link
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
......@@ -27,7 +26,7 @@ namespace Titanium.Web.Proxy.Helpers
int tcpTableLength = 0;
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)
{
......@@ -56,7 +55,7 @@ namespace Titanium.Web.Proxy.Helpers
}
else
{
NativeMethods.Tcp6Row* rowPtr = (NativeMethods.Tcp6Row*)tcpTable + 4;
NativeMethods.Tcp6Row* rowPtr = (NativeMethods.Tcp6Row*)tcpTable;
for (int i = 0; i < rowCount; ++i)
{
......
......@@ -29,7 +29,7 @@ namespace Titanium.Web.Proxy
/// <summary>
/// An default exception log func
/// </summary>
private readonly Lazy<ExceptionHandler> defaultExceptionFunc = new Lazy<ExceptionHandler>(() => (e => { }));
private readonly ExceptionHandler defaultExceptionFunc = e => { };
/// <summary>
/// Backing field for corresponding public property
......@@ -209,7 +209,7 @@ namespace Titanium.Web.Proxy
/// </summary>
public ExceptionHandler ExceptionFunc
{
get => exceptionFunc ?? defaultExceptionFunc.Value;
get => exceptionFunc ?? defaultExceptionFunc;
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