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

Commit f3e64cd

Browse files
Merge pull request #517 from roblascelles/read-body-and-cancel-request-to-server
Read body and cancel request to server
2 parents e765f16 + 9c56e63 commit f3e64cd

File tree

5 files changed

+154
-88
lines changed

5 files changed

+154
-88
lines changed

src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private async Task<byte[]> readBodyAsync(bool isRequest, CancellationToken cance
179179
internal async Task SyphonOutBodyAsync(bool isRequest, CancellationToken cancellationToken)
180180
{
181181
var requestResponse = isRequest ? (RequestResponseBase)WebSession.Request : WebSession.Response;
182-
if (requestResponse.OriginalIsBodyRead || !requestResponse.OriginalHasBody)
182+
if (requestResponse.IsBodyRead || !requestResponse.OriginalHasBody)
183183
{
184184
return;
185185
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Http;
4+
using System.Net.Mime;
5+
using System.Text;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace Titanium.Web.Proxy.IntegrationTests
9+
{
10+
[TestClass]
11+
public class InterceptionTests
12+
{
13+
[TestMethod]
14+
public void CanInterceptPostRequests()
15+
{
16+
string testUrl = "http://interceptthis.com";
17+
int proxyPort = 8086;
18+
var proxy = new ProxyTestController();
19+
proxy.StartProxy(proxyPort);
20+
21+
using (var client = CreateHttpClient(testUrl, proxyPort))
22+
{
23+
var response = client.PostAsync(new Uri(testUrl), new StringContent("hello!")).Result;
24+
25+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
26+
27+
var body = response.Content.ReadAsStringAsync().Result;
28+
Assert.IsTrue(body.Contains("TitaniumWebProxy-Stopped!!"));
29+
}
30+
}
31+
32+
private HttpClient CreateHttpClient(string url, int localProxyPort)
33+
{
34+
var handler = new HttpClientHandler
35+
{
36+
Proxy = new WebProxy($"http://localhost:{localProxyPort}", false),
37+
UseProxy = true
38+
};
39+
40+
var client = new HttpClient(handler);
41+
42+
return client;
43+
}
44+
45+
}
46+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Net;
4+
using System.Net.Security;
5+
using System.Threading.Tasks;
6+
using Titanium.Web.Proxy.EventArguments;
7+
using Titanium.Web.Proxy.Models;
8+
9+
namespace Titanium.Web.Proxy.IntegrationTests
10+
{
11+
public class ProxyTestController
12+
{
13+
private readonly ProxyServer proxyServer;
14+
15+
public ProxyTestController()
16+
{
17+
proxyServer = new ProxyServer();
18+
}
19+
20+
public void StartProxy(int proxyPort)
21+
{
22+
proxyServer.BeforeRequest += OnRequest;
23+
proxyServer.BeforeResponse += OnResponse;
24+
proxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
25+
proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
26+
27+
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, proxyPort, true);
28+
29+
// An explicit endpoint is where the client knows about the existance of a proxy
30+
// So client sends request in a proxy friendly manner
31+
proxyServer.AddEndPoint(explicitEndPoint);
32+
proxyServer.Start();
33+
34+
foreach (var endPoint in proxyServer.ProxyEndPoints)
35+
{
36+
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
37+
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
38+
}
39+
}
40+
41+
public void Stop()
42+
{
43+
proxyServer.BeforeRequest -= OnRequest;
44+
proxyServer.BeforeResponse -= OnResponse;
45+
proxyServer.ServerCertificateValidationCallback -= OnCertificateValidation;
46+
proxyServer.ClientCertificateSelectionCallback -= OnCertificateSelection;
47+
48+
proxyServer.Stop();
49+
}
50+
51+
// intecept & cancel, redirect or update requests
52+
public async Task OnRequest(object sender, SessionEventArgs e)
53+
{
54+
Debug.WriteLine(e.WebSession.Request.Url);
55+
56+
if (e.WebSession.Request.Url.Contains("interceptthis.com"))
57+
{
58+
if (e.WebSession.Request.HasBody)
59+
{
60+
var body = await e.GetRequestBodyAsString();
61+
}
62+
63+
e.Ok("<html><body>TitaniumWebProxy-Stopped!!</body></html>");
64+
return;
65+
}
66+
67+
68+
await Task.FromResult(0);
69+
}
70+
71+
// Modify response
72+
public async Task OnResponse(object sender, SessionEventArgs e)
73+
{
74+
await Task.FromResult(0);
75+
}
76+
77+
/// <summary>
78+
/// Allows overriding default certificate validation logic
79+
/// </summary>
80+
/// <param name="sender"></param>
81+
/// <param name="e"></param>
82+
public Task OnCertificateValidation(object sender, CertificateValidationEventArgs e)
83+
{
84+
// set IsValid to true/false based on Certificate Errors
85+
if (e.SslPolicyErrors == SslPolicyErrors.None)
86+
{
87+
e.IsValid = true;
88+
}
89+
90+
return Task.FromResult(0);
91+
}
92+
93+
/// <summary>
94+
/// Allows overriding default client certificate selection logic during mutual authentication
95+
/// </summary>
96+
/// <param name="sender"></param>
97+
/// <param name="e"></param>
98+
public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs e)
99+
{
100+
// set e.clientCertificate to override
101+
102+
return Task.FromResult(0);
103+
}
104+
}
105+
}
Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Net;
43
using System.Net.Http;
5-
using System.Net.Security;
6-
using System.Threading.Tasks;
74
using Microsoft.VisualStudio.TestTools.UnitTesting;
8-
using Titanium.Web.Proxy.EventArguments;
9-
using Titanium.Web.Proxy.Models;
105

116
namespace Titanium.Web.Proxy.IntegrationTests
127
{
@@ -43,86 +38,4 @@ private HttpClient CreateHttpClient(string url, int localProxyPort)
4338
return client;
4439
}
4540
}
46-
47-
public class ProxyTestController
48-
{
49-
private readonly ProxyServer proxyServer;
50-
51-
public ProxyTestController()
52-
{
53-
proxyServer = new ProxyServer();
54-
}
55-
56-
public void StartProxy(int proxyPort)
57-
{
58-
proxyServer.BeforeRequest += OnRequest;
59-
proxyServer.BeforeResponse += OnResponse;
60-
proxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
61-
proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
62-
63-
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, proxyPort, true);
64-
65-
// An explicit endpoint is where the client knows about the existance of a proxy
66-
// So client sends request in a proxy friendly manner
67-
proxyServer.AddEndPoint(explicitEndPoint);
68-
proxyServer.Start();
69-
70-
foreach (var endPoint in proxyServer.ProxyEndPoints)
71-
{
72-
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
73-
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
74-
}
75-
}
76-
77-
public void Stop()
78-
{
79-
proxyServer.BeforeRequest -= OnRequest;
80-
proxyServer.BeforeResponse -= OnResponse;
81-
proxyServer.ServerCertificateValidationCallback -= OnCertificateValidation;
82-
proxyServer.ClientCertificateSelectionCallback -= OnCertificateSelection;
83-
84-
proxyServer.Stop();
85-
}
86-
87-
// intecept & cancel, redirect or update requests
88-
public async Task OnRequest(object sender, SessionEventArgs e)
89-
{
90-
Debug.WriteLine(e.WebSession.Request.Url);
91-
await Task.FromResult(0);
92-
}
93-
94-
// Modify response
95-
public async Task OnResponse(object sender, SessionEventArgs e)
96-
{
97-
await Task.FromResult(0);
98-
}
99-
100-
/// <summary>
101-
/// Allows overriding default certificate validation logic
102-
/// </summary>
103-
/// <param name="sender"></param>
104-
/// <param name="e"></param>
105-
public Task OnCertificateValidation(object sender, CertificateValidationEventArgs e)
106-
{
107-
// set IsValid to true/false based on Certificate Errors
108-
if (e.SslPolicyErrors == SslPolicyErrors.None)
109-
{
110-
e.IsValid = true;
111-
}
112-
113-
return Task.FromResult(0);
114-
}
115-
116-
/// <summary>
117-
/// Allows overriding default client certificate selection logic during mutual authentication
118-
/// </summary>
119-
/// <param name="sender"></param>
120-
/// <param name="e"></param>
121-
public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs e)
122-
{
123-
// set e.clientCertificate to override
124-
125-
return Task.FromResult(0);
126-
}
127-
}
12841
}

tests/Titanium.Web.Proxy.IntegrationTests/Titanium.Web.Proxy.IntegrationTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
</Otherwise>
7171
</Choose>
7272
<ItemGroup>
73+
<Compile Include="InterceptionTests.cs" />
74+
<Compile Include="ProxyTestController.cs" />
7375
<Compile Include="SslTests.cs" />
7476
<Compile Include="Properties\AssemblyInfo.cs" />
7577
</ItemGroup>

0 commit comments

Comments
 (0)