NetSurf
llcache.h
Go to the documentation of this file.
1/*
2 * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/** \file
20 * Low-level resource cache (interface)
21 */
22
23#ifndef NETSURF_CONTENT_LLCACHE_H_
24#define NETSURF_CONTENT_LLCACHE_H_
25
26#include <stdbool.h>
27#include <stddef.h>
28#include <stdint.h>
29
30#include "utils/errors.h"
31#include "utils/nsurl.h"
32
33struct cert_chain;
35
36/** Handle for low-level cache object */
38
39/** POST data object for low-level cache requests */
40typedef struct llcache_post_data {
41 enum {
44 } type; /**< Type of POST data */
45 union {
46 char *urlenc; /**< URL encoded data */
47 struct fetch_multipart_data *multipart; /**< Multipart data */
48 } data; /**< POST data content */
50
51/** Flags for low-level cache object retrieval */
53 /* Note: We're permitted a maximum of 16 flags which must reside in the
54 * bottom 16 bits of the flags word. See hlcache.h for further details.
55 */
56 /** Force a new fetch */
58 /** Requested URL was verified */
60 /**< No error pages */
62 /**< Stream data (implies that object is not cacheable) */
64};
65
66/** Low-level cache event types */
67typedef enum {
68 LLCACHE_EVENT_GOT_CERTS, /**< SSL certificates arrived */
69 LLCACHE_EVENT_HAD_HEADERS, /**< Received all headers */
70 LLCACHE_EVENT_HAD_DATA, /**< Received some data */
71 LLCACHE_EVENT_DONE, /**< Finished fetching data */
72
73 LLCACHE_EVENT_ERROR, /**< An error occurred during fetch */
74 LLCACHE_EVENT_PROGRESS, /**< Fetch progress update */
75
76 LLCACHE_EVENT_REDIRECT /**< Fetch URL redirect occured */
78
79/**
80 * Low-level cache events.
81 *
82 * Lifetime of contained information is only for the duration of the event
83 * and must be copied if it is desirable to retain.
84 */
85typedef struct {
86 llcache_event_type type; /**< Type of event */
87 union {
88 struct {
89 const uint8_t *buf; /**< Buffer of data */
90 size_t len; /**< Byte length of buffer */
91 } data; /**< Received data */
92 struct {
93 nserror code; /**< The error code */
94 const char *msg; /**< Error message */
95 } error;
96 const char *progress_msg; /**< Progress message */
97 struct {
98 nsurl *from; /**< Redirect origin */
99 nsurl *to; /**< Redirect target */
100 } redirect; /**< Fetch URL redirect occured */
101 const struct cert_chain *chain; /**< Certificate chain */
102 } data; /**< Event data */
104
105/**
106 * Client callback for low-level cache events
107 *
108 * \param handle Handle for which event is issued
109 * \param event Event data
110 * \param pw Pointer to client-specific data
111 * \return NSERROR_OK on success, appropriate error otherwise.
112 */
114 const llcache_event *event, void *pw);
115
116/**
117 * Parameters to configure the low level cache backing store.
118 */
120 const char *path; /**< The path to the backing store */
121
122 size_t limit; /**< The backing store upper bound target size */
123 size_t hysteresis; /**< The hysteresis around the target size */
124};
125
126/**
127 * Parameters to configure the low level cache.
128 */
130 size_t limit; /**< The target upper bound for the RAM cache size */
131 size_t hysteresis; /**< The hysteresis around the target size */
132
133 /** The minimum lifetime to consider sending objects to backing store.*/
135
136 /** The minimum bandwidth to allow the backing store to
137 * use in bytes/second
138 */
140
141 /** The maximum bandwidth to allow the backing store to use in
142 * bytes/second
143 */
145
146 /** The time quantum over which to calculate the bandwidth values
147 */
148 unsigned long time_quantum;
149
150 /** The number of fetches to attempt when timing out */
152
154};
155
156/**
157 * Initialise the low-level cache
158 *
159 * \param parameters cache configuration parameters.
160 * \return NSERROR_OK on success, appropriate error otherwise.
161 */
162nserror llcache_initialise(const struct llcache_parameters *parameters);
163
164/**
165 * Finalise the low-level cache
166 */
167void llcache_finalise(void);
168
169/**
170 * Cause the low-level cache to attempt to perform cleanup.
171 *
172 * No guarantees are made as to whether or not cleanups will take
173 * place and what, if any, space savings will be made.
174 *
175 * \param purge Any objects held in the cache that are safely removable will
176 * be freed regardless of the configured size limits.
177 */
178void llcache_clean(bool purge);
179
180/**
181 * Retrieve a handle for a low-level cache object
182 *
183 * \param url URL of the object to fetch
184 * \param flags Object retrieval flags
185 * \param referer Referring URL, or NULL if none
186 * \param post POST data, or NULL for a GET request
187 * \param cb Client callback for events
188 * \param pw Pointer to client-specific data
189 * \param result Pointer to location to recieve cache handle
190 * \return NSERROR_OK on success, appropriate error otherwise
191 */
192nserror llcache_handle_retrieve(nsurl *url, uint32_t flags,
193 nsurl *referer, const llcache_post_data *post,
194 llcache_handle_callback cb, void *pw,
196
197/**
198 * Change the callback associated with a low-level cache handle
199 *
200 * \param handle Handle to change callback of
201 * \param cb New callback
202 * \param pw Client data for new callback
203 * \return NSERROR_OK on success, appropriate error otherwise
204 */
206 llcache_handle_callback cb, void *pw);
207
208/**
209 * Release a low-level cache handle
210 *
211 * \param handle Handle to release
212 * \return NSERROR_OK on success, appropriate error otherwise
213 */
215
216/**
217 * Clone a low-level cache handle, producing a new handle to
218 * the same fetch/content.
219 *
220 * \param handle Handle to clone
221 * \param result Pointer to location to receive cloned handle
222 * \return NSERROR_OK on success, appropriate error otherwise
223 */
225
226/**
227 * Abort a low-level fetch, informing all users of this action.
228 *
229 * \param handle Handle to abort
230 * \return NSERROR_OK on success, appropriate error otherwise
231 */
233
234/**
235 * Force a low-level cache handle into streaming mode
236 *
237 * \param handle Handle to stream
238 * \return NSERROR_OK on success, appropriate error otherwise
239 */
241
242/**
243 * Invalidate cache data for a low-level cache object
244 *
245 * \param handle Handle to invalidate
246 * \return NSERROR_OK on success, appropriate error otherwise
247 */
249
250/**
251 * Retrieve the post-redirect URL of a low-level cache object
252 *
253 * \param handle Handle to retrieve URL from
254 * \return Post-redirect URL of cache object
255 */
257
258/**
259 * Retrieve source data of a low-level cache object
260 *
261 * \param handle Handle to retrieve source data from
262 * \param size Pointer to location to receive byte length of data
263 * \return Pointer to source data
264 */
265const uint8_t *llcache_handle_get_source_data(const llcache_handle *handle,
266 size_t *size);
267
268/**
269 * Retrieve a header value associated with a low-level cache object
270 *
271 * \param handle Handle to retrieve header from
272 * \param key Header name
273 * \return Header value, or NULL if header does not exist
274 *
275 * \todo Make the key an enumeration, to avoid needless string comparisons
276 * \todo Forcing the client to parse the header value seems wrong.
277 * Better would be to return the actual value part and an array of
278 * key-value pairs for any additional parameters.
279 * \todo Deal with multiple headers of the same key (e.g. Set-Cookie)
280 */
281const char *llcache_handle_get_header(const llcache_handle *handle,
282 const char *key);
283
284/**
285 * Determine if the same underlying object is referenced by the given handles
286 *
287 * \param a First handle
288 * \param b Second handle
289 * \return True if handles reference the same object, false otherwise
290 */
292 const llcache_handle *b);
293
294#endif
STATIC char result[100]
Definition: arexx.c:77
Error codes.
nserror
Enumeration of error codes.
Definition: errors.h:29
void llcache_clean(bool purge)
Cause the low-level cache to attempt to perform cleanup.
Definition: llcache.c:3725
nserror llcache_handle_clone(llcache_handle *handle, llcache_handle **result)
Clone a low-level cache handle, producing a new handle to the same fetch/content.
Definition: llcache.c:4082
nserror llcache_initialise(const struct llcache_parameters *parameters)
Initialise the low-level cache.
Definition: llcache.c:3891
nserror(* llcache_handle_callback)(llcache_handle *handle, const llcache_event *event, void *pw)
Client callback for low-level cache events.
Definition: llcache.h:113
nserror llcache_handle_abort(llcache_handle *handle)
Abort a low-level fetch, informing all users of this action.
Definition: llcache.c:4098
struct llcache_post_data llcache_post_data
POST data object for low-level cache requests.
void llcache_finalise(void)
Finalise the low-level cache.
Definition: llcache.c:3916
bool llcache_handle_references_same_object(const llcache_handle *a, const llcache_handle *b)
Determine if the same underlying object is referenced by the given handles.
Definition: llcache.c:4229
nsurl * llcache_handle_get_url(const llcache_handle *handle)
Retrieve the post-redirect URL of a low-level cache object.
Definition: llcache.c:4195
const uint8_t * llcache_handle_get_source_data(const llcache_handle *handle, size_t *size)
Retrieve source data of a low-level cache object.
Definition: llcache.c:4201
const char * llcache_handle_get_header(const llcache_handle *handle, const char *key)
Retrieve a header value associated with a low-level cache object.
Definition: llcache.c:4210
nserror llcache_handle_change_callback(llcache_handle *handle, llcache_handle_callback cb, void *pw)
Change the callback associated with a low-level cache handle.
Definition: llcache.c:4047
nserror llcache_handle_retrieve(nsurl *url, uint32_t flags, nsurl *referer, const llcache_post_data *post, llcache_handle_callback cb, void *pw, llcache_handle **result)
Retrieve a handle for a low-level cache object.
Definition: llcache.c:3990
nserror llcache_handle_force_stream(llcache_handle *handle)
Force a low-level cache handle into streaming mode.
Definition: llcache.c:4160
nserror llcache_handle_invalidate_cache_data(llcache_handle *handle)
Invalidate cache data for a low-level cache object.
Definition: llcache.c:4182
llcache_retrieve_flag
Flags for low-level cache object retrieval.
Definition: llcache.h:52
@ LLCACHE_RETRIEVE_STREAM_DATA
Definition: llcache.h:63
@ LLCACHE_RETRIEVE_NO_ERROR_PAGES
Stream data (implies that object is not cacheable)
Definition: llcache.h:61
@ LLCACHE_RETRIEVE_VERIFIABLE
Requested URL was verified.
Definition: llcache.h:59
@ LLCACHE_RETRIEVE_FORCE_FETCH
Force a new fetch.
Definition: llcache.h:57
nserror llcache_handle_release(llcache_handle *handle)
Release a low-level cache handle.
Definition: llcache.c:4058
llcache_event_type
Low-level cache event types.
Definition: llcache.h:67
@ LLCACHE_EVENT_DONE
Finished fetching data.
Definition: llcache.h:71
@ LLCACHE_EVENT_ERROR
An error occurred during fetch.
Definition: llcache.h:73
@ LLCACHE_EVENT_GOT_CERTS
SSL certificates arrived.
Definition: llcache.h:68
@ LLCACHE_EVENT_REDIRECT
Fetch URL redirect occured.
Definition: llcache.h:76
@ LLCACHE_EVENT_PROGRESS
Fetch progress update.
Definition: llcache.h:74
@ LLCACHE_EVENT_HAD_HEADERS
Received all headers.
Definition: llcache.h:69
@ LLCACHE_EVENT_HAD_DATA
Received some data.
Definition: llcache.h:70
NetSurf URL handling (interface).
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
X509 certificate chain.
Definition: ssl_certs.h:59
Fetch POST multipart data.
Definition: fetch.h:109
Low-level cache events.
Definition: llcache.h:85
const struct cert_chain * chain
Certificate chain.
Definition: llcache.h:101
nsurl * to
Redirect target.
Definition: llcache.h:99
const char * msg
Error message.
Definition: llcache.h:94
nserror code
The error code.
Definition: llcache.h:93
size_t len
Byte length of buffer.
Definition: llcache.h:90
const char * progress_msg
Progress message.
Definition: llcache.h:96
llcache_event_type type
Type of event.
Definition: llcache.h:86
nsurl * from
Redirect origin.
Definition: llcache.h:98
const uint8_t * buf
Buffer of data.
Definition: llcache.h:89
Handle to low-level cache object.
Definition: llcache.c:76
Parameters to configure the low level cache.
Definition: llcache.h:129
struct llcache_store_parameters store
Definition: llcache.h:153
int minimum_lifetime
The minimum lifetime to consider sending objects to backing store.
Definition: llcache.h:134
size_t hysteresis
The hysteresis around the target size.
Definition: llcache.h:131
size_t limit
The target upper bound for the RAM cache size.
Definition: llcache.h:130
size_t maximum_bandwidth
The maximum bandwidth to allow the backing store to use in bytes/second.
Definition: llcache.h:144
uint32_t fetch_attempts
The number of fetches to attempt when timing out.
Definition: llcache.h:151
size_t minimum_bandwidth
The minimum bandwidth to allow the backing store to use in bytes/second.
Definition: llcache.h:139
unsigned long time_quantum
The time quantum over which to calculate the bandwidth values.
Definition: llcache.h:148
POST data object for low-level cache requests.
Definition: llcache.h:40
struct fetch_multipart_data * multipart
Multipart data.
Definition: llcache.h:47
@ LLCACHE_POST_MULTIPART
Definition: llcache.h:43
@ LLCACHE_POST_URL_ENCODED
Definition: llcache.h:42
enum llcache_post_data::@122 type
Type of POST data.
union llcache_post_data::@123 data
POST data content.
char * urlenc
URL encoded data.
Definition: llcache.h:46
Parameters to configure the low level cache backing store.
Definition: llcache.h:119
size_t hysteresis
The hysteresis around the target size.
Definition: llcache.h:123
size_t limit
The backing store upper bound target size.
Definition: llcache.h:122
const char * path
The path to the backing store.
Definition: llcache.h:120