Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit ea064f0

Browse files
committed
Merge branch 'master' into beta
2 parents ac1406a + 771ccfb commit ea064f0

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ await Http2Helper.SendHttp2(clientStream, connection.Stream, BufferSize,
321321
calledRequestHandler = true;
322322
// Now create the request
323323
await handleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter,
324-
cancellationTokenSource, connectHostname, connectArgs?.HttpClient.ConnectRequest, prefetchConnectionTask);
324+
cancellationTokenSource, connectHostname, connectArgs, prefetchConnectionTask);
325325
}
326326
catch (ProxyException e)
327327
{

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ public partial class ProxyServer
4747
/// <param name="prefetchConnectionTask">Prefetched server connection for current client using Connect/SNI headers.</param>
4848
private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, TcpClientConnection clientConnection,
4949
CustomBufferedStream clientStream, HttpResponseWriter clientStreamWriter,
50-
CancellationTokenSource cancellationTokenSource, string httpsConnectHostname, ConnectRequest connectRequest,
50+
CancellationTokenSource cancellationTokenSource, string httpsConnectHostname, TunnelConnectSessionEventArgs connectArgs,
5151
Task<TcpServerConnection> prefetchConnectionTask = null)
5252
{
53+
var connectRequest = connectArgs?.HttpClient.ConnectRequest;
54+
5355
var prefetchTask = prefetchConnectionTask;
5456
TcpServerConnection connection = null;
5557
bool closeServerConnection = false;
@@ -73,7 +75,8 @@ private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, TcpClientCon
7375
var args = new SessionEventArgs(this, endPoint, cancellationTokenSource)
7476
{
7577
ProxyClient = { Connection = clientConnection },
76-
HttpClient = { ConnectRequest = connectRequest }
78+
HttpClient = { ConnectRequest = connectRequest },
79+
UserData = connectArgs?.UserData
7780
};
7881

7982
try
@@ -204,7 +207,7 @@ await clientStreamWriter.WriteResponseAsync(args.HttpClient.Response,
204207

205208
//update connection to latest used
206209
connection = result.LatestConnection;
207-
closeServerConnection = !result.Continue;
210+
closeServerConnection = !result.Continue;
208211

209212
//throw if exception happened
210213
if (!result.IsSuccess)

tests/Titanium.Web.Proxy.IntegrationTests/Helpers/TestHelper.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ namespace Titanium.Web.Proxy.IntegrationTests.Helpers
66
{
77
public static class TestHelper
88
{
9-
public static HttpClient GetHttpClient(int localProxyPort)
9+
public static HttpClient GetHttpClient(int localProxyPort,
10+
bool enableBasicProxyAuthorization = false)
1011
{
11-
var proxy = new TestProxy($"http://localhost:{localProxyPort}");
12+
var proxy = new TestProxy($"http://localhost:{localProxyPort}", enableBasicProxyAuthorization);
1213

1314
var handler = new HttpClientHandler
1415
{
1516
Proxy = proxy,
16-
UseProxy = true
17+
UseProxy = true
1718
};
1819

1920
return new HttpClient(handler);
@@ -29,12 +30,16 @@ public class TestProxy : IWebProxy
2930
public Uri ProxyUri { get; set; }
3031
public ICredentials Credentials { get; set; }
3132

32-
public TestProxy(string proxyUri)
33+
public TestProxy(string proxyUri, bool enableAuthorization)
3334
: this(new Uri(proxyUri))
3435
{
36+
if (enableAuthorization)
37+
{
38+
Credentials = new NetworkCredential("test", "Test56");
39+
}
3540
}
3641

37-
public TestProxy(Uri proxyUri)
42+
private TestProxy(Uri proxyUri)
3843
{
3944
this.ProxyUri = proxyUri;
4045
}

tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System;
44
using System.Net;
55
using System.Net.Http;
6+
using System.Net.Http.Headers;
7+
using System.Text;
68
using System.Threading.Tasks;
79

810
namespace Titanium.Web.Proxy.IntegrationTests
@@ -35,5 +37,47 @@ public async Task Smoke_Test_Nested_Proxy()
3537
Assert.AreEqual("I am server. I received your greetings.", body);
3638
}
3739

40+
[TestMethod]
41+
public async Task Smoke_Test_Nested_Proxy_UserData()
42+
{
43+
var testSuite = new TestSuite();
44+
45+
var server = testSuite.GetServer();
46+
server.HandleRequest((context) =>
47+
{
48+
return context.Response.WriteAsync("I am server. I received your greetings.");
49+
});
50+
51+
var proxy1 = testSuite.GetProxy();
52+
proxy1.ProxyBasicAuthenticateFunc = async (session, username, password) =>
53+
{
54+
session.UserData = "Test";
55+
return await Task.FromResult(true);
56+
};
57+
58+
var proxy2 = testSuite.GetProxy();
59+
60+
proxy1.GetCustomUpStreamProxyFunc = async (session) =>
61+
{
62+
Assert.AreEqual("Test", session.UserData);
63+
64+
return await Task.FromResult(new Models.ExternalProxy()
65+
{
66+
HostName = "localhost",
67+
Port = proxy2.ProxyEndPoints[0].Port
68+
});
69+
};
70+
71+
var client = testSuite.GetClient(proxy1, true);
72+
73+
var response = await client.PostAsync(new Uri(server.ListeningHttpsUrl),
74+
new StringContent("hello server. I am a client."));
75+
76+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
77+
var body = await response.Content.ReadAsStringAsync();
78+
79+
Assert.AreEqual("I am server. I received your greetings.", body);
80+
}
81+
3882
}
3983
}

tests/Titanium.Web.Proxy.IntegrationTests/Setup/TestSuite.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public ProxyServer GetReverseProxy(ProxyServer upStreamProxy = null)
4343
return new TestProxyServer(true).ProxyServer;
4444
}
4545

46-
public HttpClient GetClient(ProxyServer proxyServer)
46+
public HttpClient GetClient(ProxyServer proxyServer, bool enableBasicProxyAuthorization = false)
4747
{
48-
return TestHelper.GetHttpClient(proxyServer.ProxyEndPoints[0].Port);
48+
return TestHelper.GetHttpClient(proxyServer.ProxyEndPoints[0].Port, enableBasicProxyAuthorization);
4949
}
5050

5151
public HttpClient GetReverseProxyClient()

0 commit comments

Comments
 (0)