summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/network/mac/WebCoreURLResponse.mm
blob: e287e5fc56a435146a24f26014fcd03e2e553043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer. 
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution. 
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "config.h"
#import "WebCoreURLResponse.h"

#import "MIMETypeRegistry.h"
#import <objc/objc-class.h>
#import <wtf/Assertions.h>
#import <wtf/RetainPtr.h>

#ifndef BUILDING_ON_TIGER
// <rdar://problem/5321972> Plain text document from HTTP server detected as application/octet-stream
// When we sniff a resource as application/octet-stream but the http response headers had "text/plain",
// we have a hard decision to make about which of the two generic MIME types to go with.
// When the URL's extension is a known binary type, we'll go with application/octet-stream.
// Otherwise, we'll trust the server.
static NSSet *createBinaryExtensionsSet()
{
    return [[NSSet alloc] initWithObjects:
        @"3g2",
        @"3gp",
        @"ai",
        @"aif",
        @"aifc",
        @"aiff",
        @"au",
        @"avi",
        @"bcpio",
        @"bin",
        @"bmp",
        @"boz",
        @"bpk",
        @"bz",
        @"bz2",
        @"chm",
        @"class",
        @"com",
        @"cpio",
        @"dcr",
        @"dir",
        @"dist",
        @"distz",
        @"dll",
        @"dmg",
        @"dms",
        @"doc",
        @"dot",
        @"dump",
        @"dv",
        @"dvi",
        @"dxr",
        @"elc",
        @"eot",
        @"eps",
        @"exe",
        @"fgd",
        @"gif",
        @"gtar",
        @"h261",
        @"h263",
        @"h264",
        @"ico",
        @"ims",
        @"indd",
        @"iso",
        @"jp2",
        @"jpe",
        @"jpeg",
        @"jpg",
        @"jpgm",
        @"jpgv",
        @"jpm",
        @"kar",
        @"kmz",
        @"lha",
        @"lrm",
        @"lzh",
        @"m1v",
        @"m2a",
        @"m2v",
        @"m3a",
        @"m3u",
        @"m4a",
        @"m4p",
        @"m4v",
        @"mdb",
        @"mid",
        @"midi",
        @"mj2",
        @"mjp2",
        @"mov",
        @"movie",
        @"mp2",
        @"mp2a",
        @"mp3",
        @"mp4",
        @"mp4a",
        @"mp4s",
        @"mp4v",
        @"mpe",
        @"mpeg",
        @"mpg",
        @"mpg4",
        @"mpga",
        @"mpp",
        @"mpt",
        @"msi",
        @"ogg",
        @"otf",
        @"pct",
        @"pdf",
        @"pfa",
        @"pfb",
        @"pic",
        @"pict",
        @"pkg",
        @"png",
        @"pot",
        @"pps",
        @"ppt",
        @"ps",
        @"psd",
        @"qt",
        @"qti",
        @"qtif",
        @"qwd",
        @"qwt",
        @"qxb",
        @"qxd",
        @"qxl",
        @"qxp",
        @"qxt",
        @"ra",
        @"ram",
        @"rm",
        @"rmi",
        @"rmp",
        @"scpt",
        @"sit",
        @"sitx",
        @"snd",
        @"so",
        @"swf",
        @"tar",
        @"tif",
        @"tiff",
        @"ttf",
        @"wav",
        @"wcm",
        @"wdb",
        @"wks",
        @"wm",
        @"wma",
        @"wmd",
        @"wmf",
        @"wmv",
        @"wmx",
        @"wmz",
        @"wpd",
        @"wpl",
        @"wps",
        @"wvx",
        @"xla",
        @"xlc",
        @"xlm",
        @"xls",
        @"xlt",
        @"xlw",
        @"xps",
        @"zip",
        nil
    ];
}
#endif

// <rdar://problem/7007389> CoreTypes UTI map is missing 100+ file extensions that GateKeeper knew about
// When we disabled content sniffing for file URLs we caused problems with these 100+ extensions that CoreTypes
// doesn't know about.
// If CoreTypes is ever brought up to speed we can remove this table and associated code.
static NSDictionary *createExtensionToMIMETypeMap()
{
    return [[NSDictionary alloc] initWithObjectsAndKeys:
        @"application/postscript", @"ai",
        @"text/plain", @"asc",
        @"application/x-bcpio", @"bcpio",
        @"image/bmp", @"bmp",
        @"application/x-netcdf", @"cdf",
        @"application/octet-stream", @"class",
        @"application/x-gzip", @"cpgz",
        @"application/x-cpio", @"cpio",
        @"application/mac-compactpro", @"cpt",
        @"application/x-csh", @"csh",
        @"text/css", @"css",
        @"application/x-director", @"dcr",
        @"application/x-director", @"dir",
        @"application/x-diskcopy", @"dmg",
        @"application/octet-stream", @"dms",
        @"application/x-dvi", @"dvi",
        @"application/x-director", @"dxr",
        @"application/postscript", @"eps",
        @"text/x-setext", @"etx",
        @"application/andrew-inset", @"ez",
        @"application/vnd.fdf", @"fdf",
        @"application/octet-stream", @"fla",
        @"application/x-filemaker", @"fp",
        @"application/x-filemaker", @"fp2",
        @"application/x-filemaker", @"fp3",
        @"application/x-filemaker", @"fp4",
        @"application/x-filemaker", @"fp5",
        @"application/x-filemaker", @"fp6",
        @"application/x-hdf", @"hdf",
        @"x-conference/x-cooltalk", @"ice",
        @"image/x-icon", @"ico",
        @"text/calendar", @"ics",
        @"image/ief", @"ief",
        @"model/iges", @"iges",
        @"model/iges", @"igs",
        @"application/octet-stream", @"iso",
        @"text/html", @"jhtml",
        @"application/x-latex", @"latex",
        @"application/octet-stream", @"lha",
        @"application/octet-stream", @"lzh",
        @"audio/x-mpegurl", @"m3u",
        @"audio/x-m4p", @"m4p",
        @"image/x-macpaint", @"mac",
        @"application/x-troff-man", @"man",
        @"application/x-troff-me", @"me",
        @"model/mesh", @"mesh",
        @"application/vnd.mif", @"mif",
        @"video/x-sgi-movie", @"movie",
        @"audio/mpeg", @"mp2",
        @"audio/mpeg", @"mpga",
        @"application/x-troff-ms", @"ms",
        @"model/mesh", @"msh",
        @"video/vnd.mpegurl", @"mxu",
        @"application/x-netcdf", @"nc",
        @"application/oda", @"oda",
        @"image/x-portable-bitmap", @"pbm",
        @"image/x-pcx", @"pcx",
        @"chemical/x-pdb", @"pdb",
        @"image/x-portable-graymap", @"pgm",
        @"application/x-chess-pgn", @"pgn",
        @"audio/scpls", @"pls",
        @"image/x-portable-anymap", @"pnm",
        @"image/x-macpaint", @"pnt",
        @"image/x-macpaint", @"pntg",
        @"image/x-portable-pixmap", @"ppm",
        @"image/x-cmu-raster", @"ras",
        @"image/x-rgb", @"rgb",
        @"application/x-troff", @"roff",
        @"audio/x-pn-realaudio-plugin", @"rpm",
        @"text/richtext", @"rtx",
        @"text/sgml", @"sgm",
        @"text/sgml", @"sgml",
        @"application/x-sh", @"sh",
        @"application/x-shar", @"shar",
        @"model/mesh", @"silo",
        @"application/x-koan", @"skd",
        @"application/x-koan", @"skm",
        @"application/x-koan", @"skp",
        @"application/x-koan", @"skt",
        @"application/x-diskcopy", @"smi",
        @"application/octet-stream", @"so",
        @"application/x-futuresplash", @"spl",
        @"application/x-wais-source", @"src",
        @"application/x-sv4cpio", @"sv4cpio",
        @"application/x-sv4crc", @"sv4crc",
        @"application/x-shockwave-flash", @"swf",
        @"application/x-troff", @"t",
        @"image/x-targa", @"targa",
        @"application/x-tcl", @"tcl",
        @"application/x-tex", @"tex",
        @"application/x-texinfo", @"texi",
        @"application/x-texinfo", @"texinfo",
        @"application/x-gzip", @"tgz",
        @"application/x-bittorrent", @"torrent",
        @"application/x-troff", @"tr",
        @"text/tab-separated-values", @"tsv",
        @"application/x-ustar", @"ustar",
        @"application/x-cdlink", @"vcd",
        @"model/vrml", @"vrml",
        @"image/vnd.wap.wbmp", @"wbmp",
        @"application/vnd.wap.wbxml", @"wbxml",
        @"application/x-webarchive", @"webarchive",
        @"application/x-ms-wmd", @"wmd",
        @"text/vnd.wap.wml", @"wml",
        @"application/vnd.wap.wmlc", @"wmlc",
        @"text/vnd.wap.wmlscript", @"wmls",
        @"application/vnd.wap.wmlscriptc", @"wmlsc",
        @"model/vrml", @"wrl",
        @"application/vnd.adobe.xdp+xml", @"xdp",
        @"application/vnd.adobe.xfd+xml", @"xfd",
        @"application/vnd.adobe.xfdf", @"xfdf",
        @"image/x-xpixmap", @"xpm",
        @"text/xml", @"xsl",
        @"image/x-xwindowdump", @"xwd",
        @"chemical/x-xyz", @"xyz",
        @"application/x-compress", @"z",
        nil
    ];
}

static RetainPtr<NSString> mimeTypeFromUTITree(CFStringRef uti)
{
    // Check if this UTI has a MIME type.
    RetainPtr<CFStringRef> mimeType(AdoptCF, UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType));
    if (mimeType)
        return (NSString *)mimeType.get();
    
    // If not, walk the ancestory of this UTI via its "ConformsTo" tags and return the first MIME type we find.
    RetainPtr<CFDictionaryRef> decl(AdoptCF, UTTypeCopyDeclaration(uti));
    if (!decl)
        return nil;
    CFTypeRef value = CFDictionaryGetValue(decl.get(), kUTTypeConformsToKey);
    if (!value)
        return nil;
    CFTypeID typeID = CFGetTypeID(value);
    
    if (typeID == CFStringGetTypeID())
        return mimeTypeFromUTITree((CFStringRef)value);

    if (typeID == CFArrayGetTypeID()) {
        CFArrayRef newTypes = (CFArrayRef)value;
        CFIndex count = CFArrayGetCount(newTypes);
        for (CFIndex i = 0; i < count; ++i) {
            CFTypeRef object = CFArrayGetValueAtIndex(newTypes, i);
            if (CFGetTypeID(object) != CFStringGetTypeID())
                continue;

            if (RetainPtr<NSString> mimeType = mimeTypeFromUTITree((CFStringRef)object))
                return mimeType;
        }
    }
    
    return nil;
}

@implementation NSURLResponse (WebCoreURLResponse)

-(void)adjustMIMETypeIfNecessary
{
    RetainPtr<NSString> result = [self MIMEType];
    RetainPtr<NSString> originalResult = result;

#ifdef BUILDING_ON_TIGER
    // When content sniffing is disabled, Tiger's CFNetwork automatically returns application/octet-stream for certain
    // extensions even when scouring the UTI maps would end up with a better result, so we'll give a chance for that to happen.
    if ([[self URL] isFileURL] && [result.get() caseInsensitiveCompare:@"application/octet-stream"] == NSOrderedSame)
        result = nil;
#endif

    if (!result) {
        NSURL *url = [self URL];
        if ([url isFileURL]) {
            if (NSString *extension = [[url path] pathExtension]) {
                // <rdar://problem/7007389> CoreTypes UTI map is missing 100+ file extensions that GateKeeper knew about
                // When this radar is resolved, we can remove this file:// url specific code.
                static NSDictionary *extensionMap = createExtensionToMIMETypeMap();
                result = [extensionMap objectForKey:[extension lowercaseString]];
                
                if (!result) {
                    // If the Gatekeeper-based map doesn't have a MIME type, we'll try to figure out what it should be by
                    // looking up the file extension in the UTI maps.
                    RetainPtr<CFStringRef> uti(AdoptCF, UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)extension, 0));
                    result = mimeTypeFromUTITree(uti.get());
                }
            }
        }
    }
    
    if (!result) {
        static NSString *defaultMIMETypeString = [(NSString *)WebCore::defaultMIMEType() retain];
        result = defaultMIMETypeString;
    }

#ifndef BUILDING_ON_TIGER
    // <rdar://problem/5321972> Plain text document from HTTP server detected as application/octet-stream
    // Make the best guess when deciding between "generic binary" and "generic text" using a table of known binary MIME types.
    if ([result.get() isEqualToString:@"application/octet-stream"] && [self respondsToSelector:@selector(allHeaderFields)] && [[[self performSelector:@selector(allHeaderFields)] objectForKey:@"Content-Type"] hasPrefix:@"text/plain"]) {
        static NSSet *binaryExtensions = createBinaryExtensionsSet();
        if (![binaryExtensions containsObject:[[[self suggestedFilename] pathExtension] lowercaseString]])
            result = @"text/plain";
    }

#endif

#ifdef BUILDING_ON_LEOPARD
    // Workaround for <rdar://problem/5539824>
    if ([result.get() isEqualToString:@"text/xml"])
        result = @"application/xml";
#endif

    if (result != originalResult)
        [self _setMIMEType:result.get()];
}

@end