Skip to content

Add FileNameStar to MultipartFileContent in WebCmdlets #19467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1707,9 +1707,7 @@ private void AddMultipartContent(object fieldName, object fieldValue, MultipartF
private static StringContent GetMultipartStringContent(object fieldName, object fieldValue)
{
ContentDispositionHeaderValue contentDisposition = new("form-data");

// .NET does not enclose field names in quotes, however, modern browsers and curl do.
contentDisposition.Name = "\"" + LanguagePrimitives.ConvertTo<string>(fieldName) + "\"";
contentDisposition.Name = LanguagePrimitives.ConvertTo<string>(fieldName);
Copy link

@rkeithhill-keysight rkeithhill-keysight Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke IWR for us when we upgraded to 7.4. We use IWR in a publish script to upload a zip to an internal PERL / CGI-based website. AFAICT the PERL CGI module requires field name values to be quoted. See #23843


StringContent result = new(LanguagePrimitives.ConvertTo<string>(fieldValue));
result.Headers.ContentDisposition = contentDisposition;
Expand All @@ -1725,9 +1723,7 @@ private static StringContent GetMultipartStringContent(object fieldName, object
private static StreamContent GetMultipartStreamContent(object fieldName, Stream stream)
{
ContentDispositionHeaderValue contentDisposition = new("form-data");

// .NET does not enclose field names in quotes, however, modern browsers and curl do.
contentDisposition.Name = "\"" + LanguagePrimitives.ConvertTo<string>(fieldName) + "\"";
contentDisposition.Name = LanguagePrimitives.ConvertTo<string>(fieldName);

StreamContent result = new(stream);
result.Headers.ContentDisposition = contentDisposition;
Expand All @@ -1745,8 +1741,8 @@ private static StreamContent GetMultipartFileContent(object fieldName, FileInfo
{
StreamContent result = GetMultipartStreamContent(fieldName: fieldName, stream: new FileStream(file.FullName, FileMode.Open));

// .NET does not enclose field names in quotes, however, modern browsers and curl do.
result.Headers.ContentDisposition.FileName = "\"" + file.Name + "\"";
result.Headers.ContentDisposition.FileName = file.Name;
result.Headers.ContentDisposition.FileNameStar = file.Name;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have tests for this new behavior.

Copy link
Contributor Author

@CarloToso CarloToso May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done
I'll fix it tomorrow
Done


return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,11 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$file2Path = Join-Path $testdrive $file2Name
$file2Contents = "Test456"
$file2Contents | Set-Content $file2Path -Force

$file3Name = "Kündigung_Mustermann_Max.TTA_2023_01_30.txt"
$file3Path = Join-Path $testdrive $file3Name
$file3Contents = "Test789"
$file3Contents | Set-Content $file3Path -Force
}

It "Verifies Invoke-WebRequest Supports Multipart String Values" {
Expand Down Expand Up @@ -1656,6 +1661,23 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Files[0].Content | Should -Match $file1Contents
}

It "Verifies Invoke-WebRequest -Form sets Content-Disposition FileName and FileNameStar." {
$ContentDisposition = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("attachment")
$ContentDisposition.FileName = $fileName
$ContentDisposition.FileNameStar = $fileName

$form = @{TestFile = [System.IO.FileInfo]$file3Path}
$uri = Get-WebListenerUrl -Test 'Multipart'
$response = Invoke-WebRequest -Uri $uri -Form $form -Method 'POST'
$result = $response.Content | ConvertFrom-Json

$result.Headers.'Content-Type' | Should -Match 'multipart/form-data'
$result.Files.Count | Should -Be 1

$result.Files[0].ContentDisposition.FileName | Should -Be $ContentDisposition.FileName
$result.Files[0].ContentDisposition.FileNameStar | Should -Be $ContentDisposition.FileNameStar
}

It "Verifies Invoke-WebRequest -Form supports a collection of file values" {
$form = @{TestFiles = [System.IO.FileInfo]$file1Path, [System.IO.FileInfo]$file2Path}
$uri = Get-WebListenerUrl -Test 'Multipart'
Expand Down Expand Up @@ -3312,6 +3334,11 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$file2Path = Join-Path $testdrive $file2Name
$file2Contents = "Test456"
$file2Contents | Set-Content $file2Path -Force

$file3Name = "Kündigung_Mustermann_Max.TTA_2023_01_30.txt"
$file3Path = Join-Path $testdrive $file3Name
$file3Contents = "Test789"
$file3Contents | Set-Content $file3Path -Force
}

It "Verifies Invoke-RestMethod Supports Multipart String Values" {
Expand Down Expand Up @@ -3381,6 +3408,22 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Files[0].Content | Should -Match $file1Contents
}

It "Verifies Invoke-RestMethod -Form sets Content-Disposition FileName and FileNameStar." {
$ContentDisposition = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("attachment")
$ContentDisposition.FileName = $fileName
$ContentDisposition.FileNameStar = $fileName

$form = @{TestFile = [System.IO.FileInfo]$file3Path}
$uri = Get-WebListenerUrl -Test 'Multipart'
$result = Invoke-RestMethod -Uri $uri -Form $form -Method 'POST'

$result.Headers.'Content-Type' | Should -Match 'multipart/form-data'
$result.Files.Count | Should -Be 1

$result.Files[0].ContentDisposition.FileName | Should -Be $ContentDisposition.FileName
$result.Files[0].ContentDisposition.FileNameStar | Should -Be $ContentDisposition.FileNameStar
}

It "Verifies Invoke-RestMethod -Form supports a collection of file values" {
$form = @{TestFiles = [System.IO.FileInfo]$file1Path, [System.IO.FileInfo]$file2Path}
$uri = Get-WebListenerUrl -Test 'Multipart'
Expand Down