Skip to content

Commit c978d46

Browse files
authored
Complete the progress bar rendering in Invoke-WebRequest when downloading is complete or cancelled (PowerShell#18130)
1 parent fc695d1 commit c978d46

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ internal static void WriteToStream(Stream input, Stream output, PSCmdlet cmdlet,
298298

299299
Task copyTask = input.CopyToAsync(output, cancellationToken);
300300

301+
bool wroteProgress = false;
301302
ProgressRecord record = new(
302303
ActivityId,
303304
WebCmdletStrings.WriteRequestProgressActivity,
@@ -313,23 +314,32 @@ internal static void WriteToStream(Stream input, Stream output, PSCmdlet cmdlet,
313314
Utils.DisplayHumanReadableFileSize(output.Position),
314315
totalDownloadSize);
315316

316-
if (contentLength != null && contentLength > 0)
317+
if (contentLength > 0)
317318
{
318319
record.PercentComplete = Math.Min((int)(output.Position * 100 / (long)contentLength), 100);
319320
}
320321

321322
cmdlet.WriteProgress(record);
322-
}
323-
324-
if (copyTask.IsCompleted)
325-
{
326-
record.StatusDescription = StringUtil.Format(WebCmdletStrings.WriteRequestComplete, output.Position);
327-
cmdlet.WriteProgress(record);
323+
wroteProgress = true;
328324
}
329325
}
330326
catch (OperationCanceledException)
331327
{
332328
}
329+
finally
330+
{
331+
if (wroteProgress)
332+
{
333+
// Write out the completion progress record only if we did render the progress.
334+
record.StatusDescription = StringUtil.Format(
335+
copyTask.IsCompleted
336+
? WebCmdletStrings.WriteRequestComplete
337+
: WebCmdletStrings.WriteRequestCancelled,
338+
output.Position);
339+
record.RecordType = ProgressRecordType.Completed;
340+
cmdlet.WriteProgress(record);
341+
}
342+
}
333343
}
334344

335345
/// <summary>

src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
<value>The cmdlet cannot run because the following parameter is missing: Proxy. Provide a valid proxy URI for the Proxy parameter when using the ProxyCredential or ProxyUseDefaultCredentials parameters, then retry.</value>
200200
</data>
201201
<data name="ReadResponseComplete" xml:space="preserve">
202-
<value>Reading web response stream completed. Downloaded: {0}</value>
202+
<value>Reading web response stream completed. Bytes downloaded: {0}</value>
203203
</data>
204204
<data name="ReadResponseProgressActivity" xml:space="preserve">
205205
<value>Reading web response stream</value>
@@ -219,6 +219,9 @@
219219
<data name="WriteRequestComplete" xml:space="preserve">
220220
<value>Web request completed. (Number of bytes processed: {0})</value>
221221
</data>
222+
<data name="WriteRequestCancelled" xml:space="preserve">
223+
<value>Web request cancelled. (Number of bytes processed: {0})</value>
224+
</data>
222225
<data name="WriteRequestProgressActivity" xml:space="preserve">
223226
<value>Web request status</value>
224227
</data>

0 commit comments

Comments
 (0)