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
9279e799
Commit
9279e799
authored
Apr 30, 2019
by
Stéphane Graziano
Committed by
Jehonathan Thomas
May 04, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimisation : Rewrite startsWith with PeekBytesAsync instead of looping on PeekByteAsync
parent
6a186156
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
30 deletions
+18
-30
ExplicitClientHandler.cs
src/Titanium.Web.Proxy/ExplicitClientHandler.cs
+1
-1
HttpHelper.cs
src/Titanium.Web.Proxy/Helpers/HttpHelper.cs
+17
-29
No files found.
src/Titanium.Web.Proxy/ExplicitClientHandler.cs
View file @
9279e799
...
@@ -49,7 +49,7 @@ namespace Titanium.Web.Proxy
...
@@ -49,7 +49,7 @@ namespace Titanium.Web.Proxy
TunnelConnectSessionEventArgs
connectArgs
=
null
;
TunnelConnectSessionEventArgs
connectArgs
=
null
;
// Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request)
// Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request)
if
(
await
HttpHelper
.
IsConnectMethod
(
clientStream
)
==
1
)
if
(
await
HttpHelper
.
IsConnectMethod
(
clientStream
,
cancellationToken
)
==
1
)
{
{
// read the first line HTTP command
// read the first line HTTP command
string
httpCmd
=
await
clientStream
.
ReadLineAsync
(
cancellationToken
);
string
httpCmd
=
await
clientStream
.
ReadLineAsync
(
cancellationToken
);
...
...
src/Titanium.Web.Proxy/Helpers/HttpHelper.cs
View file @
9279e799
using
System
;
using
System
;
using
System.Text
;
using
System.Text
;
using
System.Text.RegularExpressions
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
StreamExtended.Network
;
using
StreamExtended.Network
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
...
@@ -120,9 +122,9 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -120,9 +122,9 @@ namespace Titanium.Web.Proxy.Helpers
/// </summary>
/// </summary>
/// <param name="clientStreamReader">The client stream reader.</param>
/// <param name="clientStreamReader">The client stream reader.</param>
/// <returns>1: when CONNECT, 0: when valid HTTP method, -1: otherwise</returns>
/// <returns>1: when CONNECT, 0: when valid HTTP method, -1: otherwise</returns>
internal
static
Task
<
int
>
IsConnectMethod
(
ICustomStreamReader
clientStreamReader
)
internal
static
Task
<
int
>
IsConnectMethod
(
ICustomStreamReader
clientStreamReader
,
CancellationToken
cancellationToken
=
default
(
CancellationToken
)
)
{
{
return
startsWith
(
clientStreamReader
,
"CONNECT"
);
return
startsWith
(
clientStreamReader
,
"CONNECT"
,
cancellationToken
);
}
}
/// <summary>
/// <summary>
...
@@ -130,9 +132,9 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -130,9 +132,9 @@ namespace Titanium.Web.Proxy.Helpers
/// </summary>
/// </summary>
/// <param name="clientStreamReader">The client stream reader.</param>
/// <param name="clientStreamReader">The client stream reader.</param>
/// <returns>1: when PRI, 0: when valid HTTP method, -1: otherwise</returns>
/// <returns>1: when PRI, 0: when valid HTTP method, -1: otherwise</returns>
internal
static
Task
<
int
>
IsPriMethod
(
ICustomStreamReader
clientStreamReader
)
internal
static
Task
<
int
>
IsPriMethod
(
ICustomStreamReader
clientStreamReader
,
CancellationToken
cancellationToken
=
default
(
CancellationToken
)
)
{
{
return
startsWith
(
clientStreamReader
,
"PRI"
);
return
startsWith
(
clientStreamReader
,
"PRI"
,
cancellationToken
);
}
}
/// <summary>
/// <summary>
...
@@ -143,37 +145,23 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -143,37 +145,23 @@ namespace Titanium.Web.Proxy.Helpers
/// <returns>
/// <returns>
/// 1: when starts with the given string, 0: when valid HTTP method, -1: otherwise
/// 1: when starts with the given string, 0: when valid HTTP method, -1: otherwise
/// </returns>
/// </returns>
private
static
async
Task
<
int
>
startsWith
(
ICustomStreamReader
clientStreamReader
,
string
expectedStart
)
private
static
async
Task
<
int
>
startsWith
(
ICustomStreamReader
clientStreamReader
,
string
expectedStart
,
CancellationToken
cancellationToken
=
default
(
CancellationToken
)
)
{
{
bool
isExpected
=
true
;
int
iRet
=
-
1
;
int
lengthToCheck
=
10
;
int
lengthToCheck
=
10
;
for
(
int
i
=
0
;
i
<
lengthToCheck
;
i
++)
{
int
b
=
await
clientStreamReader
.
PeekByteAsync
(
i
);
if
(
b
==
-
1
)
{
return
-
1
;
}
if
(
b
==
' '
&&
i
>
2
)
{
return
isExpected
?
1
:
0
;
}
char
ch
=
(
char
)
b
;
var
vBuffer
=
await
clientStreamReader
.
PeekBytesAsync
(
0
,
lengthToCheck
,
cancellationToken
);
if
(!
char
.
IsLetter
(
ch
))
if
(
vBuffer
!=
null
)
{
{
return
-
1
;
var
httpMethod
=
defaultEncoding
.
GetString
(
vBuffer
);
}
if
(
i
>=
expectedStart
.
Length
||
ch
!=
expectedStart
[
i
]
)
if
(
httpMethod
.
StartsWith
(
expectedStart
)
)
{
iRet
=
1
;
isExpected
=
false
;
else
if
(
Regex
.
Match
(
httpMethod
,
@"^[a-z]{3,} "
,
RegexOptions
.
IgnoreCase
).
Success
)
//valid HTTP requests start by at least 3 letters + space
}
iRet
=
0
;
}
}
// only letters
return
iRet
;
return
isExpected
?
1
:
0
;
}
}
}
}
}
}
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