ccd903655cef0ca11a962aaab2bc6a00186af457
[WebKit-https.git] / Source / WebCore / platform / network / mac / WebCoreURLResponse.mm
1 /*
2  * Copyright (C) 2008, 2009, 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import "config.h"
30 #import "WebCoreURLResponse.h"
31
32 #import "MIMETypeRegistry.h"
33 #import "UTIUtilities.h"
34 #import "WebCoreSystemInterface.h"
35 #import <wtf/Assertions.h>
36 #import <wtf/RetainPtr.h>
37
38 namespace WebCore {
39
40 #if PLATFORM(MAC)
41 // <rdar://problem/7007389> CoreTypes UTI map is missing 100+ file extensions that GateKeeper knew about
42 // When we disabled content sniffing for file URLs we caused problems with these 100+ extensions that CoreTypes
43 // doesn't know about.
44 // If CoreTypes is ever brought up to speed we can remove this table and associated code.
45 static CFDictionaryRef createExtensionToMIMETypeMap()
46 {
47     CFStringRef keys[] = {
48         CFSTR("ai"),
49         CFSTR("asc"),
50         CFSTR("bcpio"),
51         CFSTR("bmp"),
52         CFSTR("cdf"),
53         CFSTR("class"),
54         CFSTR("cpgz"),
55         CFSTR("cpio"),
56         CFSTR("cpt"),
57         CFSTR("csh"),
58         CFSTR("css"),
59         CFSTR("dcr"),
60         CFSTR("dir"),
61         CFSTR("dmg"),
62         CFSTR("dms"),
63         CFSTR("dvi"),
64         CFSTR("dxr"),
65         CFSTR("eps"),
66         CFSTR("etx"),
67         CFSTR("ez"),
68         CFSTR("fdf"),
69         CFSTR("fla"),
70         CFSTR("fp"),
71         CFSTR("fp2"),
72         CFSTR("fp3"),
73         CFSTR("fp4"),
74         CFSTR("fp5"),
75         CFSTR("fp6"),
76         CFSTR("hdf"),
77         CFSTR("ice"),
78         CFSTR("ico"),
79         CFSTR("ics"),
80         CFSTR("ief"),
81         CFSTR("iges"),
82         CFSTR("igs"),
83         CFSTR("iso"),
84         CFSTR("jhtml"),
85         CFSTR("latex"),
86         CFSTR("lha"),
87         CFSTR("lzh"),
88         CFSTR("m3u"),
89         CFSTR("m4p"),
90         CFSTR("mac"),
91         CFSTR("man"),
92         CFSTR("me"),
93         CFSTR("mesh"),
94         CFSTR("mif"),
95         CFSTR("movie"),
96         CFSTR("mp2"),
97         CFSTR("mpga"),
98         CFSTR("ms"),
99         CFSTR("msh"),
100         CFSTR("mxu"),
101         CFSTR("nc"),
102         CFSTR("oda"),
103         CFSTR("pbm"),
104         CFSTR("pcx"),
105         CFSTR("pdb"),
106         CFSTR("pgm"),
107         CFSTR("pgn"),
108         CFSTR("pls"),
109         CFSTR("pnm"),
110         CFSTR("pnt"),
111         CFSTR("pntg"),
112         CFSTR("ppm"),
113         CFSTR("ras"),
114         CFSTR("rgb"),
115         CFSTR("roff"),
116         CFSTR("rpm"),
117         CFSTR("rtx"),
118         CFSTR("sgm"),
119         CFSTR("sgml"),
120         CFSTR("sh"),
121         CFSTR("shar"),
122         CFSTR("silo"),
123         CFSTR("skd"),
124         CFSTR("skm"),
125         CFSTR("skp"),
126         CFSTR("skt"),
127         CFSTR("smi"),
128         CFSTR("so"),
129         CFSTR("spl"),
130         CFSTR("src"),
131         CFSTR("sv4cpio"),
132         CFSTR("sv4crc"),
133         CFSTR("swf"),
134         CFSTR("t"),
135         CFSTR("targa"),
136         CFSTR("tcl"),
137         CFSTR("tex"),
138         CFSTR("texi"),
139         CFSTR("texinfo"),
140         CFSTR("tgz"),
141         CFSTR("torrent"),
142         CFSTR("tr"),
143         CFSTR("tsv"),
144         CFSTR("ustar"),
145         CFSTR("vcd"),
146         CFSTR("vrml"),
147         CFSTR("wbmp"),
148         CFSTR("wbxml"),
149         CFSTR("webarchive"),
150         CFSTR("wmd"),
151         CFSTR("wml"),
152         CFSTR("wmlc"),
153         CFSTR("wmls"),
154         CFSTR("wmlsc"),
155         CFSTR("wrl"),
156         CFSTR("xdp"),
157         CFSTR("xfd"),
158         CFSTR("xfdf"),
159         CFSTR("xpm"),
160         CFSTR("xsl"),
161         CFSTR("xwd"),
162         CFSTR("xyz"),
163         CFSTR("z")
164     };
165
166     CFStringRef values[] = {
167         CFSTR("application/postscript"),
168         CFSTR("text/plain"),
169         CFSTR("application/x-bcpio"),
170         CFSTR("image/bmp"),
171         CFSTR("application/x-netcdf"),
172         CFSTR("application/octet-stream"),
173         CFSTR("application/x-gzip"),
174         CFSTR("application/x-cpio"),
175         CFSTR("application/mac-compactpro"),
176         CFSTR("application/x-csh"),
177         CFSTR("text/css"),
178         CFSTR("application/x-director"),
179         CFSTR("application/x-director"),
180         CFSTR("application/x-diskcopy"),
181         CFSTR("application/octet-stream"),
182         CFSTR("application/x-dvi"),
183         CFSTR("application/x-director"),
184         CFSTR("application/postscript"),
185         CFSTR("text/x-setext"),
186         CFSTR("application/andrew-inset"),
187         CFSTR("application/vnd.fdf"),
188         CFSTR("application/octet-stream"),
189         CFSTR("application/x-filemaker"),
190         CFSTR("application/x-filemaker"),
191         CFSTR("application/x-filemaker"),
192         CFSTR("application/x-filemaker"),
193         CFSTR("application/x-filemaker"),
194         CFSTR("application/x-filemaker"),
195         CFSTR("application/x-hdf"),
196         CFSTR("x-conference/x-cooltalk"),
197         CFSTR("image/x-icon"),
198         CFSTR("text/calendar"),
199         CFSTR("image/ief"),
200         CFSTR("model/iges"),
201         CFSTR("model/iges"),
202         CFSTR("application/octet-stream"),
203         CFSTR("text/html"),
204         CFSTR("application/x-latex"),
205         CFSTR("application/octet-stream"),
206         CFSTR("application/octet-stream"),
207         CFSTR("audio/x-mpegurl"),
208         CFSTR("audio/x-m4p"),
209         CFSTR("image/x-macpaint"),
210         CFSTR("application/x-troff-man"),
211         CFSTR("application/x-troff-me"),
212         CFSTR("model/mesh"),
213         CFSTR("application/vnd.mif"),
214         CFSTR("video/x-sgi-movie"),
215         CFSTR("audio/mpeg"),
216         CFSTR("audio/mpeg"),
217         CFSTR("application/x-troff-ms"),
218         CFSTR("model/mesh"),
219         CFSTR("video/vnd.mpegurl"),
220         CFSTR("application/x-netcdf"),
221         CFSTR("application/oda"),
222         CFSTR("image/x-portable-bitmap"),
223         CFSTR("image/x-pcx"),
224         CFSTR("chemical/x-pdb"),
225         CFSTR("image/x-portable-graymap"),
226         CFSTR("application/x-chess-pgn"),
227         CFSTR("audio/scpls"),
228         CFSTR("image/x-portable-anymap"),
229         CFSTR("image/x-macpaint"),
230         CFSTR("image/x-macpaint"),
231         CFSTR("image/x-portable-pixmap"),
232         CFSTR("image/x-cmu-raster"),
233         CFSTR("image/x-rgb"),
234         CFSTR("application/x-troff"),
235         CFSTR("audio/x-pn-realaudio-plugin"),
236         CFSTR("text/richtext"),
237         CFSTR("text/sgml"),
238         CFSTR("text/sgml"),
239         CFSTR("application/x-sh"),
240         CFSTR("application/x-shar"),
241         CFSTR("model/mesh"),
242         CFSTR("application/x-koan"),
243         CFSTR("application/x-koan"),
244         CFSTR("application/x-koan"),
245         CFSTR("application/x-koan"),
246         CFSTR("application/x-diskcopy"),
247         CFSTR("application/octet-stream"),
248         CFSTR("application/x-futuresplash"),
249         CFSTR("application/x-wais-source"),
250         CFSTR("application/x-sv4cpio"),
251         CFSTR("application/x-sv4crc"),
252         CFSTR("application/x-shockwave-flash"),
253         CFSTR("application/x-troff"),
254         CFSTR("image/x-targa"),
255         CFSTR("application/x-tcl"),
256         CFSTR("application/x-tex"),
257         CFSTR("application/x-texinfo"),
258         CFSTR("application/x-texinfo"),
259         CFSTR("application/x-gzip"),
260         CFSTR("application/x-bittorrent"),
261         CFSTR("application/x-troff"),
262         CFSTR("text/tab-separated-values"),
263         CFSTR("application/x-ustar"),
264         CFSTR("application/x-cdlink"),
265         CFSTR("model/vrml"),
266         CFSTR("image/vnd.wap.wbmp"),
267         CFSTR("application/vnd.wap.wbxml"),
268         CFSTR("application/x-webarchive"),
269         CFSTR("application/x-ms-wmd"),
270         CFSTR("text/vnd.wap.wml"),
271         CFSTR("application/vnd.wap.wmlc"),
272         CFSTR("text/vnd.wap.wmlscript"),
273         CFSTR("application/vnd.wap.wmlscriptc"),
274         CFSTR("model/vrml"),
275         CFSTR("application/vnd.adobe.xdp+xml"),
276         CFSTR("application/vnd.adobe.xfd+xml"),
277         CFSTR("application/vnd.adobe.xfdf"),
278         CFSTR("image/x-xpixmap"),
279         CFSTR("text/xml"),
280         CFSTR("image/x-xwindowdump"),
281         CFSTR("chemical/x-xyz"),
282         CFSTR("application/x-compress")
283     };
284
285     ASSERT(sizeof(keys) == sizeof(values));
286     return CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys, (const void**)&values, sizeof(keys)/sizeof(CFStringRef), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
287 }
288
289 void adjustMIMETypeIfNecessary(CFURLResponseRef cfResponse, bool isMainResourceLoad)
290 {
291     UNUSED_PARAM(isMainResourceLoad);
292     RetainPtr<CFStringRef> result = CFURLResponseGetMIMEType(cfResponse);
293     RetainPtr<CFStringRef> originalResult = result;
294
295     if (!result) {
296         auto url = CFURLResponseGetURL(cfResponse);
297         if ([(NSURL *)url isFileURL]) {
298             RetainPtr<CFStringRef> extension = adoptCF(CFURLCopyPathExtension(url));
299             if (extension) {
300                 // <rdar://problem/7007389> CoreTypes UTI map is missing 100+ file extensions that GateKeeper knew about
301                 // When this radar is resolved, we can remove this file:// url specific code.
302                 static CFDictionaryRef extensionMap = createExtensionToMIMETypeMap();
303                 CFMutableStringRef mutableExtension = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, extension.get());
304                 CFStringLowercase(mutableExtension, NULL);
305                 extension = adoptCF(mutableExtension);
306                 result = (CFStringRef)CFDictionaryGetValue(extensionMap, extension.get());
307                 
308                 if (!result) {
309                     // If the Gatekeeper-based map doesn't have a MIME type, we'll try to figure out what it should be by
310                     // looking up the file extension in the UTI maps.
311                     RetainPtr<CFStringRef> uti = adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension.get(), 0));
312                     String MIMEType = MIMETypeFromUTITree(uti.get());
313                     if (!MIMEType.isEmpty())
314                         result = MIMEType.createCFString();
315                 }
316             }
317         }
318     }
319     
320     if (!result) {
321         static CFStringRef defaultMIMETypeString = WebCore::defaultMIMEType().createCFString().leakRef();
322         result = defaultMIMETypeString;
323     }
324
325     if (result != originalResult)
326         CFURLResponseSetMIMEType(cfResponse, result.get());
327 }
328 #endif
329
330 #if !USE(CFURLCONNECTION)
331 NSURLResponse *synthesizeRedirectResponseIfNecessary(NSURLRequest *currentRequest, NSURLRequest *newRequest, NSURLResponse *redirectResponse)
332 {
333     if (redirectResponse)
334         return redirectResponse;
335
336     if ([[[newRequest URL] scheme] isEqualToString:[[currentRequest URL] scheme]])
337         return nil;
338
339     // If the new request is a different protocol than the current request, synthesize a redirect response.
340     // This is critical for HSTS (<rdar://problem/14241270>).
341     NSDictionary *synthesizedResponseHeaderFields = @{ @"Location": [[newRequest URL] absoluteString], @"Cache-Control": @"no-store" };
342     return [[[NSHTTPURLResponse alloc] initWithURL:[currentRequest URL] statusCode:302 HTTPVersion:(NSString *)kCFHTTPVersion1_1 headerFields:synthesizedResponseHeaderFields] autorelease];
343 }
344 #endif
345
346 }