OSDN Git Service

HMMasterMissionCommandクラスをSwiftで書き換え
[kcd/KCD.git] / KCD / CacheStoragePolicy.m
1 /*
2      File: CacheStoragePolicy.m
3  Abstract: A function to determine the cache storage policy for a request.
4   Version: 1.0
5  
6  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
7  Inc. ("Apple") in consideration of your agreement to the following
8  terms, and your use, installation, modification or redistribution of
9  this Apple software constitutes acceptance of these terms.  If you do
10  not agree with these terms, please do not use, install, modify or
11  redistribute this Apple software.
12  
13  In consideration of your agreement to abide by the following terms, and
14  subject to these terms, Apple grants you a personal, non-exclusive
15  license, under Apple's copyrights in this original Apple software (the
16  "Apple Software"), to use, reproduce, modify and redistribute the Apple
17  Software, with or without modifications, in source and/or binary forms;
18  provided that if you redistribute the Apple Software in its entirety and
19  without modifications, you must retain this notice and the following
20  text and disclaimers in all such redistributions of the Apple Software.
21  Neither the name, trademarks, service marks or logos of Apple Inc. may
22  be used to endorse or promote products derived from the Apple Software
23  without specific prior written permission from Apple.  Except as
24  expressly stated in this notice, no other rights or licenses, express or
25  implied, are granted by Apple herein, including but not limited to any
26  patent rights that may be infringed by your derivative works or by other
27  works in which the Apple Software may be incorporated.
28  
29  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
30  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34  
35  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43  
44  Copyright (C) 2013 Apple Inc. All Rights Reserved.
45  
46  */
47
48 #include "CacheStoragePolicy.h"
49
50 extern NSURLCacheStoragePolicy CacheStoragePolicyForRequestAndResponse(NSURLRequest * request, NSHTTPURLResponse * response)
51     // See comment in header.
52 {
53     BOOL                        cacheable;
54     NSURLCacheStoragePolicy     result;
55
56     // First determine if the request is cacheable based on its status code.
57     
58     switch ([response statusCode]) {
59         case 200:
60         case 203:
61         case 206:
62         case 301:
63         case 304:
64         case 404:
65         case 410: {
66             cacheable = YES;
67         } break;
68         default: {
69             cacheable = NO;
70         } break;
71     }
72
73     // If the response might be cacheable, look at the "Cache-Control" header in 
74     // the response.
75
76     // IMPORTANT: We can't rely on -rangeOfString: returning valid results if the target 
77     // string is nil, so we have to explicitly test for nil in the following two cases.
78     
79     if (cacheable) {
80         NSString *  responseHeader;
81         
82         responseHeader = [[[response allHeaderFields] objectForKey:@"Cache-Control"] lowercaseString];
83         if ( (responseHeader != nil) && [responseHeader rangeOfString:@"no-store"].location != NSNotFound) {
84             cacheable = NO;
85         }
86     }
87
88     // If we still think it might be cacheable, look at the "Cache-Control" header in 
89     // the request.
90
91     if (cacheable) {
92         NSString *  requestHeader;
93
94         requestHeader = [[[request allHTTPHeaderFields] objectForKey:@"Cache-Control"] lowercaseString];
95         if ( (requestHeader != nil) 
96           && ([requestHeader rangeOfString:@"no-store"].location != NSNotFound)
97           && ([requestHeader rangeOfString:@"no-cache"].location != NSNotFound) ) {
98             cacheable = NO;
99         }
100     }
101
102     // Use the cacheable flag to determine the result.
103     
104     if (cacheable) {
105         if ([[[[request URL] scheme] lowercaseString] isEqual:@"https"]) {
106             result = NSURLCacheStorageAllowedInMemoryOnly;
107         } else {
108             result = NSURLCacheStorageAllowed;
109         }
110     } else {
111         result = NSURLCacheStorageNotAllowed;
112     }
113
114     return result;
115 }