Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
f88da921
Commit
f88da921
authored
Nov 21, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor connection generation
parent
46b3e103
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
61 deletions
+37
-61
RequestHandler.cs
src/Titanium.Web.Proxy/RequestHandler.cs
+29
-58
ResponseHandler.cs
src/Titanium.Web.Proxy/ResponseHandler.cs
+8
-3
No files found.
src/Titanium.Web.Proxy/RequestHandler.cs
View file @
f88da921
...
@@ -198,37 +198,13 @@ namespace Titanium.Web.Proxy
...
@@ -198,37 +198,13 @@ namespace Titanium.Web.Proxy
connection
=
null
;
connection
=
null
;
}
}
RetryResult
result
;
var
result
=
await
handleHttpSessionRequest
(
httpCmd
,
args
,
connection
,
if
(
request
.
UpgradeToWebSocket
)
clientConnection
.
NegotiatedApplicationProtocol
,
{
cancellationToken
,
cancellationTokenSource
);
//a connection generator task with captured parameters via closure.
Func
<
Task
<
TcpServerConnection
>>
generator
=
()
=>
tcpConnectionFactory
.
GetServerConnection
(
this
,
args
,
isConnect
:
false
,
applicationProtocol
:
clientConnection
.
NegotiatedApplicationProtocol
,
noCache
:
false
,
cancellationToken
:
cancellationToken
);
//for connection pool, retry fails until cache is exhausted.
result
=
await
retryPolicy
<
ServerConnectionException
>().
ExecuteAsync
(
async
(
serverConnection
)
=>
{
args
.
TimeLine
[
"Connection Ready"
]
=
DateTime
.
Now
;
// if upgrading to websocket then relay the request without reading the contents
await
handleWebSocketUpgrade
(
httpCmd
,
args
,
request
,
response
,
clientStream
,
clientStreamWriter
,
serverConnection
,
cancellationTokenSource
,
cancellationToken
);
closeServerConnection
=
true
;
return
false
;
},
generator
,
connection
);
}
else
{
result
=
await
handleHttpSessionRequest
(
args
,
connection
,
clientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
}
//update connection to latest used
//update connection to latest used
connection
=
result
.
LatestConnection
;
connection
=
result
.
LatestConnection
;
closeServerConnection
=
!
result
.
Continue
;
//throw if exception happened
//throw if exception happened
if
(!
result
.
IsSuccess
)
if
(!
result
.
IsSuccess
)
...
@@ -260,13 +236,13 @@ namespace Titanium.Web.Proxy
...
@@ -260,13 +236,13 @@ namespace Titanium.Web.Proxy
throw
new
Exception
(
"Session was terminated by user."
);
throw
new
Exception
(
"Session was terminated by user."
);
}
}
//
Get/r
elease server connection for each HTTP session instead of per client connection.
//
R
elease server connection for each HTTP session instead of per client connection.
//This will be more efficient especially when client is idly holding server connection
//This will be more efficient especially when client is idly holding server connection
//between sessions without using it.
//between sessions without using it.
//Do not release authenticated connections for performance reasons.
//Do not release authenticated connections for performance reasons.
//Otherwise it will keep authenticating per session.
//Otherwise it will keep authenticating per session.
if
(
EnableConnectionPool
&&
connection
!=
null
if
(
EnableConnectionPool
&&
connection
!=
null
&&
!
connection
.
IsWinAuthenticated
)
&&
!
connection
.
IsWinAuthenticated
)
{
{
await
tcpConnectionFactory
.
Release
(
connection
);
await
tcpConnectionFactory
.
Release
(
connection
);
connection
=
null
;
connection
=
null
;
...
@@ -300,45 +276,40 @@ namespace Titanium.Web.Proxy
...
@@ -300,45 +276,40 @@ namespace Titanium.Web.Proxy
}
}
}
}
private
async
Task
<
RetryResult
>
handleHttpSessionRequest
(
SessionEventArgs
args
,
private
async
Task
<
RetryResult
>
handleHttpSessionRequest
(
string
httpCmd
,
SessionEventArgs
args
,
TcpServerConnection
connection
,
TcpServerConnection
serverConnection
,
SslApplicationProtocol
sslApplicationProtocol
,
SslApplicationProtocol
protocol
,
CancellationToken
cancellationToken
,
CancellationTokenSource
cancellationTokenSource
)
CancellationToken
cancellationToken
)
{
{
//host/scheme changed from ReRequest
if
(
args
.
ReRequest
&&
(
args
.
HttpClient
.
Request
.
IsHttps
!=
connection
.
IsHttps
||
args
.
HttpClient
.
Request
.
Host
!=
connection
.
HostName
))
{
connection
=
null
;
}
//a connection generator task with captured parameters via closure.
//a connection generator task with captured parameters via closure.
Func
<
Task
<
TcpServerConnection
>>
generator
=
()
=>
Func
<
Task
<
TcpServerConnection
>>
generator
=
()
=>
tcpConnectionFactory
.
GetServerConnection
(
this
,
args
,
isConnect
:
false
,
tcpConnectionFactory
.
GetServerConnection
(
this
,
args
,
isConnect
:
false
,
applicationProtocol
:
p
rotocol
,
applicationProtocol
:
sslApplicationP
rotocol
,
noCache
:
false
,
cancellationToken
:
cancellationToken
);
noCache
:
false
,
cancellationToken
:
cancellationToken
);
//for connection pool, retry fails until cache is exhausted.
//for connection pool, retry fails until cache is exhausted.
return
await
retryPolicy
<
ServerConnectionException
>().
ExecuteAsync
(
async
(
serverC
onnection
)
=>
return
await
retryPolicy
<
ServerConnectionException
>().
ExecuteAsync
(
async
(
c
onnection
)
=>
{
{
args
.
TimeLine
[
"Connection Ready"
]
=
DateTime
.
Now
;
args
.
TimeLine
[
"Connection Ready"
]
=
DateTime
.
Now
;
if
(
args
.
HttpClient
.
Request
.
UpgradeToWebSocket
)
{
// if upgrading to websocket then relay the request without reading the contents
await
handleWebSocketUpgrade
(
httpCmd
,
args
,
args
.
HttpClient
.
Request
,
args
.
HttpClient
.
Response
,
args
.
ProxyClient
.
ClientStream
,
args
.
ProxyClient
.
ClientStreamWriter
,
connection
,
cancellationTokenSource
,
cancellationToken
);
return
false
;
}
args
.
TimeLine
[
"Connection Ready"
]
=
DateTime
.
Now
;
// construct the web request that we are going to issue on behalf of the client.
// construct the web request that we are going to issue on behalf of the client.
await
handleHttpSessionRequest
(
serverC
onnection
,
args
);
await
handleHttpSessionRequest
(
c
onnection
,
args
);
return
true
;
return
true
;
},
generator
,
c
onnection
);
},
generator
,
serverC
onnection
);
}
}
/// <summary>
private
async
Task
handleHttpSessionRequest
(
TcpServerConnection
connection
,
SessionEventArgs
args
)
/// Handle a specific session (request/response sequence)
/// </summary>
/// <param name="serverConnection">The tcp connection.</param>
/// <param name="args">The session event arguments.</param>
/// <returns></returns>
private
async
Task
handleHttpSessionRequest
(
TcpServerConnection
serverConnection
,
SessionEventArgs
args
)
{
{
var
cancellationToken
=
args
.
CancellationTokenSource
.
Token
;
var
cancellationToken
=
args
.
CancellationTokenSource
.
Token
;
var
request
=
args
.
HttpClient
.
Request
;
var
request
=
args
.
HttpClient
.
Request
;
...
@@ -350,7 +321,7 @@ namespace Titanium.Web.Proxy
...
@@ -350,7 +321,7 @@ namespace Titanium.Web.Proxy
// and see if server would return 100 conitinue
// and see if server would return 100 conitinue
if
(
request
.
ExpectContinue
)
if
(
request
.
ExpectContinue
)
{
{
args
.
HttpClient
.
SetConnection
(
serverC
onnection
);
args
.
HttpClient
.
SetConnection
(
c
onnection
);
await
args
.
HttpClient
.
SendRequest
(
Enable100ContinueBehaviour
,
args
.
IsTransparent
,
await
args
.
HttpClient
.
SendRequest
(
Enable100ContinueBehaviour
,
args
.
IsTransparent
,
cancellationToken
);
cancellationToken
);
}
}
...
@@ -377,7 +348,7 @@ namespace Titanium.Web.Proxy
...
@@ -377,7 +348,7 @@ namespace Titanium.Web.Proxy
// If expect continue is not enabled then set the connectio and send request headers
// If expect continue is not enabled then set the connectio and send request headers
if
(!
request
.
ExpectContinue
)
if
(!
request
.
ExpectContinue
)
{
{
args
.
HttpClient
.
SetConnection
(
serverC
onnection
);
args
.
HttpClient
.
SetConnection
(
c
onnection
);
await
args
.
HttpClient
.
SendRequest
(
Enable100ContinueBehaviour
,
args
.
IsTransparent
,
await
args
.
HttpClient
.
SendRequest
(
Enable100ContinueBehaviour
,
args
.
IsTransparent
,
cancellationToken
);
cancellationToken
);
}
}
...
...
src/Titanium.Web.Proxy/ResponseHandler.cs
View file @
f88da921
...
@@ -3,6 +3,7 @@ using System.Net;
...
@@ -3,6 +3,7 @@ using System.Net;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Network.WinAuth.Security
;
using
Titanium.Web.Proxy.Network.WinAuth.Security
;
namespace
Titanium.Web.Proxy
namespace
Titanium.Web.Proxy
...
@@ -63,7 +64,7 @@ namespace Titanium.Web.Proxy
...
@@ -63,7 +64,7 @@ namespace Titanium.Web.Proxy
//write custom user response with body and return.
//write custom user response with body and return.
await
clientStreamWriter
.
WriteResponseAsync
(
response
,
cancellationToken
:
cancellationToken
);
await
clientStreamWriter
.
WriteResponseAsync
(
response
,
cancellationToken
:
cancellationToken
);
if
(
args
.
HttpClient
.
Connection
!=
null
if
(
args
.
HttpClient
.
Connection
!=
null
&&
!
args
.
HttpClient
.
CloseServerConnection
)
&&
!
args
.
HttpClient
.
CloseServerConnection
)
{
{
// syphon out the original response body from server connection
// syphon out the original response body from server connection
...
@@ -78,10 +79,14 @@ namespace Titanium.Web.Proxy
...
@@ -78,10 +79,14 @@ namespace Titanium.Web.Proxy
// likely after making modifications from User Response Handler
// likely after making modifications from User Response Handler
if
(
args
.
ReRequest
)
if
(
args
.
ReRequest
)
{
{
await
tcpConnectionFactory
.
Release
(
args
.
HttpClient
.
Connection
);
// clear current response
// clear current response
await
args
.
ClearResponse
(
cancellationToken
);
await
args
.
ClearResponse
(
cancellationToken
);
await
handleHttpSessionRequest
(
args
,
args
.
HttpClient
.
Connection
,
var
httpCmd
=
Request
.
CreateRequestLine
(
args
.
HttpClient
.
Request
.
Method
,
args
.
ClientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
args
.
HttpClient
.
Request
.
OriginalUrl
,
args
.
HttpClient
.
Request
.
HttpVersion
);
await
handleHttpSessionRequest
(
httpCmd
,
args
,
null
,
args
.
ClientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
,
args
.
CancellationTokenSource
);
return
;
return
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment