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

Commit 4628c42

Browse files
committed
issue #61 , #62 , #59
Fix issues
1 parent b9079a9 commit 4628c42

File tree

12 files changed

+103
-63
lines changed

12 files changed

+103
-63
lines changed

Titanium.Web.Proxy.Test/App.config

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
1+
<?xml version="1.0" encoding="utf-8"?>
32
<configuration>
43
<startup>
5-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
65
</startup>
76
<runtime>
87
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
98
<dependentAssembly>
10-
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
11-
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
9+
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
10+
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0"/>
1211
</dependentAssembly>
1312
<dependentAssembly>
14-
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
15-
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
13+
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
14+
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0"/>
1615
</dependentAssembly>
1716
</assemblyBinding>
1817
</runtime>
1918

20-
</configuration>
19+
</configuration>

Titanium.Web.Proxy.Test/ProxyTestController.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@ public void StartProxy()
4646
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
4747
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
4848

49-
//You can also add/remove end points after proxy has been started
50-
ProxyServer.RemoveEndPoint(transparentEndPoint);
51-
5249
//Only explicit proxies can be set as system proxy!
53-
ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
54-
ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
50+
// ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
51+
// ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
5552
}
5653

5754
public void Stop()
@@ -79,16 +76,26 @@ public void OnRequest(object sender, SessionEventArgs e)
7976

8077
//Get/Set request body as string
8178
string bodyString = e.GetRequestBodyAsString();
82-
e.SetRequestBodyString(bodyString);
79+
e.SetRequestBodyString(bodyString);
8380

8481
}
85-
82+
8683
//To cancel a request with a custom HTML content
8784
//Filter URL
88-
8985
if (e.ProxySession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
9086
{
91-
e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
87+
e.Ok("<!DOCTYPE html>" +
88+
"<html><body><h1>" +
89+
"Website Blocked" +
90+
"</h1>" +
91+
"<p>Blocked by titanium web proxy.</p>" +
92+
"</body>" +
93+
"</html>");
94+
}
95+
//Redirect example
96+
if (e.ProxySession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
97+
{
98+
e.Redirect("https://www.paypal.com");
9299
}
93100
}
94101

@@ -107,7 +114,11 @@ public void OnResponse(object sender, SessionEventArgs e)
107114
{
108115
if (e.ProxySession.Response.ContentType.Trim().ToLower().Contains("text/html"))
109116
{
117+
byte[] bodyBytes = e.GetResponseBody();
118+
e.SetResponseBody(bodyBytes);
119+
110120
string body = e.GetResponseBodyAsString();
121+
e.SetResponseBodyString(body);
111122
}
112123
}
113124
}

Titanium.Web.Proxy.Test/Titanium.Web.Proxy.Test.csproj

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,53 @@
1111
<RootNamespace>Titanium.Web.Proxy.Test</RootNamespace>
1212
<AssemblyName>Titanium.Web.Proxy.Test</AssemblyName>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1617
<DebugSymbols>true</DebugSymbols>
1718
<DebugType>full</DebugType>
1819
<Optimize>false</Optimize>
1920
<OutputPath>bin\Debug\</OutputPath>
20-
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<DefineConstants>TRACE;DEBUG;NET40</DefineConstants>
2122
<ErrorReport>prompt</ErrorReport>
2223
<WarningLevel>4</WarningLevel>
23-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
24+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
25+
<Prefer32Bit>false</Prefer32Bit>
2426
</PropertyGroup>
2527
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2628
<DebugType>pdbonly</DebugType>
2729
<Optimize>true</Optimize>
2830
<OutputPath>bin\Release\</OutputPath>
29-
<DefineConstants>TRACE</DefineConstants>
31+
<DefineConstants>TRACE;NET40</DefineConstants>
3032
<ErrorReport>prompt</ErrorReport>
3133
<WarningLevel>4</WarningLevel>
32-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
34+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
35+
<Prefer32Bit>false</Prefer32Bit>
3336
</PropertyGroup>
3437
<PropertyGroup>
3538
<StartupObject />
3639
</PropertyGroup>
3740
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug-Net45|AnyCPU'">
3841
<DebugSymbols>true</DebugSymbols>
3942
<OutputPath>bin\Debug-Net45\</OutputPath>
40-
<DefineConstants>DEBUG;TRACE</DefineConstants>
43+
<DefineConstants>TRACE;DEBUG;NET45</DefineConstants>
4144
<DebugType>full</DebugType>
4245
<PlatformTarget>AnyCPU</PlatformTarget>
4346
<ErrorReport>prompt</ErrorReport>
4447
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
45-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
48+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
49+
<Prefer32Bit>true</Prefer32Bit>
4650
</PropertyGroup>
4751
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-Net45|AnyCPU'">
4852
<OutputPath>bin\Release-Net45\</OutputPath>
49-
<DefineConstants>TRACE</DefineConstants>
53+
<DefineConstants>TRACE;NET45</DefineConstants>
5054
<Optimize>true</Optimize>
5155
<DebugType>pdbonly</DebugType>
5256
<PlatformTarget>AnyCPU</PlatformTarget>
5357
<ErrorReport>prompt</ErrorReport>
5458
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
55-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
59+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
60+
<Prefer32Bit>true</Prefer32Bit>
5661
</PropertyGroup>
5762
<ItemGroup>
5863
<Reference Include="System" />

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ public void Redirect(string url)
393393
var response = new RedirectResponse();
394394

395395
response.HttpVersion = ProxySession.Request.HttpVersion;
396+
response.ResponseHeaders.Add(new Models.HttpHeader("Location", url));
396397
response.ResponseBody = Encoding.ASCII.GetBytes(string.Empty);
397398

398399
Respond(response);
@@ -404,13 +405,14 @@ public void Redirect(string url)
404405
public void Respond(Response response)
405406
{
406407
ProxySession.Request.RequestLocked = true;
407-
ProxySession.Response.ResponseLocked = true;
408408

409409
response.ResponseLocked = true;
410410
response.ResponseBodyRead = true;
411411

412412
ProxySession.Response = response;
413+
413414
ProxyServer.HandleHttpSessionResponse(this);
414415
}
416+
415417
}
416418
}

Titanium.Web.Proxy/Network/TcpExtensions.cs renamed to Titanium.Web.Proxy/Extensions/TcpExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Threading.Tasks;
88

99

10-
namespace Titanium.Web.Proxy.Network
10+
namespace Titanium.Web.Proxy.Extensions
1111
{
1212

1313
internal static class TcpExtensions

Titanium.Web.Proxy/Network/HttpWebClient.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ public bool SendChunked
100100
public string Url { get { return RequestUri.OriginalString; } }
101101

102102
internal Encoding Encoding { get { return this.GetEncoding(); } }
103-
103+
/// <summary>
104+
/// Terminates the underlying Tcp Connection to client after current request
105+
/// </summary>
104106
internal bool CancelRequest { get; set; }
105107

106108
internal byte[] RequestBody { get; set; }
@@ -248,7 +250,7 @@ internal bool IsChunked
248250
internal string ResponseBodyString { get; set; }
249251
internal bool ResponseBodyRead { get; set; }
250252
internal bool ResponseLocked { get; set; }
251-
253+
public bool Is100Continue { get; internal set; }
252254

253255
public Response()
254256
{
@@ -272,6 +274,7 @@ public bool IsSecure
272274
public Response Response { get; set; }
273275
internal TcpConnection ProxyClient { get; set; }
274276

277+
275278
public void SetConnection(TcpConnection Connection)
276279
{
277280
Connection.LastAccess = DateTime.Now;
@@ -322,11 +325,19 @@ public void ReceiveResponse()
322325
var s = ProxyClient.ServerStreamReader.ReadLine();
323326
}
324327

325-
this.Response.HttpVersion = httpResult[0];
326-
this.Response.ResponseStatusCode = httpResult[1];
327-
string status = httpResult[2];
328+
this.Response.HttpVersion = httpResult[0].Trim();
329+
this.Response.ResponseStatusCode = httpResult[1].Trim();
330+
this.Response.ResponseStatusDescription = httpResult[2].Trim();
328331

329-
this.Response.ResponseStatusDescription = status;
332+
if (this.Response.ResponseStatusCode.Equals("100")
333+
&& this.Response.ResponseStatusDescription.ToLower().Equals("continue"))
334+
{
335+
this.Response.Is100Continue = true;
336+
this.Response.ResponseStatusCode = null;
337+
ProxyClient.ServerStreamReader.ReadLine();
338+
ReceiveResponse();
339+
return;
340+
}
330341

331342
List<string> responseLines = ProxyClient.ServerStreamReader.ReadAllLines();
332343

Titanium.Web.Proxy/Network/TcpConnectionManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Titanium.Web.Proxy.Helpers;
1111
using System.Threading;
1212
using System.Security.Authentication;
13+
using Titanium.Web.Proxy.Extensions;
1314

1415
namespace Titanium.Web.Proxy.Network
1516
{

Titanium.Web.Proxy/ResponseHandler.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public static void HandleHttpSessionResponse(SessionEventArgs args)
3636

3737
args.ProxySession.Response.ResponseLocked = true;
3838

39+
if (args.ProxySession.Response.Is100Continue)
40+
{
41+
WriteResponseStatus(args.ProxySession.Response.HttpVersion, "100",
42+
"Continue", args.Client.ClientStreamWriter);
43+
args.Client.ClientStreamWriter.WriteLine();
44+
}
45+
46+
WriteResponseStatus(args.ProxySession.Response.HttpVersion, args.ProxySession.Response.ResponseStatusCode,
47+
args.ProxySession.Response.ResponseStatusDescription, args.Client.ClientStreamWriter);
48+
3949
if (args.ProxySession.Response.ResponseBodyRead)
4050
{
4151
var isChunked = args.ProxySession.Response.IsChunked;
@@ -46,16 +56,12 @@ public static void HandleHttpSessionResponse(SessionEventArgs args)
4656
args.ProxySession.Response.ResponseBody = GetCompressedResponseBody(contentEncoding, args.ProxySession.Response.ResponseBody);
4757
}
4858

49-
WriteResponseStatus(args.ProxySession.Response.HttpVersion, args.ProxySession.Response.ResponseStatusCode,
50-
args.ProxySession.Response.ResponseStatusDescription, args.Client.ClientStreamWriter);
5159
WriteResponseHeaders(args.Client.ClientStreamWriter, args.ProxySession.Response.ResponseHeaders, args.ProxySession.Response.ResponseBody.Length,
5260
isChunked);
5361
WriteResponseBody(args.Client.ClientStream, args.ProxySession.Response.ResponseBody, isChunked);
5462
}
5563
else
5664
{
57-
WriteResponseStatus(args.ProxySession.Response.HttpVersion, args.ProxySession.Response.ResponseStatusCode,
58-
args.ProxySession.Response.ResponseStatusDescription, args.Client.ClientStreamWriter);
5965
WriteResponseHeaders(args.Client.ClientStreamWriter, args.ProxySession.Response.ResponseHeaders);
6066

6167
if (args.ProxySession.Response.IsChunked || args.ProxySession.Response.ContentLength > 0)

Titanium.Web.Proxy/Responses/RedirectResponse.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ public RedirectResponse()
1010
{
1111
ResponseStatusCode = "302";
1212
ResponseStatusDescription = "Found";
13-
14-
ResponseHeaders.Add(new HttpHeader("Timestamp", DateTime.Now.ToString()));
15-
ResponseHeaders.Add(new HttpHeader("content-length", DateTime.Now.ToString()));
16-
ResponseHeaders.Add(new HttpHeader("Cache-Control", "no-cache, no-store, must-revalidate"));
17-
ResponseHeaders.Add(new HttpHeader("Pragma", "no-cache"));
18-
ResponseHeaders.Add(new HttpHeader("Expires", "0"));
1913
}
2014
}
2115
}

Titanium.Web.Proxy/Titanium.Web.Proxy.csproj

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@
4646
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
4747
<DefineConstants>NET45</DefineConstants>
4848
</PropertyGroup>
49-
<ItemGroup>
50-
<Reference Include="Ionic.Zip, Version=1.9.7.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
51-
<SpecificVersion>False</SpecificVersion>
52-
<HintPath>..\packages\DotNetZip.1.9.7\lib\net20\Ionic.Zip.dll</HintPath>
53-
</Reference>
49+
<ItemGroup Condition="'$(Configuration)' == 'Debug' Or '$(Configuration)' == 'Release'">
5450
<Reference Include="Microsoft.Threading.Tasks">
5551
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.dll</HintPath>
5652
</Reference>
@@ -60,19 +56,25 @@
6056
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop">
6157
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
6258
</Reference>
63-
<Reference Include="System" />
64-
<Reference Include="System.configuration" />
65-
<Reference Include="System.Core" />
6659
<Reference Include="System.IO">
6760
<HintPath>..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.IO.dll</HintPath>
6861
</Reference>
69-
<Reference Include="System.Net" />
7062
<Reference Include="System.Runtime">
7163
<HintPath>..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Runtime.dll</HintPath>
7264
</Reference>
7365
<Reference Include="System.Threading.Tasks">
7466
<HintPath>..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Threading.Tasks.dll</HintPath>
7567
</Reference>
68+
</ItemGroup>
69+
<ItemGroup>
70+
<Reference Include="Ionic.Zip, Version=1.9.8.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
71+
<HintPath>..\packages\DotNetZip.1.9.8\lib\net20\Ionic.Zip.dll</HintPath>
72+
<Private>True</Private>
73+
</Reference>
74+
<Reference Include="System" />
75+
<Reference Include="System.Net" />
76+
<Reference Include="System.configuration" />
77+
<Reference Include="System.Core" />
7678
<Reference Include="System.Xml.Linq" />
7779
<Reference Include="System.Data.DataSetExtensions" />
7880
<Reference Include="Microsoft.CSharp" />
@@ -99,15 +101,14 @@
99101
<Compile Include="Helpers\Firefox.cs" />
100102
<Compile Include="Helpers\SystemProxy.cs" />
101103
<Compile Include="Models\EndPoint.cs" />
102-
<Compile Include="Network\TcpExtensions.cs" />
104+
<Compile Include="Extensions\TcpExtensions.cs" />
103105
<Compile Include="Network\TcpConnectionManager.cs" />
104106
<Compile Include="Models\HttpHeader.cs" />
105107
<Compile Include="Network\HttpWebClient.cs" />
106108
<Compile Include="Properties\AssemblyInfo.cs" />
107109
<Compile Include="RequestHandler.cs" />
108110
<Compile Include="ResponseHandler.cs" />
109111
<Compile Include="Helpers\CustomBinaryReader.cs" />
110-
<Compile Include="Helpers\Compression.cs" />
111112
<Compile Include="ProxyServer.cs" />
112113
<Compile Include="EventArguments\SessionEventArgs.cs" />
113114
<Compile Include="Helpers\Tcp.cs" />
@@ -118,7 +119,14 @@
118119
<ItemGroup />
119120
<ItemGroup>
120121
<None Include="app.config" />
121-
<None Include="packages.config" />
122+
</ItemGroup>
123+
<ItemGroup Condition="'$(Configuration)' == 'Debug' Or '$(Configuration)' == 'Release'">
124+
<None Include="packages.config">
125+
<SubType>Designer</SubType>
126+
</None>
127+
</ItemGroup>
128+
<ItemGroup Condition="'$(Configuration)' == 'Debug-Net45' Or '$(Configuration)' == 'Release-Net45'">
129+
<None Include="packages-Net45.config" />
122130
</ItemGroup>
123131
<ItemGroup>
124132
<None Include="makecert.exe">
@@ -138,11 +146,11 @@
138146
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
139147
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
140148
</Target>
141-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
142-
Other similar extension points exist, see Microsoft.Common.targets.
143-
<Target Name="BeforeBuild">
144-
</Target>
145-
<Target Name="AfterBuild">
146-
</Target>
149+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
150+
Other similar extension points exist, see Microsoft.Common.targets.
151+
<Target Name="BeforeBuild">
152+
</Target>
153+
<Target Name="AfterBuild">
154+
</Target>
147155
-->
148156
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="DotNetZip" version="1.9.8" targetFramework="net45" />
4+
</packages>

Titanium.Web.Proxy/packages.config

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="DotNetZip" version="1.9.7" targetFramework="net40" />
4-
<package id="DotNetZip" version="1.9.7" targetFramework="net45" />
3+
<package id="DotNetZip" version="1.9.8" targetFramework="net40" />
54
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net40" />
65
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net40" />
76
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" />

0 commit comments

Comments
 (0)