1- using Microsoft . AspNetCore . Http ;
2- using System ;
1+ using System ;
32using Microsoft . AspNetCore . Http . Headers ;
43using Microsoft . Net . Http . Headers ;
54using System . Collections . Generic ;
@@ -8,92 +7,112 @@ namespace Microsoft.VisualStudio.Web.BrowserLink
87{
98 internal static class BrowserLinkMiddleWareUtil
109 {
11- internal static int GetRequestPort ( IHeaderDictionary headers )
10+ internal static List < int > GetRequestPort ( RequestHeaders requestHeader )
1211 {
13- RequestHeaders requestHeader = new RequestHeaders ( headers ) ;
12+ List < int > requestPortList = new List < int > ( ) ;
1413
15- foreach ( EntityTagHeaderValue value in requestHeader . IfNoneMatch )
14+ if ( requestHeader . IfNoneMatch != null )
1615 {
17- string [ ] strings = value . ToString ( ) . Split ( ':' ) ;
18-
19- if ( strings . Length >= 2 )
16+ for ( int index = 0 ; index < requestHeader . IfNoneMatch . Count ; ++ index )
2017 {
21- return Int32 . Parse ( strings [ 1 ] . Substring ( 0 , strings [ 1 ] . Length - 1 ) ) ;
18+ string [ ] strings = requestHeader . IfNoneMatch [ index ] . ToString ( ) . Split ( ':' ) ;
19+
20+ if ( strings . Length >= 2 )
21+ {
22+ int port = - 1 ;
23+
24+ if ( Int32 . TryParse ( strings [ 1 ] . Substring ( 0 , strings [ 1 ] . Length - 1 ) , out port ) )
25+ {
26+ requestPortList . Add ( port ) ;
27+ }
28+ }
2229 }
2330 }
2431
25- return - 1 ;
32+ return requestPortList ;
2633 }
2734
2835 internal static int GetCurrentPort ( string connectionString )
2936 {
30- string [ ] strings1 = connectionString . Split ( ':' ) ;
31-
32- if ( strings1 . Length >= 3 )
37+ try
3338 {
34- string [ ] strings2 = strings1 [ 2 ] . Split ( '/' ) ;
39+ Uri uri = new Uri ( connectionString ) ;
3540
36- return Int32 . Parse ( strings2 [ 0 ] ) ;
41+ return uri . Port ;
42+ }
43+ catch ( UriFormatException )
44+ {
45+ return - 1 ;
46+ }
47+ catch ( ArgumentNullException )
48+ {
49+ return - 1 ;
3750 }
38-
39- return - 1 ;
4051 }
4152
42- internal static void RemoveETagAndTimeStamp ( IHeaderDictionary headers )
53+ internal static void RemoveETagAndTimeStamp ( RequestHeaders requestHeader )
4354 {
44- RequestHeaders requestHeader = new RequestHeaders ( headers ) ;
45-
4655 requestHeader . IfNoneMatch = null ;
4756 requestHeader . IfModifiedSince = null ;
4857 }
4958
50- internal static void DeletePortFromETag ( IHeaderDictionary headers )
59+ internal static void DeletePortFromETag ( RequestHeaders requestHeader )
5160 {
52- RequestHeaders requestHeader = new RequestHeaders ( headers ) ;
5361 string newETag = "" ;
5462 IList < EntityTagHeaderValue > list = requestHeader . IfNoneMatch ;
5563
56- foreach ( EntityTagHeaderValue value in list )
64+ for ( int index = 0 ; index < list . Count ; ++ index )
5765 {
58- String [ ] strings = value . ToString ( ) . Split ( ':' ) ;
66+ String [ ] strings = list [ index ] . ToString ( ) . Split ( ':' ) ;
5967
6068 if ( strings . Length >= 2 )
6169 {
6270 newETag = strings [ 0 ] + "\" " ;
63- break ;
71+ list [ index ] = new EntityTagHeaderValue ( newETag ) ;
6472 }
6573 }
6674
67- if ( newETag . Length > 0 )
68- {
69- list [ 0 ] = new EntityTagHeaderValue ( newETag ) ;
70- requestHeader . IfNoneMatch = list ;
71- }
75+ requestHeader . IfNoneMatch = list ;
7276 }
7377
74- internal static void AddToETag ( IHeaderDictionary headers , int port )
78+ internal static void AddToETag ( ResponseHeaders responseHeader , int port )
7579 {
76- ResponseHeaders responseHeader = new ResponseHeaders ( headers ) ;
77-
78- if ( responseHeader . ETag != null )
80+ if ( responseHeader . ETag == null )
81+ {
82+ responseHeader . ETag = new EntityTagHeaderValue ( "\" :" + port + "\" " ) ;
83+ }
84+ else
7985 {
8086 string temp = responseHeader . ETag . ToString ( ) . Substring ( 0 , responseHeader . ETag . ToString ( ) . Length - 1 ) + ":" + port + "\" " ;
8187 responseHeader . ETag = new EntityTagHeaderValue ( temp ) ;
8288 }
8389 }
8490
85- internal static int FilterRequestHeader ( IHeaderDictionary headers , string connectionString )
91+ internal static bool IfMatch ( List < int > requestPortList , int currentPort )
92+ {
93+ foreach ( int port in requestPortList )
94+ {
95+ if ( port == currentPort )
96+ {
97+ return true ;
98+ }
99+ }
100+
101+ return false ;
102+ }
103+
104+ internal static int FilterRequestHeader ( RequestHeaders requestHeader , string connectionString )
86105 {
87- int requestPort = GetRequestPort ( headers ) ;
106+ List < int > requestPortList = GetRequestPort ( requestHeader ) ;
88107 int currentPort = GetCurrentPort ( connectionString ) ;
89108
90- if ( requestPort != currentPort )
109+ if ( ! IfMatch ( requestPortList , currentPort ) )
91110 {
92- RemoveETagAndTimeStamp ( headers ) ;
111+ RemoveETagAndTimeStamp ( requestHeader ) ;
93112 }
94113 else
95114 {
96- DeletePortFromETag ( headers ) ;
115+ DeletePortFromETag ( requestHeader ) ;
97116 }
98117
99118 return currentPort ;
0 commit comments