cli icon indicating copy to clipboard operation
cli copied to clipboard

strings.TrimLeft is used instead of TrimPrefix

Open GrosQuildu opened this issue 2 years ago • 0 comments

Please fill out the issue checklist below and provide ALL the requested information.

  • [X] I reviewed open and closed github issues that may be related to my problem.
  • [X] I tried updating to the latest version of the CF CLI to see if it fixed my problem.
  • [ ] I attempted to run the command with CF_TRACE=1 to help debug the issue.
  • [X] I am reporting a bug that others will be able to reproduce.

Describe the bug and the command you saw an issue with

In the getFilenameFromHeader method the "filename=" string is a cutset (a set of characters) to be removed, and not a prefix. https://github.com/cloudfoundry/cli/blob/b5a352a685bdafa86f1552870ace148ee5dd7137/cf/util/downloader/file_download.go#L90-L105

Fixed function should be like this:

 func getFilenameFromHeader(h string) string { 
 	if h == "" { 
 		return "" 
 	} 
  
 	contents := strings.Split(h, ";") 
 	for _, content := range contents { 
                content = strings.TrimSpace(content) 
 		if strings.HasPrefix(content, "filename=") { 
 			name := strings.TrimPrefix(content, "filename=") 
 			return strings.Trim(name, `"`) 
 		} 
 	} 
  
 	return "" 
 } 

Exact Steps To Reproduce

Changing line below to "Content-Disposition": []string{"attachment; filename=ameno.zip"}, https://github.com/cloudfoundry/cli/blob/b5a352a685bdafa86f1552870ace148ee5dd7137/cf/util/downloader/file_download_test.go#L85

and line below to Expect(name).To(Equal("ameno.zip")) https://github.com/cloudfoundry/cli/blob/b5a352a685bdafa86f1552870ace148ee5dd7137/cf/util/downloader/file_download_test.go#L111

will make the test fail.

GrosQuildu avatar Apr 18 '23 15:04 GrosQuildu