diff --git a/src/Titanium.Web.Proxy/Http2/Hpack/StaticTable.cs b/src/Titanium.Web.Proxy/Http2/Hpack/StaticTable.cs index 3198a3c34..99e96ef6f 100644 --- a/src/Titanium.Web.Proxy/Http2/Hpack/StaticTable.cs +++ b/src/Titanium.Web.Proxy/Http2/Hpack/StaticTable.cs @@ -28,7 +28,7 @@ public static class StaticTable /// Appendix A: Static Table Definition /// /// - private static readonly List staticTable = new List() + private static readonly List staticTable = new List { /* 1 */ new HttpHeader(":authority", string.Empty), diff --git a/src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs b/src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs index 297996fb6..e08863942 100644 --- a/src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs +++ b/src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs @@ -294,6 +294,8 @@ private async Task createServerConnection(string remoteHost session.TimeLine["Dns Resolved"] = DateTime.Now; } + Array.Sort(ipAddresses, (x, y) => x.AddressFamily.CompareTo(y.AddressFamily)); + for (int i = 0; i < ipAddresses.Length; i++) { try @@ -319,7 +321,7 @@ private async Task createServerConnection(string remoteHost tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } - await tcpClient.ConnectAsync(ipAddresses[i], port); + await tcpClient.ConnectAsync(ipAddress, port); break; } catch (Exception e) diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/ExpectContinueTests.cs b/tests/Titanium.Web.Proxy.IntegrationTests/ExpectContinueTests.cs index 1becd5eb4..1d4110041 100644 --- a/tests/Titanium.Web.Proxy.IntegrationTests/ExpectContinueTests.cs +++ b/tests/Titanium.Web.Proxy.IntegrationTests/ExpectContinueTests.cs @@ -22,7 +22,7 @@ public async Task ReverseProxy_GotContinueAndOkResponse() { var testSuite = new TestSuite(); var server = testSuite.GetServer(); - var continueServer = new HttpContinueServer() + var continueServer = new HttpContinueServer { ExpectationResponse = HttpStatusCode.Continue, ResponseBody = "I am server. I received your greetings." @@ -50,7 +50,7 @@ public async Task ReverseProxy_GotExpectationFailedResponse() { var testSuite = new TestSuite(); var server = testSuite.GetServer(); - var continueServer = new HttpContinueServer() { ExpectationResponse = HttpStatusCode.ExpectationFailed }; + var continueServer = new HttpContinueServer { ExpectationResponse = HttpStatusCode.ExpectationFailed }; server.HandleTcpRequest(continueServer.HandleRequest); var proxy = testSuite.GetReverseProxy(); @@ -73,7 +73,7 @@ public async Task ReverseProxy_GotNotFoundResponse() { var testSuite = new TestSuite(); var server = testSuite.GetServer(); - var continueServer = new HttpContinueServer() { ExpectationResponse = HttpStatusCode.NotFound }; + var continueServer = new HttpContinueServer { ExpectationResponse = HttpStatusCode.NotFound }; server.HandleTcpRequest(continueServer.HandleRequest); var proxy = testSuite.GetReverseProxy(); @@ -96,7 +96,7 @@ public async Task ReverseProxy_BeforeRequestThrows() { var testSuite = new TestSuite(); var server = testSuite.GetServer(); - var continueServer = new HttpContinueServer() { ExpectationResponse = HttpStatusCode.Continue }; + var continueServer = new HttpContinueServer { ExpectationResponse = HttpStatusCode.Continue }; server.HandleTcpRequest(continueServer.HandleRequest); var dbzEx = new DivideByZeroException("Undefined"); diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/Helpers/HttpContinueClient.cs b/tests/Titanium.Web.Proxy.IntegrationTests/Helpers/HttpContinueClient.cs index 673a226d4..f008bb4c4 100644 --- a/tests/Titanium.Web.Proxy.IntegrationTests/Helpers/HttpContinueClient.cs +++ b/tests/Titanium.Web.Proxy.IntegrationTests/Helpers/HttpContinueClient.cs @@ -9,6 +9,8 @@ namespace Titanium.Web.Proxy.IntegrationTests.Helpers { class HttpContinueClient { + private const int waitTimeout = 200; + private static Encoding MsgEncoding = HttpHelper.GetEncodingFromContentType(null); public async Task Post(string server, int port, string content) @@ -37,7 +39,7 @@ public async Task Post(string server, int port, string content) while ((response = HttpMessageParsing.ParseResponse(responseMsg)) == null) { var readTask = client.GetStream().ReadAsync(buffer, 0, 1024); - if (!readTask.Wait(200)) + if (!readTask.Wait(waitTimeout)) return null; responseMsg += MsgEncoding.GetString(buffer, 0, readTask.Result); @@ -52,7 +54,7 @@ public async Task Post(string server, int port, string content) while ((response = HttpMessageParsing.ParseResponse(responseMsg)) == null) { var readTask = client.GetStream().ReadAsync(buffer, 0, 1024); - if (!readTask.Wait(200)) + if (!readTask.Wait(waitTimeout)) return null; responseMsg += MsgEncoding.GetString(buffer, 0, readTask.Result); } diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/Helpers/HttpMessageParsing.cs b/tests/Titanium.Web.Proxy.IntegrationTests/Helpers/HttpMessageParsing.cs index a4ef90a31..6281174a7 100644 --- a/tests/Titanium.Web.Proxy.IntegrationTests/Helpers/HttpMessageParsing.cs +++ b/tests/Titanium.Web.Proxy.IntegrationTests/Helpers/HttpMessageParsing.cs @@ -24,7 +24,7 @@ internal static Request ParseRequest(string messageText, bool requireBody) try { Request.ParseRequestLine(line, out var method, out var url, out var version); - RequestResponseBase request = new Request() + RequestResponseBase request = new Request { Method = method, RequestUriString = url, HttpVersion = version }; @@ -69,7 +69,7 @@ internal static Response ParseResponse(string messageText) try { Response.ParseResponseLine(line, out var version, out var status, out var desc); - RequestResponseBase response = new Response() + RequestResponseBase response = new Response { HttpVersion = version, StatusCode = status, StatusDescription = desc }; diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs b/tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs index d0267a560..7980c6012 100644 --- a/tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs +++ b/tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs @@ -61,7 +61,7 @@ public async Task Smoke_Test_Nested_Proxy_UserData() { Assert.AreEqual("Test", session.UserData); - return await Task.FromResult(new Models.ExternalProxy() + return await Task.FromResult(new Models.ExternalProxy { HostName = "localhost", Port = proxy2.ProxyEndPoints[0].Port diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestProxyServer.cs b/tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestProxyServer.cs index ab868c15c..f6eb0bfc6 100644 --- a/tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestProxyServer.cs +++ b/tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestProxyServer.cs @@ -18,7 +18,7 @@ public class TestProxyServer : IDisposable public TestProxyServer(bool isReverseProxy, ProxyServer upStreamProxy = null) { ProxyServer = new ProxyServer(); - ProxyEndPoint explicitEndPoint = isReverseProxy ? + var explicitEndPoint = isReverseProxy ? (ProxyEndPoint)new TransparentProxyEndPoint(IPAddress.Any, 0, true) : new ExplicitProxyEndPoint(IPAddress.Any, 0, true); @@ -26,13 +26,13 @@ public TestProxyServer(bool isReverseProxy, ProxyServer upStreamProxy = null) if (upStreamProxy != null) { - ProxyServer.UpStreamHttpProxy = new ExternalProxy() + ProxyServer.UpStreamHttpProxy = new ExternalProxy { HostName = "localhost", Port = upStreamProxy.ProxyEndPoints[0].Port }; - ProxyServer.UpStreamHttpsProxy = new ExternalProxy() + ProxyServer.UpStreamHttpsProxy = new ExternalProxy { HostName = "localhost", Port = upStreamProxy.ProxyEndPoints[0].Port diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestServer.cs b/tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestServer.cs index c0eacbe23..b6dcc2d31 100644 --- a/tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestServer.cs +++ b/tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestServer.cs @@ -62,14 +62,9 @@ public TestServer(X509Certificate2 serverCertificate) .Get() .Addresses.ToArray(); - string httpAddress = addresses[0]; - HttpListeningPort = int.Parse(httpAddress.Split(':')[2]); - - string httpsAddress = addresses[1]; - HttpsListeningPort = int.Parse(httpsAddress.Split(':')[2]); - - string tcpAddress = addresses[2]; - TcpListeningPort = int.Parse(tcpAddress.Split(':')[2]); + HttpListeningPort = new Uri(addresses[0]).Port; + HttpsListeningPort = new Uri(addresses[1]).Port; + TcpListeningPort = new Uri(addresses[2]).Port; } Func requestHandler = null;