Skip to content

Commit 992d081

Browse files
author
Yang Lyu
committed
Change GetRequestPort method to return a list of ports.
Use System.uri to process the connectionstring. Modify DeletePortFromETag function. Add more unit tests.
1 parent 219a47d commit 992d081

3 files changed

Lines changed: 379 additions & 56 deletions

File tree

src/Microsoft.VisualStudio.Web.BrowserLink/BrowserLinkMiddleWare.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ internal Task Invoke(HttpContext context)
4747
{
4848
RequestHeaders requestHeader = new RequestHeaders(context.Request.Headers);
4949

50-
if (requestHeader.IfNoneMatch != null && BrowserLinkMiddleWareUtil.GetRequestPort(context.Request.Headers) != -1)
50+
if (requestHeader.IfNoneMatch != null && BrowserLinkMiddleWareUtil.GetRequestPort(requestHeader).Count != 0)
5151
{
52-
BrowserLinkMiddleWareUtil.RemoveETagAndTimeStamp(context.Request.Headers);
52+
BrowserLinkMiddleWareUtil.RemoveETagAndTimeStamp(requestHeader);
5353
}
5454

5555
return ExecuteWithoutFilter(context);
@@ -79,9 +79,11 @@ private async Task ExecuteWithFilter(IHttpSocketAdapter injectScriptSocket, stri
7979

8080
PreprocessRequestHeader(httpContext, ref currentPort);
8181

82+
RequestHeaders requestHeader = new RequestHeaders(httpContext.Request.Headers);
83+
8284
if (currentPort == -1)
8385
{
84-
BrowserLinkMiddleWareUtil.RemoveETagAndTimeStamp(httpContext.Request.Headers);
86+
BrowserLinkMiddleWareUtil.RemoveETagAndTimeStamp(requestHeader);
8587
}
8688

8789
using (ScriptInjectionFilterStream filter = new ScriptInjectionFilterStream(injectScriptSocket, filterContext))
@@ -90,7 +92,9 @@ private async Task ExecuteWithFilter(IHttpSocketAdapter injectScriptSocket, stri
9092
httpContext.Response.OnStarting(delegate ()
9193
{
9294
httpContext.Response.ContentLength = null;
93-
BrowserLinkMiddleWareUtil.AddToETag(httpContext.Response.Headers, currentPort);
95+
ResponseHeaders responseHeader = new ResponseHeaders(httpContext.Response.Headers);
96+
97+
BrowserLinkMiddleWareUtil.AddToETag(responseHeader, currentPort);
9498

9599
return StaticTaskResult.True;
96100
});
@@ -235,7 +239,7 @@ private void PreprocessRequestHeader(HttpContext httpContext, ref int currentPor
235239

236240
if (GetHostConnectionData(_applicationPath, out connectionData))
237241
{
238-
currentPort = BrowserLinkMiddleWareUtil.FilterRequestHeader(httpContext.Request.Headers, connectionData.ConnectionString);
242+
currentPort = BrowserLinkMiddleWareUtil.FilterRequestHeader(requestHeader, connectionData.ConnectionString);
239243
}
240244
}
241245
}

src/Microsoft.VisualStudio.Web.BrowserLink/BrowserLinkMiddleWareUtil.cs

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.AspNetCore.Http;
2-
using System;
1+
using System;
32
using Microsoft.AspNetCore.Http.Headers;
43
using Microsoft.Net.Http.Headers;
54
using 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

Comments
 (0)