Skip to content
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

Use Content-Disposition for output file name in Webcmdlets #19385

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
abd6e9f
content-disposition
CarloToso Mar 21, 2023
612863d
add tests
CarloToso Mar 22, 2023
85933ae
remove else
CarloToso Mar 22, 2023
60f838b
update tests
CarloToso Mar 22, 2023
e27cd5f
fix writeverbose filename
CarloToso Mar 22, 2023
9d3de54
reduce nesting
CarloToso Mar 22, 2023
289b7f3
try fix tests
CarloToso Mar 22, 2023
e1e6951
try fix tests
CarloToso Mar 22, 2023
76f5590
fix tests
CarloToso Mar 22, 2023
7cf3850
fix bug @ImportTaste
CarloToso Mar 22, 2023
b278239
Update test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlet…
CarloToso Mar 23, 2023
7e65367
Update test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlet…
CarloToso Mar 23, 2023
bcdae81
PathAndQuery
CarloToso Mar 23, 2023
f574f4c
qualifiedOutFile
CarloToso Mar 23, 2023
13563b3
remove path and strip content-disposition
CarloToso Apr 3, 2023
41ec280
add tests
CarloToso Apr 4, 2023
90cb0ea
remove windows paths
CarloToso Apr 4, 2023
7c4511f
Merge branch 'master' into content-disposition
CarloToso May 3, 2023
dae2b73
fix nullable
CarloToso May 3, 2023
af603b3
address wg-powershell-cmdlets review
CarloToso Oct 3, 2023
6862b81
Merge branch 'master' into content-disposition
CarloToso Oct 3, 2023
54e54c2
_qualifiedOutFilePath -> _qualifiedOutFile
CarloToso Oct 3, 2023
700c1e4
correct test errors
CarloToso Oct 3, 2023
ddb3769
fix 2 tests
CarloToso Oct 3, 2023
6b83431
fix typo in tests
CarloToso Oct 4, 2023
c4c7830
fix typo; fix QualifiedOutFile
CarloToso Oct 4, 2023
6d12938
fix TabCompletion tests
CarloToso Oct 4, 2023
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
Prev Previous commit
Next Next commit
PathAndQuery
  • Loading branch information
CarloToso committed Mar 23, 2023
commit bcdae81ab94f91e4f1c6a872c3ed5bd3c84ab591
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ internal static string GetOutFilePath(HttpResponseMessage response, string _qual
}

string contentDisposition = response.Content.Headers.ContentDisposition?.FileNameStar ?? response.Content.Headers.ContentDisposition?.FileName;
Copy link
Member

Choose a reason for hiding this comment

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

Per https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#directives the value may have path information and should be stripped. It may or may not be enclosed in quotes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The guideline says that "filename" is usually default name for "Save As". This force we think that we go in wrong direction trying to make the cmdlet too smart. We don't really know what the user wants in a particular scenario. Therefore, I propose follow @mklement0 's proposal and introduce a new parameter AutoFileName as enum:

  • Default. OutFile works as usually and requires exact path and file name.
  • URL. OutFile is a directory and file name is generated based on URL. Throw like curl if no last segment.
  • Form. OutFile is a directory and file name is generated based on Content-Disposition. Throw if no Content-Disposition or filename in Content-Disposition.
    This way we allow the user to do exactly what he needs to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd prefer if we left the decision to the cmdlet, if the user wants to choose a name for the file he just has to write it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I hope AutoFileName proposal will be discussed by WG.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SteveL-MSFT what should be the best course of action?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SteveL-MSFT any news on AutoFileName?

Copy link
Member

Choose a reason for hiding this comment

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

@CarloToso sorry for the delay, I'll queue this up for discussion in Cmdlets WG

string pathAndQuery = response.RequestMessage.RequestUri.PathAndQuery;

if (!string.IsNullOrEmpty(contentDisposition))
{
// Get file name from Content-Disposition header if present
return Path.Join(_qualifiedOutFile, contentDisposition);
}

if (pathAndQuery != "/")
if (response.RequestMessage.RequestUri.PathAndQuery != "/")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please clarify why we need PathAndQuery. Maybe add a comment in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need it to handle this case:

Invoke-WebRequest https://www.google.com/ -OutFile .\  #PathAndQuery == "/"
#--> OutFile: www_google_com

{
string lastUriSegment = System.Net.WebUtility.UrlDecode(response.RequestMessage.RequestUri.Segments[^1]);

Expand Down