1/* 2 * Copyright (C) 2005, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com) 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 15 * its contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#import "config.h" 31#import "LoaderNSURLExtras.h" 32 33#import <wtf/Assertions.h> 34#import <wtf/Vector.h> 35#import "KURL.h" 36#import "LocalizedStrings.h" 37#import "MIMETypeRegistry.h" 38#import "PlatformString.h" 39#import "WebCoreNSStringExtras.h" 40 41using namespace WebCore; 42 43static bool vectorContainsString(const Vector<String>& vector, const String& string) 44{ 45 int size = vector.size(); 46 for (int i = 0; i < size; i++) 47 if (vector[i] == string) 48 return true; 49 return false; 50} 51 52NSString *suggestedFilenameWithMIMEType(NSURL *url, NSString *MIMEType) 53{ 54 // Get the filename from the URL. Try the lastPathComponent first. 55 NSString *lastPathComponent = [[url path] lastPathComponent]; 56 NSString *filename = filenameByFixingIllegalCharacters(lastPathComponent); 57 NSString *extension = nil; 58 59 if ([filename length] == 0 || [lastPathComponent isEqualToString:@"/"]) { 60 // lastPathComponent is no good, try the host. 61 NSString *host = KURL(url).host(); 62 filename = filenameByFixingIllegalCharacters(host); 63 if ([filename length] == 0) { 64 // Can't make a filename using this URL, use "unknown". 65 filename = copyImageUnknownFileLabel(); 66 } 67 } else { 68 // Save the extension for later correction. Only correct the extension of the lastPathComponent. 69 // For example, if the filename ends up being the host, we wouldn't want to correct ".com" in "www.apple.com". 70 extension = [filename pathExtension]; 71 } 72 73 // No mime type reported. Just return the filename we have now. 74 if (!MIMEType) { 75 return filename; 76 } 77 78 // Do not correct filenames that are reported with a mime type of tar, and 79 // have a filename which has .tar in it or ends in .tgz 80 if (([MIMEType isEqualToString:@"application/tar"] || [MIMEType isEqualToString:@"application/x-tar"]) 81 && (hasCaseInsensitiveSubstring(filename, @".tar") 82 || hasCaseInsensitiveSuffix(filename, @".tgz"))) { 83 return filename; 84 } 85 86 // I don't think we need to worry about this for the image case 87 // If the type is known, check the extension and correct it if necessary. 88 if (![MIMEType isEqualToString:@"application/octet-stream"] && ![MIMEType isEqualToString:@"text/plain"]) { 89 Vector<String> extensions = MIMETypeRegistry::getExtensionsForMIMEType(MIMEType); 90 91 if (extensions.isEmpty() || !vectorContainsString(extensions, extension)) { 92 // The extension doesn't match the MIME type. Correct this. 93 NSString *correctExtension = MIMETypeRegistry::getPreferredExtensionForMIMEType(MIMEType); 94 if ([correctExtension length] != 0) { 95 // Append the correct extension. 96 filename = [filename stringByAppendingPathExtension:correctExtension]; 97 } 98 } 99 } 100 101 return filename; 102} 103