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

Commit f02a8bf

Browse files
Merge pull request #252 from justcoding121/develop
Beta
2 parents 7b3b1f9 + 5fb387b commit f02a8bf

File tree

9 files changed

+38
-23
lines changed

9 files changed

+38
-23
lines changed

Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ public async Task OnRequest(object sender, SessionEventArgs e)
116116
//read request headers
117117
var requestHeaders = e.WebSession.Request.RequestHeaders;
118118

119-
var method = e.WebSession.Request.Method.ToUpper();
120-
if (method == "POST" || method == "PUT" || method == "PATCH")
119+
if (e.WebSession.Request.HasBody)
121120
{
122121
//Get/Set request body bytes
123122
byte[] bodyBytes = await e.GetRequestBody();

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ internal SessionEventArgs(int bufferSize, Func<SessionEventArgs, Task> httpRespo
111111
private async Task ReadRequestBody()
112112
{
113113
//GET request don't have a request body to read
114-
var method = WebSession.Request.Method.ToUpper();
115-
if (method != "POST" && method != "PUT" && method != "PATCH")
114+
if (!WebSession.Request.HasBody)
116115
{
117116
throw new BodyNotFoundException("Request don't have a body. " +
118117
"Please verify that this request is a Http POST/PUT/PATCH and request " +

Titanium.Web.Proxy/Helpers/RunTime.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ namespace Titanium.Web.Proxy.Helpers
77
/// </summary>
88
internal class RunTime
99
{
10-
private static Lazy<bool> isMonoRuntime
11-
= new Lazy<bool>(()=> Type.GetType("Mono.Runtime") != null);
1210
/// <summary>
13-
/// Checks if current run time is Mono
11+
/// cache for mono runtime check
1412
/// </summary>
1513
/// <returns></returns>
16-
internal static bool IsRunningOnMono()
17-
{
18-
return isMonoRuntime.Value;
19-
}
14+
private static Lazy<bool> isRunningOnMono
15+
= new Lazy<bool>(()=> Type.GetType("Mono.Runtime") != null);
16+
17+
/// <summary>
18+
/// Is running on Mono?
19+
/// </summary>
20+
internal static bool IsRunningOnMono => isRunningOnMono.Value;
2021
}
2122
}

Titanium.Web.Proxy/Http/Request.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public class Request : IDisposable
2626
/// </summary>
2727
public Version HttpVersion { get; set; }
2828

29+
/// <summary>
30+
/// Has request body?
31+
/// </summary>
32+
public bool HasBody => Method == "POST" || Method == "PUT" || Method == "PATCH";
33+
2934
/// <summary>
3035
/// Request Http hostanem
3136
/// </summary>

Titanium.Web.Proxy/Network/CertificateManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal CertificateEngine Engine
3939
set
4040
{
4141
//For Mono only Bouncy Castle is supported
42-
if (RunTime.IsRunningOnMono())
42+
if (RunTime.IsRunningOnMono)
4343
{
4444
value = CertificateEngine.BouncyCastle;
4545
}
@@ -226,7 +226,7 @@ public void TrustRootCertificate()
226226
/// <returns></returns>
227227
public bool TrustRootCertificateAsAdministrator()
228228
{
229-
if (RunTime.IsRunningOnMono())
229+
if (RunTime.IsRunningOnMono)
230230
{
231231
return false;
232232
}

Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public void RemoveEndPoint(ProxyEndPoint endPoint)
348348
/// <param name="endPoint"></param>
349349
public void SetAsSystemHttpProxy(ExplicitProxyEndPoint endPoint)
350350
{
351-
if (RunTime.IsRunningOnMono())
351+
if (RunTime.IsRunningOnMono)
352352
{
353353
throw new Exception("Mono Runtime do not support system proxy settings.");
354354
}
@@ -375,7 +375,7 @@ public void SetAsSystemHttpProxy(ExplicitProxyEndPoint endPoint)
375375
/// <param name="endPoint"></param>
376376
public void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
377377
{
378-
if (RunTime.IsRunningOnMono())
378+
if (RunTime.IsRunningOnMono)
379379
{
380380
throw new Exception("Mono Runtime do not support system proxy settings.");
381381
}
@@ -416,7 +416,7 @@ public void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
416416
/// </summary>
417417
public void DisableSystemHttpProxy()
418418
{
419-
if (RunTime.IsRunningOnMono())
419+
if (RunTime.IsRunningOnMono)
420420
{
421421
throw new Exception("Mono Runtime do not support system proxy settings.");
422422
}
@@ -429,7 +429,7 @@ public void DisableSystemHttpProxy()
429429
/// </summary>
430430
public void DisableSystemHttpsProxy()
431431
{
432-
if (RunTime.IsRunningOnMono())
432+
if (RunTime.IsRunningOnMono)
433433
{
434434
throw new Exception("Mono Runtime do not support system proxy settings.");
435435
}
@@ -442,7 +442,7 @@ public void DisableSystemHttpsProxy()
442442
/// </summary>
443443
public void DisableAllSystemProxies()
444444
{
445-
if (RunTime.IsRunningOnMono())
445+
if (RunTime.IsRunningOnMono)
446446
{
447447
throw new Exception("Mono Runtime do not support system proxy settings.");
448448
}
@@ -474,8 +474,9 @@ public void Start()
474474

475475
CertificateManager.ClearIdleCertificates(CertificateCacheTimeOutMinutes);
476476

477-
if (!RunTime.IsRunningOnMono())
477+
if (!RunTime.IsRunningOnMono)
478478
{
479+
//clear orphaned windows auth states every 2 minutes
479480
WinAuthEndPoint.ClearIdleStates(2);
480481
}
481482

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,16 @@ await CheckAuthorization(clientStreamWriter,
322322
PrepareRequestHeaders(args.WebSession.Request.RequestHeaders, args.WebSession);
323323
args.WebSession.Request.Host = args.WebSession.Request.RequestUri.Authority;
324324

325+
//if win auth is enabled
326+
//we need a cache of request body
327+
//so that we can send it after authentication in WinAuthHandler.cs
328+
if (EnableWinAuth
329+
&& !RunTime.IsRunningOnMono
330+
&& args.WebSession.Request.HasBody)
331+
{
332+
await args.GetRequestBody();
333+
}
334+
325335
//If user requested interception do it
326336
if (BeforeRequest != null)
327337
{
@@ -461,8 +471,7 @@ await WriteResponseStatus(args.WebSession.Response.HttpVersion, "417",
461471
if (!args.WebSession.Request.ExpectationFailed)
462472
{
463473
//If its a post/put/patch request, then read the client html body and send it to server
464-
var method = args.WebSession.Request.Method.ToUpper();
465-
if (method == "POST" || method == "PUT" || method == "PATCH")
474+
if (args.WebSession.Request.HasBody)
466475
{
467476
await SendClientRequestBody(args);
468477
}

Titanium.Web.Proxy/ResponseHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private async Task<bool> HandleHttpSessionResponse(SessionEventArgs args)
3838

3939
//check for windows authentication
4040
if(EnableWinAuth
41-
&& !RunTime.IsRunningOnMono()
41+
&& !RunTime.IsRunningOnMono
4242
&& args.WebSession.Response.ResponseStatusCode == "401")
4343
{
4444
var disposed = await Handle401UnAuthorized(args);

Titanium.Web.Proxy/WinAuthHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ internal async Task<bool> Handle401UnAuthorized(SessionEventArgs args)
9696
//challenge value will start with any of the scheme selected
9797
else
9898
{
99-
scheme = authSchemes.FirstOrDefault(x => authHeader.Value.StartsWith(x, StringComparison.OrdinalIgnoreCase));
99+
scheme = authSchemes.FirstOrDefault(x => authHeader.Value.StartsWith(x, StringComparison.OrdinalIgnoreCase)
100+
&& authHeader.Value.Length > x.Length + 1);
100101

101102
var serverToken = authHeader.Value.Substring(scheme.Length + 1);
102103
var clientToken = WinAuthHandler.GetFinalAuthToken(args.WebSession.Request.Host, serverToken, args.Id);

0 commit comments

Comments
 (0)