Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 188 lines (150 sloc) 6.512 kb
d2d4ed0 Full Support for Google Reader Sync!
Salvatore Ansani authored
1 /* Copyright (c) 2011 Google Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //
17 // GTMHTTPFetchHistory.h
18 //
19
20 //
21 // Users of the GTMHTTPFetcher class may optionally create and set a fetch
22 // history object. The fetch history provides "memory" between subsequent
23 // fetches, including:
24 //
25 // - For fetch responses with Etag headers, the fetch history
26 // remembers the response headers. Future fetcher requests to the same URL
27 // will be given an "If-None-Match" header, telling the server to return
28 // a 304 Not Modified status if the response is unchanged, reducing the
29 // server load and network traffic.
30 //
31 // - Optionally, the fetch history can cache the ETagged data that was returned
32 // in the responses that contained Etag headers. If a later fetch
33 // results in a 304 status, the fetcher will return the cached ETagged data
34 // to the client along with a 200 status, hiding the 304.
35 //
36 // - The fetch history can track cookies.
37 //
38
39 #pragma once
40
41 #import <Foundation/Foundation.h>
42
43 #import "GTMHTTPFetcher.h"
44
45 #undef _EXTERN
46 #undef _INITIALIZE_AS
47 #ifdef GTMHTTPFETCHHISTORY_DEFINE_GLOBALS
48 #define _EXTERN
49 #define _INITIALIZE_AS(x) =x
50 #else
51 #if defined(__cplusplus)
52 #define _EXTERN extern "C"
53 #else
54 #define _EXTERN extern
55 #endif
56 #define _INITIALIZE_AS(x)
57 #endif
58
59
60 // default data cache size for when we're caching responses to handle "not
61 // modified" errors for the client
62 #if GTM_IPHONE
63 // iPhone: up to 1MB memory
64 _EXTERN const NSUInteger kGTMDefaultETaggedDataCacheMemoryCapacity _INITIALIZE_AS(1*1024*1024);
65 #else
66 // Mac OS X: up to 15MB memory
67 _EXTERN const NSUInteger kGTMDefaultETaggedDataCacheMemoryCapacity _INITIALIZE_AS(15*1024*1024);
68 #endif
69
70 // forward declarations
71 @class GTMURLCache;
72 @class GTMCookieStorage;
73
74 @interface GTMHTTPFetchHistory : NSObject <GTMHTTPFetchHistoryProtocol> {
75 @private
76 GTMURLCache *etaggedDataCache_;
77 BOOL shouldRememberETags_;
78 BOOL shouldCacheETaggedData_; // if NO, then only headers are cached
79 GTMCookieStorage *cookieStorage_;
80 }
81
82 // With caching enabled, previously-cached data will be returned instead of
83 // 304 Not Modified responses when repeating a fetch of an URL that previously
84 // included an ETag header in its response
85 @property (assign) BOOL shouldRememberETags; // default: NO
86 @property (assign) BOOL shouldCacheETaggedData; // default: NO
87
88 // the default ETag data cache capacity is kGTMDefaultETaggedDataCacheMemoryCapacity
89 @property (assign) NSUInteger memoryCapacity;
90
91 @property (retain) GTMCookieStorage *cookieStorage;
92
93 - (id)initWithMemoryCapacity:(NSUInteger)totalBytes
94 shouldCacheETaggedData:(BOOL)shouldCacheETaggedData;
95
96 - (void)updateRequest:(NSMutableURLRequest *)request isHTTPGet:(BOOL)isHTTPGet;
97
98 - (void)clearETaggedDataCache;
99 - (void)clearHistory;
100
101 - (void)removeAllCookies;
102
103 @end
104
105
106 // GTMURLCache and GTMCachedURLResponse have interfaces similar to their
107 // NSURLCache counterparts, in hopes that someday the NSURLCache versions
108 // can be used. But in 10.5.8, those are not reliable enough except when
109 // used with +setSharedURLCache. Our goal here is just to cache
110 // responses for handling If-None-Match requests that return
111 // "Not Modified" responses, not for replacing the general URL
112 // caches.
113
114 @interface GTMCachedURLResponse : NSObject {
115 @private
116 NSURLResponse *response_;
117 NSData *data_;
118 NSDate *useDate_; // date this response was last saved or used
119 NSDate *reservationDate_; // date this response's ETag was used
120 }
121
122 @property (readonly) NSURLResponse* response;
123 @property (readonly) NSData* data;
124
125 // date the response was saved or last accessed
126 @property (retain) NSDate *useDate;
127
128 // date the response's ETag header was last used for a fetch request
129 @property (retain) NSDate *reservationDate;
130
131 - (id)initWithResponse:(NSURLResponse *)response data:(NSData *)data;
132 @end
133
134 @interface GTMURLCache : NSObject {
135 NSMutableDictionary *responses_; // maps request URL to GTMCachedURLResponse
136 NSUInteger memoryCapacity_; // capacity of NSDatas in the responses
137 NSUInteger totalDataSize_; // sum of sizes of NSDatas of all responses
138 NSTimeInterval reservationInterval_; // reservation expiration interval
139 }
140
141 @property (assign) NSUInteger memoryCapacity;
142
143 - (id)initWithMemoryCapacity:(NSUInteger)totalBytes;
144
145 - (GTMCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;
146 - (void)storeCachedResponse:(GTMCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;
147 - (void)removeCachedResponseForRequest:(NSURLRequest *)request;
148 - (void)removeAllCachedResponses;
149
150 // for unit testing
151 - (void)setReservationInterval:(NSTimeInterval)secs;
152 - (NSDictionary *)responses;
153 - (NSUInteger)totalDataSize;
154 @end
155
156 @interface GTMCookieStorage : NSObject <GTMCookieStorageProtocol> {
157 @private
158 // The cookie storage object manages an array holding cookies, but the array
159 // is allocated externally (it may be in a fetcher object or the static
160 // fetcher cookie array.) See the fetcher's setCookieStorageMethod:
161 // for allocation of this object and assignment of its cookies array.
162 NSMutableArray *cookies_;
163 }
164
165 // add all NSHTTPCookies in the supplied array to the storage array,
166 // replacing cookies in the storage array as appropriate
167 // Side effect: removes expired cookies from the storage array
168 - (void)setCookies:(NSArray *)newCookies;
169
170 // retrieve all cookies appropriate for the given URL, considering
171 // domain, path, cookie name, expiration, security setting.
172 // Side effect: removes expired cookies from the storage array
173 - (NSArray *)cookiesForURL:(NSURL *)theURL;
174
175 // return a cookie with the same name, domain, and path as the
176 // given cookie, or else return nil if none found
177 //
178 // Both the cookie being tested and all stored cookies should
179 // be valid (non-nil name, domains, paths)
180 - (NSHTTPCookie *)cookieMatchingCookie:(NSHTTPCookie *)cookie;
181
182 // remove any expired cookies, excluding cookies with nil expirations
183 - (void)removeExpiredCookies;
184
185 - (void)removeAllCookies;
186
187 @end
Something went wrong with that request. Please try again.