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

Commit ac02c68

Browse files
committed
Fix fake tunnel failure
1 parent 48c041e commit ac02c68

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net;
5+
using System.Net.Sockets;
56
#if NETCOREAPP2_1
67
using System.Net.Security;
78
#endif
@@ -188,8 +189,20 @@ await clientStreamWriter.WriteResponseAsync(args.HttpClient.Response,
188189
//If prefetch task is available.
189190
if (connection == null && prefetchTask != null)
190191
{
191-
connection = await prefetchTask;
192+
try
193+
{
194+
connection = await prefetchTask;
195+
}
196+
catch (SocketException e)
197+
{
198+
if(e.SocketErrorCode != SocketError.HostNotFound)
199+
{
200+
throw;
201+
}
202+
}
203+
192204
prefetchTask = null;
205+
193206
}
194207

195208
// create a new connection if cache key changes.

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,34 @@ public async Task Can_Handle_Https_Request()
3333
Assert.AreEqual("I am server. I received your greetings.", body);
3434
}
3535

36+
[TestMethod]
37+
public async Task Can_Handle_Https_Fake_Tunnel_Request()
38+
{
39+
var testSuite = new TestSuite();
40+
41+
var server = testSuite.GetServer();
42+
server.HandleRequest((context) =>
43+
{
44+
return context.Response.WriteAsync("I am server. I received your greetings.");
45+
});
46+
47+
var proxy = testSuite.GetProxy();
48+
proxy.BeforeRequest += async (sender, e) =>
49+
{
50+
e.HttpClient.Request.RequestUri = new Uri(server.ListeningHttpUrl);
51+
await Task.FromResult(0);
52+
};
53+
54+
var client = testSuite.GetClient(proxy);
55+
56+
var response = await client.PostAsync(new Uri($"https://{Guid.NewGuid().ToString()}.com"),
57+
new StringContent("hello server. I am a client."));
58+
59+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
60+
var body = await response.Content.ReadAsStringAsync();
61+
62+
Assert.AreEqual("I am server. I received your greetings.", body);
63+
}
64+
3665
}
3766
}

0 commit comments

Comments
 (0)