#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include "String.h"
#include "URLCache.h"
Go to the source code of this file.
|
| Class * | _URLCache (void) |
| |
| static bool | cacheControlDisablesCaching (const char *value) |
| | Returns true if the provided Cache-Control value disables caching.
|
| |
| static URLCachedResponse * | cachedResponseForRequest (URLCache *self, const URLRequest *request) |
| |
| static void | dealloc (Object *self) |
| |
| static void | evictIfNeeded (URLCache *self) |
| | Removes the oldest cached response while this cache exceeds its maximum size.
|
| |
| static const String * | headerValueForField (const Dictionary *headers, const char *field) |
| | Returns the value for the specified HTTP header field, case-insensitively.
|
| |
| static URLCache * | init (URLCache *self) |
| |
| static void | initialize (Class *clazz) |
| |
| static void | removeAllCachedResponses (URLCache *self) |
| |
| static void | removeCachedResponseForRequest (URLCache *self, const URLRequest *request) |
| |
| 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 void | setMaxSize (URLCache *self, size_t maxSize) |
| |
| static void | storeCachedResponseForRequest (URLCache *self, const URLRequest *request, const URLResponse *response, const Data *data) |
| |
| static bool | stringEqualsIgnoreCase (const char *a, const char *b) |
| | Tests two C strings for case-insensitive equality.
|
| |
| 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.
|
| |
◆ _Class
◆ URLCACHE_DEFAULT_MAX_SIZE
| #define URLCACHE_DEFAULT_MAX_SIZE (1024 * 1024) |
◆ _URLCache()
| Class * _URLCache |
( |
void |
| ) |
|
Definition at line 411 of file URLCache.c.
411 {
414
417 .name = "URLCache",
420 .interfaceOffset = offsetof(
URLCache, interface),
421 .interfaceSize = sizeof(URLCacheInterface),
423 });
424 });
425
426 return clazz;
427}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
static void initialize(Class *clazz)
#define do_once(once, block)
Executes the given block at most one time.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
A cache for HTTP responses.
◆ cacheControlDisablesCaching()
| static bool cacheControlDisablesCaching |
( |
const char * |
value | ) |
|
|
static |
Returns true if the provided Cache-Control value disables caching.
Definition at line 104 of file URLCache.c.
104 {
105
106 if (value == NULL) {
107 return false;
108 }
109
110 const char *cursor = value;
111 while (*cursor) {
112
113 while (*cursor == ' ' || *cursor == '\t' || *cursor == ',') {
114 cursor++;
115 }
116
117 const char *end = cursor;
118 while (*end && *end != ',') {
119 end++;
120 }
121
122 const char *
start = cursor;
123 while (
start < end && isspace((
unsigned char) *
start)) {
125 }
126
127 while (end >
start && isspace((
unsigned char) *(end - 1))) {
128 end--;
129 }
130
131 const size_t length = (size_t) (end -
start);
132 if (length > 0) {
133
135 return true;
136 }
137
139 return true;
140 }
141
143 if (strtol(
start + 8, NULL, 10) <= 0) {
144 return true;
145 }
146 }
147 }
148
149 cursor = end;
150 if (*cursor == ',') {
151 cursor++;
152 }
153 }
154
155 return false;
156}
static void start(Operation *self)
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.
◆ cachedResponseForRequest()
Definition at line 268 of file URLCache.c.
268 {
269
271 return NULL;
272 }
273
276 synchronized(self->
lock, {
278 if (cachedResponse) {
280 }
281 });
282
283 return cachedResponse;
284}
ident retain(ident obj)
Atomically increment the given Object's reference count.
static Dictionary * dictionary(void)
static ident objectForKey(const Dictionary *self, const ident key)
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 requestIsCacheable(const URLRequest *request)
Returns true if this request is cacheable.
Lock * lock
The lock protecting this cache.
Dictionary * responses
The cached responses, keyed by URLRequest.
◆ dealloc()
| static void dealloc |
( |
Object * |
self | ) |
|
|
static |
- See also
- Object::dealloc(Object *)
Definition at line 252 of file URLCache.c.
252 {
253
255
258
260}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
#define super(type, obj, method,...)
static void lock(Lock *self)
static void dealloc(Object *self)
Object is the root Class of The Objectively Class hierarchy.
◆ evictIfNeeded()
| static void evictIfNeeded |
( |
URLCache * |
self | ) |
|
|
static |
Removes the oldest cached response while this cache exceeds its maximum size.
Definition at line 213 of file URLCache.c.
213 {
214
216
218
220
223
224 for (
size_t i = 0; i < keys->
count; i++) {
225
228
229 if (candidate && (oldestResponse == NULL
231 oldestKey = key;
232 oldestResponse = candidate;
233 }
234 }
235
237
238 if (oldestKey == NULL || oldestResponse == NULL) {
239 break;
240 }
241
244 }
245}
static ident objectAtIndex(const Array *self, size_t index)
static Order compareTo(const Date *self, const Date *other)
static void removeObjectForKey(Dictionary *self, const ident key)
static Array * allKeys(const Dictionary *self)
size_t count
The count of elements.
size_t count
The count of elements.
size_t currentSize
The current cached body size.
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.
◆ headerValueForField()
| static const String * headerValueForField |
( |
const Dictionary * |
headers, |
|
|
const char * |
field |
|
) |
| |
|
static |
Returns the value for the specified HTTP header field, case-insensitively.
Definition at line 79 of file URLCache.c.
79 {
80
81 if (headers && field) {
82
84 for (
size_t i = 0; i < keys->
count; i++) {
85
88
91 return value;
92 }
93 }
94
96 }
97
98 return NULL;
99}
static bool stringEqualsIgnoreCase(const char *a, const char *b)
Tests two C strings for case-insensitive equality.
char * chars
The backing null-terminated UTF-8 encoded character array.
◆ init()
Definition at line 290 of file URLCache.c.
290 {
291
293 if (self) {
296
299
301 }
302
303 return self;
304}
#define alloc(type)
Allocate and initialize and instance of type.
#define URLCACHE_DEFAULT_MAX_SIZE
static URLCache * init(URLCache *self)
◆ initialize()
| static void initialize |
( |
Class * |
clazz | ) |
|
|
static |
- See also
- Class::initialize(Class *)
Definition at line 395 of file URLCache.c.
395 {
396
398
405}
static URLCachedResponse * cachedResponseForRequest(URLCache *self, const URLRequest *request)
static void removeCachedResponseForRequest(URLCache *self, const URLRequest *request)
static void storeCachedResponseForRequest(URLCache *self, const URLRequest *request, const URLResponse *response, const Data *data)
static void setMaxSize(URLCache *self, size_t maxSize)
static void removeAllCachedResponses(URLCache *self)
ident interface
The interface of the Class.
Object * init(Object *self)
Initializes this Object.
void removeAllCachedResponses(URLCache *self)
Removes all cached responses.
◆ removeAllCachedResponses()
| static void removeAllCachedResponses |
( |
URLCache * |
self | ) |
|
|
static |
Definition at line 310 of file URLCache.c.
310 {
311
312 synchronized(self->
lock, {
315 });
316}
static void removeAllObjects(Array *self)
◆ removeCachedResponseForRequest()
| static void removeCachedResponseForRequest |
( |
URLCache * |
self, |
|
|
const URLRequest * |
request |
|
) |
| |
|
static |
Definition at line 322 of file URLCache.c.
322 {
323
324 synchronized(self->
lock, {
327 if (cachedResponse) {
330 }
331 });
332}
◆ requestIsCacheable()
| static bool requestIsCacheable |
( |
const URLRequest * |
request | ) |
|
|
static |
Returns true if this request is cacheable.
Definition at line 161 of file URLCache.c.
161 {
162
164 return false;
165 }
166
170 break;
171 default:
172 return false;
173 }
174
177 return false;
178 }
179
182 return false;
183 }
184
185 return true;
186}
static const String * headerValueForField(const Dictionary *headers, const char *field)
Returns the value for the specified HTTP header field, case-insensitively.
static bool cacheControlDisablesCaching(const char *value)
Returns true if the provided Cache-Control value disables caching.
◆ responseIsCacheable()
| static bool responseIsCacheable |
( |
const URLResponse * |
response | ) |
|
|
static |
Returns true if this response is cacheable.
Definition at line 191 of file URLCache.c.
191 {
192
194 return false;
195 }
196
199 return false;
200 }
201
204 return false;
205 }
206
207 return true;
208}
Dictionary * httpHeaders
The HTTP response headers.
int httpStatusCode
The HTTP response status code.
◆ setMaxSize()
| static void setMaxSize |
( |
URLCache * |
self, |
|
|
size_t |
maxSize |
|
) |
| |
|
static |
Definition at line 338 of file URLCache.c.
338 {
339
340 synchronized(self->
lock, {
342
346 } else {
348 }
349 });
350}
static void evictIfNeeded(URLCache *self)
Removes the oldest cached response while this cache exceeds its maximum size.
◆ storeCachedResponseForRequest()
Definition at line 356 of file URLCache.c.
357 {
358
360 return;
361 }
362
364 if (cachedResponse == NULL) {
365 return;
366 }
367
368 synchronized(self->
lock, {
370
373 if (existing) {
376 }
377
381
384 }
385 });
386
388}
static Object * copy(const Object *self)
static void setObjectForKey(Dictionary *self, const ident obj, const ident key)
static bool responseIsCacheable(const URLResponse *response)
Returns true if this response is cacheable.
static URLCachedResponse * initWithResponseData(URLCachedResponse *self, const URLResponse *response, const Data *data)
◆ stringEqualsIgnoreCase()
| static bool stringEqualsIgnoreCase |
( |
const char * |
a, |
|
|
const char * |
b |
|
) |
| |
|
static |
Tests two C strings for case-insensitive equality.
Definition at line 40 of file URLCache.c.
40 {
41
42 if (a == NULL || b == NULL) {
43 return false;
44 }
45
46 while (*a && *b) {
47 if (tolower((unsigned char) *a) != tolower((unsigned char) *b)) {
48 return false;
49 }
50
51 a++;
52 b++;
53 }
54
55 return *a == 0 && *b == 0;
56}
◆ stringEqualsIgnoreCaseN()
| static bool stringEqualsIgnoreCaseN |
( |
const char * |
a, |
|
|
const char * |
b, |
|
|
size_t |
length |
|
) |
| |
|
static |
Tests the first length characters of two C strings for case-insensitive equality.
Definition at line 61 of file URLCache.c.
61 {
62
63 if (a == NULL || b == NULL) {
64 return false;
65 }
66
67 for (size_t i = 0; i < length; i++) {
68 if (tolower((unsigned char) a[i]) != tolower((unsigned char) b[i])) {
69 return false;
70 }
71 }
72
73 return true;
74}