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

Commit 2d85486

Browse files
Merge pull request #94 from justcoding121/master
Sync with master
2 parents a954c57 + 216b8c9 commit 2d85486

File tree

6 files changed

+58
-40
lines changed

6 files changed

+58
-40
lines changed

.build/default.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Task Restore-Packages {
5656
}
5757

5858
Task Install-MSBuild {
59-
if(!(Test-Path "${env:ProgramFiles(x86)}\MSBuild\12.0\Bin\msbuild.exe"))
59+
if(!(Test-Path "${env:ProgramFiles(x86)}\MSBuild\14.0\Bin\msbuild.exe"))
6060
{
6161
cinst microsoft-build-tools -y
6262
}

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ After installing nuget package mark following files to be copied to app director
3535
Setup HTTP proxy:
3636

3737
```csharp
38-
var ProxyServer = new ProxyServer();
38+
var proxyServer = new ProxyServer();
3939

40-
ProxyServer.BeforeRequest += OnRequest;
41-
ProxyServer.BeforeResponse += OnResponse;
42-
ProxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
43-
ProxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
40+
proxyServer.BeforeRequest += OnRequest;
41+
proxyServer.BeforeResponse += OnResponse;
42+
proxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
43+
proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
4444

4545

4646
//Exclude Https addresses you don't want to proxy
@@ -53,8 +53,8 @@ Setup HTTP proxy:
5353

5454
//An explicit endpoint is where the client knows about the existance of a proxy
5555
//So client sends request in a proxy friendly manner
56-
ProxyServer.AddEndPoint(explicitEndPoint);
57-
ProxyServer.Start();
56+
proxyServer.AddEndPoint(explicitEndPoint);
57+
proxyServer.Start();
5858

5959

6060
//Transparent endpoint is usefull for reverse proxying (client is not aware of the existance of proxy)
@@ -67,26 +67,29 @@ Setup HTTP proxy:
6767
{
6868
GenericCertificateName = "google.com"
6969
};
70-
ProxyServer.AddEndPoint(transparentEndPoint);
70+
proxyServer.AddEndPoint(transparentEndPoint);
7171

72-
//ProxyServer.UpStreamHttpProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
73-
//ProxyServer.UpStreamHttpsProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
72+
//proxyServer.UpStreamHttpProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
73+
//proxyServer.UpStreamHttpsProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
7474
75-
foreach (var endPoint in ProxyServer.ProxyEndPoints)
75+
foreach (var endPoint in proxyServer.ProxyEndPoints)
7676
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
7777
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
7878

7979
//Only explicit proxies can be set as system proxy!
80-
ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
81-
ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
80+
proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
81+
proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
8282

8383
//wait here (You can use something else as a wait function, I am using this as a demo)
8484
Console.Read();
8585

8686
//Unsubscribe & Quit
87-
ProxyServer.BeforeRequest -= OnRequest;
88-
ProxyServer.BeforeResponse -= OnResponse;
89-
ProxyServer.Stop();
87+
proxyServer.BeforeRequest -= OnRequest;
88+
proxyServer.BeforeResponse -= OnResponse;
89+
proxyServer.ServerCertificateValidationCallback -= OnCertificateValidation;
90+
proxyServer.ClientCertificateSelectionCallback -= OnCertificateSelection;
91+
92+
proxyServer.Stop();
9093

9194
```
9295
Sample request and response event handlers

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ internal SessionEventArgs(int bufferSize, Func<SessionEventArgs, Task> httpRespo
6262
ProxyClient = new ProxyClient();
6363
WebSession = new HttpWebClient();
6464
}
65-
66-
65+
6766
/// <summary>
6867
/// Read request body content as bytes[] for current session
6968
/// </summary>
@@ -98,7 +97,7 @@ private async Task ReadRequestBody()
9897
await this.ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream, WebSession.Request.ContentLength);
9998

10099
}
101-
else if(WebSession.Request.HttpVersion.Major == 1 && WebSession.Request.HttpVersion.Minor == 0)
100+
else if (WebSession.Request.HttpVersion.Major == 1 && WebSession.Request.HttpVersion.Minor == 0)
102101
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, requestBodyStream, long.MaxValue);
103102
}
104103
WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding, requestBodyStream.ToArray());
@@ -108,7 +107,7 @@ private async Task ReadRequestBody()
108107
//So that next time we can deliver body from cache
109108
WebSession.Request.RequestBodyRead = true;
110109
}
111-
110+
112111
}
113112

114113
/// <summary>
@@ -134,7 +133,7 @@ private async Task ReadResponseBody()
134133
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, WebSession.Response.ContentLength);
135134

136135
}
137-
else if(WebSession.Response.HttpVersion.Major == 1 && WebSession.Response.HttpVersion.Minor == 0)
136+
else if (WebSession.Response.HttpVersion.Major == 1 && WebSession.Response.HttpVersion.Minor == 0)
138137
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, long.MaxValue);
139138
}
140139

@@ -152,10 +151,13 @@ private async Task ReadResponseBody()
152151
/// <returns></returns>
153152
public async Task<byte[]> GetRequestBody()
154153
{
155-
if (WebSession.Request.RequestLocked)
156-
throw new Exception("You cannot call this function after request is made to server.");
154+
if (!WebSession.Request.RequestBodyRead)
155+
{
156+
if (WebSession.Request.RequestLocked)
157+
throw new Exception("You cannot call this function after request is made to server.");
157158

158-
await ReadRequestBody();
159+
await ReadRequestBody();
160+
}
159161
return WebSession.Request.RequestBody;
160162
}
161163
/// <summary>
@@ -164,12 +166,13 @@ public async Task<byte[]> GetRequestBody()
164166
/// <returns></returns>
165167
public async Task<string> GetRequestBodyAsString()
166168
{
167-
if (WebSession.Request.RequestLocked)
168-
throw new Exception("You cannot call this function after request is made to server.");
169-
170-
171-
await ReadRequestBody();
169+
if (!WebSession.Request.RequestBodyRead)
170+
{
171+
if (WebSession.Request.RequestLocked)
172+
throw new Exception("You cannot call this function after request is made to server.");
172173

174+
await ReadRequestBody();
175+
}
173176
//Use the encoding specified in request to decode the byte[] data to string
174177
return WebSession.Request.RequestBodyString ?? (WebSession.Request.RequestBodyString = WebSession.Request.Encoding.GetString(WebSession.Request.RequestBody));
175178
}
@@ -285,7 +288,7 @@ public async Task SetResponseBodyString(string body)
285288
var bodyBytes = WebSession.Response.Encoding.GetBytes(body);
286289

287290
await SetResponseBody(bodyBytes);
288-
}
291+
}
289292

290293
private async Task<byte[]> GetDecompressedResponseBody(string encodingType, byte[] responseBodyStream)
291294
{
@@ -345,7 +348,7 @@ public async Task Redirect(string url)
345348

346349
WebSession.Request.CancelRequest = true;
347350
}
348-
351+
349352
/// a generic responder method
350353
public async Task Respond(Response response)
351354
{

Titanium.Web.Proxy/Http/HttpWebClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ internal async Task ReceiveResponse()
115115

116116
if (string.IsNullOrEmpty(httpResult[0]))
117117
{
118-
await ServerConnection.StreamReader.ReadLineAsync();
118+
//Empty content in first-line, try again
119+
httpResult = (await ServerConnection.StreamReader.ReadLineAsync()).Split(ProxyConstants.SpaceSplit, 3);
119120
}
121+
120122
var httpVersion = httpResult[0].Trim().ToLower();
121123

122124
var version = new Version(1,1);

Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public void SetAsSystemHttpProxy(ExplicitProxyEndPoint endPoint)
186186
#if !DEBUG
187187
firefoxProxySettingsManager.AddFirefox();
188188
#endif
189-
Console.WriteLine("Set endpoint at Ip {1} and port: {2} as System HTTPS Proxy", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
189+
Console.WriteLine("Set endpoint at Ip {1} and port: {2} as System HTTP Proxy", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
190190

191191
}
192192

Titanium.Web.Proxy/ResponseHandler.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,28 @@ private void FixResponseProxyHeaders(List<HttpHeader> headers)
181181
/// <summary>
182182
/// Handle dispose of a client/server session
183183
/// </summary>
184-
/// <param name="client"></param>
184+
/// <param name="tcpClient"></param>
185185
/// <param name="clientStream"></param>
186186
/// <param name="clientStreamReader"></param>
187187
/// <param name="clientStreamWriter"></param>
188188
/// <param name="args"></param>
189-
private void Dispose(TcpClient client, IDisposable clientStream, IDisposable clientStreamReader,
189+
private void Dispose(TcpClient tcpClient, IDisposable clientStream, IDisposable clientStreamReader,
190190
IDisposable clientStreamWriter, IDisposable args)
191191
{
192+
193+
if (clientStream != null)
194+
{
195+
(clientStream as Stream).Close();
196+
(clientStream as Stream).Dispose();
197+
}
198+
199+
if (tcpClient != null)
200+
{
201+
tcpClient.Client.Close();
202+
tcpClient.Close();
203+
tcpClient.Client.Dispose();
204+
}
205+
192206
if (args != null)
193207
args.Dispose();
194208

@@ -198,11 +212,7 @@ private void Dispose(TcpClient client, IDisposable clientStream, IDisposable cl
198212
if (clientStreamWriter != null)
199213
clientStreamWriter.Dispose();
200214

201-
if (clientStream != null)
202-
clientStream.Dispose();
203215

204-
if (client != null)
205-
client.Close();
206216
}
207217
}
208218
}

0 commit comments

Comments
 (0)