Commit bd135e16 authored by cblu's avatar cblu

WebFoundation:

	Fix for checking and creating proper download filenames.

        Reviewed by mjs, kocienda, trey.

        * CacheLoader.subproj/WebResourceResponse.h:
        * CacheLoader.subproj/WebResourceResponse.m: removed the filename method. That is now a category in WebKit
        * CacheLoader.subproj/WebResourceResponsePrivate.h:
        * Misc.subproj/WebNSStringExtras.h:
        * Misc.subproj/WebNSStringExtras.m:
        (-[NSString _web_filenameByFixingIllegalCharacters]): new, turns "/" into "-", strips leading dots
        * ProtocolHandlers.subproj/WebHTTPProtocolHandler.m:
        (-[WebHTTPProtocolHandler setResponseMetadata:]): don't parse out the content disposition. This is done in WebKit.

WebKit:

	Fix for checking and creating proper download filenames.

        Reviewed by mjs, kocienda, trey.

        * WebKit.pbproj/project.pbxproj:
        * WebView.subproj/WebControllerPolicyDelegate.h: changes for renamed savePathForResponse:andRequest: method
        * WebView.subproj/WebDefaultPolicyDelegate.m: changes for renamed savePathForResponse:andRequest: method
        (-[WebDefaultPolicyDelegate savePathForResponse:andRequest:]): renamed
        * WebView.subproj/WebMainResourceClient.m:
        (-[WebMainResourceClient continueAfterContentPolicy:response:]): changes for renamed savePathForResponse:andRequest: method

WebBrowser:

	Fix for checking and creating proper download filenames.

        Reviewed by mjs, kocienda, trey.

        * BrowserDocument.m:
        (-[BrowserDocument pageName]): call _web_filenameByFixingIllegalCharacters on the web page title when saving the document
        * BrowserWebController.m:
        (-[BrowserWebController savePathForResponse:andRequest:]): renamed, removed temporary that checks for invalid filename. That work is done in WebKit.
        * HTMLSourceDocument.m:
        (-[HTMLSourceDocument displayName]): call suggestedFilenameForSaving


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@3316 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent b87b8515
2003-01-12 Chris Blumenberg <cblu@apple.com>
Fix for checking and creating proper download filenames.
Reviewed by mjs, kocienda, trey.
* WebKit.pbproj/project.pbxproj:
* WebView.subproj/WebControllerPolicyDelegate.h: changes for renamed savePathForResponse:andRequest: method
* WebView.subproj/WebDefaultPolicyDelegate.m: changes for renamed savePathForResponse:andRequest: method
(-[WebDefaultPolicyDelegate savePathForResponse:andRequest:]): renamed
* WebView.subproj/WebMainResourceClient.m:
(-[WebMainResourceClient continueAfterContentPolicy:response:]): changes for renamed savePathForResponse:andRequest: method
2003-01-12 Darin Adler <darin@apple.com>
Reviewed by John.
......
/*
WebResourceResponseExtras.h
Copyright 2003, Apple, Inc. All rights reserved.
Public header file.
*/
#import <WebFoundation/WebResourceResponse.h>
@interface WebResourceResponse (WebResourceResponseExtras)
/*!
@method suggestedFilenameForSaving
@abstract Returns a suggested filename if the resource were saved to disk.
@discussion The method first checks if the server has specified a filename using the
content disposition header. If no valid filename is specified using that mechanism,
this method checks the last path component of the URL. If no valid filename can be
obtained using the last path component, this method uses the URL's host as the filename.
If the URL's host can't be converted to a valid filename, the filename "unknown" is used.
In mose cases, this method appends the proper file extension based on the MIME type.
This method always returns a valid filename.
*/
- (NSString *)suggestedFilenameForSaving;
@end
//
// WebResourceResponseExtras.m
// WebKit
//
// Created by Chris Blumenberg on Thu Jan 09 2003.
// Copyright (c) 2003 Apple Inc. All rights reserved.
//
#import <WebKit/WebResourceResponseExtras.h>
#import <WebFoundation/WebAssertions.h>
#import <WebFoundation/WebFileTypeMappings.h>
#import <WebFoundation/WebHTTPResourceResponse.h>
#import <WebFoundation/WebLocalizableStrings.h>
#import <WebFoundation/WebNSStringExtras.h>
@interface NSURL (WebResourceResponseInternalURLExtras)
- (NSString *)_web_suggestedFilenameForSavingWithMIMEType:(NSString *)MIMEType;
@end
@implementation WebResourceResponse (WebResourceResponseExtras)
- (NSString *)suggestedFilenameForSaving
{
ASSERT([self URL]);
// Get the filename from the URL and the MIME type. Always returns something valid.
return [[self URL] _web_suggestedFilenameForSavingWithMIMEType:[self contentType]];
}
@end
@implementation WebHTTPResourceResponse (WebResourceResponseExtras)
- (NSString *)suggestedFilenameForSaving
{
NSString *filename = nil;
// Use the content disposition of the filename if present.
NSString *contentDispositionHeader = [[self headers] objectForKey:@"Content-Disposition"];
filename = [contentDispositionHeader _web_fileNameFromContentDispositionHeader];
filename = [filename _web_filenameByFixingIllegalCharacters];
if ([filename length] == 0) {
// Get the filename from the URL and the MIME type. Always returns something valid.
filename = [super suggestedFilenameForSaving];
}
return filename;
}
@end
@implementation NSURL (WebResourceResponseInternalURLExtras)
- (NSString *)_web_suggestedFilenameForSavingWithMIMEType:(NSString *)MIMEType
{
// Get the filename from the URL. Try the lastPathComponent first.
NSString *filename = [[[self path] lastPathComponent] _web_filenameByFixingIllegalCharacters];
NSString *extension = nil;
if ([filename length] == 0) {
// lastPathComponent is no good, try the host.
filename = [[self host] _web_filenameByFixingIllegalCharacters];
if ([filename length] == 0) {
// Can't make a filename using this URL, use "unknown".
filename = UI_STRING("unknown", "Unknown filename");
}
} else {
// Save the extension for later correction. Only correct the extension of the lastPathComponent.
// For example, if the filename ends up being the host, we wouldn't want to correct ".com" in "www.apple.com".
extension = [filename pathExtension];
}
// If the type is known, check the extension and correct it if necessary.
if (MIMEType && ![MIMEType isEqualToString:@"application/octet-stream"]) {
WebFileTypeMappings *mappings = [WebFileTypeMappings sharedMappings];
NSArray *extensions = [mappings extensionsForMIMEType:MIMEType];
if (![extension length] || (extensions && ![extensions containsObject:extension])) {
// The extension doesn't match the MIME type. Correct this.
NSString *correctExtension = [mappings preferredExtensionForMIMEType:MIMEType];
if ([correctExtension length] != 0) {
if ([extension length] != 0) {
// Remove the incorrect extension.
filename = [filename stringByDeletingPathExtension];
}
// Append the correct extension.
filename = [filename stringByAppendingPathExtension:correctExtension];
}
}
}
return filename;
}
@end
\ No newline at end of file
/*
WebResourceResponseExtras.h
Copyright 2003, Apple, Inc. All rights reserved.
Public header file.
*/
#import <WebFoundation/WebResourceResponse.h>
@interface WebResourceResponse (WebResourceResponseExtras)
/*!
@method suggestedFilenameForSaving
@abstract Returns a suggested filename if the resource were saved to disk.
@discussion The method first checks if the server has specified a filename using the
content disposition header. If no valid filename is specified using that mechanism,
this method checks the last path component of the URL. If no valid filename can be
obtained using the last path component, this method uses the URL's host as the filename.
If the URL's host can't be converted to a valid filename, the filename "unknown" is used.
In mose cases, this method appends the proper file extension based on the MIME type.
This method always returns a valid filename.
*/
- (NSString *)suggestedFilenameForSaving;
@end
//
// WebResourceResponseExtras.m
// WebKit
//
// Created by Chris Blumenberg on Thu Jan 09 2003.
// Copyright (c) 2003 Apple Inc. All rights reserved.
//
#import <WebKit/WebResourceResponseExtras.h>
#import <WebFoundation/WebAssertions.h>
#import <WebFoundation/WebFileTypeMappings.h>
#import <WebFoundation/WebHTTPResourceResponse.h>
#import <WebFoundation/WebLocalizableStrings.h>
#import <WebFoundation/WebNSStringExtras.h>
@interface NSURL (WebResourceResponseInternalURLExtras)
- (NSString *)_web_suggestedFilenameForSavingWithMIMEType:(NSString *)MIMEType;
@end
@implementation WebResourceResponse (WebResourceResponseExtras)
- (NSString *)suggestedFilenameForSaving
{
ASSERT([self URL]);
// Get the filename from the URL and the MIME type. Always returns something valid.
return [[self URL] _web_suggestedFilenameForSavingWithMIMEType:[self contentType]];
}
@end
@implementation WebHTTPResourceResponse (WebResourceResponseExtras)
- (NSString *)suggestedFilenameForSaving
{
NSString *filename = nil;
// Use the content disposition of the filename if present.
NSString *contentDispositionHeader = [[self headers] objectForKey:@"Content-Disposition"];
filename = [contentDispositionHeader _web_fileNameFromContentDispositionHeader];
filename = [filename _web_filenameByFixingIllegalCharacters];
if ([filename length] == 0) {
// Get the filename from the URL and the MIME type. Always returns something valid.
filename = [super suggestedFilenameForSaving];
}
return filename;
}
@end
@implementation NSURL (WebResourceResponseInternalURLExtras)
- (NSString *)_web_suggestedFilenameForSavingWithMIMEType:(NSString *)MIMEType
{
// Get the filename from the URL. Try the lastPathComponent first.
NSString *filename = [[[self path] lastPathComponent] _web_filenameByFixingIllegalCharacters];
NSString *extension = nil;
if ([filename length] == 0) {
// lastPathComponent is no good, try the host.
filename = [[self host] _web_filenameByFixingIllegalCharacters];
if ([filename length] == 0) {
// Can't make a filename using this URL, use "unknown".
filename = UI_STRING("unknown", "Unknown filename");
}
} else {
// Save the extension for later correction. Only correct the extension of the lastPathComponent.
// For example, if the filename ends up being the host, we wouldn't want to correct ".com" in "www.apple.com".
extension = [filename pathExtension];
}
// If the type is known, check the extension and correct it if necessary.
if (MIMEType && ![MIMEType isEqualToString:@"application/octet-stream"]) {
WebFileTypeMappings *mappings = [WebFileTypeMappings sharedMappings];
NSArray *extensions = [mappings extensionsForMIMEType:MIMEType];
if (![extension length] || (extensions && ![extensions containsObject:extension])) {
// The extension doesn't match the MIME type. Correct this.
NSString *correctExtension = [mappings preferredExtensionForMIMEType:MIMEType];
if ([correctExtension length] != 0) {
if ([extension length] != 0) {
// Remove the incorrect extension.
filename = [filename stringByDeletingPathExtension];
}
// Append the correct extension.
filename = [filename stringByAppendingPathExtension:correctExtension];
}
}
}
return filename;
}
@end
\ No newline at end of file
......@@ -297,6 +297,7 @@
511D5554033FD51000CA2ACD,
516F297003A6C45A00CA2D3A,
832B2D1603B10990009CF105,
84A992C403CE696800CA2ACA,
);
isa = PBXHeadersBuildPhase;
runOnlyForDeploymentPostprocessing = 0;
......@@ -411,6 +412,7 @@
394460A5020F50ED0ECA1767,
394460A6020F50ED0ECA1767,
832B2D1703B10990009CF105,
84A992C503CE696800CA2ACA,
);
isa = PBXSourcesBuildPhase;
runOnlyForDeploymentPostprocessing = 0;
......@@ -502,6 +504,8 @@
9345DDAF0365FB27008635CE,
832B2D1403B10990009CF105,
832B2D1503B10990009CF105,
84A992C203CE696800CA2ACA,
84A992C303CE696800CA2ACA,
F560BEBC030DAF4401C1A526,
F560BEBD030DAF4401C1A526,
F59668C802AD2923018635CA,
......@@ -1902,6 +1906,33 @@
path = WebPluginViewFactory.h;
refType = 4;
};
84A992C203CE696800CA2ACA = {
fileEncoding = 4;
isa = PBXFileReference;
path = WebResourceResponseExtras.h;
refType = 4;
};
84A992C303CE696800CA2ACA = {
fileEncoding = 4;
isa = PBXFileReference;
path = WebResourceResponseExtras.m;
refType = 4;
};
84A992C403CE696800CA2ACA = {
fileRef = 84A992C203CE696800CA2ACA;
isa = PBXBuildFile;
settings = {
ATTRIBUTES = (
Public,
);
};
};
84A992C503CE696800CA2ACA = {
fileRef = 84A992C303CE696800CA2ACA;
isa = PBXBuildFile;
settings = {
};
};
84D4BFF70348EF7600CA2ACA = {
fileEncoding = 4;
isa = PBXFileReference;
......
......@@ -122,8 +122,8 @@ typedef enum {
@param response The response for the partially loaded content.
@param request A WebResourceRequest for the partially loaded content.
*/
- (NSString *)saveFilenameForResponse:(WebResourceResponse *)response
andRequest:(WebResourceRequest *)request;
- (NSString *)savePathForResponse:(WebResourceResponse *)response
andRequest:(WebResourceRequest *)request;
/*!
......
......@@ -49,8 +49,8 @@ - (WebPolicyAction)contentPolicyForMIMEType:(NSString *)type
}
}
- (NSString *)saveFilenameForResponse:(WebResourceResponse *)response
andRequest:(WebResourceRequest *)request
- (NSString *)savePathForResponse:(WebResourceResponse *)response
andRequest:(WebResourceRequest *)request
{
return nil;
}
......
......@@ -192,16 +192,15 @@ -(void)continueAfterContentPolicy:(WebPolicyAction)contentPolicy response:(WebRe
[dataSource _setIsDownloading:YES];
if ([dataSource downloadPath] == nil) {
// FIXME: Should this be the filename or path?
NSString *saveFilename = [[[dataSource controller] policyDelegate]
saveFilenameForResponse:r andRequest:req];
NSString *savePath = [[[dataSource controller] policyDelegate]
savePathForResponse:r andRequest:req];
// FIXME: Maybe there a cleaner way handle the bad filename case?
if(!saveFilename || [saveFilename length] == 0 || ![saveFilename isAbsolutePath]){
ERROR("Nil or empty response to saveFilenameForResponse:andRequest:.");
if ([savePath length] == 0 || ![savePath isAbsolutePath]) {
ERROR("Nil, empty or non-absolute path returned from savePathForResponse:andRequest:.");
[self stopLoadingForPolicyChange];
return;
}
[dataSource _setDownloadPath:saveFilename];
[dataSource _setDownloadPath:savePath];
}
[self interruptForPolicyChangeAndKeepLoading:YES];
......
......@@ -192,16 +192,15 @@ -(void)continueAfterContentPolicy:(WebPolicyAction)contentPolicy response:(WebRe
[dataSource _setIsDownloading:YES];
if ([dataSource downloadPath] == nil) {
// FIXME: Should this be the filename or path?
NSString *saveFilename = [[[dataSource controller] policyDelegate]
saveFilenameForResponse:r andRequest:req];
NSString *savePath = [[[dataSource controller] policyDelegate]
savePathForResponse:r andRequest:req];
// FIXME: Maybe there a cleaner way handle the bad filename case?
if(!saveFilename || [saveFilename length] == 0 || ![saveFilename isAbsolutePath]){
ERROR("Nil or empty response to saveFilenameForResponse:andRequest:.");
if ([savePath length] == 0 || ![savePath isAbsolutePath]) {
ERROR("Nil, empty or non-absolute path returned from savePathForResponse:andRequest:.");
[self stopLoadingForPolicyChange];
return;
}
[dataSource _setDownloadPath:saveFilename];
[dataSource _setDownloadPath:savePath];
}
[self interruptForPolicyChangeAndKeepLoading:YES];
......
......@@ -122,8 +122,8 @@ typedef enum {
@param response The response for the partially loaded content.
@param request A WebResourceRequest for the partially loaded content.
*/
- (NSString *)saveFilenameForResponse:(WebResourceResponse *)response
andRequest:(WebResourceRequest *)request;
- (NSString *)savePathForResponse:(WebResourceResponse *)response
andRequest:(WebResourceRequest *)request;
/*!
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment