1/*
2 * Copyright (C) 2005, 2008, 2012 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 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 "URL.h"
36#import "LocalizedStrings.h"
37#import "MIMETypeRegistry.h"
38#import "WebCoreNSStringExtras.h"
39#import <wtf/text/WTFString.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, const String& 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 = URL(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    // Do not correct filenames that are reported with a mime type of tar, and
74    // have a filename which has .tar in it or ends in .tgz
75    if ((mimeType == "application/tar" || mimeType == "application/x-tar")
76        && (hasCaseInsensitiveSubstring(filename, @".tar")
77        || hasCaseInsensitiveSuffix(filename, @".tgz"))) {
78        return filename;
79    }
80
81    // I don't think we need to worry about this for the image case
82    // If the type is known, check the extension and correct it if necessary.
83    if (mimeType != "application/octet-stream" && mimeType != "text/plain") {
84        Vector<String> extensions = MIMETypeRegistry::getExtensionsForMIMEType(mimeType);
85
86        if (extensions.isEmpty() || !vectorContainsString(extensions, extension)) {
87            // The extension doesn't match the MIME type. Correct this.
88            NSString *correctExtension = MIMETypeRegistry::getPreferredExtensionForMIMEType(mimeType);
89            if ([correctExtension length] != 0) {
90                // Append the correct extension.
91                filename = [filename stringByAppendingPathExtension:correctExtension];
92            }
93        }
94    }
95
96    return filename;
97}
98