31#define _Class _URLCache
33#define URLCACHE_DEFAULT_MAX_SIZE (1024 * 1024)
42 if (a == NULL || b == NULL) {
47 if (tolower((
unsigned char) *a) != tolower((
unsigned char) *b)) {
55 return *a == 0 && *b == 0;
63 if (a == NULL || b == NULL) {
67 for (
size_t i = 0; i < length; i++) {
68 if (tolower((
unsigned char) a[i]) != tolower((
unsigned char) b[i])) {
81 if (headers && field) {
84 for (
size_t i = 0; i < keys->
count; i++) {
110 const char *cursor = value;
113 while (*cursor ==
' ' || *cursor ==
'\t' || *cursor ==
',') {
117 const char *end = cursor;
118 while (*end && *end !=
',') {
122 const char *
start = cursor;
123 while (
start < end && isspace((
unsigned char) *
start)) {
127 while (end >
start && isspace((
unsigned char) *(end - 1))) {
131 const size_t length = (size_t) (end -
start);
143 if (strtol(
start + 8, NULL, 10) <= 0) {
150 if (*cursor ==
',') {
224 for (
size_t i = 0; i < keys->
count; i++) {
229 if (candidate && (oldestResponse == NULL
232 oldestResponse = candidate;
238 if (oldestKey == NULL || oldestResponse == NULL) {
262#pragma mark - URLCache
276 synchronized(self->
lock, {
278 if (cachedResponse) {
283 return cachedResponse;
312 synchronized(self->
lock, {
324 synchronized(self->
lock, {
327 if (cachedResponse) {
340 synchronized(self->
lock, {
364 if (cachedResponse == NULL) {
368 synchronized(self->
lock, {
390#pragma mark - Class lifecycle
420 .interfaceOffset = offsetof(
URLCache, interface),
421 .interfaceSize =
sizeof(URLCacheInterface),
static ident objectAtIndex(const Array *self, size_t index)
static void removeAllObjects(Array *self)
static Object * copy(const Object *self)
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Class * _initialize(const ClassDef *def)
Initializes the given Class.
ident retain(ident obj)
Atomically increment the given Object's reference count.
#define alloc(type)
Allocate and initialize and instance of type.
#define super(type, obj, method,...)
static Order compareTo(const Date *self, const Date *other)
static void setObjectForKey(Dictionary *self, const ident obj, const ident key)
static void removeObjectForKey(Dictionary *self, const ident key)
static Dictionary * dictionary(void)
static Array * allKeys(const Dictionary *self)
static ident objectForKey(const Dictionary *self, const ident key)
static void lock(Lock *self)
static void start(Operation *self)
static int request(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, Data **out_data)
void * ident
The identity type, similar to Objective-C id.
static bool stringEqualsIgnoreCase(const char *a, const char *b)
Tests two C strings for case-insensitive equality.
static void evictIfNeeded(URLCache *self)
Removes the oldest cached response while this cache exceeds its maximum size.
#define URLCACHE_DEFAULT_MAX_SIZE
static bool stringEqualsIgnoreCaseN(const char *a, const char *b, size_t length)
Tests the first length characters of two C strings for case-insensitive equality.
static bool requestIsCacheable(const URLRequest *request)
Returns true if this request is cacheable.
static bool responseIsCacheable(const URLResponse *response)
Returns true if this response is cacheable.
static URLCachedResponse * cachedResponseForRequest(URLCache *self, const URLRequest *request)
static void removeCachedResponseForRequest(URLCache *self, const URLRequest *request)
static URLCache * init(URLCache *self)
static void storeCachedResponseForRequest(URLCache *self, const URLRequest *request, const URLResponse *response, const Data *data)
static void dealloc(Object *self)
static void setMaxSize(URLCache *self, size_t maxSize)
static void removeAllCachedResponses(URLCache *self)
static const String * headerValueForField(const Dictionary *headers, const char *field)
Returns the value for the specified HTTP header field, case-insensitively.
static void initialize(Class *clazz)
static bool cacheControlDisablesCaching(const char *value)
Returns true if the provided Cache-Control value disables caching.
A cache for HTTP responses.
static URLCachedResponse * initWithResponseData(URLCachedResponse *self, const URLResponse *response, const Data *data)
#define do_once(once, block)
Executes the given block at most one time.
size_t count
The count of elements.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
size_t count
The count of elements.
Object is the root Class of The Objectively Class hierarchy.
Object * init(Object *self)
Initializes this Object.
char * chars
The backing null-terminated UTF-8 encoded character array.
A cache for HTTP responses.
Lock * lock
The lock protecting this cache.
size_t currentSize
The current cached body size.
void removeAllCachedResponses(URLCache *self)
Removes all cached responses.
Dictionary * responses
The cached responses, keyed by URLRequest.
size_t maxSize
The maximum cached body size.
Date * timestamp
The time this response was cached.
size_t size
The cached body size.
A protocol-agnostic abstraction for requesting resources via URLs.
A protocol-agnostic abstraction for URLSessionTask responses.
Dictionary * httpHeaders
The HTTP response headers.
int httpStatusCode
The HTTP response status code.