feat: use ContentType to control HTML extension
This commit is contained in:
+19
-2
@@ -130,9 +130,12 @@ func runPostDownload(
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to close writer: %v\n", cerr)
|
||||
}
|
||||
|
||||
// Adjust extension: append .html to HTML files without extension
|
||||
// Adjust extension: append .html to HTML files without extension.
|
||||
// When ContentType is available (WebDAV, Gopher), only apply to
|
||||
// text/html responses. When ContentType is empty (HTTP legacy
|
||||
// path), preserve the existing wget-compatible behaviour.
|
||||
if *f.AdjustExt && *f.Output != "" {
|
||||
if filepath.Ext(*f.Output) == "" {
|
||||
if filepath.Ext(*f.Output) == "" && isHTMLContentType(result.ContentType) {
|
||||
newPath := *f.Output + ".html"
|
||||
if err := os.Rename(*f.Output, newPath); err == nil {
|
||||
*f.Output = newPath
|
||||
@@ -289,6 +292,20 @@ func runPostDownload(
|
||||
return 0
|
||||
}
|
||||
|
||||
// isHTMLContentType returns true when ct indicates HTML content.
|
||||
// An empty string is treated as HTML for backward compatibility with
|
||||
// protocol handlers that do not populate ContentType (e.g. HTTP).
|
||||
func isHTMLContentType(ct string) bool {
|
||||
if ct == "" {
|
||||
return true
|
||||
}
|
||||
// Strip parameters (charset, boundary, …).
|
||||
if i := strings.IndexByte(ct, ';'); i >= 0 {
|
||||
ct = ct[:i]
|
||||
}
|
||||
return strings.TrimSpace(ct) == "text/html"
|
||||
}
|
||||
|
||||
// splitCommand splits a shell-like command string into tokens respecting double quotes.
|
||||
// Used to parse --on-complete and --on-complete-url without invoking a shell.
|
||||
func splitCommand(raw string) []string {
|
||||
|
||||
Reference in New Issue
Block a user