1
1
using System ;
2
2
using System . Text ;
3
+ using System . Text . RegularExpressions ;
4
+ using System . Threading ;
3
5
using System . Threading . Tasks ;
4
6
using StreamExtended . Network ;
5
7
using Titanium . Web . Proxy . Extensions ;
@@ -120,19 +122,19 @@ internal static string GetWildCardDomainName(string hostname)
120
122
/// </summary>
121
123
/// <param name="clientStreamReader">The client stream reader.</param>
122
124
/// <returns>1: when CONNECT, 0: when valid HTTP method, -1: otherwise</returns>
123
- internal static Task < int > IsConnectMethod ( ICustomStreamReader clientStreamReader )
125
+ internal static Task < int > IsConnectMethod ( ICustomStreamReader clientStreamReader , CancellationToken cancellationToken = default ( CancellationToken ) )
124
126
{
125
- return startsWith ( clientStreamReader , "CONNECT" ) ;
127
+ return startsWith ( clientStreamReader , "CONNECT" , cancellationToken ) ;
126
128
}
127
129
128
130
/// <summary>
129
131
/// Determines whether is pri method (HTTP/2).
130
132
/// </summary>
131
133
/// <param name="clientStreamReader">The client stream reader.</param>
132
134
/// <returns>1: when PRI, 0: when valid HTTP method, -1: otherwise</returns>
133
- internal static Task < int > IsPriMethod ( ICustomStreamReader clientStreamReader )
135
+ internal static Task < int > IsPriMethod ( ICustomStreamReader clientStreamReader , CancellationToken cancellationToken = default ( CancellationToken ) )
134
136
{
135
- return startsWith ( clientStreamReader , "PRI" ) ;
137
+ return startsWith ( clientStreamReader , "PRI" , cancellationToken ) ;
136
138
}
137
139
138
140
/// <summary>
@@ -143,37 +145,23 @@ internal static Task<int> IsPriMethod(ICustomStreamReader clientStreamReader)
143
145
/// <returns>
144
146
/// 1: when starts with the given string, 0: when valid HTTP method, -1: otherwise
145
147
/// </returns>
146
- private static async Task < int > startsWith ( ICustomStreamReader clientStreamReader , string expectedStart )
148
+ private static async Task < int > startsWith ( ICustomStreamReader clientStreamReader , string expectedStart , CancellationToken cancellationToken = default ( CancellationToken ) )
147
149
{
148
- bool isExpected = true ;
150
+ int iRet = - 1 ;
149
151
int lengthToCheck = 10 ;
150
- for ( int i = 0 ; i < lengthToCheck ; i ++ )
151
- {
152
- int b = await clientStreamReader . PeekByteAsync ( i ) ;
153
- if ( b == - 1 )
154
- {
155
- return - 1 ;
156
- }
157
-
158
- if ( b == ' ' && i > 2 )
159
- {
160
- return isExpected ? 1 : 0 ;
161
- }
162
152
163
- char ch = ( char ) b ;
164
- if ( ! char . IsLetter ( ch ) )
165
- {
166
- return - 1 ;
167
- }
153
+ var vBuffer = await clientStreamReader . PeekBytesAsync ( 0 , lengthToCheck , cancellationToken ) ;
154
+ if ( vBuffer != null )
155
+ {
156
+ var httpMethod = defaultEncoding . GetString ( vBuffer ) ;
168
157
169
- if ( i >= expectedStart . Length || ch != expectedStart [ i ] )
170
- {
171
- isExpected = false ;
172
- }
158
+ if ( httpMethod . StartsWith ( expectedStart ) )
159
+ iRet = 1 ;
160
+ else if ( Regex . Match ( httpMethod , @"^[a-z]{3,} " , RegexOptions . IgnoreCase ) . Success ) //valid HTTP requests start by at least 3 letters + space
161
+ iRet = 0 ;
173
162
}
174
163
175
- // only letters
176
- return isExpected ? 1 : 0 ;
164
+ return iRet ;
177
165
}
178
166
}
179
167
}
0 commit comments