From fdd1bb72524a737dfe9f477ebcfe3c9a69b76d78 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Tue, 29 Apr 2025 10:07:45 -0700 Subject: [PATCH] WebCmdlet: Add Request and Response Debug Messaging --- .../Common/WebRequestPSCmdlet.Common.cs | 66 ++++++++++--------- .../resources/WebCmdletStrings.resx | 8 ++- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 810b54a8391..47895a88c09 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -567,29 +567,10 @@ protected override void ProcessRecord() FillRequestStream(request); try { - long requestContentLength = request.Content is null ? 0 : request.Content.Headers.ContentLength.Value; - - string reqVerboseMsg = string.Format( - CultureInfo.CurrentCulture, - WebCmdletStrings.WebMethodInvocationVerboseMsg, - request.Version, - request.Method, - requestContentLength); - - WriteVerbose(reqVerboseMsg); - _maximumRedirection = WebSession.MaximumRedirection; using HttpResponseMessage response = GetResponse(client, request, handleRedirect); - string contentType = ContentHelper.GetContentType(response); - long? contentLength = response.Content.Headers.ContentLength; - string respVerboseMsg = contentLength is null - ? string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebResponseNoSizeVerboseMsg, response.Version, contentType) - : string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebResponseVerboseMsg, response.Version, contentLength, contentType); - - WriteVerbose(respVerboseMsg); - bool _isSuccess = response.IsSuccessStatusCode; // Check if the Resume range was not satisfiable because the file already completed downloading. @@ -638,6 +619,9 @@ protected override void ProcessRecord() string detailMsg = string.Empty; try { + string contentType = ContentHelper.GetContentType(response); + long? contentLength = response.Content.Headers.ContentLength; + // We can't use ReadAsStringAsync because it doesn't have per read timeouts TimeSpan perReadTimeout = ConvertTimeoutSecondsToTimeSpan(OperationTimeoutSeconds); string characterSet = WebResponseHelper.GetCharacterSet(response); @@ -1296,7 +1280,40 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM _cancelToken = new CancellationTokenSource(); try { + long requestContentLength = request.Content is null ? 0 : request.Content.Headers.ContentLength.Value; + + string reqVerboseMsg = string.Format( + CultureInfo.CurrentCulture, + WebCmdletStrings.WebMethodInvocationVerboseMsg, + request.Version, + request.Method, + requestContentLength); + + WriteVerbose(reqVerboseMsg); + + string reqDebugMsg = string.Format( + CultureInfo.CurrentCulture, + WebCmdletStrings.WebRequestDebugMsg, + request.ToString()); + + WriteDebug(reqDebugMsg); + response = client.SendAsync(currentRequest, HttpCompletionOption.ResponseHeadersRead, _cancelToken.Token).GetAwaiter().GetResult(); + + string contentType = ContentHelper.GetContentType(response); + long? contentLength = response.Content.Headers.ContentLength; + string respVerboseMsg = contentLength is null + ? string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebResponseNoSizeVerboseMsg, response.Version, contentType) + : string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.WebResponseVerboseMsg, response.Version, contentLength, contentType); + + WriteVerbose(respVerboseMsg); + + string resDebugMsg = string.Format( + CultureInfo.CurrentCulture, + WebCmdletStrings.WebResponseDebugMsg, + response.ToString()); + + WriteDebug(resDebugMsg); } catch (TaskCanceledException ex) { @@ -1361,17 +1378,6 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM { FillRequestStream(requestWithoutRange); - long requestContentLength = requestWithoutRange.Content is null ? 0 : requestWithoutRange.Content.Headers.ContentLength.Value; - - string reqVerboseMsg = string.Format( - CultureInfo.CurrentCulture, - WebCmdletStrings.WebMethodInvocationVerboseMsg, - requestWithoutRange.Version, - requestWithoutRange.Method, - requestContentLength); - - WriteVerbose(reqVerboseMsg); - response.Dispose(); response = GetResponse(client, requestWithoutRange, handleRedirect); } diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx index 6e73043747e..fb704891b89 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx @@ -164,7 +164,7 @@ Cannot follow an insecure redirection by default. Reissue the command specifying the -AllowInsecureRedirect switch. - + Cannot convert the JSON string because it contains keys with different casing. Please use the -AsHashTable switch instead. The key that was attempted to be added to the existing key '{0}' was '{1}'. @@ -243,6 +243,12 @@ Received HTTP/{0} {1}-byte response of content type {2} + + Request {0} + + + Response {0} + Received HTTP/{0} response of content type {1} of unknown size