2010-03-28 16:56:39 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Low-level resource cache (interface)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef NETSURF_CONTENT_LLCACHE_H_
|
|
|
|
#define NETSURF_CONTENT_LLCACHE_H_
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "utils/errors.h"
|
2011-09-28 15:26:30 +04:00
|
|
|
#include "utils/nsurl.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
struct cert_chain;
|
2010-03-28 16:56:39 +04:00
|
|
|
struct fetch_multipart_data;
|
|
|
|
|
|
|
|
/** Handle for low-level cache object */
|
|
|
|
typedef struct llcache_handle llcache_handle;
|
|
|
|
|
|
|
|
/** POST data object for low-level cache requests */
|
2016-06-06 18:10:47 +03:00
|
|
|
typedef struct llcache_post_data {
|
2010-03-28 16:56:39 +04:00
|
|
|
enum {
|
|
|
|
LLCACHE_POST_URL_ENCODED,
|
|
|
|
LLCACHE_POST_MULTIPART
|
|
|
|
} type; /**< Type of POST data */
|
|
|
|
union {
|
|
|
|
char *urlenc; /**< URL encoded data */
|
|
|
|
struct fetch_multipart_data *multipart; /**< Multipart data */
|
|
|
|
} data; /**< POST data content */
|
|
|
|
} llcache_post_data;
|
|
|
|
|
|
|
|
/** Flags for low-level cache object retrieval */
|
2010-03-29 03:25:21 +04:00
|
|
|
enum llcache_retrieve_flag {
|
2010-04-06 01:35:38 +04:00
|
|
|
/* Note: We're permitted a maximum of 16 flags which must reside in the
|
2014-03-22 18:54:51 +04:00
|
|
|
* bottom 16 bits of the flags word. See hlcache.h for further details.
|
2010-04-06 01:35:38 +04:00
|
|
|
*/
|
2010-03-29 03:25:21 +04:00
|
|
|
/** Force a new fetch */
|
2014-03-22 18:54:51 +04:00
|
|
|
LLCACHE_RETRIEVE_FORCE_FETCH = (1 << 0),
|
2010-03-29 03:25:21 +04:00
|
|
|
/** Requested URL was verified */
|
2014-03-22 18:54:51 +04:00
|
|
|
LLCACHE_RETRIEVE_VERIFIABLE = (1 << 1),
|
2010-03-29 03:25:21 +04:00
|
|
|
/**< No error pages */
|
2011-09-04 10:28:09 +04:00
|
|
|
LLCACHE_RETRIEVE_NO_ERROR_PAGES = (1 << 2),
|
2010-04-06 01:35:38 +04:00
|
|
|
/**< Stream data (implies that object is not cacheable) */
|
2011-09-04 10:28:09 +04:00
|
|
|
LLCACHE_RETRIEVE_STREAM_DATA = (1 << 3)
|
2010-03-29 03:25:21 +04:00
|
|
|
};
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2019-08-03 23:13:19 +03:00
|
|
|
/** Low-level cache event types */
|
|
|
|
typedef enum {
|
2019-08-05 20:11:13 +03:00
|
|
|
LLCACHE_EVENT_GOT_CERTS, /**< SSL certificates arrived */
|
2019-08-03 23:13:19 +03:00
|
|
|
LLCACHE_EVENT_HAD_HEADERS, /**< Received all headers */
|
|
|
|
LLCACHE_EVENT_HAD_DATA, /**< Received some data */
|
|
|
|
LLCACHE_EVENT_DONE, /**< Finished fetching data */
|
|
|
|
|
|
|
|
LLCACHE_EVENT_ERROR, /**< An error occurred during fetch */
|
|
|
|
LLCACHE_EVENT_PROGRESS, /**< Fetch progress update */
|
|
|
|
|
|
|
|
LLCACHE_EVENT_REDIRECT /**< Fetch URL redirect occured */
|
|
|
|
} llcache_event_type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Low-level cache events.
|
|
|
|
*
|
|
|
|
* Lifetime of contained information is only for the duration of the event
|
|
|
|
* and must be copied if it is desirable to retain.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2020-02-23 19:06:52 +03:00
|
|
|
llcache_event_type type; /**< Type of event */
|
2019-08-03 23:13:19 +03:00
|
|
|
union {
|
|
|
|
struct {
|
2020-02-23 19:06:52 +03:00
|
|
|
const uint8_t *buf; /**< Buffer of data */
|
|
|
|
size_t len; /**< Byte length of buffer */
|
|
|
|
} data; /**< Received data */
|
2019-08-05 22:56:07 +03:00
|
|
|
struct {
|
2020-02-23 19:06:52 +03:00
|
|
|
nserror code; /**< The error code */
|
|
|
|
const char *msg; /**< Error message */
|
2019-08-05 22:56:07 +03:00
|
|
|
} error;
|
2020-02-23 19:06:52 +03:00
|
|
|
const char *progress_msg; /**< Progress message */
|
2019-08-03 23:13:19 +03:00
|
|
|
struct {
|
2020-02-23 19:06:52 +03:00
|
|
|
nsurl *from; /**< Redirect origin */
|
|
|
|
nsurl *to; /**< Redirect target */
|
|
|
|
} redirect; /**< Fetch URL redirect occured */
|
|
|
|
const struct cert_chain *chain; /**< Certificate chain */
|
|
|
|
} data; /**< Event data */
|
2019-08-03 23:13:19 +03:00
|
|
|
} llcache_event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Client callback for low-level cache events
|
|
|
|
*
|
|
|
|
* \param handle Handle for which event is issued
|
|
|
|
* \param event Event data
|
|
|
|
* \param pw Pointer to client-specific data
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise.
|
|
|
|
*/
|
|
|
|
typedef nserror (*llcache_handle_callback)(llcache_handle *handle,
|
|
|
|
const llcache_event *event, void *pw);
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/**
|
|
|
|
* Parameters to configure the low level cache backing store.
|
|
|
|
*/
|
|
|
|
struct llcache_store_parameters {
|
|
|
|
const char *path; /**< The path to the backing store */
|
|
|
|
|
|
|
|
size_t limit; /**< The backing store upper bound target size */
|
|
|
|
size_t hysteresis; /**< The hysteresis around the target size */
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameters to configure the low level cache.
|
|
|
|
*/
|
|
|
|
struct llcache_parameters {
|
|
|
|
size_t limit; /**< The target upper bound for the RAM cache size */
|
|
|
|
size_t hysteresis; /**< The hysteresis around the target size */
|
|
|
|
|
2014-11-28 22:17:42 +03:00
|
|
|
/** The minimum lifetime to consider sending objects to backing store.*/
|
2020-02-23 19:06:52 +03:00
|
|
|
int minimum_lifetime;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2014-11-28 22:17:42 +03:00
|
|
|
/** The minimum bandwidth to allow the backing store to
|
|
|
|
* use in bytes/second
|
|
|
|
*/
|
2020-02-23 19:06:52 +03:00
|
|
|
size_t minimum_bandwidth;
|
2014-11-28 22:17:42 +03:00
|
|
|
|
|
|
|
/** The maximum bandwidth to allow the backing store to use in
|
|
|
|
* bytes/second
|
|
|
|
*/
|
2020-02-23 19:06:52 +03:00
|
|
|
size_t maximum_bandwidth;
|
2014-11-28 22:17:42 +03:00
|
|
|
|
|
|
|
/** The time quantum over which to calculate the bandwidth values
|
|
|
|
*/
|
|
|
|
unsigned long time_quantum;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2015-11-11 00:51:54 +03:00
|
|
|
/** The number of fetches to attempt when timing out */
|
|
|
|
uint32_t fetch_attempts;
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
struct llcache_store_parameters store;
|
|
|
|
};
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Initialise the low-level cache
|
|
|
|
*
|
2014-11-09 15:50:30 +03:00
|
|
|
* \param parameters cache configuration parameters.
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise.
|
|
|
|
*/
|
2014-03-22 18:54:51 +04:00
|
|
|
nserror llcache_initialise(const struct llcache_parameters *parameters);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-04-27 10:48:09 +04:00
|
|
|
/**
|
|
|
|
* Finalise the low-level cache
|
|
|
|
*/
|
|
|
|
void llcache_finalise(void);
|
|
|
|
|
2011-03-13 21:26:46 +03:00
|
|
|
/**
|
2014-05-29 17:00:13 +04:00
|
|
|
* Cause the low-level cache to attempt to perform cleanup.
|
|
|
|
*
|
|
|
|
* No guarantees are made as to whether or not cleanups will take
|
2011-03-13 21:26:46 +03:00
|
|
|
* place and what, if any, space savings will be made.
|
2014-05-29 17:00:13 +04:00
|
|
|
*
|
|
|
|
* \param purge Any objects held in the cache that are safely removable will
|
|
|
|
* be freed regardless of the configured size limits.
|
2011-03-13 21:26:46 +03:00
|
|
|
*/
|
2014-05-29 17:00:13 +04:00
|
|
|
void llcache_clean(bool purge);
|
2011-03-13 21:26:46 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Retrieve a handle for a low-level cache object
|
|
|
|
*
|
|
|
|
* \param url URL of the object to fetch
|
|
|
|
* \param flags Object retrieval flags
|
|
|
|
* \param referer Referring URL, or NULL if none
|
|
|
|
* \param post POST data, or NULL for a GET request
|
|
|
|
* \param cb Client callback for events
|
|
|
|
* \param pw Pointer to client-specific data
|
|
|
|
* \param result Pointer to location to recieve cache handle
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2011-09-28 15:26:30 +04:00
|
|
|
nserror llcache_handle_retrieve(nsurl *url, uint32_t flags,
|
|
|
|
nsurl *referer, const llcache_post_data *post,
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_handle_callback cb, void *pw,
|
|
|
|
llcache_handle **result);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the callback associated with a low-level cache handle
|
|
|
|
*
|
|
|
|
* \param handle Handle to change callback of
|
|
|
|
* \param cb New callback
|
|
|
|
* \param pw Client data for new callback
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_handle_change_callback(llcache_handle *handle,
|
|
|
|
llcache_handle_callback cb, void *pw);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release a low-level cache handle
|
|
|
|
*
|
|
|
|
* \param handle Handle to release
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_handle_release(llcache_handle *handle);
|
|
|
|
|
2010-04-04 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* Clone a low-level cache handle, producing a new handle to
|
|
|
|
* the same fetch/content.
|
|
|
|
*
|
|
|
|
* \param handle Handle to clone
|
|
|
|
* \param result Pointer to location to receive cloned handle
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_handle_clone(llcache_handle *handle, llcache_handle **result);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abort a low-level fetch, informing all users of this action.
|
|
|
|
*
|
|
|
|
* \param handle Handle to abort
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_handle_abort(llcache_handle *handle);
|
|
|
|
|
2010-04-06 01:35:38 +04:00
|
|
|
/**
|
|
|
|
* Force a low-level cache handle into streaming mode
|
|
|
|
*
|
|
|
|
* \param handle Handle to stream
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_handle_force_stream(llcache_handle *handle);
|
|
|
|
|
2010-04-17 00:42:10 +04:00
|
|
|
/**
|
|
|
|
* Invalidate cache data for a low-level cache object
|
|
|
|
*
|
|
|
|
* \param handle Handle to invalidate
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_handle_invalidate_cache_data(llcache_handle *handle);
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Retrieve the post-redirect URL of a low-level cache object
|
|
|
|
*
|
|
|
|
* \param handle Handle to retrieve URL from
|
|
|
|
* \return Post-redirect URL of cache object
|
|
|
|
*/
|
2011-09-28 18:21:35 +04:00
|
|
|
nsurl *llcache_handle_get_url(const llcache_handle *handle);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve source data of a low-level cache object
|
|
|
|
*
|
|
|
|
* \param handle Handle to retrieve source data from
|
|
|
|
* \param size Pointer to location to receive byte length of data
|
|
|
|
* \return Pointer to source data
|
|
|
|
*/
|
|
|
|
const uint8_t *llcache_handle_get_source_data(const llcache_handle *handle,
|
|
|
|
size_t *size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a header value associated with a low-level cache object
|
|
|
|
*
|
|
|
|
* \param handle Handle to retrieve header from
|
|
|
|
* \param key Header name
|
|
|
|
* \return Header value, or NULL if header does not exist
|
|
|
|
*
|
|
|
|
* \todo Make the key an enumeration, to avoid needless string comparisons
|
2014-03-22 18:54:51 +04:00
|
|
|
* \todo Forcing the client to parse the header value seems wrong.
|
|
|
|
* Better would be to return the actual value part and an array of
|
2010-03-28 16:56:39 +04:00
|
|
|
* key-value pairs for any additional parameters.
|
2010-03-29 03:25:21 +04:00
|
|
|
* \todo Deal with multiple headers of the same key (e.g. Set-Cookie)
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2014-03-22 18:54:51 +04:00
|
|
|
const char *llcache_handle_get_header(const llcache_handle *handle,
|
2010-03-28 16:56:39 +04:00
|
|
|
const char *key);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the same underlying object is referenced by the given handles
|
|
|
|
*
|
|
|
|
* \param a First handle
|
|
|
|
* \param b Second handle
|
|
|
|
* \return True if handles reference the same object, false otherwise
|
|
|
|
*/
|
2014-03-22 18:54:51 +04:00
|
|
|
bool llcache_handle_references_same_object(const llcache_handle *a,
|
2010-03-28 16:56:39 +04:00
|
|
|
const llcache_handle *b);
|
|
|
|
|
|
|
|
#endif
|