/core/externals/update-engine/externals/gdata-objectivec-client/Source/HTTPFetcher/GTMHTTPFetchHistory.h

http://macfuse.googlecode.com/ · C++ Header · 175 lines · 69 code · 38 blank · 68 comment · 0 complexity · e21a47aa847710a16e58119223140abb MD5 · raw file

  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. // GTMHTTPFetchHistory.h
  17. //
  18. //
  19. // Users of the GTMHTTPFetcher class may optionally create and set a fetch
  20. // history object. The fetch history provides "memory" between subsequent
  21. // fetches, including:
  22. //
  23. // - For fetch responses with Etag headers, the fetch history
  24. // remembers the response headers. Future fetcher requests to the same URL
  25. // will be given an "If-None-Match" header, telling the server to return
  26. // a 304 Not Modified status if the response is unchanged, reducing the
  27. // server load and network traffic.
  28. //
  29. // - Optionally, the fetch history can cache the ETagged data that was returned
  30. // in the responses that contained Etag headers. If a later fetch
  31. // results in a 304 status, the fetcher will return the cached ETagged data
  32. // to the client along with a 200 status, hiding the 304.
  33. //
  34. // - The fetch history can track cookies.
  35. //
  36. #pragma once
  37. #import <Foundation/Foundation.h>
  38. #import "GTMHTTPFetcher.h"
  39. // default data cache size for when we're caching responses to handle "not
  40. // modified" errors for the client
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. extern const NSUInteger kGTMDefaultETaggedDataCacheMemoryCapacity;
  45. #ifdef __cplusplus
  46. }
  47. #endif
  48. // forward declarations
  49. @class GTMURLCache;
  50. @class GTMCookieStorage;
  51. @interface GTMHTTPFetchHistory : NSObject <GTMHTTPFetchHistoryProtocol> {
  52. @private
  53. GTMURLCache *etaggedDataCache_;
  54. BOOL shouldRememberETags_;
  55. BOOL shouldCacheETaggedData_; // if NO, then only headers are cached
  56. GTMCookieStorage *cookieStorage_;
  57. }
  58. // With caching enabled, previously-cached data will be returned instead of
  59. // 304 Not Modified responses when repeating a fetch of an URL that previously
  60. // included an ETag header in its response
  61. @property (assign) BOOL shouldRememberETags; // default: NO
  62. @property (assign) BOOL shouldCacheETaggedData; // default: NO
  63. // the default ETag data cache capacity is kGTMDefaultETaggedDataCacheMemoryCapacity
  64. @property (assign) NSUInteger memoryCapacity;
  65. @property (retain) GTMCookieStorage *cookieStorage;
  66. - (id)initWithMemoryCapacity:(NSUInteger)totalBytes
  67. shouldCacheETaggedData:(BOOL)shouldCacheETaggedData;
  68. - (void)updateRequest:(NSMutableURLRequest *)request isHTTPGet:(BOOL)isHTTPGet;
  69. - (void)clearETaggedDataCache;
  70. - (void)clearHistory;
  71. - (void)removeAllCookies;
  72. @end
  73. // GTMURLCache and GTMCachedURLResponse have interfaces similar to their
  74. // NSURLCache counterparts, in hopes that someday the NSURLCache versions
  75. // can be used. But in 10.5.8, those are not reliable enough except when
  76. // used with +setSharedURLCache. Our goal here is just to cache
  77. // responses for handling If-None-Match requests that return
  78. // "Not Modified" responses, not for replacing the general URL
  79. // caches.
  80. @interface GTMCachedURLResponse : NSObject {
  81. @private
  82. NSURLResponse *response_;
  83. NSData *data_;
  84. NSDate *useDate_; // date this response was last saved or used
  85. NSDate *reservationDate_; // date this response's ETag was used
  86. }
  87. @property (readonly) NSURLResponse* response;
  88. @property (readonly) NSData* data;
  89. // date the response was saved or last accessed
  90. @property (retain) NSDate *useDate;
  91. // date the response's ETag header was last used for a fetch request
  92. @property (retain) NSDate *reservationDate;
  93. - (id)initWithResponse:(NSURLResponse *)response data:(NSData *)data;
  94. @end
  95. @interface GTMURLCache : NSObject {
  96. NSMutableDictionary *responses_; // maps request URL to GTMCachedURLResponse
  97. NSUInteger memoryCapacity_; // capacity of NSDatas in the responses
  98. NSUInteger totalDataSize_; // sum of sizes of NSDatas of all responses
  99. NSTimeInterval reservationInterval_; // reservation expiration interval
  100. }
  101. @property (assign) NSUInteger memoryCapacity;
  102. - (id)initWithMemoryCapacity:(NSUInteger)totalBytes;
  103. - (GTMCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;
  104. - (void)storeCachedResponse:(GTMCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;
  105. - (void)removeCachedResponseForRequest:(NSURLRequest *)request;
  106. - (void)removeAllCachedResponses;
  107. // for unit testing
  108. - (void)setReservationInterval:(NSTimeInterval)secs;
  109. - (NSDictionary *)responses;
  110. - (NSUInteger)totalDataSize;
  111. @end
  112. @interface GTMCookieStorage : NSObject <GTMCookieStorageProtocol> {
  113. @private
  114. // The cookie storage object manages an array holding cookies, but the array
  115. // is allocated externally (it may be in a fetcher object or the static
  116. // fetcher cookie array.) See the fetcher's setCookieStorageMethod:
  117. // for allocation of this object and assignment of its cookies array.
  118. NSMutableArray *cookies_;
  119. }
  120. // add all NSHTTPCookies in the supplied array to the storage array,
  121. // replacing cookies in the storage array as appropriate
  122. // Side effect: removes expired cookies from the storage array
  123. - (void)setCookies:(NSArray *)newCookies;
  124. // retrieve all cookies appropriate for the given URL, considering
  125. // domain, path, cookie name, expiration, security setting.
  126. // Side effect: removes expired cookies from the storage array
  127. - (NSArray *)cookiesForURL:(NSURL *)theURL;
  128. // return a cookie with the same name, domain, and path as the
  129. // given cookie, or else return nil if none found
  130. //
  131. // Both the cookie being tested and all stored cookies should
  132. // be valid (non-nil name, domains, paths)
  133. - (NSHTTPCookie *)cookieMatchingCookie:(NSHTTPCookie *)cookie;
  134. // remove any expired cookies, excluding cookies with nil expirations
  135. - (void)removeExpiredCookies;
  136. - (void)removeAllCookies;
  137. @end