Skip to content

Commit 6427d9c

Browse files
authored
Use default interfaces for getting and setting headers (#31519)
1 parent 78d0241 commit 6427d9c

File tree

138 files changed

+12680
-7563
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+12680
-7563
lines changed

src/Antiforgery/src/Internal/DefaultAntiforgery.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private void SaveCookieTokenAndHeader(HttpContext httpContext, string cookieToke
266266
// Adding X-Frame-Options header to prevent ClickJacking. See
267267
// http://tools.ietf.org/html/draft-ietf-websec-x-frame-options-10
268268
// for more information.
269-
httpContext.Response.Headers[HeaderNames.XFrameOptions] = "SAMEORIGIN";
269+
httpContext.Response.Headers.XFrameOptions = "SAMEORIGIN";
270270
}
271271
}
272272

@@ -387,12 +387,12 @@ protected virtual void SetDoNotCacheHeaders(HttpContext httpContext)
387387
if (!cacheControlHeaderValue.NoCache || !cacheControlHeaderValue.NoStore)
388388
{
389389
logWarning = true;
390-
responseHeaders[HeaderNames.CacheControl] = "no-cache, no-store";
390+
responseHeaders.CacheControl = "no-cache, no-store";
391391
}
392392
}
393393
else
394394
{
395-
responseHeaders[HeaderNames.CacheControl] = "no-cache, no-store";
395+
responseHeaders.CacheControl = "no-cache, no-store";
396396
}
397397

398398
if (responseHeaders.TryGetValue(HeaderNames.Pragma, out var pragmaHeader) && pragmaHeader.Count > 0)
@@ -401,12 +401,12 @@ protected virtual void SetDoNotCacheHeaders(HttpContext httpContext)
401401
if (!string.Equals(pragmaHeader[0], "no-cache", StringComparison.OrdinalIgnoreCase))
402402
{
403403
logWarning = true;
404-
httpContext.Response.Headers[HeaderNames.Pragma] = "no-cache";
404+
httpContext.Response.Headers.Pragma = "no-cache";
405405
}
406406
}
407407
else
408408
{
409-
httpContext.Response.Headers[HeaderNames.Pragma] = "no-cache";
409+
httpContext.Response.Headers.Pragma = "no-cache";
410410
}
411411

412412
// Since antiforgery token generation is not very obvious to the end users (ex: MVC's form tag generates them

src/Antiforgery/test/DefaultAntiforgeryTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ public void GetAndStoreTokens_ExistingValidCookieToken_NotOverriden_AndSetsDoNot
313313

314314
Assert.NotNull(antiforgeryFeature);
315315
Assert.Equal(context.TestTokenSet.OldCookieToken, antiforgeryFeature.CookieToken);
316-
Assert.Equal("no-cache, no-store", context.HttpContext.Response.Headers[HeaderNames.CacheControl]);
317-
Assert.Equal("no-cache", context.HttpContext.Response.Headers[HeaderNames.Pragma]);
316+
Assert.Equal("no-cache, no-store", context.HttpContext.Response.Headers.CacheControl);
317+
Assert.Equal("no-cache", context.HttpContext.Response.Headers.Pragma);
318318
}
319319

320320
[Fact]
@@ -328,7 +328,7 @@ public void GetAndStoreTokens_ExistingCachingHeaders_Overriden()
328328
isOldCookieValid: true,
329329
antiforgeryFeature: antiforgeryFeature);
330330
var antiforgery = GetAntiforgery(context);
331-
context.HttpContext.Response.Headers["Cache-Control"] = "public";
331+
context.HttpContext.Response.Headers.CacheControl = "public";
332332

333333
// Act
334334
var tokenSet = antiforgery.GetAndStoreTokens(context.HttpContext);
@@ -344,8 +344,8 @@ public void GetAndStoreTokens_ExistingCachingHeaders_Overriden()
344344

345345
Assert.NotNull(antiforgeryFeature);
346346
Assert.Equal(context.TestTokenSet.OldCookieToken, antiforgeryFeature.CookieToken);
347-
Assert.Equal("no-cache, no-store", context.HttpContext.Response.Headers[HeaderNames.CacheControl]);
348-
Assert.Equal("no-cache", context.HttpContext.Response.Headers[HeaderNames.Pragma]);
347+
Assert.Equal("no-cache, no-store", context.HttpContext.Response.Headers.CacheControl);
348+
Assert.Equal("no-cache", context.HttpContext.Response.Headers.Pragma);
349349
}
350350

351351
private string GetAndStoreTokens_CacheHeadersArrangeAct(TestSink testSink, string headerName, string headerValue)
@@ -506,8 +506,8 @@ public void GetAndStoreTokens_NoExistingCookieToken_Saved_AndSetsDoNotCacheHeade
506506
Assert.NotNull(antiforgeryFeature);
507507
Assert.True(antiforgeryFeature.HaveDeserializedCookieToken);
508508
Assert.Equal(context.TestTokenSet.OldCookieToken, antiforgeryFeature.CookieToken);
509-
Assert.Equal("no-cache, no-store", context.HttpContext.Response.Headers[HeaderNames.CacheControl]);
510-
Assert.Equal("no-cache", context.HttpContext.Response.Headers[HeaderNames.Pragma]);
509+
Assert.Equal("no-cache, no-store", context.HttpContext.Response.Headers.CacheControl);
510+
Assert.Equal("no-cache", context.HttpContext.Response.Headers.Pragma);
511511
}
512512

513513
[Fact]

src/Antiforgery/test/DefaultAntiforgeryTokenStoreTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public void SaveCookieToken_NonNullAntiforgeryOptionsConfigureCookieOptionsDomai
460460
private HttpContext GetHttpContext(string cookieName, string cookieValue)
461461
{
462462
var context = GetHttpContext();
463-
context.Request.Headers[HeaderNames.Cookie] = $"{cookieName}={cookieValue}";
463+
context.Request.Headers.Cookie = $"{cookieName}={cookieValue}";
464464
return context;
465465
}
466466

src/Azure/AzureAD/Authentication.AzureADB2C.UI/test/AzureAdB2COpenIDConnectEventHandlersTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public async Task OnRemoteError_HandlesResponseWhenTryingToResetPasswordFromTheL
114114

115115
// Assert
116116
Assert.Equal(StatusCodes.Status302Found, remoteFailureContext.Response.StatusCode);
117-
Assert.Equal("/AzureADB2C/Account/ResetPassword/AzureADB2C", remoteFailureContext.Response.Headers[HeaderNames.Location]);
117+
Assert.Equal("/AzureADB2C/Account/ResetPassword/AzureADB2C", remoteFailureContext.Response.Headers.Location);
118118
Assert.True(remoteFailureContext.Result.Handled);
119119
}
120120

@@ -141,7 +141,7 @@ public async Task OnRemoteError_HandlesResponseWhenUserCancelsFlowFromTheAzureAD
141141

142142
// Assert
143143
Assert.Equal(StatusCodes.Status302Found, remoteFailureContext.Response.StatusCode);
144-
Assert.Equal("/", remoteFailureContext.Response.Headers[HeaderNames.Location]);
144+
Assert.Equal("/", remoteFailureContext.Response.Headers.Location);
145145
Assert.True(remoteFailureContext.Result.Handled);
146146
}
147147

@@ -168,7 +168,7 @@ public async Task OnRemoteError_HandlesResponseWhenErrorIsUnknown()
168168

169169
// Assert
170170
Assert.Equal(StatusCodes.Status302Found, remoteFailureContext.Response.StatusCode);
171-
Assert.Equal("/AzureADB2C/Account/Error", remoteFailureContext.Response.Headers[HeaderNames.Location]);
171+
Assert.Equal("/AzureADB2C/Account/Error", remoteFailureContext.Response.Headers.Location);
172172
Assert.True(remoteFailureContext.Result.Handled);
173173
}
174174
}

src/Azure/samples/AzureAppServicesHostingStartupSample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
2525

2626
await context.Response.WriteAsync("Address:" + Environment.NewLine);
2727
await context.Response.WriteAsync("Scheme: " + context.Request.Scheme + Environment.NewLine);
28-
await context.Response.WriteAsync("Host: " + context.Request.Headers["Host"] + Environment.NewLine);
28+
await context.Response.WriteAsync("Host: " + context.Request.Headers.Host + Environment.NewLine);
2929
await context.Response.WriteAsync("PathBase: " + context.Request.PathBase.Value + Environment.NewLine);
3030
await context.Response.WriteAsync("Path: " + context.Request.Path.Value + Environment.NewLine);
3131
await context.Response.WriteAsync("Query: " + context.Request.QueryString.Value + Environment.NewLine);

src/Azure/samples/AzureAppServicesSample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
3737

3838
await context.Response.WriteAsync("Address:" + Environment.NewLine);
3939
await context.Response.WriteAsync("Scheme: " + context.Request.Scheme + Environment.NewLine);
40-
await context.Response.WriteAsync("Host: " + context.Request.Headers["Host"] + Environment.NewLine);
40+
await context.Response.WriteAsync("Host: " + context.Request.Headers.Host + Environment.NewLine);
4141
await context.Response.WriteAsync("PathBase: " + context.Request.PathBase.Value + Environment.NewLine);
4242
await context.Response.WriteAsync("Path: " + context.Request.Path.Value + Environment.NewLine);
4343
await context.Response.WriteAsync("Query: " + context.Request.QueryString.Value + Environment.NewLine);

src/Components/WebAssembly/Server/src/ContentEncodingNegotiator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Task InvokeAsync(HttpContext context)
4141

4242
private void NegotiateEncoding(HttpContext context)
4343
{
44-
var accept = context.Request.Headers[HeaderNames.AcceptEncoding];
44+
var accept = context.Request.Headers.AcceptEncoding;
4545

4646
if (StringValues.IsNullOrEmpty(accept))
4747
{
@@ -91,7 +91,7 @@ private void NegotiateEncoding(HttpContext context)
9191
if (_encodingExtensionMap.TryGetValue(selectedEncoding, out var extension))
9292
{
9393
context.Request.Path = context.Request.Path + extension;
94-
context.Response.Headers[HeaderNames.ContentEncoding] = selectedEncoding.Value;
94+
context.Response.Headers.ContentEncoding = selectedEncoding.Value;
9595
context.Response.Headers.Append(HeaderNames.Vary, HeaderNames.ContentEncoding);
9696
}
9797

src/Components/test/testassets/TestServer/Controllers/PersonController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task<string> Post()
3434
[HttpGet("referrer")]
3535
public string GetReferer()
3636
{
37-
return $"The referrer is: {Request.Headers["Referer"].ToString()}";
37+
return $"The referrer is: {Request.Headers.Referer.ToString()}";
3838
}
3939

4040
// PUT api/person

src/Hosting/Hosting/src/Internal/ErrorPageBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static RequestDelegate BuildErrorPageApplication(
3333
return context =>
3434
{
3535
context.Response.StatusCode = 500;
36-
context.Response.Headers[HeaderNames.CacheControl] = "no-cache,no-store";
37-
context.Response.Headers[HeaderNames.Pragma] = "no-cache";
36+
context.Response.Headers.CacheControl = "no-cache,no-store";
37+
context.Response.Headers.Pragma = "no-cache";
3838
context.Response.ContentType = "text/html; charset=utf-8";
3939
return errorPage.ExecuteAsync(context);
4040
};

src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,17 @@ private static void RecordRequestStartEventLog(HttpContext httpContext)
264264
}
265265

266266
var headers = httpContext.Request.Headers;
267-
if (!headers.TryGetValue(HeaderNames.TraceParent, out var requestId))
267+
var requestId = headers.TraceParent;
268+
if (requestId.Count == 0)
268269
{
269-
headers.TryGetValue(HeaderNames.RequestId, out requestId);
270+
requestId = headers.RequestId;
270271
}
271272

272273
if (!StringValues.IsNullOrEmpty(requestId))
273274
{
274275
activity.SetParentId(requestId);
275-
if (headers.TryGetValue(HeaderNames.TraceState, out var traceState))
276+
var traceState = headers.TraceState;
277+
if (traceState.Count > 0)
276278
{
277279
activity.TraceStateString = traceState;
278280
}

0 commit comments

Comments
 (0)