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
ee38be01
Commit
ee38be01
authored
Jan 05, 2016
by
titanium007
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add connection cache
parent
a413adc7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
5 deletions
+35
-5
TcpConnectionManager.cs
Titanium.Web.Proxy/Http/TcpConnectionManager.cs
+30
-1
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+5
-4
No files found.
Titanium.Web.Proxy/Http/TcpConnectionManager.cs
View file @
ee38be01
...
@@ -13,6 +13,10 @@ namespace Titanium.Web.Proxy.Http
...
@@ -13,6 +13,10 @@ namespace Titanium.Web.Proxy.Http
{
{
public
class
TcpConnection
public
class
TcpConnection
{
{
public
string
HostName
{
get
;
set
;
}
public
int
port
{
get
;
set
;
}
public
bool
IsSecure
{
get
;
set
;
}
public
TcpClient
Client
{
get
;
set
;
}
public
TcpClient
Client
{
get
;
set
;
}
public
CustomBinaryReader
ServerStreamReader
{
get
;
set
;
}
public
CustomBinaryReader
ServerStreamReader
{
get
;
set
;
}
public
Stream
Stream
{
get
;
set
;
}
public
Stream
Stream
{
get
;
set
;
}
...
@@ -20,9 +24,21 @@ namespace Titanium.Web.Proxy.Http
...
@@ -20,9 +24,21 @@ namespace Titanium.Web.Proxy.Http
internal
class
TcpConnectionManager
internal
class
TcpConnectionManager
{
{
static
List
<
TcpConnection
>
ConnectionCache
=
new
List
<
TcpConnection
>();
public
static
TcpConnection
GetClient
(
string
Hostname
,
int
port
,
bool
IsSecure
)
public
static
TcpConnection
GetClient
(
string
Hostname
,
int
port
,
bool
IsSecure
)
{
{
lock
(
ConnectionCache
)
{
var
cached
=
ConnectionCache
.
FirstOrDefault
(
x
=>
x
.
HostName
==
Hostname
&&
x
.
port
==
port
&&
x
.
IsSecure
==
IsSecure
&&
x
.
Client
.
Connected
);
if
(
cached
!=
null
)
{
ConnectionCache
.
Remove
(
cached
);
return
cached
;
}
}
return
CreateClient
(
Hostname
,
port
,
IsSecure
);
return
CreateClient
(
Hostname
,
port
,
IsSecure
);
}
}
...
@@ -48,7 +64,20 @@ namespace Titanium.Web.Proxy.Http
...
@@ -48,7 +64,20 @@ namespace Titanium.Web.Proxy.Http
}
}
}
}
return
new
TcpConnection
()
{
Client
=
client
,
ServerStreamReader
=
new
CustomBinaryReader
(
stream
,
Encoding
.
ASCII
),
Stream
=
stream
};
return
new
TcpConnection
()
{
HostName
=
Hostname
,
port
=
port
,
IsSecure
=
IsSecure
,
Client
=
client
,
ServerStreamReader
=
new
CustomBinaryReader
(
stream
,
Encoding
.
ASCII
),
Stream
=
stream
};
}
public
static
void
ReleaseClient
(
TcpConnection
Connection
)
{
ConnectionCache
.
Add
(
Connection
);
}
}
}
}
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
ee38be01
...
@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy
...
@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy
private
static
void
HandleHttpSessionRequest
(
TcpClient
client
,
string
httpCmd
,
Stream
clientStream
,
private
static
void
HandleHttpSessionRequest
(
TcpClient
client
,
string
httpCmd
,
Stream
clientStream
,
CustomBinaryReader
clientStreamReader
,
StreamWriter
clientStreamWriter
,
string
secureTunnelHostName
)
CustomBinaryReader
clientStreamReader
,
StreamWriter
clientStreamWriter
,
string
secureTunnelHostName
)
{
{
TcpConnection
connection
=
null
;
while
(
true
)
while
(
true
)
{
{
if
(
string
.
IsNullOrEmpty
(
httpCmd
))
if
(
string
.
IsNullOrEmpty
(
httpCmd
))
...
@@ -203,7 +203,7 @@ namespace Titanium.Web.Proxy
...
@@ -203,7 +203,7 @@ namespace Titanium.Web.Proxy
//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.
connection
=
connection
==
null
?
TcpConnectionManager
.
GetClient
(
args
.
ProxySession
.
Request
.
RequestUri
.
Host
,
args
.
ProxySession
.
Request
.
RequestUri
.
Port
,
args
.
IsHttps
)
:
connection
;
var
connection
=
TcpConnectionManager
.
GetClient
(
args
.
ProxySession
.
Request
.
RequestUri
.
Host
,
args
.
ProxySession
.
Request
.
RequestUri
.
Port
,
args
.
IsHttps
)
;
args
.
ProxySession
.
SetConnection
(
connection
);
args
.
ProxySession
.
SetConnection
(
connection
);
args
.
ProxySession
.
SendRequest
();
args
.
ProxySession
.
SendRequest
();
...
@@ -228,9 +228,12 @@ namespace Titanium.Web.Proxy
...
@@ -228,9 +228,12 @@ namespace Titanium.Web.Proxy
//if connection is closing exit
//if connection is closing exit
if
(
args
.
ProxySession
.
Response
.
ResponseKeepAlive
==
false
)
if
(
args
.
ProxySession
.
Response
.
ResponseKeepAlive
==
false
)
{
{
connection
.
Client
.
Close
();
Dispose
(
client
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
args
);
Dispose
(
client
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
args
);
return
;
return
;
}
}
else
TcpConnectionManager
.
ReleaseClient
(
connection
);
// read the next request
// read the next request
httpCmd
=
clientStreamReader
.
ReadLine
();
httpCmd
=
clientStreamReader
.
ReadLine
();
...
@@ -243,8 +246,6 @@ namespace Titanium.Web.Proxy
...
@@ -243,8 +246,6 @@ namespace Titanium.Web.Proxy
}
}
}
}
if
(
connection
!=
null
)
connection
.
Client
.
Close
();
}
}
private
static
void
WriteConnectResponse
(
StreamWriter
clientStreamWriter
,
string
httpVersion
)
private
static
void
WriteConnectResponse
(
StreamWriter
clientStreamWriter
,
string
httpVersion
)
...
...
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