Skip to content

Commit 82090b4

Browse files
committed
supporting SocketsHttpHandler
1 parent f987285 commit 82090b4

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectly.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,31 @@ public override void Initialize(AnalysisContext context)
4141

4242
context.RegisterCompilationStartAction(context =>
4343
{
44-
var propertySymbol = context.Compilation
44+
var httpClientHandlerPropSymbol = context.Compilation
4545
.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNetHttpHttpClientHandler)
4646
?.GetMembers(MaxResponseHeadersLengthPropertyName)
4747
.FirstOrDefault();
4848

49-
if (propertySymbol is null)
49+
var socketClientHandlerPropSymbol = context.Compilation
50+
.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNetHttpSocketsHttpHandler)
51+
?.GetMembers(MaxResponseHeadersLengthPropertyName)
52+
.FirstOrDefault();
53+
54+
if (httpClientHandlerPropSymbol is null || socketClientHandlerPropSymbol is null)
5055
{
5156
return;
5257
}
5358

54-
context.RegisterOperationAction(context => AnalyzeSimpleAssignmentOperationAndCreateDiagnostic(context, propertySymbol), OperationKind.SimpleAssignment);
59+
ImmutableArray<ISymbol> symbols = ImmutableArray.Create(httpClientHandlerPropSymbol, socketClientHandlerPropSymbol);
60+
context.RegisterOperationAction(context => AnalyzeSimpleAssignmentOperationAndCreateDiagnostic(context, symbols), OperationKind.SimpleAssignment);
5561
});
5662
}
5763

58-
private static void AnalyzeSimpleAssignmentOperationAndCreateDiagnostic(OperationAnalysisContext context, ISymbol propertySymbol)
64+
private static void AnalyzeSimpleAssignmentOperationAndCreateDiagnostic(OperationAnalysisContext context, ImmutableArray<ISymbol> propSymbols)
5965
{
6066
var assignmentOperation = (ISimpleAssignmentOperation)context.Operation;
6167

62-
if (!IsValidPropertyAssignmentOperation(assignmentOperation, propertySymbol))
68+
if (!IsValidPropertyAssignmentOperation(assignmentOperation, propSymbols))
6369
{
6470
return;
6571
}
@@ -70,20 +76,20 @@ private static void AnalyzeSimpleAssignmentOperationAndCreateDiagnostic(Operatio
7076
}
7177

7278
// If the user set the value to int.MaxValue, their intention is to disable the limit, and we shouldn't emit a warning.
73-
if (propertyValue > MaxLimitToReport && propertyValue != int.MaxValue)
79+
if (propertyValue is > MaxLimitToReport and not int.MaxValue)
7480
{
7581
context.ReportDiagnostic(context.Operation.CreateDiagnostic(EnsureMaxResponseHeaderLengthRule, propertyValue));
7682
}
7783
}
7884

79-
private static bool IsValidPropertyAssignmentOperation(ISimpleAssignmentOperation operation, ISymbol propertySymbol)
85+
private static bool IsValidPropertyAssignmentOperation(ISimpleAssignmentOperation operation, ImmutableArray<ISymbol> propSymbols)
8086
{
8187
if (operation.Target is not IPropertyReferenceOperation propertyReferenceOperation)
8288
{
8389
return false;
8490
}
8591

86-
if (!propertyReferenceOperation.Member.Equals(propertySymbol))
92+
if (!propSymbols.Contains(propertyReferenceOperation.Member))
8793
{
8894
return false;
8995
}

src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Usage/ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectlyTests.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,42 @@ public class TestClass {
2727
2828
public void TestMethod() {
2929
30-
HttpClientHandler handler3 = new HttpClientHandler()
30+
HttpClientHandler handler = new HttpClientHandler()
3131
{
3232
MaxResponseHeadersLength = GetValue()
3333
};
3434
35-
HttpClientHandler handler = new HttpClientHandler()
35+
HttpClientHandler handler2 = new HttpClientHandler()
3636
{
3737
{|#0:MaxResponseHeadersLength = val|}
3838
};
3939
40-
HttpClientHandler handler2 = new HttpClientHandler()
40+
HttpClientHandler handler3 = new HttpClientHandler()
4141
{
4242
{|#1:MaxResponseHeadersLength = 1414|}
4343
};
44+
45+
HttpClientHandler handler4 = new HttpClientHandler()
46+
{
47+
MaxResponseHeadersLength = int.MaxValue
48+
};
49+
50+
SocketsHttpHandler handler5 = new SocketsHttpHandler()
51+
{
52+
MaxResponseHeadersLength = int.MaxValue
53+
};
54+
55+
SocketsHttpHandler handler6 = new SocketsHttpHandler()
56+
{
57+
{|#2:MaxResponseHeadersLength = 1000|}
58+
};
4459
}
4560
}
4661
4762
",
4863
VerifyCS.Diagnostic(ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectly.RuleId).WithLocation(0).WithArguments(242424),
49-
VerifyCS.Diagnostic(ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectly.RuleId).WithLocation(1).WithArguments(1414)
64+
VerifyCS.Diagnostic(ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectly.RuleId).WithLocation(1).WithArguments(1414),
65+
VerifyCS.Diagnostic(ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectly.RuleId).WithLocation(2).WithArguments(1000)
5066
);
5167

5268
await VerifyVB.VerifyAnalyzerAsync(@"

src/Utilities/Compiler/WellKnownTypeNames.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ internal static class WellKnownTypeNames
266266
public const string SystemMemoryExtensions = "System.MemoryExtensions";
267267
public const string SystemNetHttpHttpClient = "System.Net.Http.HttpClient";
268268
public const string SystemNetHttpHttpClientHandler = "System.Net.Http.HttpClientHandler";
269+
public const string SystemNetHttpSocketsHttpHandler = "System.Net.Http.SocketsHttpHandler";
269270
public const string SystemNetHttpWinHttpHandler = "System.Net.Http.WinHttpHandler";
270271
public const string SystemNetSecurityProtocolType = "System.Net.SecurityProtocolType";
271272
public const string SystemNetSecurityRemoteCertificateValidationCallback = "System.Net.Security.RemoteCertificateValidationCallback";

0 commit comments

Comments
 (0)