From e7c8b60d757933f1d935b6485e54e58276901adb Mon Sep 17 00:00:00 2001 From: Johann Wimmer Date: Mon, 31 May 2021 17:56:24 +0200 Subject: [PATCH 1/2] Fixes 33115 - Addition to #33135 - the ACCEPT Header was previously only set during the `StartInBackground` method, but was missing from the `IsSpaProxyRunning` method. - Extract HttpClient creation into method to ensure that the HttpClient used for probing the frontend development server is instantiated with consistent settings. --- .../Spa/SpaProxy/src/SpaProxyLaunchManager.cs | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs b/src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs index 325b133acbd3..542144606fcc 100644 --- a/src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs +++ b/src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs @@ -33,14 +33,6 @@ public SpaProxyLaunchManager( public void StartInBackground(CancellationToken cancellationToken) { - var httpClient = new HttpClient(new HttpClientHandler() - { - // It's ok for us to do this here since this service is only plugged in during development. - ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator - }); - // Certain frontend build tools rely on the the ACCEPT Header being set, otherwise these tools - // might be unable to perform their client-side fallback logic. - httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); _logger.LogInformation($"No SPA development server running at {_options.ServerUrl} found."); // We are not waiting for the SPA proxy to launch, instead we are going to rely on a piece of @@ -53,7 +45,7 @@ public void StartInBackground(CancellationToken cancellationToken) { if (_launchTask == null) { - _launchTask = UpdateStatus(StartSpaProcessAndProbeForLiveness(httpClient, cancellationToken)); + _launchTask = UpdateStatus(StartSpaProcessAndProbeForLiveness(cancellationToken)); } } @@ -79,11 +71,7 @@ async Task UpdateStatus(Task launchTask) public async Task IsSpaProxyRunning(CancellationToken cancellationToken) { - var httpClient = new HttpClient(new HttpClientHandler() - { - // It's ok for us to do this here since this service is only plugged in during development. - ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator - }); + var httpClient = CreateHttpClient(); using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, cancellationToken); @@ -102,6 +90,19 @@ exception is TaskCanceledException || } } + private static HttpClient CreateHttpClient() + { + var httpClient = new HttpClient(new HttpClientHandler() + { + // It's ok for us to do this here since this service is only plugged in during development. + ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator + }); + // Certain frontend build tools rely on the the ACCEPT Header being set, otherwise these tools + // might be unable to perform their client-side fallback logic. + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); + return httpClient; + } + private async Task ProbeSpaDevelopmentServerUrl(HttpClient httpClient, CancellationToken cancellationToken) { using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); @@ -121,12 +122,13 @@ exception is TaskCanceledException || } } - private async Task StartSpaProcessAndProbeForLiveness(HttpClient httpClient, CancellationToken cancellationToken) + private async Task StartSpaProcessAndProbeForLiveness(CancellationToken cancellationToken) { LaunchDevelopmentProxy(); var sw = Stopwatch.StartNew(); var livenessProbeSucceeded = false; var maxTimeoutReached = false; + var httpClient = CreateHttpClient(); while (_spaProcess != null && !_spaProcess.HasExited && !maxTimeoutReached) { livenessProbeSucceeded = await ProbeSpaDevelopmentServerUrl(httpClient, cancellationToken); From f46f16a9bf45fc60c8e49675504ad02175694ad2 Mon Sep 17 00:00:00 2001 From: Johann Wimmer Date: Tue, 1 Jun 2021 21:04:52 +0200 Subject: [PATCH 2/2] - Clarify comment about regarding accept header value - Add low quality factor to 'Any MIME type' accept header --- src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs b/src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs index 542144606fcc..f7a7847caa59 100644 --- a/src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs +++ b/src/Middleware/Spa/SpaProxy/src/SpaProxyLaunchManager.cs @@ -97,9 +97,8 @@ private static HttpClient CreateHttpClient() // It's ok for us to do this here since this service is only plugged in during development. ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator }); - // Certain frontend build tools rely on the the ACCEPT Header being set, otherwise these tools - // might be unable to perform their client-side fallback logic. - httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); + // We don't care about the returned content type as long as the server is able to answer with 2XX + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*", 0.1)); return httpClient; }