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
|
2010-03-28 20:00:54 +04:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2010-03-28 16:56:39 +04:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-10-16 12:48:09 +04:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
*
|
2014-03-22 18:54:51 +04:00
|
|
|
* Low-level resource cache implementation
|
|
|
|
*
|
|
|
|
* This is the implementation of the low level cache. This cache
|
2016-11-20 18:38:14 +03:00
|
|
|
* stores source objects in memory and may use a persistent backing
|
2014-03-22 18:54:51 +04:00
|
|
|
* store to extend their lifetime.
|
|
|
|
*
|
|
|
|
* \todo fix writeout conditions and ordering.
|
|
|
|
*
|
|
|
|
* \todo instrument and (auto)tune
|
|
|
|
*
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-11-27 00:29:13 +03:00
|
|
|
#include <stdint.h>
|
2010-03-28 16:56:39 +04:00
|
|
|
#include <string.h>
|
2014-12-28 01:48:37 +03:00
|
|
|
#include <strings.h>
|
2014-11-27 00:29:13 +03:00
|
|
|
#include <nsutils/time.h>
|
2020-02-23 19:06:52 +03:00
|
|
|
#include <nsutils/base64.h>
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2017-01-21 17:20:55 +03:00
|
|
|
#include "netsurf/inttypes.h"
|
2014-05-13 19:37:49 +04:00
|
|
|
#include "utils/config.h"
|
2014-01-25 00:19:20 +04:00
|
|
|
#include "utils/corestrings.h"
|
2010-03-29 10:27:37 +04:00
|
|
|
#include "utils/log.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "utils/messages.h"
|
2011-09-28 15:26:30 +04:00
|
|
|
#include "utils/nsurl.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "utils/utils.h"
|
2014-05-15 04:18:52 +04:00
|
|
|
#include "utils/time.h"
|
2019-06-10 23:56:23 +03:00
|
|
|
#include "utils/http.h"
|
2021-03-26 02:31:37 +03:00
|
|
|
#include "utils/nsoption.h"
|
2016-05-30 13:23:32 +03:00
|
|
|
#include "netsurf/misc.h"
|
2014-10-16 12:48:09 +04:00
|
|
|
#include "desktop/gui_internal.h"
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
#include "content/fetch.h"
|
|
|
|
#include "content/backing_store.h"
|
|
|
|
#include "content/urldb.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* State of a low-level cache object fetch.
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
typedef enum {
|
|
|
|
LLCACHE_FETCH_INIT, /**< Initial state, before fetch */
|
|
|
|
LLCACHE_FETCH_HEADERS, /**< Fetching headers */
|
|
|
|
LLCACHE_FETCH_DATA, /**< Fetching object data */
|
|
|
|
LLCACHE_FETCH_COMPLETE /**< Fetch completed */
|
|
|
|
} llcache_fetch_state;
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Type of low-level cache object.
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
typedef struct llcache_object llcache_object;
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Handle to low-level cache object.
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
struct llcache_handle {
|
|
|
|
llcache_object *object; /**< Pointer to associated object */
|
|
|
|
|
|
|
|
llcache_handle_callback cb; /**< Client callback */
|
|
|
|
void *pw; /**< Client data */
|
|
|
|
|
|
|
|
llcache_fetch_state state; /**< Last known state of object fetch */
|
|
|
|
size_t bytes; /**< Last reported byte count */
|
|
|
|
};
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Low-level cache object user record.
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
typedef struct llcache_object_user {
|
2010-12-16 00:15:49 +03:00
|
|
|
llcache_handle *handle; /**< Handle data for client */
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2018-04-22 01:24:41 +03:00
|
|
|
bool iterator_target; /**< This is the iterator target */
|
2010-03-28 16:56:39 +04:00
|
|
|
bool queued_for_delete; /**< This user is queued for deletion */
|
|
|
|
|
|
|
|
struct llcache_object_user *prev; /**< Previous in list */
|
|
|
|
struct llcache_object_user *next; /**< Next in list */
|
|
|
|
} llcache_object_user;
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Low-level cache object fetch context.
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
typedef struct {
|
|
|
|
uint32_t flags; /**< Fetch flags */
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl *referer; /**< Referring URL, or NULL if none */
|
2014-02-21 15:31:39 +04:00
|
|
|
llcache_post_data *post; /**< POST data, or NULL for GET */
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
struct fetch *fetch; /**< Fetch handle for this object */
|
|
|
|
|
|
|
|
llcache_fetch_state state; /**< Current state of object fetch */
|
2010-04-11 17:58:12 +04:00
|
|
|
|
2010-04-17 00:00:10 +04:00
|
|
|
uint32_t redirect_count; /**< Count of redirects followed */
|
|
|
|
|
2015-11-11 00:51:54 +03:00
|
|
|
uint32_t retries_remaining; /**< Number of times to retry on timeout */
|
|
|
|
|
2018-04-22 07:09:24 +03:00
|
|
|
bool hsts_in_use; /**< Whether HSTS applies to this fetch */
|
|
|
|
|
2010-04-17 00:00:10 +04:00
|
|
|
bool tried_with_auth; /**< Whether we've tried with auth */
|
2011-03-03 03:40:50 +03:00
|
|
|
|
2022-11-05 15:35:42 +03:00
|
|
|
bool tried_with_tls_downgrade; /**< Whether we've tried TLS 1.2 */
|
2013-01-05 03:13:23 +04:00
|
|
|
|
2018-04-22 01:54:04 +03:00
|
|
|
bool tainted_tls; /**< Whether the TLS transport is tainted */
|
2010-03-28 16:56:39 +04:00
|
|
|
} llcache_fetch_ctx;
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Validation control.
|
|
|
|
*/
|
2011-02-25 00:22:24 +03:00
|
|
|
typedef enum {
|
|
|
|
LLCACHE_VALIDATE_FRESH, /**< Only revalidate if not fresh */
|
|
|
|
LLCACHE_VALIDATE_ALWAYS, /**< Always revalidate */
|
|
|
|
LLCACHE_VALIDATE_ONCE /**< Revalidate once only */
|
|
|
|
} llcache_validate;
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
2018-04-22 01:24:41 +03:00
|
|
|
* Cache control value for invalid age.
|
2014-11-23 18:43:03 +03:00
|
|
|
*/
|
2014-03-22 18:54:51 +04:00
|
|
|
#define INVALID_AGE -1
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/** Cache control data */
|
|
|
|
typedef struct {
|
|
|
|
time_t req_time; /**< Time of request */
|
|
|
|
time_t res_time; /**< Time of response */
|
2014-03-22 18:54:51 +04:00
|
|
|
time_t fin_time; /**< Time of request completion */
|
2010-03-28 16:56:39 +04:00
|
|
|
time_t date; /**< Date: response header */
|
|
|
|
time_t expires; /**< Expires: response header */
|
|
|
|
int age; /**< Age: response header */
|
|
|
|
int max_age; /**< Max-Age Cache-control parameter */
|
2011-02-25 00:22:24 +03:00
|
|
|
llcache_validate no_cache; /**< No-Cache Cache-control parameter */
|
2010-03-28 16:56:39 +04:00
|
|
|
char *etag; /**< Etag: response header */
|
|
|
|
time_t last_modified; /**< Last-Modified: response header */
|
|
|
|
} llcache_cache_control;
|
|
|
|
|
|
|
|
/** Representation of a fetch header */
|
|
|
|
typedef struct {
|
|
|
|
char *name; /**< Header name */
|
|
|
|
char *value; /**< Header value */
|
|
|
|
} llcache_header;
|
|
|
|
|
2018-04-22 01:24:41 +03:00
|
|
|
/** Current status of an object's data */
|
2014-03-22 18:54:51 +04:00
|
|
|
typedef enum {
|
|
|
|
LLCACHE_STATE_RAM = 0, /**< source data is stored in RAM only */
|
|
|
|
LLCACHE_STATE_DISC, /**< source data is stored on disc */
|
|
|
|
} llcache_store_state;
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Low-level cache object
|
|
|
|
*
|
|
|
|
* \todo Consider whether a list is a sane container.
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
struct llcache_object {
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_object *prev; /**< Previous in list */
|
|
|
|
llcache_object *next; /**< Next in list */
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
nsurl *url; /**< Post-redirect URL for object */
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/** \todo We need a generic dynamic buffer object */
|
2014-03-22 18:54:51 +04:00
|
|
|
uint8_t *source_data; /**< Source data for object */
|
|
|
|
size_t source_len; /**< Byte length of source data */
|
|
|
|
size_t source_alloc; /**< Allocated size of source buffer */
|
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
struct cert_chain *chain; /**< Certificate chain from the fetch */
|
2019-11-30 14:41:19 +03:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_store_state store_state; /**< where the data for the object is stored */
|
|
|
|
|
|
|
|
llcache_object_user *users; /**< List of users */
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_fetch_ctx fetch; /**< Fetch context for object */
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_cache_control cache; /**< Cache control data for object */
|
|
|
|
llcache_object *candidate; /**< Object to use, if fetch determines
|
|
|
|
* that it is still fresh
|
|
|
|
*/
|
|
|
|
uint32_t candidate_count; /**< Count of objects this is a
|
|
|
|
* candidate for
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_header *headers; /**< Fetch headers */
|
|
|
|
size_t num_headers; /**< Number of fetch headers */
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2016-11-20 18:38:14 +03:00
|
|
|
/* Instrumentation. These elements are strictly for information
|
|
|
|
* to improve the cache performance and to provide performance
|
2018-04-22 01:24:41 +03:00
|
|
|
* metrics. The values are non-authoritative and must not be used to
|
2014-03-22 18:54:51 +04:00
|
|
|
* determine object lifetime etc.
|
|
|
|
*/
|
|
|
|
time_t last_used; /**< time the last user was removed from the object */
|
2010-03-28 16:56:39 +04:00
|
|
|
};
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Core llcache control context.
|
|
|
|
*/
|
2011-09-10 04:55:39 +04:00
|
|
|
struct llcache_s {
|
|
|
|
/** Head of the low-level cached object list */
|
|
|
|
llcache_object *cached_objects;
|
|
|
|
|
|
|
|
/** Head of the low-level uncached object list */
|
|
|
|
llcache_object *uncached_objects;
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/** The target upper bound for the RAM cache size */
|
2011-09-10 04:55:39 +04:00
|
|
|
uint32_t limit;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2015-11-11 00:51:54 +03:00
|
|
|
/** The number of fetch attempts we make when timing out */
|
|
|
|
uint32_t fetch_attempts;
|
|
|
|
|
2014-12-03 23:13:10 +03:00
|
|
|
/** Whether or not our users are caught up */
|
|
|
|
bool all_caught_up;
|
|
|
|
|
|
|
|
|
|
|
|
/* backing store elements */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The minimum lifetime to consider sending objects to backing
|
|
|
|
* store.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
int minimum_lifetime;
|
|
|
|
|
2014-12-03 23:13:10 +03:00
|
|
|
/**
|
|
|
|
* The time over which to apply the bandwidth calculations in ms
|
|
|
|
*/
|
2014-11-28 22:17:42 +03:00
|
|
|
unsigned long time_quantum;
|
|
|
|
|
2014-12-03 23:13:10 +03:00
|
|
|
/**
|
|
|
|
* The minimum bandwidth to allow the backing store to use in
|
2014-11-28 22:17:42 +03:00
|
|
|
* bytes/second. Below this the backing store will be
|
|
|
|
* disabled.
|
|
|
|
*/
|
|
|
|
size_t minimum_bandwidth;
|
|
|
|
|
2014-12-03 23:13:10 +03:00
|
|
|
/**
|
|
|
|
* The maximum bandwidth to allow the backing store to use in
|
|
|
|
* bytes/second
|
2014-11-28 22:17:42 +03:00
|
|
|
*/
|
|
|
|
size_t maximum_bandwidth;
|
|
|
|
|
2014-12-03 23:13:10 +03:00
|
|
|
/**
|
|
|
|
* Total number of bytes written to backing store.
|
|
|
|
*/
|
|
|
|
uint64_t total_written;
|
|
|
|
|
|
|
|
/**
|
2016-11-20 18:38:14 +03:00
|
|
|
* Total number of milliseconds taken to write to backing store.
|
2014-12-03 23:13:10 +03:00
|
|
|
*/
|
|
|
|
uint64_t total_elapsed;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/** low level cache state */
|
|
|
|
static struct llcache_s *llcache = NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* forward referenced callback function */
|
|
|
|
static void llcache_fetch_callback(const fetch_msg *msg, void *p);
|
2014-12-03 23:13:10 +03:00
|
|
|
|
2014-06-03 20:00:23 +04:00
|
|
|
/* forward referenced catch up function */
|
|
|
|
static void llcache_users_not_caught_up(void);
|
2011-09-28 15:26:30 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* Low-level cache internals *
|
|
|
|
******************************************************************************/
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
2014-12-03 23:13:10 +03:00
|
|
|
* Create a new object user.
|
2012-04-28 17:24:09 +04:00
|
|
|
*
|
2014-12-03 23:13:10 +03:00
|
|
|
* \param cb Callback routine.
|
|
|
|
* \param pw Private data for callback.
|
|
|
|
* \param user Pointer to location to receive result.
|
2012-04-28 17:24:09 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
static nserror llcache_object_user_new(llcache_handle_callback cb, void *pw,
|
|
|
|
llcache_object_user **user)
|
|
|
|
{
|
|
|
|
llcache_handle *h;
|
|
|
|
llcache_object_user *u;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
h = calloc(1, sizeof(llcache_handle));
|
2015-06-24 12:31:13 +03:00
|
|
|
if (h == NULL) {
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_NOMEM;
|
2015-06-24 12:31:13 +03:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
u = calloc(1, sizeof(llcache_object_user));
|
|
|
|
if (u == NULL) {
|
|
|
|
free(h);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
h->cb = cb;
|
|
|
|
h->pw = pw;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
u->handle = h;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG,
|
|
|
|
"Created user %p (%p, %p, %p)", u, h, (void *) cb, pw);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
*user = u;
|
2010-10-28 02:12:00 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
2010-10-28 02:12:00 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* Destroy an object user
|
|
|
|
*
|
|
|
|
* \param user User to destroy
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*
|
|
|
|
* \pre User is not attached to an object
|
|
|
|
*/
|
|
|
|
static nserror llcache_object_user_destroy(llcache_object_user *user)
|
2010-09-16 23:47:15 +04:00
|
|
|
{
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Destroyed user %p", user);
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
assert(user->next == NULL);
|
|
|
|
assert(user->prev == NULL);
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (user->handle != NULL)
|
|
|
|
free(user->handle);
|
2011-02-24 13:14:50 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
free(user);
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2010-09-16 23:47:15 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* Remove a user from a low-level cache object
|
|
|
|
*
|
|
|
|
* \param object Object to remove user from
|
|
|
|
* \param user User to remove
|
|
|
|
* \return NSERROR_OK.
|
|
|
|
*/
|
2014-02-21 15:31:39 +04:00
|
|
|
static nserror llcache_object_remove_user(llcache_object *object,
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object_user *user)
|
|
|
|
{
|
|
|
|
assert(user != NULL);
|
|
|
|
assert(object != NULL);
|
|
|
|
assert(object->users != NULL);
|
|
|
|
assert(user->handle == NULL || user->handle->object == object);
|
|
|
|
assert((user->prev != NULL) || (object->users == user));
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (user == object->users)
|
|
|
|
object->users = user->next;
|
|
|
|
else
|
|
|
|
user->prev->next = user->next;
|
|
|
|
|
|
|
|
if (user->next != NULL)
|
|
|
|
user->next->prev = user->prev;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
user->next = user->prev = NULL;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* record the time the last user was removed from the object */
|
|
|
|
if (object->users == NULL) {
|
|
|
|
object->last_used = time(NULL);
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Removing user %p from %p", user, object);
|
2010-09-16 23:47:15 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* Iterate the users of an object, calling their callbacks.
|
|
|
|
*
|
|
|
|
* \param object The object to iterate
|
|
|
|
* \param event The event to pass to the callback.
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise.
|
|
|
|
*/
|
|
|
|
static nserror llcache_send_event_to_users(llcache_object *object,
|
|
|
|
llcache_event *event)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
llcache_object_user *user, *next_user;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
user = object->users;
|
|
|
|
while (user != NULL) {
|
2019-08-02 18:57:55 +03:00
|
|
|
bool was_target = user->iterator_target;
|
2012-04-28 17:24:09 +04:00
|
|
|
user->iterator_target = true;
|
2011-09-10 04:55:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
error = user->handle->cb(user->handle, event,
|
|
|
|
user->handle->pw);
|
2011-09-10 04:55:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
next_user = user->next;
|
2011-09-28 15:26:30 +04:00
|
|
|
|
2019-08-02 18:57:55 +03:00
|
|
|
user->iterator_target = was_target;
|
2011-09-28 15:26:30 +04:00
|
|
|
|
2019-08-04 12:46:17 +03:00
|
|
|
if (user->queued_for_delete && !was_target) {
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_user_destroy(user);
|
|
|
|
}
|
2011-09-28 15:26:30 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (error != NSERROR_OK)
|
|
|
|
break;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
user = next_user;
|
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* Create a new low-level cache object
|
|
|
|
*
|
|
|
|
* \param url URL of object to create
|
|
|
|
* \param result Pointer to location to receive result
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
static nserror llcache_object_new(nsurl *url, llcache_object **result)
|
2010-04-27 10:48:09 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object *obj = calloc(1, sizeof(llcache_object));
|
|
|
|
if (obj == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
2010-04-27 10:48:09 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Created object %p (%s)", obj, nsurl_access(url));
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
obj->url = nsurl_ref(url);
|
2010-04-27 10:48:09 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
*result = obj;
|
2010-04-27 10:48:09 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-04-27 10:48:09 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* Clone a POST data object
|
|
|
|
*
|
|
|
|
* \param orig Object to clone
|
|
|
|
* \param clone Pointer to location to receive clone
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2014-02-21 15:31:39 +04:00
|
|
|
static nserror llcache_post_data_clone(const llcache_post_data *orig,
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_post_data **clone)
|
|
|
|
{
|
|
|
|
llcache_post_data *post_clone;
|
2010-04-27 10:48:09 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
post_clone = calloc(1, sizeof(llcache_post_data));
|
|
|
|
if (post_clone == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
2010-04-27 10:48:09 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
post_clone->type = orig->type;
|
2010-04-27 10:48:09 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Deep-copy the type-specific data */
|
|
|
|
if (orig->type == LLCACHE_POST_URL_ENCODED) {
|
|
|
|
post_clone->data.urlenc = strdup(orig->data.urlenc);
|
|
|
|
if (post_clone->data.urlenc == NULL) {
|
|
|
|
free(post_clone);
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_NOMEM;
|
2010-04-27 10:48:09 +04:00
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
} else {
|
|
|
|
post_clone->data.multipart = fetch_multipart_data_clone(
|
|
|
|
orig->data.multipart);
|
|
|
|
if (post_clone->data.multipart == NULL) {
|
|
|
|
free(post_clone);
|
2010-04-27 10:48:09 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2010-04-27 10:48:09 +04:00
|
|
|
}
|
2011-09-10 04:55:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
*clone = post_clone;
|
2011-09-28 15:26:30 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
2010-04-27 10:48:09 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* Split a fetch header into name and value
|
|
|
|
*
|
2018-08-12 14:50:57 +03:00
|
|
|
* HTTP header splitting according to grammar defined in RFC7230 section 3.2
|
|
|
|
* https://tools.ietf.org/html/rfc7230#section-3.2
|
|
|
|
*
|
|
|
|
* This implementation is non conformant in that it:
|
|
|
|
* - includes carrige return and newline in whitespace (3.2.3)
|
|
|
|
* - allows whitespace before and after the field-name token (3.2.4)
|
|
|
|
* - does not handle obsolete line folding (3.2.4)
|
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* \param data Header string
|
|
|
|
* \param len Byte length of header
|
|
|
|
* \param name Pointer to location to receive header name
|
|
|
|
* \param value Pointer to location to receive header value
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2014-02-21 15:31:39 +04:00
|
|
|
static nserror llcache_fetch_split_header(const uint8_t *data, size_t len,
|
2012-04-28 17:24:09 +04:00
|
|
|
char **name, char **value)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
char *n, *v;
|
|
|
|
const uint8_t *colon;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-08 18:25:15 +04:00
|
|
|
/* Strip leading whitespace from name */
|
|
|
|
while (data[0] == ' ' || data[0] == '\t' ||
|
|
|
|
data[0] == '\r' || data[0] == '\n') {
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Find colon */
|
|
|
|
colon = (const uint8_t *) strchr((const char *) data, ':');
|
|
|
|
if (colon == NULL) {
|
|
|
|
/* Failed, assume a key with no value */
|
2014-03-08 18:25:15 +04:00
|
|
|
colon = data + strlen((const char *)data);
|
|
|
|
|
|
|
|
/* Strip trailing whitespace from name */
|
|
|
|
while ((colon > data) &&
|
|
|
|
(colon[-1] == ' ' || colon[-1] == '\t' ||
|
|
|
|
colon[-1] == '\r' || colon[-1] == '\n')) {
|
|
|
|
colon--;
|
|
|
|
}
|
|
|
|
n = strndup((const char *) data, colon - data);
|
2012-04-28 17:24:09 +04:00
|
|
|
if (n == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
v = strdup("");
|
|
|
|
if (v == NULL) {
|
|
|
|
free(n);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Split header into name & value */
|
|
|
|
|
|
|
|
/* Strip trailing whitespace from name */
|
2014-02-21 15:31:39 +04:00
|
|
|
while (colon > data && (colon[-1] == ' ' ||
|
|
|
|
colon[-1] == '\t' || colon[-1] == '\r' ||
|
2012-04-28 17:24:09 +04:00
|
|
|
colon[-1] == '\n'))
|
|
|
|
colon--;
|
|
|
|
|
|
|
|
n = strndup((const char *) data, colon - data);
|
|
|
|
if (n == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
/* Find colon again */
|
|
|
|
while (*colon != ':') {
|
|
|
|
colon++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip over colon and any subsequent whitespace */
|
|
|
|
do {
|
|
|
|
colon++;
|
2014-02-21 15:31:39 +04:00
|
|
|
} while (*colon == ' ' || *colon == '\t' ||
|
2012-04-28 17:24:09 +04:00
|
|
|
*colon == '\r' || *colon == '\n');
|
|
|
|
|
|
|
|
/* Strip trailing whitespace from value */
|
2014-02-21 15:31:39 +04:00
|
|
|
while (len > 0 && (data[len - 1] == ' ' ||
|
|
|
|
data[len - 1] == '\t' ||
|
2012-04-28 17:24:09 +04:00
|
|
|
data[len - 1] == '\r' ||
|
|
|
|
data[len - 1] == '\n')) {
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
v = strndup((const char *) colon, len - (colon - data));
|
|
|
|
if (v == NULL) {
|
|
|
|
free(n);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
*name = n;
|
|
|
|
*value = v;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
2018-08-12 14:50:57 +03:00
|
|
|
* parse cache control header value
|
2012-04-28 17:24:09 +04:00
|
|
|
*
|
2018-08-12 14:50:57 +03:00
|
|
|
* \param object Object to parse header for
|
|
|
|
* \param value header value
|
2012-04-28 17:24:09 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2018-08-12 14:50:57 +03:00
|
|
|
static nserror
|
|
|
|
llcache_fetch_parse_cache_control(llcache_object *object, char *value)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2019-06-10 23:56:23 +03:00
|
|
|
http_cache_control *cc;
|
|
|
|
nserror error;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2019-06-10 23:56:23 +03:00
|
|
|
error = http_parse_cache_control(value, &cc);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
/* Ignore parse errors */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2019-06-10 22:51:31 +03:00
|
|
|
|
2019-06-10 23:56:23 +03:00
|
|
|
if (http_cache_control_no_cache(cc) ||
|
|
|
|
http_cache_control_no_store(cc)) {
|
|
|
|
/**
|
|
|
|
* \todo When we get a disk cache we should
|
|
|
|
* distinguish between these two.
|
|
|
|
*/
|
|
|
|
object->cache.no_cache = LLCACHE_VALIDATE_ALWAYS;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2019-06-11 00:36:52 +03:00
|
|
|
if (http_cache_control_has_max_age(cc)) {
|
|
|
|
object->cache.max_age = http_cache_control_max_age(cc);
|
|
|
|
}
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2019-06-10 23:56:23 +03:00
|
|
|
http_cache_control_destroy(cc);
|
2016-04-17 01:27:38 +03:00
|
|
|
|
2018-08-12 14:50:57 +03:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update cache control from appropriate header
|
|
|
|
*
|
|
|
|
* \param object Object to parse header for
|
|
|
|
* \param name header name
|
|
|
|
* \param value header value
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
llcache_fetch_header_cache_control(llcache_object *object,
|
|
|
|
char *name,
|
|
|
|
char *value)
|
|
|
|
{
|
|
|
|
nserror res;
|
|
|
|
size_t name_len;
|
2012-04-28 17:24:09 +04:00
|
|
|
|
2018-08-12 14:50:57 +03:00
|
|
|
/* Parse cache headers to populate cache control data */
|
|
|
|
name_len = strlen(name);
|
|
|
|
|
|
|
|
switch (name_len) {
|
|
|
|
case 3:
|
|
|
|
if (strcasecmp(name, "Age") == 0) {
|
|
|
|
/* extract Age header */
|
|
|
|
if ('0' <= *value && *value <= '9') {
|
|
|
|
object->cache.age = atoi(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
if (strcasecmp(name, "Date") == 0) {
|
|
|
|
/* extract Date header */
|
|
|
|
res = nsc_strntimet(value,
|
|
|
|
strlen(value),
|
|
|
|
&object->cache.date);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
NSLOG(llcache, INFO,
|
|
|
|
"Processing Date header value \"%s\" returned %d",
|
|
|
|
value, res);
|
|
|
|
}
|
|
|
|
} else if (strcasecmp(name, "ETag") == 0) {
|
|
|
|
/* extract ETag header */
|
|
|
|
free(object->cache.etag);
|
|
|
|
object->cache.etag = strdup(value);
|
|
|
|
if (object->cache.etag == NULL) {
|
|
|
|
NSLOG(llcache, INFO,
|
|
|
|
"No memory to duplicate ETag");
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
if (strcasecmp(name, "Expires") == 0) {
|
|
|
|
/* process Expires header value */
|
|
|
|
res = nsc_strntimet(value,
|
|
|
|
strlen(value),
|
|
|
|
&object->cache.expires);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
NSLOG(llcache, INFO,
|
|
|
|
"Processing Expires header value \"%s\" returned %d",
|
|
|
|
value, res);
|
|
|
|
object->cache.expires = (time_t)0x7fffffff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 13:
|
|
|
|
if (strcasecmp(name, "Cache-Control") == 0) {
|
|
|
|
/* parse Cache-Control header value */
|
|
|
|
llcache_fetch_parse_cache_control(object,value);
|
|
|
|
} else if (strcasecmp(name, "Last-Modified") == 0) {
|
|
|
|
/* parse Last-Modified header value */
|
|
|
|
nsc_strntimet(value,
|
|
|
|
strlen(value),
|
|
|
|
&object->cache.last_modified);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
|
2014-02-21 15:31:39 +04:00
|
|
|
return NSERROR_OK;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Destroy headers.
|
|
|
|
*
|
|
|
|
* \param object The object to destroy headers within.
|
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static inline void llcache_destroy_headers(llcache_object *object)
|
2010-04-04 16:41:19 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
while (object->num_headers > 0) {
|
|
|
|
object->num_headers--;
|
|
|
|
|
|
|
|
free(object->headers[object->num_headers].name);
|
|
|
|
free(object->headers[object->num_headers].value);
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
free(object->headers);
|
|
|
|
object->headers = NULL;
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Invalidate cache control data.
|
|
|
|
*
|
|
|
|
* \param object The object to invalidate cache control for.
|
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static inline void llcache_invalidate_cache_control_data(llcache_object *object)
|
2010-04-04 16:41:19 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
free(object->cache.etag);
|
|
|
|
memset(&(object->cache), 0, sizeof(llcache_cache_control));
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
object->cache.age = INVALID_AGE;
|
|
|
|
object->cache.max_age = INVALID_AGE;
|
|
|
|
}
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* Process a fetch header
|
|
|
|
*
|
|
|
|
* \param object Object being fetched
|
|
|
|
* \param data Header string
|
|
|
|
* \param len Byte length of header
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2014-02-21 15:31:39 +04:00
|
|
|
static nserror llcache_fetch_process_header(llcache_object *object,
|
2012-04-28 17:24:09 +04:00
|
|
|
const uint8_t *data, size_t len)
|
|
|
|
{
|
2018-08-12 14:50:57 +03:00
|
|
|
nserror res;
|
2012-04-28 17:24:09 +04:00
|
|
|
char *name, *value;
|
|
|
|
llcache_header *temp;
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* \note The headers for multiple HTTP responses may be
|
|
|
|
* delivered to us if the fetch layer receives a 401 response
|
|
|
|
* for which it has authentication credentials. This will
|
|
|
|
* result in a silent re-request after which we'll receive the
|
|
|
|
* actual response headers for the object we want to fetch
|
|
|
|
* (assuming that the credentials were correct of course)
|
2012-04-28 17:24:09 +04:00
|
|
|
*
|
2014-02-21 15:31:39 +04:00
|
|
|
* Therefore, if the header is an HTTP response start marker, then we
|
|
|
|
* must discard any headers we've read so far, reset the cache data
|
2012-04-28 17:24:09 +04:00
|
|
|
* that we might have computed, and start again.
|
|
|
|
*/
|
|
|
|
/** \todo Properly parse the response line */
|
|
|
|
if (strncmp((const char *) data, "HTTP/", SLEN("HTTP/")) == 0) {
|
|
|
|
time_t req_time = object->cache.req_time;
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2010-09-16 23:47:15 +04:00
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-04-06 01:35:38 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Restore request time, so we compute object's age correctly */
|
|
|
|
object->cache.req_time = req_time;
|
2010-04-06 01:35:38 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_destroy_headers(object);
|
2010-04-06 01:35:38 +04:00
|
|
|
}
|
|
|
|
|
2018-08-12 14:50:57 +03:00
|
|
|
/* Set fetch response time if not already set */
|
|
|
|
if (object->cache.res_time == 0) {
|
|
|
|
object->cache.res_time = time(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse header into name-value pair */
|
|
|
|
res = llcache_fetch_split_header(data, len, &name, &value);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return res;
|
2013-05-02 01:15:24 +04:00
|
|
|
}
|
2010-04-06 01:35:38 +04:00
|
|
|
|
2014-03-08 18:25:15 +04:00
|
|
|
/* deal with empty header */
|
|
|
|
if (name[0] == 0) {
|
|
|
|
free(name);
|
|
|
|
free(value);
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:50:57 +03:00
|
|
|
/* update cache control data from header */
|
|
|
|
res = llcache_fetch_header_cache_control(object, name, value);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
free(name);
|
|
|
|
free(value);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Append header data to the object's headers array */
|
2018-08-12 14:50:57 +03:00
|
|
|
temp = realloc(object->headers,
|
|
|
|
(object->num_headers + 1) * sizeof(llcache_header));
|
2012-04-28 17:24:09 +04:00
|
|
|
if (temp == NULL) {
|
|
|
|
free(name);
|
|
|
|
free(value);
|
|
|
|
return NSERROR_NOMEM;
|
2010-04-17 00:42:10 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
object->headers = temp;
|
2010-04-17 00:42:10 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
object->headers[object->num_headers].name = name;
|
|
|
|
object->headers[object->num_headers].value = value;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
object->num_headers++;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2021-03-26 02:31:37 +03:00
|
|
|
/**
|
|
|
|
* construct a Referer header appropriate for the request
|
|
|
|
*
|
|
|
|
* \param url The url being navigated to
|
|
|
|
* \param referer The referring url
|
|
|
|
* \param header_out A pointer to receive the header. The buffer must
|
|
|
|
* be freed by the caller.
|
|
|
|
* \return NSERROR_OK and \a header_out updated on success else error code
|
|
|
|
*/
|
|
|
|
static nserror get_referer_header(nsurl *url, nsurl *referer, char **header_out)
|
|
|
|
{
|
|
|
|
nserror res = NSERROR_INVALID;
|
|
|
|
lwc_string *ref_scheme;
|
|
|
|
lwc_string *scheme;
|
|
|
|
bool match;
|
|
|
|
bool match1;
|
|
|
|
bool match2;
|
|
|
|
char *header;
|
|
|
|
|
|
|
|
/* Determine whether to send the Referer header */
|
|
|
|
if (!nsoption_bool(send_referer)) {
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
scheme = nsurl_get_component(url, NSURL_SCHEME);
|
|
|
|
if (scheme == NULL) {
|
|
|
|
return NSERROR_BAD_URL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref_scheme = nsurl_get_component(referer, NSURL_SCHEME);
|
|
|
|
if (ref_scheme == NULL) {
|
|
|
|
/* referer has no scheme so no header */
|
|
|
|
lwc_string_unref(scheme);
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* User permits us to send the header
|
|
|
|
* Only send it if:
|
|
|
|
* 1) The fetch and referer schemes match
|
|
|
|
* or 2) The fetch is https and the referer is http
|
|
|
|
*
|
|
|
|
* This ensures that referer information is only sent
|
|
|
|
* across schemes in the special case of an https
|
|
|
|
* request from a page served over http. The inverse
|
|
|
|
* (https -> http) should not send the referer (15.1.3)
|
|
|
|
*/
|
|
|
|
if (lwc_string_isequal(scheme, ref_scheme,
|
|
|
|
&match) != lwc_error_ok) {
|
|
|
|
match = false;
|
|
|
|
}
|
|
|
|
if (lwc_string_isequal(scheme, corestring_lwc_https,
|
|
|
|
&match1) != lwc_error_ok) {
|
|
|
|
match1 = false;
|
|
|
|
}
|
|
|
|
if (lwc_string_isequal(ref_scheme, corestring_lwc_http,
|
|
|
|
&match2) != lwc_error_ok) {
|
|
|
|
match2 = false;
|
|
|
|
}
|
|
|
|
if (match == true || (match1 == true && match2 == true)) {
|
|
|
|
const size_t len = SLEN("Referer: ") +
|
|
|
|
nsurl_length(referer) + 1;
|
|
|
|
|
|
|
|
header = malloc(len);
|
|
|
|
if (header == NULL) {
|
|
|
|
res = NSERROR_NOMEM;
|
|
|
|
} else {
|
|
|
|
snprintf(header, len, "Referer: %s",
|
|
|
|
nsurl_access(referer));
|
|
|
|
|
|
|
|
*header_out = header;
|
|
|
|
res = NSERROR_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lwc_string_unref(scheme);
|
|
|
|
lwc_string_unref(ref_scheme);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* (Re)fetch an object
|
|
|
|
*
|
2018-04-22 01:24:41 +03:00
|
|
|
* Sets up headers and attempts to start an actual fetch from the
|
2015-06-24 12:31:13 +03:00
|
|
|
* fetchers system updating the llcache object with the new fetch on
|
2016-11-20 18:38:14 +03:00
|
|
|
* successful start.
|
2015-06-24 12:31:13 +03:00
|
|
|
*
|
2014-11-23 18:43:03 +03:00
|
|
|
* \pre The fetch parameters in object->fetch must be populated
|
|
|
|
*
|
2015-06-24 12:31:13 +03:00
|
|
|
* \param object Object to refetch
|
2012-04-28 17:24:09 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
2014-02-21 15:31:39 +04:00
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static nserror llcache_object_refetch(llcache_object *object)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
const char *urlenc = NULL;
|
|
|
|
struct fetch_multipart_data *multipart = NULL;
|
|
|
|
char **headers = NULL;
|
|
|
|
int header_idx = 0;
|
2015-06-24 12:31:13 +03:00
|
|
|
nserror res;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (object->fetch.post != NULL) {
|
2014-11-23 18:43:03 +03:00
|
|
|
if (object->fetch.post->type == LLCACHE_POST_URL_ENCODED) {
|
2012-04-28 17:24:09 +04:00
|
|
|
urlenc = object->fetch.post->data.urlenc;
|
2014-11-23 18:43:03 +03:00
|
|
|
} else {
|
2012-04-28 17:24:09 +04:00
|
|
|
multipart = object->fetch.post->data.multipart;
|
2014-11-23 18:43:03 +03:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2021-03-26 02:31:37 +03:00
|
|
|
/* Generate headers */
|
|
|
|
headers = malloc(4 * sizeof(char *));
|
2015-06-24 12:31:13 +03:00
|
|
|
if (headers == NULL) {
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_NOMEM;
|
2015-06-24 12:31:13 +03:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2021-03-26 02:31:37 +03:00
|
|
|
/* cache-control header for etag */
|
2012-04-28 17:24:09 +04:00
|
|
|
if (object->cache.etag != NULL) {
|
2014-02-21 15:31:39 +04:00
|
|
|
const size_t len = SLEN("If-None-Match: ") +
|
2012-04-28 17:24:09 +04:00
|
|
|
strlen(object->cache.etag) + 1;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
headers[header_idx] = malloc(len);
|
|
|
|
if (headers[header_idx] == NULL) {
|
|
|
|
free(headers);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
snprintf(headers[header_idx], len, "If-None-Match: %s",
|
|
|
|
object->cache.etag);
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
header_idx++;
|
|
|
|
}
|
2014-11-23 18:43:03 +03:00
|
|
|
|
2021-03-26 02:31:37 +03:00
|
|
|
/* cache-control header for modification time */
|
2019-08-02 14:22:30 +03:00
|
|
|
if (object->cache.last_modified != 0) {
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Maximum length of an RFC 1123 date is 29 bytes */
|
|
|
|
const size_t len = SLEN("If-Modified-Since: ") + 29 + 1;
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
headers[header_idx] = malloc(len);
|
|
|
|
if (headers[header_idx] == NULL) {
|
|
|
|
while (--header_idx >= 0)
|
|
|
|
free(headers[header_idx]);
|
|
|
|
free(headers);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(headers[header_idx], len, "If-Modified-Since: %s",
|
2019-08-02 14:22:30 +03:00
|
|
|
rfc1123_date(object->cache.last_modified));
|
2012-04-28 17:24:09 +04:00
|
|
|
|
|
|
|
header_idx++;
|
2010-12-16 00:15:49 +03:00
|
|
|
}
|
2021-03-26 02:31:37 +03:00
|
|
|
|
|
|
|
/* Referer header */
|
|
|
|
if (object->fetch.referer != NULL) {
|
|
|
|
if (get_referer_header(object->url,
|
|
|
|
object->fetch.referer,
|
|
|
|
&headers[header_idx]) == NSERROR_OK) {
|
|
|
|
header_idx++;
|
|
|
|
}
|
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
headers[header_idx] = NULL;
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Reset cache control data */
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
|
|
|
object->cache.req_time = time(NULL);
|
2014-03-22 18:54:51 +04:00
|
|
|
object->cache.fin_time = object->cache.req_time;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Reset fetch state */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_INIT;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Re-fetching %p", object);
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Kick off fetch */
|
2015-06-24 12:31:13 +03:00
|
|
|
res = fetch_start(object->url,
|
|
|
|
object->fetch.referer,
|
|
|
|
llcache_fetch_callback,
|
|
|
|
object,
|
|
|
|
object->fetch.flags & LLCACHE_RETRIEVE_NO_ERROR_PAGES,
|
|
|
|
urlenc,
|
|
|
|
multipart,
|
|
|
|
object->fetch.flags & LLCACHE_RETRIEVE_VERIFIABLE,
|
|
|
|
object->fetch.tried_with_tls_downgrade,
|
|
|
|
(const char **)headers,
|
|
|
|
&object->fetch.fetch);
|
2012-04-28 17:24:09 +04:00
|
|
|
|
|
|
|
/* Clean up cache-control headers */
|
2015-06-24 12:31:13 +03:00
|
|
|
while (--header_idx >= 0) {
|
2012-04-28 17:24:09 +04:00
|
|
|
free(headers[header_idx]);
|
2015-06-24 12:31:13 +03:00
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
free(headers);
|
|
|
|
|
2015-06-24 12:31:13 +03:00
|
|
|
return res;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Kick-off a fetch for an object
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2014-11-23 18:43:03 +03:00
|
|
|
* \pre object::url must contain the URL to fetch
|
|
|
|
* \pre If there is a freshness validation candidate,
|
|
|
|
* object::candidate and object::cache must be filled in
|
|
|
|
* \pre There must not be a fetch in progress for \a object
|
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* \param object Object to fetch
|
|
|
|
* \param flags Fetch flags
|
|
|
|
* \param referer Referring URL, or NULL for none
|
|
|
|
* \param post POST data, or NULL for GET
|
|
|
|
* \param redirect_count Number of redirects followed so far
|
2018-04-22 07:09:24 +03:00
|
|
|
* \param hsts_in_use Whether HSTS applies to this fetch
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
2010-04-14 14:56:44 +04:00
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static nserror llcache_object_fetch(llcache_object *object, uint32_t flags,
|
|
|
|
nsurl *referer, const llcache_post_data *post,
|
2018-04-22 07:09:24 +03:00
|
|
|
uint32_t redirect_count, bool hsts_in_use)
|
2010-04-14 14:56:44 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
nserror error;
|
|
|
|
nsurl *referer_clone = NULL;
|
|
|
|
llcache_post_data *post_clone = NULL;
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Starting fetch for %p", object);
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (post != NULL) {
|
|
|
|
error = llcache_post_data_clone(post, &post_clone);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
}
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (referer != NULL)
|
|
|
|
referer_clone = nsurl_ref(referer);
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
object->fetch.flags = flags;
|
|
|
|
object->fetch.referer = referer_clone;
|
|
|
|
object->fetch.post = post_clone;
|
|
|
|
object->fetch.redirect_count = redirect_count;
|
2015-11-11 00:51:54 +03:00
|
|
|
object->fetch.retries_remaining = llcache->fetch_attempts;
|
2018-04-22 07:09:24 +03:00
|
|
|
object->fetch.hsts_in_use = hsts_in_use;
|
2010-12-16 01:59:19 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return llcache_object_refetch(object);
|
2010-04-14 14:56:44 +04:00
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Destroy a low-level cache object
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* \pre Object is detached from cache list
|
|
|
|
* \pre Object has no users
|
|
|
|
* \pre Object is not a candidate (i.e. object::candidate_count == 0)
|
2014-11-23 18:43:03 +03:00
|
|
|
*
|
|
|
|
* \param object Object to destroy
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static nserror llcache_object_destroy(llcache_object *object)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
size_t i;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Destroying object %p, %s", object,
|
|
|
|
nsurl_access(object->url));
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
cert_chain_free(object->chain);
|
2019-11-30 14:41:19 +03:00
|
|
|
|
2014-11-23 21:21:56 +03:00
|
|
|
if (object->source_data != NULL) {
|
|
|
|
if (object->store_state == LLCACHE_STATE_DISC) {
|
|
|
|
guit->llcache->release(object->url, BACKING_STORE_NONE);
|
|
|
|
} else {
|
|
|
|
free(object->source_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
nsurl_unref(object->url);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (object->fetch.fetch != NULL) {
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
2011-10-05 22:05:41 +04:00
|
|
|
}
|
2010-04-14 18:53:21 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (object->fetch.referer != NULL)
|
|
|
|
nsurl_unref(object->fetch.referer);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (object->fetch.post != NULL) {
|
|
|
|
if (object->fetch.post->type == LLCACHE_POST_URL_ENCODED) {
|
|
|
|
free(object->fetch.post->data.urlenc);
|
|
|
|
} else {
|
|
|
|
fetch_multipart_data_destroy(
|
|
|
|
object->fetch.post->data.multipart);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
free(object->fetch.post);
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
free(object->cache.etag);
|
|
|
|
|
|
|
|
for (i = 0; i < object->num_headers; i++) {
|
|
|
|
free(object->headers[i].name);
|
|
|
|
free(object->headers[i].value);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
free(object->headers);
|
|
|
|
|
|
|
|
free(object);
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Add a low-level cache object to a cache list
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* \param object Object to add
|
|
|
|
* \param list List to add to
|
|
|
|
* \return NSERROR_OK
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static nserror llcache_object_add_to_list(llcache_object *object,
|
|
|
|
llcache_object **list)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
object->prev = NULL;
|
|
|
|
object->next = *list;
|
2010-08-29 21:41:54 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (*list != NULL)
|
|
|
|
(*list)->prev = object;
|
|
|
|
*list = object;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
2013-01-07 22:45:29 +04:00
|
|
|
* Determine the remaining lifetime of a cache object using the
|
2012-04-28 17:24:09 +04:00
|
|
|
*
|
2014-11-09 15:50:30 +03:00
|
|
|
* \param cd cache control data.
|
|
|
|
* \return The length of time remaining for the object or 0 if expired.
|
2012-04-28 17:24:09 +04:00
|
|
|
*/
|
2013-01-07 22:45:29 +04:00
|
|
|
static int
|
|
|
|
llcache_object_rfc2616_remaining_lifetime(const llcache_cache_control *cd)
|
2012-04-28 17:24:09 +04:00
|
|
|
{
|
|
|
|
int current_age, freshness_lifetime;
|
|
|
|
time_t now = time(NULL);
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Calculate staleness of cached object as per RFC 2616 13.2.3/13.2.4 */
|
|
|
|
current_age = max(0, (cd->res_time - cd->date));
|
|
|
|
current_age = max(current_age, (cd->age == INVALID_AGE) ? 0 : cd->age);
|
|
|
|
current_age += cd->res_time - cd->req_time + now - cd->res_time;
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Determine freshness lifetime of this object */
|
2017-09-04 16:31:12 +03:00
|
|
|
if (cd->max_age != INVALID_AGE) {
|
2012-04-28 17:24:09 +04:00
|
|
|
freshness_lifetime = cd->max_age;
|
2017-09-04 16:31:12 +03:00
|
|
|
} else if (cd->expires != 0) {
|
2012-04-28 17:24:09 +04:00
|
|
|
freshness_lifetime = cd->expires - cd->date;
|
2017-09-04 16:31:12 +03:00
|
|
|
} else if (cd->last_modified != 0) {
|
2012-04-28 17:24:09 +04:00
|
|
|
freshness_lifetime = (now - cd->last_modified) / 10;
|
2017-09-04 16:31:12 +03:00
|
|
|
} else {
|
2012-04-28 17:24:09 +04:00
|
|
|
freshness_lifetime = 0;
|
2017-09-04 16:31:12 +03:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "%d:%d", freshness_lifetime, current_age);
|
2013-01-07 22:45:29 +04:00
|
|
|
|
|
|
|
if ((cd->no_cache == LLCACHE_VALIDATE_FRESH) &&
|
|
|
|
(freshness_lifetime > current_age)) {
|
|
|
|
/* object was not forbidden from being returned from
|
|
|
|
* the cache unvalidated (i.e. the response contained
|
|
|
|
* a no-cache directive)
|
|
|
|
*
|
|
|
|
* The object current age is within the freshness lifetime.
|
|
|
|
*/
|
|
|
|
return freshness_lifetime - current_age;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* object has no remaining lifetime */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if an object is still fresh
|
|
|
|
*
|
|
|
|
* \param object Object to consider
|
|
|
|
* \return True if object is still fresh, false otherwise
|
|
|
|
*/
|
|
|
|
static bool llcache_object_is_fresh(const llcache_object *object)
|
|
|
|
{
|
|
|
|
int remaining_lifetime;
|
|
|
|
const llcache_cache_control *cd = &object->cache;
|
|
|
|
|
|
|
|
remaining_lifetime = llcache_object_rfc2616_remaining_lifetime(cd);
|
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "%p: (%d > 0 || %d != %d)", object,
|
2013-01-07 22:45:29 +04:00
|
|
|
remaining_lifetime,
|
2015-05-28 19:06:18 +03:00
|
|
|
object->fetch.state, LLCACHE_FETCH_COMPLETE);
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2010-09-16 22:58:32 +04:00
|
|
|
/* The object is fresh if:
|
2013-01-07 22:45:29 +04:00
|
|
|
* - it was not forbidden from being returned from the cache
|
|
|
|
* unvalidated.
|
2010-09-16 22:58:32 +04:00
|
|
|
*
|
2014-02-21 15:31:39 +04:00
|
|
|
* - it has remaining lifetime or still being fetched.
|
2010-09-16 22:58:32 +04:00
|
|
|
*/
|
2013-01-07 22:45:29 +04:00
|
|
|
return ((cd->no_cache == LLCACHE_VALIDATE_FRESH) &&
|
|
|
|
((remaining_lifetime > 0) ||
|
|
|
|
(object->fetch.state != LLCACHE_FETCH_COMPLETE)));
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone an object's cache data
|
|
|
|
*
|
2014-11-23 18:43:03 +03:00
|
|
|
* \post If \a deep is false, then any pointers in \a source will be set to NULL
|
|
|
|
*
|
2010-03-28 16:56:39 +04:00
|
|
|
* \param source Source object containing cache data to clone
|
|
|
|
* \param destination Destination object to clone cache data into
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param deep Whether to deep-copy the data or not
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static nserror llcache_object_clone_cache_data(llcache_object *source,
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object *destination, bool deep)
|
|
|
|
{
|
|
|
|
/* ETag must be first, as it can fail when deep cloning */
|
|
|
|
if (source->cache.etag != NULL) {
|
|
|
|
char *etag = source->cache.etag;
|
|
|
|
|
|
|
|
if (deep) {
|
|
|
|
/* Copy the etag */
|
|
|
|
etag = strdup(source->cache.etag);
|
|
|
|
if (etag == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
2010-09-16 23:33:18 +04:00
|
|
|
} else {
|
|
|
|
/* Destination takes ownership */
|
|
|
|
source->cache.etag = NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (destination->cache.etag != NULL)
|
|
|
|
free(destination->cache.etag);
|
|
|
|
|
|
|
|
destination->cache.etag = etag;
|
|
|
|
}
|
|
|
|
|
|
|
|
destination->cache.req_time = source->cache.req_time;
|
|
|
|
destination->cache.res_time = source->cache.res_time;
|
2014-03-22 18:54:51 +04:00
|
|
|
destination->cache.fin_time = source->cache.fin_time;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
if (source->cache.date != 0)
|
|
|
|
destination->cache.date = source->cache.date;
|
|
|
|
|
|
|
|
if (source->cache.expires != 0)
|
|
|
|
destination->cache.expires = source->cache.expires;
|
|
|
|
|
|
|
|
if (source->cache.age != INVALID_AGE)
|
|
|
|
destination->cache.age = source->cache.age;
|
|
|
|
|
|
|
|
if (source->cache.max_age != INVALID_AGE)
|
|
|
|
destination->cache.max_age = source->cache.max_age;
|
|
|
|
|
2011-02-25 00:22:24 +03:00
|
|
|
if (source->cache.no_cache != LLCACHE_VALIDATE_FRESH)
|
2010-03-28 16:56:39 +04:00
|
|
|
destination->cache.no_cache = source->cache.no_cache;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
if (source->cache.last_modified != 0)
|
|
|
|
destination->cache.last_modified = source->cache.last_modified;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/**
|
|
|
|
* Remove a low-level cache object from a cache list
|
|
|
|
*
|
|
|
|
* \param object Object to remove
|
|
|
|
* \param list List to remove from
|
|
|
|
* \return NSERROR_OK
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
llcache_object_remove_from_list(llcache_object *object, llcache_object **list)
|
|
|
|
{
|
|
|
|
if (object == *list)
|
|
|
|
*list = object->next;
|
|
|
|
else
|
|
|
|
object->prev->next = object->next;
|
|
|
|
|
|
|
|
if (object->next != NULL)
|
|
|
|
object->next->prev = object->prev;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-20 18:38:14 +03:00
|
|
|
* Retrieve source data for an object from persistent store if necessary.
|
2014-03-22 18:54:51 +04:00
|
|
|
*
|
2018-04-22 01:24:41 +03:00
|
|
|
* If an object's source data has been placed in the persistent store
|
|
|
|
* and there is no in-memory copy, then attempt to retrieve the source
|
|
|
|
* data.
|
2014-03-22 18:54:51 +04:00
|
|
|
*
|
2014-11-23 18:43:03 +03:00
|
|
|
* \param object the object to operate on.
|
2016-11-20 18:38:14 +03:00
|
|
|
* \return appropriate error code.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
2018-04-22 01:24:41 +03:00
|
|
|
static nserror llcache_retrieve_persisted_data(llcache_object *object)
|
2014-03-22 18:54:51 +04:00
|
|
|
{
|
|
|
|
/* ensure the source data is present if necessary */
|
|
|
|
if ((object->source_data != NULL) ||
|
|
|
|
(object->store_state != LLCACHE_STATE_DISC)) {
|
2016-11-20 18:38:14 +03:00
|
|
|
/* source data does not require retrieving from
|
|
|
|
* persistent store.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-20 18:38:14 +03:00
|
|
|
/* Source data for the object may be in the persistent store */
|
2014-03-22 18:54:51 +04:00
|
|
|
return guit->llcache->fetch(object->url,
|
2014-11-23 02:56:13 +03:00
|
|
|
BACKING_STORE_NONE,
|
2014-03-22 18:54:51 +04:00
|
|
|
&object->source_data,
|
|
|
|
&object->source_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-22 01:24:41 +03:00
|
|
|
* Generate a serialised version of an object's metadata
|
2014-03-22 18:54:51 +04:00
|
|
|
*
|
2014-11-23 18:43:03 +03:00
|
|
|
* The metadata includes object headers.
|
|
|
|
*
|
2016-11-20 18:38:14 +03:00
|
|
|
* \param object The cache object to serialise the metadata of.
|
2014-11-23 18:43:03 +03:00
|
|
|
* \param data_out Where the serialised buffer will be placed.
|
|
|
|
* \param datasize_out The size of the serialised data.
|
|
|
|
* \return NSERROR_OK on success with \a data_out and \a datasize_out
|
|
|
|
* updated, NSERROR_NOMEM on memory exhaustion or
|
|
|
|
* NSERROR_INVALID if there was an error serialising the
|
|
|
|
* stream.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
llcache_serialise_metadata(llcache_object *object,
|
|
|
|
uint8_t **data_out,
|
|
|
|
size_t *datasize_out)
|
|
|
|
{
|
|
|
|
size_t allocsize;
|
|
|
|
int datasize;
|
|
|
|
uint8_t *data;
|
|
|
|
char *op;
|
|
|
|
unsigned int hloop;
|
|
|
|
int use;
|
2020-02-23 19:06:52 +03:00
|
|
|
size_t cert_chain_depth;
|
|
|
|
|
|
|
|
if (object->chain != NULL) {
|
|
|
|
cert_chain_depth = object->chain->depth;
|
|
|
|
} else {
|
|
|
|
cert_chain_depth = 0;
|
|
|
|
}
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
allocsize = 10 + 1; /* object length */
|
|
|
|
|
|
|
|
allocsize += 10 + 1; /* request time */
|
|
|
|
|
|
|
|
allocsize += 10 + 1; /* response time */
|
|
|
|
|
|
|
|
allocsize += 10 + 1; /* completion time */
|
|
|
|
|
|
|
|
allocsize += 10 + 1; /* space for number of header entries */
|
|
|
|
|
|
|
|
for (hloop = 0 ; hloop < object->num_headers ; hloop++) {
|
|
|
|
allocsize += strlen(object->headers[hloop].name) + 1;
|
|
|
|
allocsize += strlen(object->headers[hloop].value) + 1;
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
allocsize += nsurl_length(object->url) + 1;
|
|
|
|
|
|
|
|
/* space for number of DER formatted certificates */
|
|
|
|
allocsize += 10 + 1;
|
|
|
|
|
|
|
|
for (hloop = 0; hloop < cert_chain_depth; hloop++) {
|
|
|
|
allocsize += 10 + 1; /* error status */
|
|
|
|
allocsize += 4 * ((object->chain->certs[hloop].der_length + 2) / 3);
|
2019-11-30 16:40:13 +03:00
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
data = malloc(allocsize);
|
|
|
|
if (data == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
op = (char *)data;
|
|
|
|
datasize = allocsize;
|
|
|
|
|
|
|
|
/* the url, used for checking for collisions */
|
2014-05-15 04:18:52 +04:00
|
|
|
use = snprintf(op, datasize, "%s", nsurl_access(object->url));
|
|
|
|
if (use < 0) {
|
|
|
|
goto operror;
|
|
|
|
}
|
|
|
|
use++; /* does not count the null */
|
|
|
|
if (use > datasize) {
|
2014-03-22 18:54:51 +04:00
|
|
|
goto overflow;
|
2014-05-15 04:18:52 +04:00
|
|
|
}
|
2014-03-22 18:54:51 +04:00
|
|
|
op += use;
|
|
|
|
datasize -= use;
|
|
|
|
|
|
|
|
/* object size */
|
2016-03-18 00:55:12 +03:00
|
|
|
use = snprintf(op, datasize, "%" PRIsizet, object->source_len);
|
2014-05-15 04:18:52 +04:00
|
|
|
if (use < 0) {
|
|
|
|
goto operror;
|
|
|
|
}
|
|
|
|
use++; /* does not count the null */
|
2014-03-22 18:54:51 +04:00
|
|
|
if (use > datasize)
|
|
|
|
goto overflow;
|
|
|
|
op += use;
|
|
|
|
datasize -= use;
|
|
|
|
|
|
|
|
/* Time of request */
|
2014-05-15 04:18:52 +04:00
|
|
|
use = nsc_sntimet(op, datasize, &object->cache.req_time);
|
2014-03-22 18:54:51 +04:00
|
|
|
if (use == 0)
|
|
|
|
goto overflow;
|
|
|
|
use++; /* does not count the null */
|
|
|
|
op += use;
|
|
|
|
datasize -= use;
|
|
|
|
|
|
|
|
/* Time of response */
|
2014-05-15 04:18:52 +04:00
|
|
|
use = nsc_sntimet(op, datasize, &object->cache.res_time);
|
2014-03-22 18:54:51 +04:00
|
|
|
if (use == 0)
|
|
|
|
goto overflow;
|
|
|
|
use++; /* does not count the null */
|
|
|
|
op += use;
|
|
|
|
datasize -= use;
|
|
|
|
|
|
|
|
/* Time of completion */
|
2014-05-15 04:18:52 +04:00
|
|
|
use = nsc_sntimet(op, datasize, &object->cache.fin_time);
|
2014-03-22 18:54:51 +04:00
|
|
|
if (use == 0)
|
|
|
|
goto overflow;
|
|
|
|
use++; /* does not count the null */
|
|
|
|
op += use;
|
|
|
|
datasize -= use;
|
|
|
|
|
|
|
|
/* number of headers */
|
2016-03-18 00:55:12 +03:00
|
|
|
use = snprintf(op, datasize, "%" PRIsizet, object->num_headers);
|
2014-05-15 04:18:52 +04:00
|
|
|
if (use < 0) {
|
|
|
|
goto operror;
|
|
|
|
}
|
|
|
|
use++; /* does not count the null */
|
2014-03-22 18:54:51 +04:00
|
|
|
if (use > datasize)
|
|
|
|
goto overflow;
|
|
|
|
op += use;
|
|
|
|
datasize -= use;
|
|
|
|
|
|
|
|
/* headers */
|
|
|
|
for (hloop = 0 ; hloop < object->num_headers ; hloop++) {
|
|
|
|
use = snprintf(op, datasize,
|
2014-05-15 04:18:52 +04:00
|
|
|
"%s:%s",
|
2014-03-22 18:54:51 +04:00
|
|
|
object->headers[hloop].name,
|
2014-05-15 04:18:52 +04:00
|
|
|
object->headers[hloop].value);
|
|
|
|
if (use < 0) {
|
|
|
|
goto operror;
|
|
|
|
}
|
|
|
|
use++; /* does not count the null */
|
2014-03-22 18:54:51 +04:00
|
|
|
if (use > datasize)
|
|
|
|
goto overflow;
|
|
|
|
op += use;
|
|
|
|
datasize -= use;
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
/* number of DER formatted ssl certificates */
|
|
|
|
use = snprintf(op, datasize, "%" PRIsizet, cert_chain_depth);
|
2019-11-30 16:40:13 +03:00
|
|
|
if (use < 0) {
|
|
|
|
goto operror;
|
|
|
|
}
|
|
|
|
use++; /* does not count the null */
|
|
|
|
if (use > datasize)
|
|
|
|
goto overflow;
|
|
|
|
op += use;
|
|
|
|
datasize -= use;
|
|
|
|
|
|
|
|
/* SSL certificates */
|
2020-02-23 19:06:52 +03:00
|
|
|
for (hloop = 0; hloop < cert_chain_depth; hloop++) {
|
|
|
|
nsuerror res;
|
|
|
|
|
|
|
|
/* Certificate error code */
|
|
|
|
use = snprintf(op, datasize, "%d",
|
|
|
|
(int)(object->chain->certs[hloop].err));
|
2019-11-30 16:40:13 +03:00
|
|
|
if (use < 0) {
|
|
|
|
goto operror;
|
|
|
|
}
|
|
|
|
use++; /* does not count the null */
|
|
|
|
if (use > datasize)
|
|
|
|
goto overflow;
|
|
|
|
op += use;
|
|
|
|
datasize -= use;
|
2020-02-23 19:06:52 +03:00
|
|
|
|
|
|
|
/* DER certificate data in base64 encoding */
|
|
|
|
if (object->chain->certs[hloop].der != NULL) {
|
2020-02-24 18:09:37 +03:00
|
|
|
size_t output_length = datasize;
|
2020-02-23 19:06:52 +03:00
|
|
|
res = nsu_base64_encode(
|
|
|
|
object->chain->certs[hloop].der,
|
|
|
|
object->chain->certs[hloop].der_length,
|
|
|
|
(uint8_t *)op,
|
|
|
|
&output_length);
|
|
|
|
if (res != NSUERROR_OK) {
|
|
|
|
goto operror;
|
|
|
|
}
|
|
|
|
use = output_length;
|
|
|
|
} else {
|
|
|
|
use = 0;
|
2019-11-30 16:40:13 +03:00
|
|
|
}
|
2020-02-23 19:06:52 +03:00
|
|
|
use++; /* allow for null */
|
2019-11-30 16:40:13 +03:00
|
|
|
if (use > datasize)
|
|
|
|
goto overflow;
|
|
|
|
op += use;
|
2020-02-24 18:09:37 +03:00
|
|
|
*(op - 1) = 0;
|
2019-11-30 16:40:13 +03:00
|
|
|
datasize -= use;
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Filled buffer with %d spare", datasize);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
*data_out = data;
|
|
|
|
*datasize_out = allocsize - datasize;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
|
|
|
|
overflow:
|
|
|
|
/* somehow we overflowed the buffer - hth? */
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO, "Overflowed metadata buffer");
|
2014-03-22 18:54:51 +04:00
|
|
|
free(data);
|
|
|
|
return NSERROR_INVALID;
|
2014-05-15 04:18:52 +04:00
|
|
|
|
|
|
|
operror:
|
|
|
|
/* output error */
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO, "Output error");
|
2014-05-15 04:18:52 +04:00
|
|
|
free(data);
|
|
|
|
return NSERROR_INVALID;
|
2014-03-22 18:54:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-22 01:24:41 +03:00
|
|
|
* Deserialisation of an object's metadata.
|
2014-11-23 18:43:03 +03:00
|
|
|
*
|
2016-11-20 18:38:14 +03:00
|
|
|
* Attempt to retrieve and deserialise the metadata for an object from
|
2014-11-23 18:43:03 +03:00
|
|
|
* the backing store.
|
|
|
|
*
|
2016-11-20 18:38:14 +03:00
|
|
|
* This must only update object if it is successful otherwise difficult
|
2015-01-29 01:36:10 +03:00
|
|
|
* to debug crashes happen later by using bad leftover object state.
|
|
|
|
*
|
2014-11-23 18:43:03 +03:00
|
|
|
* \param object The object to retrieve the metadata for.
|
2016-11-20 18:38:14 +03:00
|
|
|
* \return NSERROR_OK if the metatdata was retrieved and deserialised
|
|
|
|
* or error code if URL is not in persistent storage or in
|
2014-11-23 18:43:03 +03:00
|
|
|
* event of deserialisation error.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
llcache_process_metadata(llcache_object *object)
|
|
|
|
{
|
|
|
|
nserror res;
|
|
|
|
uint8_t *metadata = NULL;
|
|
|
|
size_t metadatalen = 0;
|
2019-11-30 17:05:23 +03:00
|
|
|
size_t remaining = 0;
|
2014-03-22 18:54:51 +04:00
|
|
|
nsurl *metadataurl;
|
|
|
|
unsigned int line;
|
|
|
|
char *ln;
|
|
|
|
int lnsize;
|
2015-01-29 01:36:10 +03:00
|
|
|
|
|
|
|
size_t source_length;
|
|
|
|
time_t request_time;
|
2018-04-22 01:24:41 +03:00
|
|
|
time_t response_time;
|
2015-01-29 01:36:10 +03:00
|
|
|
time_t completion_time;
|
2014-03-22 18:54:51 +04:00
|
|
|
size_t num_headers;
|
|
|
|
size_t hloop;
|
2019-11-30 17:05:23 +03:00
|
|
|
size_t ssl_cert_count = 0;
|
2020-02-23 19:06:52 +03:00
|
|
|
struct cert_chain *chain = NULL;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO, "Retrieving metadata");
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
/* attempt to retrieve object metadata from the backing store */
|
|
|
|
res = guit->llcache->fetch(object->url,
|
2014-11-23 02:56:13 +03:00
|
|
|
BACKING_STORE_META,
|
2014-03-22 18:54:51 +04:00
|
|
|
&metadata,
|
|
|
|
&metadatalen);
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO, "Processing retrieved data");
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2019-11-30 17:05:23 +03:00
|
|
|
/* metadata is stored as a sequence of NULL terminated strings
|
|
|
|
* which we call 'line's here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We track remaining data because as we extend this data structure
|
|
|
|
* we need to know if we should continue to parse
|
|
|
|
*/
|
|
|
|
remaining = metadatalen;
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* metadata line 1 is the url the metadata referrs to */
|
|
|
|
line = 1;
|
|
|
|
ln = (char *)metadata;
|
|
|
|
lnsize = strlen(ln);
|
2019-11-30 17:05:23 +03:00
|
|
|
remaining -= lnsize + 1;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2015-01-29 01:36:10 +03:00
|
|
|
if (lnsize < 7) {
|
|
|
|
res = NSERROR_INVALID;
|
2014-03-22 18:54:51 +04:00
|
|
|
goto format_error;
|
2015-01-29 01:36:10 +03:00
|
|
|
}
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
res = nsurl_create(ln, &metadataurl);
|
2015-01-29 01:36:10 +03:00
|
|
|
if (res != NSERROR_OK)
|
|
|
|
goto format_error;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
if (nsurl_compare(object->url, metadataurl, NSURL_COMPLETE) != true) {
|
|
|
|
/* backing store returned the wrong object for the
|
2016-11-20 18:38:14 +03:00
|
|
|
* request. This may occur if the backing store had
|
2014-11-23 02:56:13 +03:00
|
|
|
* a collision in its storage method. We cope with this
|
2014-03-22 18:54:51 +04:00
|
|
|
* by simply skipping caching of this object.
|
|
|
|
*/
|
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO, "Got metadata for %s instead of %s",
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
nsurl_access(metadataurl), nsurl_access(object->url));
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
nsurl_unref(metadataurl);
|
|
|
|
|
2014-11-23 02:56:13 +03:00
|
|
|
guit->llcache->release(object->url, BACKING_STORE_META);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
return NSERROR_BAD_URL;
|
|
|
|
}
|
|
|
|
nsurl_unref(metadataurl);
|
|
|
|
|
|
|
|
|
2018-04-22 01:24:41 +03:00
|
|
|
/* metadata line 2 is the object's length */
|
2014-03-22 18:54:51 +04:00
|
|
|
line = 2;
|
|
|
|
ln += lnsize + 1;
|
|
|
|
lnsize = strlen(ln);
|
2019-11-30 17:05:23 +03:00
|
|
|
remaining -= lnsize + 1;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2016-03-18 00:55:12 +03:00
|
|
|
if ((lnsize < 1) || (sscanf(ln, "%" PRIsizet, &source_length) != 1)) {
|
2015-01-29 01:36:10 +03:00
|
|
|
res = NSERROR_INVALID;
|
2014-03-22 18:54:51 +04:00
|
|
|
goto format_error;
|
2015-01-29 01:36:10 +03:00
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
/* metadata line 3 is the time of request */
|
|
|
|
line = 3;
|
|
|
|
ln += lnsize + 1;
|
|
|
|
lnsize = strlen(ln);
|
2019-11-30 17:05:23 +03:00
|
|
|
remaining -= lnsize + 1;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2015-01-29 01:36:10 +03:00
|
|
|
res = nsc_snptimet(ln, lnsize, &request_time);
|
|
|
|
if (res != NSERROR_OK)
|
2014-03-22 18:54:51 +04:00
|
|
|
goto format_error;
|
|
|
|
|
2015-01-29 01:36:10 +03:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* metadata line 4 is the time of response */
|
|
|
|
line = 4;
|
|
|
|
ln += lnsize + 1;
|
|
|
|
lnsize = strlen(ln);
|
2019-11-30 17:05:23 +03:00
|
|
|
remaining -= lnsize + 1;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2018-04-22 01:24:41 +03:00
|
|
|
res = nsc_snptimet(ln, lnsize, &response_time);
|
2015-01-29 01:36:10 +03:00
|
|
|
if (res != NSERROR_OK)
|
2014-03-22 18:54:51 +04:00
|
|
|
goto format_error;
|
|
|
|
|
2015-01-29 01:36:10 +03:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* metadata line 5 is the time of request completion */
|
|
|
|
line = 5;
|
|
|
|
ln += lnsize + 1;
|
|
|
|
lnsize = strlen(ln);
|
2019-11-30 17:05:23 +03:00
|
|
|
remaining -= lnsize + 1;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2015-01-29 01:36:10 +03:00
|
|
|
res = nsc_snptimet(ln, lnsize, &completion_time);
|
|
|
|
if (res != NSERROR_OK)
|
2014-03-22 18:54:51 +04:00
|
|
|
goto format_error;
|
|
|
|
|
2015-01-29 01:36:10 +03:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* metadata line 6 is the number of headers */
|
|
|
|
line = 6;
|
|
|
|
ln += lnsize + 1;
|
|
|
|
lnsize = strlen(ln);
|
2019-11-30 17:05:23 +03:00
|
|
|
remaining -= lnsize + 1;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2016-03-18 00:55:12 +03:00
|
|
|
if ((lnsize < 1) || (sscanf(ln, "%" PRIsizet, &num_headers) != 1)) {
|
2015-01-29 01:36:10 +03:00
|
|
|
res = NSERROR_INVALID;
|
2014-03-22 18:54:51 +04:00
|
|
|
goto format_error;
|
2015-01-29 01:36:10 +03:00
|
|
|
}
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
/* read headers */
|
|
|
|
for (hloop = 0 ; hloop < num_headers; hloop++) {
|
|
|
|
line++;
|
|
|
|
ln += lnsize + 1;
|
|
|
|
lnsize = strlen(ln);
|
2019-11-30 17:05:23 +03:00
|
|
|
remaining -= lnsize + 1;
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2015-01-29 01:36:10 +03:00
|
|
|
res = llcache_fetch_process_header(object,
|
|
|
|
(uint8_t *)ln,
|
|
|
|
lnsize);
|
|
|
|
if (res != NSERROR_OK)
|
|
|
|
goto format_error;
|
2014-03-22 18:54:51 +04:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:05:23 +03:00
|
|
|
if (remaining == 0) {
|
|
|
|
goto skip_ssl_certificates;
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
/* Next line is the number of DER base64 encoded certificates */
|
2019-11-30 17:05:23 +03:00
|
|
|
line++;
|
|
|
|
ln += lnsize + 1;
|
|
|
|
lnsize = strlen(ln);
|
|
|
|
remaining -= lnsize + 1;
|
|
|
|
|
|
|
|
if ((lnsize < 1) || (sscanf(ln, "%" PRIsizet, &ssl_cert_count) != 1)) {
|
|
|
|
res = NSERROR_INVALID;
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl_cert_count == 0) {
|
|
|
|
goto skip_ssl_certificates;
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
if (ssl_cert_count > MAX_CERT_DEPTH) {
|
|
|
|
res = NSERROR_INVALID;
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = cert_chain_alloc(ssl_cert_count, &chain);
|
|
|
|
if (res != NSERROR_OK) {
|
2019-11-30 17:05:23 +03:00
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (hloop = 0; hloop < ssl_cert_count; hloop++) {
|
|
|
|
int errcode;
|
2020-02-23 19:06:52 +03:00
|
|
|
nsuerror nsures;
|
|
|
|
|
2019-11-30 17:05:23 +03:00
|
|
|
/* Certificate error code */
|
|
|
|
line++;
|
|
|
|
ln += lnsize + 1;
|
|
|
|
lnsize = strlen(ln);
|
|
|
|
remaining -= lnsize + 1;
|
|
|
|
if ((lnsize < 1) || (sscanf(ln, "%d", &errcode) != 1)) {
|
|
|
|
res = NSERROR_INVALID;
|
|
|
|
goto format_error;
|
|
|
|
}
|
|
|
|
if (errcode < SSL_CERT_ERR_OK ||
|
|
|
|
errcode > SSL_CERT_ERR_MAX_KNOWN) {
|
|
|
|
/* Error with the cert code, assume UNKNOWN */
|
2020-02-23 19:06:52 +03:00
|
|
|
chain->certs[hloop].err = SSL_CERT_ERR_UNKNOWN;
|
2019-11-30 17:05:23 +03:00
|
|
|
} else {
|
2020-02-23 19:06:52 +03:00
|
|
|
chain->certs[hloop].err = (ssl_cert_err)errcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* base64 encoded DER certificate data */
|
|
|
|
line++;
|
|
|
|
ln += lnsize + 1;
|
|
|
|
lnsize = strlen(ln);
|
|
|
|
remaining -= lnsize + 1;
|
|
|
|
if (lnsize > 0) {
|
|
|
|
nsures = nsu_base64_decode_alloc((const uint8_t *)ln,
|
|
|
|
lnsize,
|
|
|
|
&chain->certs[hloop].der,
|
|
|
|
&chain->certs[hloop].der_length);
|
|
|
|
if (nsures != NSUERROR_OK) {
|
|
|
|
res = NSERROR_NOMEM;
|
|
|
|
goto format_error;
|
|
|
|
}
|
2019-11-30 17:05:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
skip_ssl_certificates:
|
2014-11-23 02:56:13 +03:00
|
|
|
guit->llcache->release(object->url, BACKING_STORE_META);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2015-01-29 01:36:10 +03:00
|
|
|
/* update object on successful parse of metadata */
|
|
|
|
object->source_len = source_length;
|
|
|
|
|
|
|
|
/** \todo really not sure this is right, nothing is allocated here? */
|
|
|
|
object->source_alloc = metadatalen;
|
|
|
|
|
|
|
|
object->cache.req_time = request_time;
|
2018-04-22 01:24:41 +03:00
|
|
|
object->cache.res_time = response_time;
|
2015-01-29 01:36:10 +03:00
|
|
|
object->cache.fin_time = completion_time;
|
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
object->chain = chain;
|
2019-11-30 17:05:23 +03:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* object stored in backing store */
|
|
|
|
object->store_state = LLCACHE_STATE_DISC;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
|
|
|
|
format_error:
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO,
|
|
|
|
"metadata error on line %d error code %d\n",
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
line, res);
|
2014-11-23 02:56:13 +03:00
|
|
|
guit->llcache->release(object->url, BACKING_STORE_META);
|
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
cert_chain_free(chain);
|
2019-11-30 17:05:23 +03:00
|
|
|
|
2015-01-29 01:36:10 +03:00
|
|
|
return res;
|
2014-03-22 18:54:51 +04:00
|
|
|
}
|
|
|
|
|
2019-11-10 19:47:55 +03:00
|
|
|
/**
|
|
|
|
* Check whether a scheme is persistable.
|
|
|
|
*
|
|
|
|
* \param url URL to check.
|
|
|
|
* \return true iff url has a persistable scheme.
|
|
|
|
*/
|
|
|
|
static inline bool llcache__scheme_is_persistable(const nsurl *url)
|
|
|
|
{
|
|
|
|
lwc_string *scheme = nsurl_get_component(url, NSURL_SCHEME);
|
|
|
|
bool persistable = false;
|
|
|
|
bool match;
|
|
|
|
|
|
|
|
/* nsurl ensures lower case schemes, and corestrings are lower
|
|
|
|
* case, so it's safe to use case-sensitive comparison. */
|
|
|
|
if ((lwc_string_isequal(scheme, corestring_lwc_http,
|
|
|
|
&match) == lwc_error_ok &&
|
|
|
|
(match == true)) ||
|
|
|
|
(lwc_string_isequal(scheme, corestring_lwc_https,
|
|
|
|
&match) == lwc_error_ok &&
|
|
|
|
(match == true))) {
|
|
|
|
persistable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwc_string_unref(scheme);
|
|
|
|
|
|
|
|
return persistable;
|
|
|
|
}
|
|
|
|
|
2019-11-10 19:03:07 +03:00
|
|
|
/**
|
|
|
|
* Check whether a scheme is cachable.
|
|
|
|
*
|
|
|
|
* \param url URL to check.
|
|
|
|
* \return true iff url has a cachable scheme.
|
|
|
|
*/
|
|
|
|
static inline bool llcache__scheme_is_cachable(const nsurl *url)
|
|
|
|
{
|
|
|
|
lwc_string *scheme = nsurl_get_component(url, NSURL_SCHEME);
|
|
|
|
bool cachable = false;
|
|
|
|
bool match;
|
|
|
|
|
|
|
|
/* nsurl ensures lower case schemes, and corestrings are lower
|
|
|
|
* case, so it's safe to use case-sensitive comparison. */
|
|
|
|
if ((lwc_string_isequal(scheme, corestring_lwc_http,
|
|
|
|
&match) == lwc_error_ok &&
|
|
|
|
(match == true)) ||
|
|
|
|
(lwc_string_isequal(scheme, corestring_lwc_https,
|
|
|
|
&match) == lwc_error_ok &&
|
|
|
|
(match == true)) ||
|
|
|
|
(lwc_string_isequal(scheme, corestring_lwc_data,
|
|
|
|
&match) == lwc_error_ok &&
|
|
|
|
(match == true)) ||
|
|
|
|
(lwc_string_isequal(scheme, corestring_lwc_resource,
|
|
|
|
&match) == lwc_error_ok &&
|
|
|
|
(match == true)) ||
|
|
|
|
(lwc_string_isequal(scheme, corestring_lwc_file,
|
|
|
|
&match) == lwc_error_ok &&
|
|
|
|
(match == true))) {
|
|
|
|
cachable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwc_string_unref(scheme);
|
|
|
|
|
|
|
|
return cachable;
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/**
|
2016-11-20 18:38:14 +03:00
|
|
|
* Attempt to retrieve an object from persistent storage.
|
2014-11-23 18:43:03 +03:00
|
|
|
*
|
2016-11-20 18:38:14 +03:00
|
|
|
* \param object The object to populate from persistent store.
|
2014-11-23 18:43:03 +03:00
|
|
|
* \param flags Fetch flags.
|
|
|
|
* \param referer The referring url.
|
|
|
|
* \param post Post data for fetch.
|
|
|
|
* \param redirect_count how many times this fetch has been redirected.
|
2016-11-20 18:38:14 +03:00
|
|
|
* \return NSERROR_OK if the object was successfully retrieved from the
|
2014-11-23 18:43:03 +03:00
|
|
|
* cache else appropriate error code.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
static nserror
|
2018-04-22 01:24:41 +03:00
|
|
|
llcache_object_fetch_persistent(llcache_object *object,
|
2014-03-22 18:54:51 +04:00
|
|
|
uint32_t flags,
|
|
|
|
nsurl *referer,
|
|
|
|
const llcache_post_data *post,
|
|
|
|
uint32_t redirect_count)
|
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
nsurl *referer_clone = NULL;
|
|
|
|
llcache_post_data *post_clone = NULL;
|
|
|
|
|
2019-11-10 19:47:55 +03:00
|
|
|
if (!llcache__scheme_is_persistable(object->url)) {
|
|
|
|
/* Don't bother looking up non-http(s) stuff; we don't
|
|
|
|
* persist it. */
|
|
|
|
return NSERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
object->cache.req_time = time(NULL);
|
|
|
|
object->cache.fin_time = object->cache.req_time;
|
|
|
|
|
|
|
|
/* retrieve and process metadata */
|
|
|
|
error = llcache_process_metadata(object);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* entry came out of cache - need to setup object state */
|
|
|
|
if (post != NULL) {
|
|
|
|
error = llcache_post_data_clone(post, &post_clone);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (referer != NULL) {
|
|
|
|
referer_clone = nsurl_ref(referer);
|
|
|
|
}
|
|
|
|
|
|
|
|
object->fetch.flags = flags;
|
|
|
|
object->fetch.referer = referer_clone;
|
|
|
|
object->fetch.post = post_clone;
|
|
|
|
object->fetch.redirect_count = redirect_count;
|
|
|
|
|
|
|
|
/* fetch is "finished" */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Retrieve a potentially cached object
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* \param url URL of object to retrieve
|
2010-04-14 14:56:44 +04:00
|
|
|
* \param flags Fetch flags
|
2012-04-28 17:24:09 +04:00
|
|
|
* \param referer Referring URL, or NULL if none
|
|
|
|
* \param post POST data, or NULL for a GET request
|
2010-04-11 17:58:12 +04:00
|
|
|
* \param redirect_count Number of redirects followed so far
|
2018-04-22 07:09:24 +03:00
|
|
|
* \param hsts_in_use Whether HSTS applies to this fetch
|
2016-11-20 18:38:14 +03:00
|
|
|
* \param result Pointer to location to receive retrieved object
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2014-03-22 18:54:51 +04:00
|
|
|
static nserror
|
|
|
|
llcache_object_retrieve_from_cache(nsurl *url,
|
|
|
|
uint32_t flags,
|
|
|
|
nsurl *referer,
|
|
|
|
const llcache_post_data *post,
|
|
|
|
uint32_t redirect_count,
|
2018-04-22 07:09:24 +03:00
|
|
|
bool hsts_in_use,
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_object **result)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
nserror error;
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object *obj, *newest = NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2018-08-12 14:50:57 +03:00
|
|
|
NSLOG(llcache, DEBUG,
|
2023-06-17 12:29:39 +03:00
|
|
|
"Searching cache for %s flags:%"PRIx32" referer:%s post:%p",
|
2018-08-12 14:50:57 +03:00
|
|
|
nsurl_access(url), flags,
|
|
|
|
referer==NULL?"":nsurl_access(referer),
|
|
|
|
post);
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Search for the most recently fetched matching object */
|
|
|
|
for (obj = llcache->cached_objects; obj != NULL; obj = obj->next) {
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-02-21 15:31:39 +04:00
|
|
|
if ((newest == NULL ||
|
2014-03-22 18:54:51 +04:00
|
|
|
obj->cache.req_time > newest->cache.req_time) &&
|
|
|
|
nsurl_compare(obj->url, url,
|
|
|
|
NSURL_COMPLETE) == true) {
|
2012-04-28 17:24:09 +04:00
|
|
|
newest = obj;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* No viable object found in cache create one and attempt to
|
2016-11-20 18:38:14 +03:00
|
|
|
* pull from persistent store.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
if (newest == NULL) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "No viable object found in llcache");
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
error = llcache_object_new(url, &obj);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
2016-11-20 18:38:14 +03:00
|
|
|
/* attempt to retrieve object from persistent store */
|
2018-04-22 01:24:41 +03:00
|
|
|
error = llcache_object_fetch_persistent(obj, flags, referer, post, redirect_count);
|
2014-03-22 18:54:51 +04:00
|
|
|
if (error == NSERROR_OK) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "retrieved object from persistent store");
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2016-11-20 18:38:14 +03:00
|
|
|
/* set newest object from persistent store which
|
2014-12-04 19:38:11 +03:00
|
|
|
* will cause the normal object handling to be used.
|
|
|
|
*/
|
2014-03-22 18:54:51 +04:00
|
|
|
newest = obj;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* Add new object to cached object list */
|
|
|
|
llcache_object_add_to_list(obj, &llcache->cached_objects);
|
|
|
|
|
|
|
|
}
|
2016-11-20 18:38:14 +03:00
|
|
|
/* else no object found and irretrievable from cache,
|
2014-12-04 19:38:11 +03:00
|
|
|
* fall through with newest unset to start fetch
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((newest != NULL) && (llcache_object_is_fresh(newest))) {
|
|
|
|
/* Found a suitable object, and it's still fresh */
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Found fresh %p", newest);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* The client needs to catch up with the object's state.
|
|
|
|
* This will occur the next time that llcache_poll is called.
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* ensure the source data is present */
|
2018-04-22 01:24:41 +03:00
|
|
|
error = llcache_retrieve_persisted_data(newest);
|
2014-03-22 18:54:51 +04:00
|
|
|
if (error == NSERROR_OK) {
|
2016-11-20 18:38:14 +03:00
|
|
|
/* source data was successfully retrieved from
|
|
|
|
* persistent store
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
*result = newest;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-20 18:38:14 +03:00
|
|
|
/* retrieval of source data from persistent store
|
2014-03-22 18:54:51 +04:00
|
|
|
* failed, destroy cache object and fall though to
|
2014-11-23 02:56:13 +03:00
|
|
|
* cache miss to re-fetch
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Persistent retrieval failed for %p", newest);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
llcache_object_remove_from_list(newest, &llcache->cached_objects);
|
|
|
|
llcache_object_destroy(newest);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
error = llcache_object_new(url, &obj);
|
2012-04-28 17:24:09 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
return error;
|
2014-02-21 15:31:39 +04:00
|
|
|
}
|
2014-03-22 18:54:51 +04:00
|
|
|
} else if (newest != NULL) {
|
|
|
|
/* Found a candidate object but it needs freshness validation */
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* ensure the source data is present */
|
2018-04-22 01:24:41 +03:00
|
|
|
error = llcache_retrieve_persisted_data(newest);
|
2014-03-22 18:54:51 +04:00
|
|
|
if (error == NSERROR_OK) {
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* Create a new object */
|
|
|
|
error = llcache_object_new(url, &obj);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Found candidate %p (%p)", obj, newest);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
/* Clone candidate's cache data */
|
|
|
|
error = llcache_object_clone_cache_data(newest, obj, true);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
llcache_object_destroy(obj);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Record candidate, so we can fall back if it is still fresh */
|
|
|
|
newest->candidate_count++;
|
|
|
|
obj->candidate = newest;
|
|
|
|
|
|
|
|
/* Attempt to kick-off fetch */
|
|
|
|
error = llcache_object_fetch(obj, flags, referer, post,
|
2018-04-22 07:09:24 +03:00
|
|
|
redirect_count, hsts_in_use);
|
2014-03-22 18:54:51 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
newest->candidate_count--;
|
|
|
|
llcache_object_destroy(obj);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new object to cache */
|
|
|
|
llcache_object_add_to_list(obj, &llcache->cached_objects);
|
|
|
|
|
|
|
|
*result = obj;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2012-04-28 17:24:09 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Persistent retrieval failed for %p", newest);
|
2010-04-17 00:08:27 +04:00
|
|
|
|
2016-11-20 18:38:14 +03:00
|
|
|
/* retrieval of source data from persistent store
|
2014-03-22 18:54:51 +04:00
|
|
|
* failed, destroy cache object and fall though to
|
|
|
|
* cache miss to re-retch
|
|
|
|
*/
|
|
|
|
llcache_object_remove_from_list(newest,
|
|
|
|
&llcache->cached_objects);
|
|
|
|
llcache_object_destroy(newest);
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
error = llcache_object_new(url, &obj);
|
2012-04-28 17:24:09 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
return error;
|
|
|
|
}
|
2014-03-22 18:54:51 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* Attempt to kick-off fetch */
|
2018-04-22 07:09:24 +03:00
|
|
|
error = llcache_object_fetch(obj, flags, referer, post,
|
|
|
|
redirect_count, hsts_in_use);
|
2014-03-22 18:54:51 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
llcache_object_destroy(obj);
|
|
|
|
return error;
|
2012-04-28 17:24:09 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* Add new object to cache */
|
|
|
|
llcache_object_add_to_list(obj, &llcache->cached_objects);
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
*result = obj;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Retrieve an object from the cache, fetching it if necessary.
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* \param url URL of object to retrieve
|
|
|
|
* \param flags Fetch flags
|
|
|
|
* \param referer Referring URL, or NULL if none
|
|
|
|
* \param post POST data, or NULL for a GET request
|
|
|
|
* \param redirect_count Number of redirects followed so far
|
2018-04-22 07:09:24 +03:00
|
|
|
* \param hsts_in_use Whether HSTS applies to this fetch
|
2016-11-20 18:38:14 +03:00
|
|
|
* \param result Pointer to location to receive retrieved object
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2015-06-24 12:31:13 +03:00
|
|
|
static nserror
|
|
|
|
llcache_object_retrieve(nsurl *url,
|
|
|
|
uint32_t flags,
|
|
|
|
nsurl *referer,
|
|
|
|
const llcache_post_data *post,
|
|
|
|
uint32_t redirect_count,
|
2018-04-22 07:09:24 +03:00
|
|
|
bool hsts_in_use,
|
2015-06-24 12:31:13 +03:00
|
|
|
llcache_object **result)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
nserror error;
|
|
|
|
llcache_object *obj;
|
|
|
|
nsurl *defragmented_url;
|
2014-03-07 03:55:40 +04:00
|
|
|
bool uncachable = false;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2023-06-17 12:29:39 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Retrieve %s (%"PRIx32", %s, %p)", nsurl_access(url), flags,
|
2015-05-28 19:06:18 +03:00
|
|
|
referer==NULL?"":nsurl_access(referer), post);
|
2010-03-29 10:27:37 +04:00
|
|
|
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Get rid of any url fragment */
|
2014-03-05 19:10:02 +04:00
|
|
|
error = nsurl_defragment(url, &defragmented_url);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-07 03:55:40 +04:00
|
|
|
/* determine if content is cachable */
|
|
|
|
if ((flags & LLCACHE_RETRIEVE_FORCE_FETCH) != 0) {
|
|
|
|
/* Forced fetches are never cached */
|
|
|
|
uncachable = true;
|
|
|
|
} else if (post != NULL) {
|
|
|
|
/* POST requests are never cached */
|
|
|
|
uncachable = true;
|
|
|
|
} else {
|
2019-11-10 19:03:07 +03:00
|
|
|
uncachable = !llcache__scheme_is_cachable(defragmented_url);
|
2014-03-07 03:55:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uncachable) {
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Create new object */
|
|
|
|
error = llcache_object_new(defragmented_url, &obj);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
nsurl_unref(defragmented_url);
|
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Attempt to kick-off fetch */
|
2014-02-21 15:31:39 +04:00
|
|
|
error = llcache_object_fetch(obj, flags, referer, post,
|
2018-04-22 07:09:24 +03:00
|
|
|
redirect_count, hsts_in_use);
|
2012-04-28 17:24:09 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
llcache_object_destroy(obj);
|
|
|
|
nsurl_unref(defragmented_url);
|
|
|
|
return error;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Add new object to uncached list */
|
|
|
|
llcache_object_add_to_list(obj, &llcache->uncached_objects);
|
|
|
|
} else {
|
|
|
|
error = llcache_object_retrieve_from_cache(defragmented_url,
|
2018-04-22 07:09:24 +03:00
|
|
|
flags, referer, post, redirect_count,
|
|
|
|
hsts_in_use, &obj);
|
2012-04-28 17:24:09 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
nsurl_unref(defragmented_url);
|
|
|
|
return error;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Returned object is already in the cached list */
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Retrieved %p", obj);
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
*result = obj;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
nsurl_unref(defragmented_url);
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Add a user to a low-level cache object
|
|
|
|
*
|
2014-11-23 18:43:03 +03:00
|
|
|
* \param object Object to add user to
|
|
|
|
* \param user User to add
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK.
|
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static nserror llcache_object_add_user(llcache_object *object,
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object_user *user)
|
|
|
|
{
|
2010-04-09 04:06:36 +04:00
|
|
|
assert(user->next == NULL);
|
|
|
|
assert(user->prev == NULL);
|
2013-05-04 11:52:12 +04:00
|
|
|
assert(user->handle != NULL);
|
2010-04-09 04:06:36 +04:00
|
|
|
|
2010-12-16 00:15:49 +03:00
|
|
|
user->handle->object = object;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
user->prev = NULL;
|
|
|
|
user->next = object->users;
|
|
|
|
|
|
|
|
if (object->users != NULL)
|
|
|
|
object->users->prev = user;
|
|
|
|
object->users = user;
|
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Adding user %p to %p", user, object);
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-22 01:54:04 +03:00
|
|
|
/**
|
|
|
|
* Transform a request-URI based on HSTS policy
|
|
|
|
*
|
|
|
|
* \param url URL to transform
|
|
|
|
* \param result Pointer to location to receive transformed URL
|
|
|
|
* \param hsts_in_use Pointer to location to receive HSTS in-use flag
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
static nserror llcache_hsts_transform_url(nsurl *url, nsurl **result,
|
|
|
|
bool *hsts_in_use)
|
|
|
|
{
|
|
|
|
lwc_string *scheme = NULL;
|
|
|
|
bool match;
|
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
|
|
|
|
scheme = nsurl_get_component(url, NSURL_SCHEME);
|
|
|
|
if (lwc_string_caseless_isequal(scheme, corestring_lwc_http,
|
|
|
|
&match) != lwc_error_ok || match == false) {
|
2024-01-20 21:10:51 +03:00
|
|
|
/* Non-HTTP fetch: no transform required */
|
|
|
|
if (lwc_string_caseless_isequal(scheme, corestring_lwc_https,
|
|
|
|
&match) == lwc_error_ok && match) {
|
|
|
|
/* HTTPS: ask urldb if HSTS is enabled */
|
|
|
|
*hsts_in_use = urldb_get_hsts_enabled(url);
|
|
|
|
} else {
|
|
|
|
/* Anything else: no HSTS */
|
|
|
|
*hsts_in_use = false;
|
|
|
|
}
|
2018-04-22 01:54:04 +03:00
|
|
|
lwc_string_unref(scheme);
|
|
|
|
*result = nsurl_ref(url);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
lwc_string_unref(scheme);
|
|
|
|
|
|
|
|
if (urldb_get_hsts_enabled(url)) {
|
|
|
|
/* Only need to force HTTPS. If original port was explicitly
|
|
|
|
* specified as 80, nsurl_create/join will remove it (as
|
|
|
|
* it's redundant) */
|
|
|
|
error = nsurl_replace_scheme(url, corestring_lwc_https,
|
|
|
|
result);
|
|
|
|
*hsts_in_use = (error == NSERROR_OK);
|
|
|
|
} else {
|
|
|
|
*result = nsurl_ref(url);
|
|
|
|
*hsts_in_use = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update HSTS policy for target domain.
|
|
|
|
*
|
|
|
|
* \param object Newly-fetched cache object
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
static nserror llcache_hsts_update_policy(llcache_object *object)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
lwc_string *scheme = NULL;
|
|
|
|
bool match = false;
|
|
|
|
|
|
|
|
scheme = nsurl_get_component(object->url, NSURL_SCHEME);
|
|
|
|
if (lwc_string_caseless_isequal(scheme, corestring_lwc_https,
|
|
|
|
&match) != lwc_error_ok || match == false) {
|
|
|
|
/* Non-HTTPS fetch: ignore */
|
|
|
|
lwc_string_unref(scheme);
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
lwc_string_unref(scheme);
|
|
|
|
|
|
|
|
if (object->fetch.tainted_tls) {
|
|
|
|
/* Transport is tainted: ignore */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < object->num_headers; i++) {
|
|
|
|
if (strcasecmp("Strict-Transport-Security",
|
|
|
|
object->headers[i].name) == 0) {
|
|
|
|
urldb_set_hsts_policy(object->url,
|
|
|
|
object->headers[i].value);
|
|
|
|
/* Only process the first one we find */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Handle FETCH_REDIRECT event
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* \param object Object being redirected
|
|
|
|
* \param target Target of redirect (may be relative)
|
|
|
|
* \param replacement Pointer to location to receive replacement object
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2016-02-08 14:38:41 +03:00
|
|
|
static nserror llcache_fetch_redirect(llcache_object *object,
|
|
|
|
const char *target, llcache_object **replacement)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
nserror error;
|
|
|
|
llcache_object *dest;
|
|
|
|
llcache_object_user *user, *next;
|
|
|
|
const llcache_post_data *post = object->fetch.post;
|
2018-04-22 01:54:04 +03:00
|
|
|
nsurl *url, *hsts_url;
|
2012-04-28 17:24:09 +04:00
|
|
|
lwc_string *scheme;
|
|
|
|
lwc_string *object_scheme;
|
2018-04-22 01:54:04 +03:00
|
|
|
bool match, hsts_in_use;
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Extract HTTP response code from the fetch object */
|
|
|
|
long http_code = fetch_http_code(object->fetch.fetch);
|
2013-05-27 17:51:00 +04:00
|
|
|
llcache_event event;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Abort fetch for this object */
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Invalidate the cache control data */
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
|
|
|
|
|
|
|
/* And mark it complete */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2018-04-22 07:25:35 +03:00
|
|
|
(void) llcache_hsts_update_policy(object);
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Forcibly stop redirecting if we've followed too many redirects */
|
|
|
|
#define REDIRECT_LIMIT 10
|
|
|
|
if (object->fetch.redirect_count > REDIRECT_LIMIT) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO, "Too many nested redirects");
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
2019-08-05 22:56:07 +03:00
|
|
|
event.data.error.code = NSERROR_BAD_REDIRECT;
|
|
|
|
event.data.error.msg = messages_get("BadRedirect");
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return llcache_send_event_to_users(object, &event);
|
|
|
|
}
|
|
|
|
#undef REDIRECT_LIMIT
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Make target absolute */
|
|
|
|
error = nsurl_join(object->url, target, &url);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2018-04-22 01:54:04 +03:00
|
|
|
/* Perform HSTS transform */
|
|
|
|
error = llcache_hsts_transform_url(url, &hsts_url, &hsts_in_use);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
nsurl_unref(url);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
nsurl_unref(url);
|
|
|
|
|
2013-05-27 17:51:00 +04:00
|
|
|
/* Inform users of redirect */
|
|
|
|
event.type = LLCACHE_EVENT_REDIRECT;
|
|
|
|
event.data.redirect.from = object->url;
|
2018-04-22 01:54:04 +03:00
|
|
|
event.data.redirect.to = hsts_url;
|
2013-05-27 17:51:00 +04:00
|
|
|
|
|
|
|
error = llcache_send_event_to_users(object, &event);
|
|
|
|
|
|
|
|
if (error != NSERROR_OK) {
|
2018-04-22 01:54:04 +03:00
|
|
|
nsurl_unref(hsts_url);
|
2013-05-27 17:51:00 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Reject attempts to redirect from unvalidated to validated schemes
|
|
|
|
* A "validated" scheme is one over which we have some guarantee that
|
|
|
|
* the source is trustworthy. */
|
|
|
|
object_scheme = nsurl_get_component(object->url, NSURL_SCHEME);
|
2018-04-22 01:54:04 +03:00
|
|
|
scheme = nsurl_get_component(hsts_url, NSURL_SCHEME);
|
2012-04-28 17:24:09 +04:00
|
|
|
|
|
|
|
/* resource: and about: are allowed to redirect anywhere */
|
2014-01-25 00:19:20 +04:00
|
|
|
if ((lwc_string_isequal(object_scheme, corestring_lwc_resource,
|
2012-04-28 17:24:09 +04:00
|
|
|
&match) == lwc_error_ok && match == false) &&
|
2014-01-25 00:19:20 +04:00
|
|
|
(lwc_string_isequal(object_scheme, corestring_lwc_about,
|
2012-04-28 17:24:09 +04:00
|
|
|
&match) == lwc_error_ok && match == false)) {
|
|
|
|
/* file, about and resource are not valid redirect targets */
|
2014-01-25 00:19:20 +04:00
|
|
|
if ((lwc_string_isequal(object_scheme, corestring_lwc_file,
|
2012-04-28 17:24:09 +04:00
|
|
|
&match) == lwc_error_ok && match == true) ||
|
2014-01-25 00:19:20 +04:00
|
|
|
(lwc_string_isequal(object_scheme, corestring_lwc_about,
|
2012-04-28 17:24:09 +04:00
|
|
|
&match) == lwc_error_ok && match == true) ||
|
2014-01-25 00:19:20 +04:00
|
|
|
(lwc_string_isequal(object_scheme, corestring_lwc_resource,
|
2012-04-28 17:24:09 +04:00
|
|
|
&match) == lwc_error_ok && match == true)) {
|
|
|
|
lwc_string_unref(object_scheme);
|
|
|
|
lwc_string_unref(scheme);
|
2018-04-22 01:54:04 +03:00
|
|
|
nsurl_unref(hsts_url);
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-12-16 00:15:49 +03:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
lwc_string_unref(scheme);
|
|
|
|
lwc_string_unref(object_scheme);
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Bail out if we've no way of handling this URL */
|
2018-04-22 01:54:04 +03:00
|
|
|
if (fetch_can_fetch(hsts_url) == false) {
|
|
|
|
nsurl_unref(hsts_url);
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (http_code == 301 || http_code == 302 || http_code == 303) {
|
|
|
|
/* 301, 302, 303 redirects are all unconditional GET requests */
|
|
|
|
post = NULL;
|
|
|
|
} else if (http_code != 307 || post != NULL) {
|
|
|
|
/** \todo 300, 305, 307 with POST */
|
2018-04-22 01:54:04 +03:00
|
|
|
nsurl_unref(hsts_url);
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Attempt to fetch target URL */
|
2018-04-22 01:54:04 +03:00
|
|
|
error = llcache_object_retrieve(hsts_url, object->fetch.flags,
|
2014-02-21 15:31:39 +04:00
|
|
|
object->fetch.referer, post,
|
2018-04-22 07:09:24 +03:00
|
|
|
object->fetch.redirect_count + 1,
|
|
|
|
hsts_in_use, &dest);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* No longer require url */
|
2018-04-22 01:54:04 +03:00
|
|
|
nsurl_unref(hsts_url);
|
2012-04-28 17:24:09 +04:00
|
|
|
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
/* Move user(s) to replacement object */
|
|
|
|
for (user = object->users; user != NULL; user = next) {
|
|
|
|
next = user->next;
|
|
|
|
|
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_add_user(dest, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dest is now our object */
|
|
|
|
*replacement = dest;
|
|
|
|
|
2014-02-21 15:31:39 +04:00
|
|
|
return NSERROR_OK;
|
2012-04-28 17:24:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an object's cache state
|
|
|
|
*
|
|
|
|
* \param object Object to update cache for
|
|
|
|
* \return NSERROR_OK.
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static nserror llcache_object_cache_update(llcache_object *object)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
if (object->cache.date == 0)
|
|
|
|
object->cache.date = time(NULL);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle FETCH_NOTMODIFIED event
|
|
|
|
*
|
|
|
|
* \param object Object to process
|
|
|
|
* \param replacement Pointer to location to receive replacement object
|
|
|
|
* \return NSERROR_OK.
|
|
|
|
*/
|
|
|
|
static nserror llcache_fetch_notmodified(llcache_object *object,
|
|
|
|
llcache_object **replacement)
|
|
|
|
{
|
|
|
|
/* There may be no candidate if the server erroneously responded
|
|
|
|
* to an unconditional request with a 304 Not Modified response.
|
|
|
|
* In this case, we simply retain the initial object, having
|
|
|
|
* invalidated it and marked it as complete.
|
|
|
|
*/
|
|
|
|
if (object->candidate != NULL) {
|
|
|
|
llcache_object_user *user, *next;
|
|
|
|
|
|
|
|
/* Move user(s) to candidate content */
|
|
|
|
for (user = object->users; user != NULL; user = next) {
|
|
|
|
next = user->next;
|
|
|
|
|
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_add_user(object->candidate, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Candidate is no longer a candidate for us */
|
|
|
|
object->candidate->candidate_count--;
|
|
|
|
|
|
|
|
/* Clone our cache control data into the candidate */
|
2014-02-21 15:31:39 +04:00
|
|
|
llcache_object_clone_cache_data(object, object->candidate,
|
2012-04-28 17:24:09 +04:00
|
|
|
false);
|
|
|
|
/* Bring candidate's cache data up to date */
|
|
|
|
llcache_object_cache_update(object->candidate);
|
|
|
|
/* Revert no-cache to normal, if required */
|
2014-02-21 15:31:39 +04:00
|
|
|
if (object->candidate->cache.no_cache ==
|
2012-04-28 17:24:09 +04:00
|
|
|
LLCACHE_VALIDATE_ONCE) {
|
2014-02-21 15:31:39 +04:00
|
|
|
object->candidate->cache.no_cache =
|
2012-04-28 17:24:09 +04:00
|
|
|
LLCACHE_VALIDATE_FRESH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Candidate is now our object */
|
|
|
|
*replacement = object->candidate;
|
|
|
|
object->candidate = NULL;
|
|
|
|
} else {
|
|
|
|
/* There was no candidate: retain object */
|
|
|
|
*replacement = object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure fetch has stopped */
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
|
|
|
/* Invalidate our cache-control data */
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
|
|
|
|
|
|
|
/* Mark it complete */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
2018-04-22 01:54:04 +03:00
|
|
|
(void) llcache_hsts_update_policy(object);
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Old object will be flushed from the cache on the next poll */
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-06 01:35:38 +04:00
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Process a chunk of fetched data
|
2010-04-06 01:35:38 +04:00
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* \param object Object being fetched
|
|
|
|
* \param data Data to process
|
|
|
|
* \param len Byte length of data
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise.
|
2010-04-06 01:35:38 +04:00
|
|
|
*/
|
2014-05-29 17:00:13 +04:00
|
|
|
static nserror
|
|
|
|
llcache_fetch_process_data(llcache_object *object,
|
|
|
|
const uint8_t *data,
|
|
|
|
size_t len)
|
2010-04-06 01:35:38 +04:00
|
|
|
{
|
2014-05-29 17:00:13 +04:00
|
|
|
if (object->fetch.state != LLCACHE_FETCH_DATA) {
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* \note
|
|
|
|
* On entry into this state, check if we need to
|
2014-05-29 17:00:13 +04:00
|
|
|
* invalidate the cache control data. We are guaranteed
|
|
|
|
* to have received all response headers.
|
|
|
|
*
|
|
|
|
* There are two cases in which we want to suppress
|
|
|
|
* cacheing of an object:
|
|
|
|
*
|
|
|
|
* 1) The HTTP response code is not 200 or 203
|
|
|
|
* 2) The request URI had a query string and the
|
|
|
|
* response headers did not provide an explicit
|
|
|
|
* object expiration time.
|
|
|
|
*/
|
|
|
|
long http_code = fetch_http_code(object->fetch.fetch);
|
|
|
|
|
|
|
|
if ((http_code != 200 && http_code != 203) ||
|
|
|
|
(nsurl_has_component(object->url, NSURL_QUERY) &&
|
|
|
|
(object->cache.max_age == INVALID_AGE &&
|
|
|
|
object->cache.expires == 0))) {
|
|
|
|
/* Invalidate cache control data */
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release candidate, if any */
|
|
|
|
if (object->candidate != NULL) {
|
|
|
|
object->candidate->candidate_count--;
|
|
|
|
object->candidate = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
object->fetch.state = LLCACHE_FETCH_DATA;
|
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Resize source buffer if it's too small */
|
|
|
|
if (object->source_len + len >= object->source_alloc) {
|
|
|
|
const size_t new_len = object->source_len + len + 64 * 1024;
|
|
|
|
uint8_t *temp = realloc(object->source_data, new_len);
|
|
|
|
if (temp == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
2010-04-06 01:35:38 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
object->source_data = temp;
|
|
|
|
object->source_alloc = new_len;
|
2010-04-06 01:35:38 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Append this data chunk to source buffer */
|
|
|
|
memcpy(object->source_data + object->source_len, data, len);
|
|
|
|
object->source_len += len;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2010-04-06 01:35:38 +04:00
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* Handle an authentication request
|
|
|
|
*
|
|
|
|
* \param object Object being fetched
|
|
|
|
* \param realm Authentication realm
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise.
|
|
|
|
*/
|
|
|
|
static nserror llcache_fetch_auth(llcache_object *object, const char *realm)
|
|
|
|
{
|
|
|
|
const char *auth;
|
|
|
|
nserror error = NSERROR_OK;
|
2010-08-11 01:46:45 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Abort fetch for this object */
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Invalidate cache-control data */
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Destroy headers */
|
|
|
|
llcache_destroy_headers(object);
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* If there was no realm, then default to the URL */
|
|
|
|
/** \todo If there was no WWW-Authenticate header, use response body */
|
|
|
|
if (realm == NULL)
|
|
|
|
realm = nsurl_access(object->url);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-10-09 14:20:21 +04:00
|
|
|
auth = urldb_get_auth_details(object->url, realm);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (auth == NULL || object->fetch.tried_with_auth == true) {
|
2019-08-03 23:13:19 +03:00
|
|
|
llcache_event event;
|
2012-04-28 17:24:09 +04:00
|
|
|
/* No authentication details, or tried what we had, so ask */
|
|
|
|
object->fetch.tried_with_auth = false;
|
2011-09-08 12:26:16 +04:00
|
|
|
|
2019-08-05 22:56:07 +03:00
|
|
|
/* Mark object complete */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2019-08-05 22:56:07 +03:00
|
|
|
/* Inform client(s) that object fetch failed */
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
|
|
|
/** \todo More appropriate error message */
|
|
|
|
event.data.error.code = NSERROR_BAD_AUTH;
|
2019-08-06 13:25:11 +03:00
|
|
|
event.data.error.msg = realm;
|
2010-12-16 01:59:19 +03:00
|
|
|
|
2019-08-03 23:13:19 +03:00
|
|
|
error = llcache_send_event_to_users(object, &event);
|
2012-04-28 17:24:09 +04:00
|
|
|
} else {
|
|
|
|
/* Flag that we've tried to refetch with credentials, so
|
|
|
|
* that if the fetch fails again, we ask the user again */
|
|
|
|
object->fetch.tried_with_auth = true;
|
|
|
|
error = llcache_object_refetch(object);
|
|
|
|
}
|
2010-12-16 01:59:19 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return error;
|
|
|
|
}
|
2011-09-04 10:28:09 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
|
|
|
* Handle a TLS certificate verification failure
|
|
|
|
*
|
|
|
|
* \param object Object being fetched
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2019-08-05 20:11:13 +03:00
|
|
|
static nserror llcache_fetch_cert_error(llcache_object *object)
|
2012-04-28 17:24:09 +04:00
|
|
|
{
|
|
|
|
nserror error = NSERROR_OK;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Fetch has been stopped, and destroyed. Invalidate object's pointer */
|
|
|
|
object->fetch.fetch = NULL;
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Invalidate cache-control data */
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2018-04-22 01:54:04 +03:00
|
|
|
/* Consider the TLS transport tainted */
|
|
|
|
object->fetch.tainted_tls = true;
|
|
|
|
|
2018-04-22 07:09:24 +03:00
|
|
|
/* Only give the user a chance if HSTS isn't in use for this fetch */
|
2019-08-03 23:13:19 +03:00
|
|
|
if (object->fetch.hsts_in_use == false) {
|
|
|
|
llcache_event event;
|
2010-12-16 01:59:19 +03:00
|
|
|
|
2019-08-05 22:56:07 +03:00
|
|
|
/* Mark object complete */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
2019-08-03 23:13:19 +03:00
|
|
|
|
2019-08-05 22:56:07 +03:00
|
|
|
/* Inform client(s) that object fetch failed */
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
|
|
|
/** \todo More appropriate error message */
|
|
|
|
event.data.error.code = NSERROR_BAD_CERTS;
|
|
|
|
event.data.error.msg = messages_get("FetchFailed");
|
2011-09-08 12:26:16 +04:00
|
|
|
|
2019-08-03 23:13:19 +03:00
|
|
|
error = llcache_send_event_to_users(object, &event);
|
2012-04-28 17:24:09 +04:00
|
|
|
} else {
|
|
|
|
llcache_event event;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Mark object complete */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
2010-12-13 02:37:21 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Inform client(s) that object fetch failed */
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
|
|
|
/** \todo More appropriate error message */
|
2019-08-05 22:56:07 +03:00
|
|
|
event.data.error.code = NSERROR_UNKNOWN;
|
|
|
|
event.data.error.msg = messages_get("FetchFailed");
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
error = llcache_send_event_to_users(object, &event);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2019-10-29 02:50:01 +03:00
|
|
|
|
2013-01-05 03:13:23 +04:00
|
|
|
/**
|
|
|
|
* Handle a TLS connection setup failure
|
|
|
|
*
|
|
|
|
* \param object Object being fetched
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
static nserror llcache_fetch_ssl_error(llcache_object *object)
|
|
|
|
{
|
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
|
|
|
|
/* Fetch has been stopped, and destroyed. Invalidate object's pointer */
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
|
|
|
/* Invalidate cache-control data */
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
|
|
|
|
2018-04-22 01:54:04 +03:00
|
|
|
/* Consider the TLS transport tainted */
|
|
|
|
object->fetch.tainted_tls = true;
|
|
|
|
|
2018-04-22 07:09:24 +03:00
|
|
|
/* Make no attempt to downgrade if HSTS is in use
|
|
|
|
* (i.e. assume server does TLS properly) */
|
|
|
|
if (object->fetch.hsts_in_use ||
|
2019-10-29 02:50:01 +03:00
|
|
|
object->fetch.tried_with_tls_downgrade) {
|
2013-01-05 03:13:23 +04:00
|
|
|
/* Have already tried to downgrade, so give up */
|
|
|
|
llcache_event event;
|
|
|
|
|
|
|
|
/* Mark object complete */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
|
|
|
/* Inform client(s) that object fetch failed */
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
|
|
|
/** \todo More appropriate error message */
|
2019-08-05 22:56:07 +03:00
|
|
|
event.data.error.code = NSERROR_UNKNOWN;
|
|
|
|
event.data.error.msg = messages_get("FetchFailed");
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2013-01-05 03:13:23 +04:00
|
|
|
error = llcache_send_event_to_users(object, &event);
|
|
|
|
} else {
|
2013-01-07 22:45:29 +04:00
|
|
|
/* Flag that we've tried to downgrade, so that if the
|
2013-01-05 03:13:23 +04:00
|
|
|
* fetch fails again, we give up */
|
|
|
|
object->fetch.tried_with_tls_downgrade = true;
|
|
|
|
error = llcache_object_refetch(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2019-10-29 02:50:01 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* handle time out while trying to fetch.
|
|
|
|
*
|
|
|
|
* \param object Object being fetched
|
|
|
|
* \return NSERROR_OK on success otherwise error code
|
|
|
|
*/
|
|
|
|
static nserror llcache_fetch_timeout(llcache_object *object)
|
|
|
|
{
|
|
|
|
llcache_event event;
|
|
|
|
|
|
|
|
/* The fetch has already been cleaned up by the fetcher but
|
|
|
|
* we would like to retry if we can.
|
|
|
|
*/
|
|
|
|
if (object->fetch.retries_remaining > 1) {
|
|
|
|
object->fetch.retries_remaining--;
|
|
|
|
return llcache_object_refetch(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The fetch has has already been cleaned up by the fetcher */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
|
|
|
/* Release candidate, if any */
|
|
|
|
if (object->candidate != NULL) {
|
|
|
|
object->candidate->candidate_count--;
|
|
|
|
object->candidate = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invalidate cache control data */
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
|
|
|
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
|
|
|
event.data.error.code = NSERROR_TIMEOUT;
|
|
|
|
event.data.error.msg = NULL;
|
|
|
|
|
|
|
|
return llcache_send_event_to_users(object, &event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/**
|
2014-11-23 18:43:03 +03:00
|
|
|
* Construct a sorted list of objects available for writeout operation.
|
2014-03-22 18:54:51 +04:00
|
|
|
*
|
|
|
|
* The list contains fresh cacheable objects held in RAM with no
|
|
|
|
* pending fetches. Any objects with a remaining lifetime less than
|
|
|
|
* the configured minimum lifetime are simply not considered, they will
|
|
|
|
* become stale before pushing to backing store is worth the cost.
|
|
|
|
*
|
|
|
|
* \todo calculate useful cost metrics to improve sorting.
|
|
|
|
*
|
2014-11-23 18:43:03 +03:00
|
|
|
* \param[out] lst_out list of candidate objects.
|
|
|
|
* \param[out] lst_len_out Number of candidate objects in result.
|
|
|
|
* \return NSERROR_OK with \a lst_out and \a lst_len_out updated or
|
|
|
|
* error code.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
build_candidate_list(struct llcache_object ***lst_out, int *lst_len_out)
|
|
|
|
{
|
|
|
|
llcache_object *object, *next;
|
|
|
|
struct llcache_object **lst;
|
|
|
|
int lst_len = 0;
|
|
|
|
int remaining_lifetime;
|
|
|
|
|
2014-11-30 02:56:33 +03:00
|
|
|
#define MAX_PERSIST_PER_RUN 128
|
2014-06-04 01:30:04 +04:00
|
|
|
|
|
|
|
lst = calloc(MAX_PERSIST_PER_RUN, sizeof(struct llcache_object *));
|
2014-11-28 22:17:42 +03:00
|
|
|
if (lst == NULL) {
|
2014-03-22 18:54:51 +04:00
|
|
|
return NSERROR_NOMEM;
|
2014-11-28 22:17:42 +03:00
|
|
|
}
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
for (object = llcache->cached_objects; object != NULL; object = next) {
|
|
|
|
next = object->next;
|
|
|
|
|
2019-11-10 19:47:55 +03:00
|
|
|
/* Only consider http(s) for the disc cache. */
|
|
|
|
if (!llcache__scheme_is_persistable(object->url)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-08 14:38:41 +03:00
|
|
|
remaining_lifetime = llcache_object_rfc2616_remaining_lifetime(
|
|
|
|
&object->cache);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
/* cacehable objects with no pending fetches, not
|
|
|
|
* already on disc and with sufficient lifetime to
|
2016-11-20 18:38:14 +03:00
|
|
|
* make disc cache worthwhile
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
if ((object->candidate_count == 0) &&
|
|
|
|
(object->fetch.fetch == NULL) &&
|
|
|
|
(object->store_state == LLCACHE_STATE_RAM) &&
|
|
|
|
(remaining_lifetime > llcache->minimum_lifetime)) {
|
|
|
|
lst[lst_len] = object;
|
|
|
|
lst_len++;
|
2014-06-04 01:30:04 +04:00
|
|
|
if (lst_len == MAX_PERSIST_PER_RUN)
|
2014-03-22 18:54:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lst_len == 0) {
|
|
|
|
free(lst);
|
|
|
|
return NSERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2014-06-04 01:30:04 +04:00
|
|
|
/** \todo sort list here */
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
*lst_len_out = lst_len;
|
|
|
|
*lst_out = lst;
|
|
|
|
|
2014-06-04 01:30:04 +04:00
|
|
|
#undef MAX_PERSIST_PER_RUN
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/**
|
|
|
|
* Write an object to the backing store.
|
|
|
|
*
|
|
|
|
* \param object The object to put in the backing store.
|
|
|
|
* \param written_out The amount of data written out.
|
2014-11-27 16:31:43 +03:00
|
|
|
* \param elapsed The time in ms it took to complete the write to backing store.
|
2014-11-23 18:43:03 +03:00
|
|
|
* \return NSERROR_OK on success or appropriate error code.
|
|
|
|
*/
|
2014-03-22 18:54:51 +04:00
|
|
|
static nserror
|
2014-11-27 16:31:43 +03:00
|
|
|
write_backing_store(struct llcache_object *object,
|
|
|
|
size_t *written_out,
|
|
|
|
unsigned long *elapsed)
|
2014-03-22 18:54:51 +04:00
|
|
|
{
|
|
|
|
nserror ret;
|
|
|
|
uint8_t *metadata;
|
|
|
|
size_t metadatasize;
|
2014-11-27 00:29:13 +03:00
|
|
|
uint64_t startms = 0;
|
|
|
|
uint64_t endms = 1000;
|
2014-11-26 02:11:41 +03:00
|
|
|
|
2014-11-27 00:29:13 +03:00
|
|
|
nsu_getmonotonic_ms(&startms);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
/* put object data in backing store */
|
|
|
|
ret = guit->llcache->store(object->url,
|
|
|
|
BACKING_STORE_NONE,
|
|
|
|
object->source_data,
|
|
|
|
object->source_len);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
/* unable to put source data in backing store */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = llcache_serialise_metadata(object, &metadata, &metadatasize);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
/* There has been a metadata serialisation error. Ensure the
|
|
|
|
* already written data object is invalidated.
|
|
|
|
*/
|
|
|
|
guit->llcache->invalidate(object->url);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = guit->llcache->store(object->url,
|
|
|
|
BACKING_STORE_META,
|
|
|
|
metadata,
|
|
|
|
metadatasize);
|
2014-11-23 02:56:13 +03:00
|
|
|
guit->llcache->release(object->url, BACKING_STORE_META);
|
2014-03-22 18:54:51 +04:00
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
/* There has been an error putting the metadata in the
|
|
|
|
* backing store. Ensure the data object is invalidated.
|
|
|
|
*/
|
|
|
|
guit->llcache->invalidate(object->url);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-27 00:29:13 +03:00
|
|
|
nsu_getmonotonic_ms(&endms);
|
2014-11-26 02:11:41 +03:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
object->store_state = LLCACHE_STATE_DISC;
|
|
|
|
|
|
|
|
*written_out = object->source_len + metadatasize;
|
|
|
|
|
2014-11-27 00:29:13 +03:00
|
|
|
/* by ignoring the overflow this assumes the writeout took
|
|
|
|
* less than 5 weeks.
|
|
|
|
*/
|
|
|
|
*elapsed = endms - startms;
|
2014-11-26 02:11:41 +03:00
|
|
|
|
2014-12-01 12:18:31 +03:00
|
|
|
/* ensure the writeout is reported to have taken at least the
|
|
|
|
* minimal amount of time
|
|
|
|
*/
|
|
|
|
if (*elapsed == 0) {
|
|
|
|
*elapsed = 1;
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-03 23:13:10 +03:00
|
|
|
/**
|
|
|
|
* Check for overall write performance.
|
|
|
|
*
|
|
|
|
* If the overall write bandwidth has fallen below a useful level for
|
|
|
|
* the backing store to be effective disable it.
|
|
|
|
*
|
2015-04-30 16:28:18 +03:00
|
|
|
* It is important to ensure a useful amount of data has been written
|
|
|
|
* before calculating bandwidths otherwise tiny files taking a
|
|
|
|
* disproportionately long time to write might trigger this erroneously.
|
|
|
|
*
|
2014-12-03 23:13:10 +03:00
|
|
|
* \param p The context pointer passed to the callback.
|
|
|
|
*/
|
|
|
|
static void llcache_persist_slowcheck(void *p)
|
|
|
|
{
|
2015-04-30 16:28:18 +03:00
|
|
|
uint64_t total_bandwidth; /* total bandwidth */
|
|
|
|
|
|
|
|
if (llcache->total_written > (2 * llcache->minimum_bandwidth)) {
|
2014-12-03 23:13:10 +03:00
|
|
|
|
2015-04-30 16:28:18 +03:00
|
|
|
total_bandwidth = (llcache->total_written * 1000) / llcache->total_elapsed;
|
|
|
|
|
|
|
|
if (total_bandwidth < llcache->minimum_bandwidth) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO,
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
"Current bandwidth %"PRIu64" less than minimum %"PRIsizet,
|
|
|
|
total_bandwidth,
|
|
|
|
llcache->minimum_bandwidth);
|
2015-04-30 16:28:18 +03:00
|
|
|
guit->llcache->finalise();
|
|
|
|
}
|
2014-12-03 23:13:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/**
|
2014-11-23 18:43:03 +03:00
|
|
|
* Possibly write objects data to backing store.
|
|
|
|
*
|
|
|
|
* \param p The context pointer passed to the callback.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
static void llcache_persist(void *p)
|
|
|
|
{
|
|
|
|
nserror ret;
|
2014-11-30 02:56:33 +03:00
|
|
|
struct llcache_object **lst; /* candidate object list */
|
|
|
|
int lst_count; /* number of candidates in list */
|
|
|
|
int idx; /* current candidate object index in list */
|
2014-11-30 03:27:05 +03:00
|
|
|
int next = -1; /* when the next run should be scheduled for */
|
2014-11-30 02:56:33 +03:00
|
|
|
|
|
|
|
unsigned long write_limit; /* max number of bytes to write in this run*/
|
|
|
|
|
|
|
|
size_t written; /* all bytes written for a single object */
|
|
|
|
unsigned long elapsed; /* how long writing an object took */
|
|
|
|
|
|
|
|
size_t total_written = 0; /* total bytes written in this run */
|
|
|
|
unsigned long total_elapsed = 1; /* total ms used to write bytes */
|
|
|
|
unsigned long total_bandwidth = 0; /* total bandwidth */
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
ret = build_candidate_list(&lst, &lst_count);
|
2014-11-26 02:11:41 +03:00
|
|
|
if (ret != NSERROR_OK) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Unable to construct candidate list for persistent writeout");
|
2014-11-26 02:11:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2014-11-28 22:17:42 +03:00
|
|
|
write_limit = (llcache->maximum_bandwidth * llcache->time_quantum) / 1000;
|
|
|
|
|
2016-11-20 18:38:14 +03:00
|
|
|
/* obtained a candidate list, make each object persistent in turn */
|
2014-11-26 02:11:41 +03:00
|
|
|
for (idx = 0; idx < lst_count; idx++) {
|
2014-11-30 02:56:33 +03:00
|
|
|
ret = write_backing_store(lst[idx], &written, &elapsed);
|
2014-12-01 14:32:33 +03:00
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-20 18:38:14 +03:00
|
|
|
/* successfully wrote object to backing store */
|
2014-12-01 14:32:33 +03:00
|
|
|
total_written += written;
|
|
|
|
total_elapsed += elapsed;
|
|
|
|
total_bandwidth = (total_written * 1000) / total_elapsed;
|
2014-12-03 23:13:10 +03:00
|
|
|
|
2018-08-19 01:44:15 +03:00
|
|
|
NSLOG(llcache, DEBUG,
|
2022-06-03 04:34:22 +03:00
|
|
|
"Wrote %"PRIsizet" bytes in %lums bw:%lu %s",
|
2018-08-19 01:44:15 +03:00
|
|
|
written, elapsed, (written * 1000) / elapsed,
|
|
|
|
nsurl_access(lst[idx]->url) );
|
2014-11-30 03:27:05 +03:00
|
|
|
|
2014-12-01 14:32:33 +03:00
|
|
|
/* check to for the time quantum or the size
|
|
|
|
* (bandwidth) for this run being exceeded.
|
|
|
|
*/
|
|
|
|
if (total_elapsed > llcache->time_quantum) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO, "Overran timeslot");
|
2014-12-01 14:32:33 +03:00
|
|
|
/* writeout has exhausted the available time.
|
|
|
|
* Either the writeout is slow or the last
|
|
|
|
* object was very large.
|
|
|
|
*/
|
|
|
|
if (total_bandwidth < llcache->minimum_bandwidth) {
|
2014-12-03 23:13:10 +03:00
|
|
|
/* Writeout was slow in this time quantum.
|
|
|
|
* Schedule a check in the future to see if
|
|
|
|
* overall performance is too slow to be useful.
|
|
|
|
*/
|
2016-04-16 00:50:28 +03:00
|
|
|
guit->misc->schedule(
|
2016-02-08 14:38:41 +03:00
|
|
|
llcache->time_quantum * 100,
|
|
|
|
llcache_persist_slowcheck,
|
|
|
|
NULL);
|
2014-12-01 14:32:33 +03:00
|
|
|
break;
|
|
|
|
} else {
|
2014-11-30 02:56:33 +03:00
|
|
|
if (total_bandwidth > llcache->maximum_bandwidth) {
|
2014-12-01 14:32:33 +03:00
|
|
|
/* fast writeout of large file
|
|
|
|
* so calculate delay as if
|
|
|
|
* write happened only at max
|
|
|
|
* limit
|
2014-11-30 02:56:33 +03:00
|
|
|
*/
|
|
|
|
next = ((total_written * llcache->time_quantum) / write_limit) - total_elapsed;
|
|
|
|
} else {
|
2014-12-01 14:32:33 +03:00
|
|
|
next = llcache->time_quantum;
|
2014-11-30 02:56:33 +03:00
|
|
|
}
|
2014-11-28 22:17:42 +03:00
|
|
|
break;
|
|
|
|
}
|
2014-12-01 14:32:33 +03:00
|
|
|
} else if (total_written > write_limit) {
|
|
|
|
/* The bandwidth limit has been reached. */
|
|
|
|
|
|
|
|
if (total_bandwidth > llcache->maximum_bandwidth) {
|
|
|
|
/* fast writeout of large file so
|
|
|
|
* calculate delay as if write
|
|
|
|
* happened only at max limit
|
|
|
|
*/
|
|
|
|
next = ((total_written * llcache->time_quantum) / write_limit) - total_elapsed;
|
|
|
|
} else {
|
|
|
|
next = llcache->time_quantum - total_elapsed;
|
|
|
|
}
|
|
|
|
break;
|
2014-11-26 02:11:41 +03:00
|
|
|
}
|
2014-12-01 14:32:33 +03:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
}
|
2014-11-30 03:27:05 +03:00
|
|
|
free(lst);
|
|
|
|
|
2014-12-01 14:32:33 +03:00
|
|
|
/* Completed list without running out of allowed bytes or time */
|
2014-11-30 03:27:05 +03:00
|
|
|
if (idx == lst_count) {
|
2014-12-01 14:32:33 +03:00
|
|
|
/* only reschedule if writing is making any progress at all */
|
|
|
|
if (total_written > 0) {
|
|
|
|
next = llcache->time_quantum - total_elapsed;
|
|
|
|
} else {
|
|
|
|
next = -1;
|
|
|
|
}
|
2014-11-30 03:27:05 +03:00
|
|
|
}
|
2014-11-26 02:11:41 +03:00
|
|
|
|
2014-12-03 23:13:10 +03:00
|
|
|
llcache->total_written += total_written;
|
|
|
|
llcache->total_elapsed += total_elapsed;
|
|
|
|
|
2018-08-19 01:44:15 +03:00
|
|
|
NSLOG(llcache, DEBUG,
|
2022-06-03 04:34:22 +03:00
|
|
|
"writeout size:%"PRIsizet" time:%lu bandwidth:%lubytes/s",
|
2018-08-19 01:44:15 +03:00
|
|
|
total_written, total_elapsed, total_bandwidth);
|
2014-11-28 22:17:42 +03:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Rescheduling writeout in %dms", next);
|
2016-04-16 00:50:28 +03:00
|
|
|
guit->misc->schedule(next, llcache_persist, NULL);
|
2014-03-22 18:54:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-04 16:41:19 +04:00
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Handler for fetch events
|
2010-04-04 16:41:19 +04:00
|
|
|
*
|
2014-11-23 18:43:03 +03:00
|
|
|
* \param msg Fetch event
|
|
|
|
* \param p Our private data
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static void llcache_fetch_callback(const fetch_msg *msg, void *p)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
llcache_object *object = p;
|
|
|
|
llcache_event event;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2019-06-15 23:13:09 +03:00
|
|
|
if (llcache == NULL) {
|
|
|
|
NSLOG(llcache, CRITICAL, "Callback happened after llcache finalisation");
|
|
|
|
assert(false);
|
|
|
|
/* In case assertions are off, return here */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Fetch event %d for %p", msg->type, object);
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
switch (msg->type) {
|
|
|
|
case FETCH_HEADER:
|
|
|
|
/* Received a fetch header */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_HEADERS;
|
2010-09-17 02:46:40 +04:00
|
|
|
|
2014-02-21 15:31:39 +04:00
|
|
|
error = llcache_fetch_process_header(object,
|
|
|
|
msg->data.header_or_data.buf,
|
2011-11-09 01:51:42 +04:00
|
|
|
msg->data.header_or_data.len);
|
2010-09-17 02:46:40 +04:00
|
|
|
break;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* 3xx responses */
|
|
|
|
case FETCH_REDIRECT:
|
|
|
|
/* Request resulted in a redirect */
|
2010-09-17 02:04:06 +04:00
|
|
|
|
|
|
|
/* Release candidate, if any */
|
|
|
|
if (object->candidate != NULL) {
|
|
|
|
object->candidate->candidate_count--;
|
|
|
|
object->candidate = NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-21 15:31:39 +04:00
|
|
|
error = llcache_fetch_redirect(object,
|
2011-11-09 01:51:42 +04:00
|
|
|
msg->data.redirect, &object);
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
2019-10-29 02:50:01 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
case FETCH_NOTMODIFIED:
|
|
|
|
/* Conditional request determined that cached object is fresh */
|
|
|
|
error = llcache_fetch_notmodified(object, &object);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Normal 2xx state machine */
|
|
|
|
case FETCH_DATA:
|
|
|
|
/* Received some data */
|
2014-02-21 15:31:39 +04:00
|
|
|
error = llcache_fetch_process_data(object,
|
2011-11-09 01:51:42 +04:00
|
|
|
msg->data.header_or_data.buf,
|
|
|
|
msg->data.header_or_data.len);
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
2019-10-29 02:50:01 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
case FETCH_FINISHED:
|
|
|
|
/* Finished fetching */
|
2010-04-12 01:26:20 +04:00
|
|
|
{
|
|
|
|
uint8_t *temp;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
2010-04-12 01:26:20 +04:00
|
|
|
/* Shrink source buffer to required size */
|
2014-02-21 15:31:39 +04:00
|
|
|
temp = realloc(object->source_data,
|
2010-04-12 01:26:20 +04:00
|
|
|
object->source_len);
|
2010-04-18 01:17:08 +04:00
|
|
|
/* If source_len is 0, then temp may be NULL */
|
|
|
|
if (temp != NULL || object->source_len == 0) {
|
2010-04-12 01:26:20 +04:00
|
|
|
object->source_data = temp;
|
|
|
|
object->source_alloc = object->source_len;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object_cache_update(object);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
/* record when the fetch finished */
|
|
|
|
object->cache.fin_time = time(NULL);
|
|
|
|
|
2018-04-22 01:54:04 +03:00
|
|
|
(void) llcache_hsts_update_policy(object);
|
|
|
|
|
2016-04-16 00:50:28 +03:00
|
|
|
guit->misc->schedule(5000, llcache_persist, NULL);
|
2010-04-12 01:26:20 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Out-of-band information */
|
2015-11-11 00:51:54 +03:00
|
|
|
case FETCH_TIMEDOUT:
|
|
|
|
/* Timed out while trying to fetch. */
|
2019-10-29 02:50:01 +03:00
|
|
|
error = llcache_fetch_timeout(object);
|
|
|
|
break;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
case FETCH_ERROR:
|
|
|
|
/* An error occurred while fetching */
|
2010-03-31 01:36:39 +04:00
|
|
|
/* The fetch has has already been cleaned up by the fetcher */
|
2010-04-15 00:42:03 +04:00
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
2010-03-28 16:56:39 +04:00
|
|
|
object->fetch.fetch = NULL;
|
2010-04-08 16:20:20 +04:00
|
|
|
|
2010-09-17 02:04:06 +04:00
|
|
|
/* Release candidate, if any */
|
|
|
|
if (object->candidate != NULL) {
|
|
|
|
object->candidate->candidate_count--;
|
|
|
|
object->candidate = NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-08 16:20:20 +04:00
|
|
|
/* Invalidate cache control data */
|
2010-09-16 23:47:15 +04:00
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/** \todo Consider using errorcode for something */
|
|
|
|
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
2019-08-05 22:56:07 +03:00
|
|
|
event.data.error.code = NSERROR_UNKNOWN;
|
|
|
|
event.data.error.msg = msg->data.error;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2010-04-14 14:56:44 +04:00
|
|
|
error = llcache_send_event_to_users(object, &event);
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
2019-10-29 02:50:01 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
case FETCH_PROGRESS:
|
|
|
|
/* Progress update */
|
|
|
|
event.type = LLCACHE_EVENT_PROGRESS;
|
2019-08-05 22:56:07 +03:00
|
|
|
event.data.progress_msg = msg->data.progress;
|
2011-11-09 01:51:42 +04:00
|
|
|
|
2010-04-14 14:56:44 +04:00
|
|
|
error = llcache_send_event_to_users(object, &event);
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
2019-10-29 02:50:01 +03:00
|
|
|
|
2019-08-05 20:11:13 +03:00
|
|
|
case FETCH_CERTS:
|
|
|
|
/* Certificate information from the fetch */
|
2019-11-30 14:41:19 +03:00
|
|
|
|
2020-05-24 19:01:05 +03:00
|
|
|
if (object->chain != NULL) {
|
|
|
|
cert_chain_free(object->chain);
|
|
|
|
object->chain = NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
/* Persist the chain onto our object */
|
|
|
|
error = cert_chain_dup(msg->data.chain, &object->chain);
|
|
|
|
if (error != NSERROR_OK) {
|
2020-05-04 20:29:33 +03:00
|
|
|
NSLOG(llcache, ERROR,
|
|
|
|
"Unable to duplicate cert chain into cache: %s",
|
|
|
|
messages_get_errorcode(error));
|
2020-02-23 19:06:52 +03:00
|
|
|
}
|
2020-05-04 20:29:33 +03:00
|
|
|
|
|
|
|
/* Now pass on the event */
|
|
|
|
event.type = LLCACHE_EVENT_GOT_CERTS;
|
|
|
|
event.data.chain = msg->data.chain;
|
|
|
|
|
|
|
|
error = llcache_send_event_to_users(object, &event);
|
|
|
|
|
2019-08-05 20:11:13 +03:00
|
|
|
break;
|
2019-10-29 02:50:01 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Events requiring action */
|
|
|
|
case FETCH_AUTH:
|
|
|
|
/* Need Authentication */
|
2010-09-17 02:04:06 +04:00
|
|
|
|
|
|
|
/* Release candidate, if any */
|
|
|
|
if (object->candidate != NULL) {
|
|
|
|
object->candidate->candidate_count--;
|
|
|
|
object->candidate = NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
error = llcache_fetch_auth(object, msg->data.auth.realm);
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
2019-10-29 02:50:01 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
case FETCH_CERT_ERR:
|
|
|
|
/* Something went wrong when validating TLS certificates */
|
2010-09-17 02:04:06 +04:00
|
|
|
|
|
|
|
/* Release candidate, if any */
|
|
|
|
if (object->candidate != NULL) {
|
|
|
|
object->candidate->candidate_count--;
|
|
|
|
object->candidate = NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-05 20:11:13 +03:00
|
|
|
error = llcache_fetch_cert_error(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
2019-10-29 02:50:01 +03:00
|
|
|
|
2013-01-05 03:13:23 +04:00
|
|
|
case FETCH_SSL_ERR:
|
|
|
|
/* TLS connection setup failed */
|
|
|
|
|
|
|
|
/* Release candidate, if any */
|
|
|
|
if (object->candidate != NULL) {
|
|
|
|
object->candidate->candidate_count--;
|
|
|
|
object->candidate = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = llcache_fetch_ssl_error(object);
|
|
|
|
break;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Deal with any errors reported by event handlers */
|
|
|
|
if (error != NSERROR_OK) {
|
2014-05-29 17:00:13 +04:00
|
|
|
if (error == NSERROR_NOMEM) {
|
|
|
|
/* attempt to purge the cache to free some
|
|
|
|
* memory. will not help this fetch, but may
|
|
|
|
* allow the UI to report errors etc.
|
|
|
|
*/
|
|
|
|
llcache_clean(true);
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
if (object->fetch.fetch != NULL) {
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
2010-04-15 00:42:03 +04:00
|
|
|
|
|
|
|
/* Invalidate cache control data */
|
2010-09-16 23:47:15 +04:00
|
|
|
llcache_invalidate_cache_control_data(object);
|
|
|
|
|
2010-04-15 00:42:03 +04:00
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-03 20:00:23 +04:00
|
|
|
|
|
|
|
/* There may be users which are not caught up so schedule ourselves */
|
|
|
|
llcache_users_not_caught_up();
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Find a user of a low-level cache object
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* \param handle External cache handle to search for
|
|
|
|
* \return Pointer to corresponding user, or NULL if not found
|
|
|
|
*/
|
2016-02-08 14:38:41 +03:00
|
|
|
static llcache_object_user *llcache_object_find_user(
|
|
|
|
const llcache_handle *handle)
|
2012-04-28 17:24:09 +04:00
|
|
|
{
|
|
|
|
llcache_object_user *user;
|
|
|
|
|
|
|
|
assert(handle->object != NULL);
|
|
|
|
|
|
|
|
for (user = handle->object->users; user != NULL; user = user->next) {
|
|
|
|
if (user->handle == handle)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a low-level cache object resides in a given list
|
|
|
|
*
|
|
|
|
* \param object Object to search for
|
|
|
|
* \param list List to search in
|
|
|
|
* \return True if object resides in list, false otherwise
|
|
|
|
*/
|
|
|
|
static bool llcache_object_in_list(const llcache_object *object,
|
|
|
|
const llcache_object *list)
|
|
|
|
{
|
|
|
|
while (list != NULL) {
|
|
|
|
if (list == object)
|
|
|
|
break;
|
|
|
|
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify users of an object's current state
|
|
|
|
*
|
|
|
|
* \param object Object to notify users about
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2012-04-28 17:24:09 +04:00
|
|
|
static nserror llcache_object_notify_users(llcache_object *object)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
nserror error;
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object_user *user, *next_user;
|
|
|
|
llcache_event event;
|
|
|
|
bool emitted_notify = false;
|
2010-09-16 23:47:15 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/**
|
2014-02-21 15:31:39 +04:00
|
|
|
* State transitions and event emission for users.
|
2012-04-28 17:24:09 +04:00
|
|
|
* Rows: user state. Cols: object state.
|
|
|
|
*
|
2014-11-09 15:50:30 +03:00
|
|
|
* User - Obj INIT HEADERS DATA COMPLETE
|
2012-04-28 17:24:09 +04:00
|
|
|
* INIT - T T* T*
|
|
|
|
* HEADERS - - T T*
|
|
|
|
* DATA - - M T
|
|
|
|
* COMPLETE - - - -
|
|
|
|
*
|
|
|
|
* T => transition user to object state
|
|
|
|
* M => no transition required, but may need to emit event
|
|
|
|
*
|
|
|
|
* The transitions marked with an asterisk can be removed by moving
|
|
|
|
* the user context into the subsequent state and then reevaluating.
|
|
|
|
*
|
|
|
|
* Events are issued as follows:
|
|
|
|
*
|
|
|
|
* HAD_HEADERS: on transition from HEADERS -> DATA state
|
|
|
|
* HAD_DATA : in DATA state, whenever there's new source data
|
|
|
|
* DONE : on transition from DATA -> COMPLETE state
|
|
|
|
*/
|
2010-04-11 17:58:12 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
for (user = object->users; user != NULL; user = next_user) {
|
|
|
|
/* Emit necessary events to bring the user up-to-date */
|
|
|
|
llcache_handle *handle = user->handle;
|
|
|
|
const llcache_fetch_state objstate = object->fetch.state;
|
2010-04-11 17:58:12 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Flag that this user is the current iteration target
|
|
|
|
* in case the client attempts to destroy it underneath us */
|
|
|
|
user->iterator_target = true;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* A note on the computation of next_user:
|
2014-02-21 15:31:39 +04:00
|
|
|
*
|
|
|
|
* Within this loop, we may make a number of calls to
|
2012-04-28 17:24:09 +04:00
|
|
|
* client code. Our contract with clients is that they
|
|
|
|
* can do whatever they like from within their callback
|
|
|
|
* handlers. This is so that we limit the pain of
|
|
|
|
* reentrancy to this module alone.
|
|
|
|
*
|
|
|
|
* One of the things a client can do from within its
|
|
|
|
* callback handler is to remove users from this object's
|
|
|
|
* user list. In the common case, the user they attempt
|
|
|
|
* to remove is the current iteration target, and we
|
|
|
|
* already protect against that causing problems here.
|
2014-02-21 15:31:39 +04:00
|
|
|
* However, no such protection exists if the client
|
2012-04-28 17:24:09 +04:00
|
|
|
* attempts to remove other users from this object's
|
|
|
|
* user list.
|
|
|
|
*
|
|
|
|
* Therefore, we cannot compute next_user up-front
|
|
|
|
* and expect it to remain valid across calls to
|
|
|
|
* client code (as the identity of the next user
|
|
|
|
* in the list may change underneath us). Instead,
|
|
|
|
* we must compute next_user at the point where we
|
|
|
|
* are about to cause another iteration of this loop
|
|
|
|
* (i.e. at the very end, and also at the points where
|
|
|
|
* continue is used)
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (handle->state != objstate) {
|
|
|
|
if (emitted_notify == false) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG,
|
|
|
|
"Notifying users of %p",
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
object);
|
2012-04-28 17:24:09 +04:00
|
|
|
emitted_notify = true;
|
|
|
|
}
|
2011-02-24 01:27:55 +03:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG,
|
|
|
|
"User %p state: %d Object state: %d",
|
|
|
|
user,
|
|
|
|
handle->state,
|
|
|
|
objstate);
|
2011-02-24 01:27:55 +03:00
|
|
|
}
|
2010-04-11 18:06:02 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* User: INIT, Obj: HEADERS, DATA, COMPLETE => User->HEADERS */
|
2014-02-21 15:31:39 +04:00
|
|
|
if (handle->state == LLCACHE_FETCH_INIT &&
|
2012-04-28 17:24:09 +04:00
|
|
|
objstate > LLCACHE_FETCH_INIT) {
|
|
|
|
handle->state = LLCACHE_FETCH_HEADERS;
|
2019-11-30 14:41:19 +03:00
|
|
|
|
|
|
|
/* Emit any certificate data we hold */
|
2020-02-23 19:06:52 +03:00
|
|
|
if (object->chain != NULL) {
|
2019-11-30 14:41:19 +03:00
|
|
|
event.type = LLCACHE_EVENT_GOT_CERTS;
|
2020-02-23 19:06:52 +03:00
|
|
|
event.data.chain = object->chain;
|
2019-11-30 14:41:19 +03:00
|
|
|
error = handle->cb(handle, &event, handle->pw);
|
|
|
|
} else {
|
|
|
|
error = NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user->queued_for_delete) {
|
|
|
|
next_user = user->next;
|
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_user_destroy(user);
|
|
|
|
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
} else if (error == NSERROR_NEED_DATA) {
|
|
|
|
/* User requested replay */
|
|
|
|
handle->state = LLCACHE_FETCH_HEADERS;
|
|
|
|
|
|
|
|
/* Continue with the next user -- we'll
|
|
|
|
* reemit the event next time round */
|
|
|
|
user->iterator_target = false;
|
|
|
|
next_user = user->next;
|
|
|
|
llcache_users_not_caught_up();
|
|
|
|
continue;
|
|
|
|
} else if (error != NSERROR_OK) {
|
|
|
|
user->iterator_target = false;
|
|
|
|
return error;
|
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
}
|
2011-09-27 15:07:32 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* User: HEADERS, Obj: DATA, COMPLETE => User->DATA */
|
|
|
|
if (handle->state == LLCACHE_FETCH_HEADERS &&
|
|
|
|
objstate > LLCACHE_FETCH_HEADERS) {
|
|
|
|
handle->state = LLCACHE_FETCH_DATA;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Emit HAD_HEADERS event */
|
|
|
|
event.type = LLCACHE_EVENT_HAD_HEADERS;
|
|
|
|
|
|
|
|
error = handle->cb(handle, &event, handle->pw);
|
|
|
|
|
|
|
|
if (user->queued_for_delete) {
|
|
|
|
next_user = user->next;
|
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_user_destroy(user);
|
|
|
|
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
} else if (error == NSERROR_NEED_DATA) {
|
|
|
|
/* User requested replay */
|
|
|
|
handle->state = LLCACHE_FETCH_HEADERS;
|
|
|
|
|
2014-02-21 15:31:39 +04:00
|
|
|
/* Continue with the next user -- we'll
|
2012-04-28 17:24:09 +04:00
|
|
|
* reemit the event next time round */
|
|
|
|
user->iterator_target = false;
|
|
|
|
next_user = user->next;
|
2014-06-03 20:00:23 +04:00
|
|
|
llcache_users_not_caught_up();
|
2012-04-28 17:24:09 +04:00
|
|
|
continue;
|
|
|
|
} else if (error != NSERROR_OK) {
|
|
|
|
user->iterator_target = false;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* User: DATA, Obj: DATA, COMPLETE, more source available */
|
|
|
|
if (handle->state == LLCACHE_FETCH_DATA &&
|
|
|
|
objstate >= LLCACHE_FETCH_DATA &&
|
|
|
|
object->source_len > handle->bytes) {
|
|
|
|
size_t orig_handle_read;
|
|
|
|
|
|
|
|
/* Construct HAD_DATA event */
|
|
|
|
event.type = LLCACHE_EVENT_HAD_DATA;
|
2014-02-21 15:31:39 +04:00
|
|
|
event.data.data.buf =
|
2012-04-28 17:24:09 +04:00
|
|
|
object->source_data + handle->bytes;
|
2014-02-21 15:31:39 +04:00
|
|
|
event.data.data.len =
|
2012-04-28 17:24:09 +04:00
|
|
|
object->source_len - handle->bytes;
|
|
|
|
|
|
|
|
/* Update record of last byte emitted */
|
2014-02-21 15:31:39 +04:00
|
|
|
if (object->fetch.flags &
|
2012-04-28 17:24:09 +04:00
|
|
|
LLCACHE_RETRIEVE_STREAM_DATA) {
|
2014-02-21 15:31:39 +04:00
|
|
|
/* Streaming, so reset to zero to
|
2012-04-28 17:24:09 +04:00
|
|
|
* minimise amount of cached source data.
|
|
|
|
* Additionally, we don't support replay
|
|
|
|
* when streaming. */
|
|
|
|
orig_handle_read = 0;
|
|
|
|
handle->bytes = object->source_len = 0;
|
|
|
|
} else {
|
|
|
|
orig_handle_read = handle->bytes;
|
|
|
|
handle->bytes = object->source_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Emit event */
|
|
|
|
error = handle->cb(handle, &event, handle->pw);
|
|
|
|
if (user->queued_for_delete) {
|
|
|
|
next_user = user->next;
|
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_user_destroy(user);
|
|
|
|
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
} else if (error == NSERROR_NEED_DATA) {
|
|
|
|
/* User requested replay */
|
|
|
|
handle->bytes = orig_handle_read;
|
|
|
|
|
2014-02-21 15:31:39 +04:00
|
|
|
/* Continue with the next user -- we'll
|
2012-04-28 17:24:09 +04:00
|
|
|
* reemit the data next time round */
|
|
|
|
user->iterator_target = false;
|
|
|
|
next_user = user->next;
|
2014-06-03 20:00:23 +04:00
|
|
|
llcache_users_not_caught_up();
|
2012-04-28 17:24:09 +04:00
|
|
|
continue;
|
|
|
|
} else if (error != NSERROR_OK) {
|
|
|
|
user->iterator_target = false;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* User: DATA, Obj: COMPLETE => User->COMPLETE */
|
|
|
|
if (handle->state == LLCACHE_FETCH_DATA &&
|
|
|
|
objstate > LLCACHE_FETCH_DATA) {
|
|
|
|
handle->state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
|
|
|
/* Emit DONE event */
|
|
|
|
event.type = LLCACHE_EVENT_DONE;
|
|
|
|
|
|
|
|
error = handle->cb(handle, &event, handle->pw);
|
|
|
|
if (user->queued_for_delete) {
|
|
|
|
next_user = user->next;
|
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_user_destroy(user);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
continue;
|
|
|
|
} else if (error == NSERROR_NEED_DATA) {
|
|
|
|
/* User requested replay */
|
|
|
|
handle->state = LLCACHE_FETCH_DATA;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-02-21 15:31:39 +04:00
|
|
|
/* Continue with the next user -- we'll
|
2012-04-28 17:24:09 +04:00
|
|
|
* reemit the event next time round */
|
|
|
|
user->iterator_target = false;
|
|
|
|
next_user = user->next;
|
2014-06-03 20:00:23 +04:00
|
|
|
llcache_users_not_caught_up();
|
2012-04-28 17:24:09 +04:00
|
|
|
continue;
|
|
|
|
} else if (error != NSERROR_OK) {
|
|
|
|
user->iterator_target = false;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* No longer the target of an iterator */
|
|
|
|
user->iterator_target = false;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
next_user = user->next;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-28 17:24:09 +04:00
|
|
|
* Make a snapshot of the current state of an llcache_object.
|
2010-03-28 16:56:39 +04:00
|
|
|
*
|
2012-04-28 17:24:09 +04:00
|
|
|
* This has the side-effect of the new object being non-cacheable,
|
|
|
|
* also not-fetching and not a candidate for any other object.
|
|
|
|
*
|
|
|
|
* Also note that this new object has no users and at least one
|
|
|
|
* should be assigned to it before llcache_clean is entered or it
|
|
|
|
* will be immediately cleaned up.
|
|
|
|
*
|
|
|
|
* \param object The object to take a snapshot of
|
|
|
|
* \param snapshot Pointer to receive snapshot of \a object
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2014-03-22 18:54:51 +04:00
|
|
|
static nserror
|
|
|
|
llcache_object_snapshot(llcache_object *object, llcache_object **snapshot)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object *newobj;
|
|
|
|
nserror error;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
error = llcache_object_new(object->url, &newobj);
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
newobj->source_alloc = newobj->source_len = object->source_len;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (object->source_len > 0) {
|
|
|
|
newobj->source_data = malloc(newobj->source_alloc);
|
|
|
|
if (newobj->source_data == NULL) {
|
|
|
|
llcache_object_destroy(newobj);
|
|
|
|
return NSERROR_NOMEM;
|
2011-02-17 20:50:14 +03:00
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
memcpy(newobj->source_data, object->source_data,
|
2012-04-28 17:24:09 +04:00
|
|
|
newobj->source_len);
|
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (object->num_headers > 0) {
|
2014-02-21 15:31:39 +04:00
|
|
|
newobj->headers = calloc(sizeof(llcache_header),
|
2012-04-28 17:24:09 +04:00
|
|
|
object->num_headers);
|
|
|
|
if (newobj->headers == NULL) {
|
|
|
|
llcache_object_destroy(newobj);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
while (newobj->num_headers < object->num_headers) {
|
2014-02-21 15:31:39 +04:00
|
|
|
llcache_header *nh =
|
2012-04-28 17:24:09 +04:00
|
|
|
&(newobj->headers[newobj->num_headers]);
|
2014-02-21 15:31:39 +04:00
|
|
|
llcache_header *oh =
|
2012-04-28 17:24:09 +04:00
|
|
|
&(object->headers[newobj->num_headers]);
|
|
|
|
newobj->num_headers += 1;
|
|
|
|
nh->name = strdup(oh->name);
|
|
|
|
nh->value = strdup(oh->value);
|
|
|
|
if (nh->name == NULL || nh->value == NULL) {
|
|
|
|
llcache_object_destroy(newobj);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2011-02-25 00:22:24 +03:00
|
|
|
}
|
2011-02-17 20:50:14 +03:00
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
if (object->chain != NULL) {
|
|
|
|
error = cert_chain_dup(object->chain, &newobj->chain);
|
|
|
|
if (error != NSERROR_OK) {
|
2019-11-30 14:41:19 +03:00
|
|
|
llcache_object_destroy(newobj);
|
2020-02-23 19:06:52 +03:00
|
|
|
return error;
|
2019-11-30 14:41:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
newobj->fetch.state = LLCACHE_FETCH_COMPLETE;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
*snapshot = newobj;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/**
|
|
|
|
* total ram usage of object
|
2014-11-23 18:43:03 +03:00
|
|
|
*
|
2016-11-20 18:38:14 +03:00
|
|
|
* \param object The object to calculate the total RAM usage of.
|
2014-11-23 18:43:03 +03:00
|
|
|
* \return The total RAM usage in bytes.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
static inline uint32_t
|
|
|
|
total_object_size(llcache_object *object)
|
|
|
|
{
|
|
|
|
uint32_t tot;
|
|
|
|
size_t hdrc;
|
|
|
|
|
|
|
|
tot = sizeof(*object);
|
|
|
|
tot += nsurl_length(object->url);
|
|
|
|
|
|
|
|
if (object->source_data != NULL) {
|
|
|
|
tot += object->source_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
tot += sizeof(llcache_header) * object->num_headers;
|
|
|
|
|
|
|
|
for (hdrc = 0; hdrc < object->num_headers; hdrc++) {
|
|
|
|
if (object->headers[hdrc].name != NULL) {
|
|
|
|
tot += strlen(object->headers[hdrc].name);
|
|
|
|
}
|
|
|
|
if (object->headers[hdrc].value != NULL) {
|
|
|
|
tot += strlen(object->headers[hdrc].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:06:52 +03:00
|
|
|
tot += cert_chain_size(object->chain);
|
2019-11-30 14:41:19 +03:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
return tot;
|
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
|
2021-03-26 02:31:37 +03:00
|
|
|
/**
|
|
|
|
* Catch up the cache users with state changes from fetchers.
|
|
|
|
*
|
|
|
|
* \param ignored We ignore this because all our state comes from llcache.
|
|
|
|
*/
|
|
|
|
static void llcache_catch_up_all_users(void *ignored)
|
|
|
|
{
|
|
|
|
llcache_object *object;
|
|
|
|
|
|
|
|
/* Assume after this we'll be all caught up. If any user of a handle
|
|
|
|
* defers then we'll invalidate all_caught_up and reschedule via
|
|
|
|
* llcache_users_not_caught_up()
|
|
|
|
*/
|
|
|
|
llcache->all_caught_up = true;
|
|
|
|
|
|
|
|
/* Catch new users up with state of objects */
|
|
|
|
for (object = llcache->cached_objects; object != NULL;
|
|
|
|
object = object->next) {
|
|
|
|
llcache_object_notify_users(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (object = llcache->uncached_objects; object != NULL;
|
|
|
|
object = object->next) {
|
|
|
|
llcache_object_notify_users(object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ask for ::llcache_catch_up_all_users to be scheduled ASAP to pump the
|
|
|
|
* user state machines.
|
|
|
|
*/
|
|
|
|
static void llcache_users_not_caught_up(void)
|
|
|
|
{
|
|
|
|
if (llcache->all_caught_up) {
|
|
|
|
llcache->all_caught_up = false;
|
|
|
|
guit->misc->schedule(0, llcache_catch_up_all_users, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* Public API *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-29 17:00:13 +04:00
|
|
|
/*
|
2012-04-28 17:24:09 +04:00
|
|
|
* Attempt to clean the cache
|
2014-03-22 18:54:51 +04:00
|
|
|
*
|
|
|
|
* The memory cache cleaning discards objects in order of increasing value.
|
2014-05-29 17:00:13 +04:00
|
|
|
*
|
|
|
|
* Exported interface documented in llcache.h
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2014-05-29 17:00:13 +04:00
|
|
|
void llcache_clean(bool purge)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object *object, *next;
|
|
|
|
uint32_t llcache_size = 0;
|
2013-01-07 22:45:29 +04:00
|
|
|
int remaining_lifetime;
|
2014-05-29 17:00:13 +04:00
|
|
|
uint32_t limit;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Attempting cache clean");
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-05-29 17:00:13 +04:00
|
|
|
/* If the cache is being purged set the size limit to zero. */
|
|
|
|
if (purge) {
|
|
|
|
limit = 0;
|
|
|
|
} else {
|
|
|
|
limit = llcache->limit;
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* Uncacheable objects with no users or fetches */
|
|
|
|
for (object = llcache->uncached_objects;
|
|
|
|
object != NULL;
|
|
|
|
object = next) {
|
2012-04-28 17:24:09 +04:00
|
|
|
next = object->next;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* The candidate count of uncacheable objects is always 0 */
|
2014-02-21 15:31:39 +04:00
|
|
|
if ((object->users == NULL) &&
|
2012-04-28 17:24:09 +04:00
|
|
|
(object->candidate_count == 0) &&
|
2019-08-05 22:56:07 +03:00
|
|
|
(object->fetch.fetch == NULL)) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Discarding uncachable object with no users (%p) %s",
|
2015-05-28 19:06:18 +03:00
|
|
|
object, nsurl_access(object->url));
|
2014-02-21 15:43:48 +04:00
|
|
|
|
2014-02-21 15:31:39 +04:00
|
|
|
llcache_object_remove_from_list(object,
|
2012-04-28 17:24:09 +04:00
|
|
|
&llcache->uncached_objects);
|
|
|
|
llcache_object_destroy(object);
|
|
|
|
} else {
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_size += total_object_size(object);
|
2012-04-28 17:24:09 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
/* Stale cacheable objects with no users or pending fetches */
|
|
|
|
for (object = llcache->cached_objects;
|
|
|
|
object != NULL;
|
|
|
|
object = next) {
|
2012-04-28 17:24:09 +04:00
|
|
|
next = object->next;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2016-02-08 14:38:41 +03:00
|
|
|
remaining_lifetime = llcache_object_rfc2616_remaining_lifetime(
|
|
|
|
&object->cache);
|
2013-01-07 22:45:29 +04:00
|
|
|
|
|
|
|
if ((object->users == NULL) &&
|
2012-04-28 17:24:09 +04:00
|
|
|
(object->candidate_count == 0) &&
|
|
|
|
(object->fetch.fetch == NULL) &&
|
2014-03-22 18:54:51 +04:00
|
|
|
(remaining_lifetime <= 0)) {
|
2014-11-23 02:56:13 +03:00
|
|
|
/* object is stale */
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG, "discarding stale cacheable object with no "
|
2016-02-08 14:38:41 +03:00
|
|
|
"users or pending fetches (%p) %s",
|
|
|
|
object, nsurl_access(object->url));
|
2014-02-21 15:43:48 +04:00
|
|
|
|
2013-01-07 22:45:29 +04:00
|
|
|
llcache_object_remove_from_list(object,
|
|
|
|
&llcache->cached_objects);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
if (object->store_state == LLCACHE_STATE_DISC) {
|
|
|
|
guit->llcache->invalidate(object->url);
|
|
|
|
}
|
|
|
|
|
2013-01-07 22:45:29 +04:00
|
|
|
llcache_object_destroy(object);
|
2014-03-22 18:54:51 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
} else {
|
2014-03-22 18:54:51 +04:00
|
|
|
/* object has users so account for the storage */
|
|
|
|
llcache_size += total_object_size(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* if the cache limit is exceeded try to make some objects
|
2016-11-20 18:38:14 +03:00
|
|
|
* persistent so their RAM can be reclaimed in the next
|
2014-03-22 18:54:51 +04:00
|
|
|
* step
|
2012-04-28 17:24:09 +04:00
|
|
|
*/
|
2014-05-29 17:00:13 +04:00
|
|
|
if (limit < llcache_size) {
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_persist(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Source data of fresh cacheable objects with no users, no
|
2016-11-20 18:38:14 +03:00
|
|
|
* pending fetches and pushed to persistent store while the
|
2014-03-22 18:54:51 +04:00
|
|
|
* cache exceeds the configured size.
|
|
|
|
*/
|
|
|
|
for (object = llcache->cached_objects;
|
2014-05-29 17:00:13 +04:00
|
|
|
((limit < llcache_size) && (object != NULL));
|
2014-03-22 18:54:51 +04:00
|
|
|
object = next) {
|
|
|
|
next = object->next;
|
|
|
|
if ((object->users == NULL) &&
|
|
|
|
(object->candidate_count == 0) &&
|
|
|
|
(object->fetch.fetch == NULL) &&
|
|
|
|
(object->store_state == LLCACHE_STATE_DISC)) {
|
2014-11-23 02:56:13 +03:00
|
|
|
guit->llcache->release(object->url, BACKING_STORE_NONE);
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
object->source_data = NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_size -= object->source_len;
|
2014-02-21 15:43:48 +04:00
|
|
|
|
2018-08-19 01:44:15 +03:00
|
|
|
NSLOG(llcache, DEBUG,
|
2022-06-03 04:34:22 +03:00
|
|
|
"Freeing source data for %p len:%"PRIsizet,
|
2018-08-19 01:44:15 +03:00
|
|
|
object, object->source_len);
|
2014-03-22 18:54:51 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* Fresh cacheable objects with no users, no pending fetches
|
2016-11-20 18:38:14 +03:00
|
|
|
* and pushed to persistent store while the cache exceeds
|
|
|
|
* the configured size. Effectively just the llcache object metadata.
|
2014-03-22 18:54:51 +04:00
|
|
|
*/
|
|
|
|
for (object = llcache->cached_objects;
|
2014-05-29 17:00:13 +04:00
|
|
|
((limit < llcache_size) && (object != NULL));
|
2014-03-22 18:54:51 +04:00
|
|
|
object = next) {
|
|
|
|
next = object->next;
|
|
|
|
if ((object->users == NULL) &&
|
|
|
|
(object->candidate_count == 0) &&
|
|
|
|
(object->fetch.fetch == NULL) &&
|
|
|
|
(object->store_state == LLCACHE_STATE_DISC) &&
|
|
|
|
(object->source_data == NULL)) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG,
|
2022-06-03 04:34:22 +03:00
|
|
|
"discarding backed object len:%"PRIsizet" age:%ld (%p) %s",
|
2017-09-04 16:31:12 +03:00
|
|
|
object->source_len,
|
2017-09-08 21:38:11 +03:00
|
|
|
(long)(time(NULL) - object->last_used),
|
2017-09-04 16:31:12 +03:00
|
|
|
object,
|
|
|
|
nsurl_access(object->url));
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
llcache_size -= total_object_size(object);
|
|
|
|
|
|
|
|
llcache_object_remove_from_list(object,
|
2012-04-28 17:24:09 +04:00
|
|
|
&llcache->cached_objects);
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_object_destroy(object);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fresh cacheable objects with no users or pending fetches
|
2014-11-23 02:56:13 +03:00
|
|
|
* while the cache exceeds the configured size. These are the
|
2016-11-20 18:38:14 +03:00
|
|
|
* most valuable objects as replacing them is a full network
|
2014-03-22 18:54:51 +04:00
|
|
|
* fetch
|
|
|
|
*/
|
|
|
|
for (object = llcache->cached_objects;
|
2014-05-29 17:00:13 +04:00
|
|
|
((limit < llcache_size) && (object != NULL));
|
2014-03-22 18:54:51 +04:00
|
|
|
object = next) {
|
|
|
|
next = object->next;
|
|
|
|
|
|
|
|
if ((object->users == NULL) &&
|
|
|
|
(object->candidate_count == 0) &&
|
|
|
|
(object->fetch.fetch == NULL) &&
|
|
|
|
(object->store_state == LLCACHE_STATE_RAM)) {
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, DEBUG,
|
2022-06-03 04:34:22 +03:00
|
|
|
"discarding fresh object len:%"PRIsizet" age:%ld (%p) %s",
|
2017-09-04 16:31:12 +03:00
|
|
|
object->source_len,
|
2017-09-08 22:47:22 +03:00
|
|
|
(long)(time(NULL) - object->last_used),
|
2017-09-04 16:31:12 +03:00
|
|
|
object,
|
|
|
|
nsurl_access(object->url));
|
2014-03-22 18:54:51 +04:00
|
|
|
|
|
|
|
llcache_size -= object->source_len + sizeof(*object);
|
|
|
|
|
|
|
|
llcache_object_remove_from_list(object,
|
|
|
|
&llcache->cached_objects);
|
|
|
|
llcache_object_destroy(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-17 12:29:39 +03:00
|
|
|
NSLOG(llcache, DEBUG, "Size: %"PRIu32" (limit: %"PRIu32")", llcache_size, limit);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/* Exported interface documented in content/llcache.h */
|
2014-02-21 15:31:39 +04:00
|
|
|
nserror
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache_initialise(const struct llcache_parameters *prm)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache = calloc(1, sizeof(struct llcache_s));
|
|
|
|
if (llcache == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
llcache->limit = prm->limit;
|
|
|
|
llcache->minimum_lifetime = prm->minimum_lifetime;
|
2014-11-28 22:17:42 +03:00
|
|
|
llcache->minimum_bandwidth = prm->minimum_bandwidth;
|
|
|
|
llcache->maximum_bandwidth = prm->maximum_bandwidth;
|
|
|
|
llcache->time_quantum = prm->time_quantum;
|
2015-11-11 00:51:54 +03:00
|
|
|
llcache->fetch_attempts = prm->fetch_attempts;
|
2014-06-03 20:00:23 +04:00
|
|
|
llcache->all_caught_up = true;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO,
|
2023-06-17 13:20:09 +03:00
|
|
|
"llcache initialising with a limit of %"PRIu32" bytes",
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
llcache->limit);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* backing store initialisation */
|
|
|
|
return guit->llcache->initialise(&prm->store);
|
2012-04-28 17:24:09 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
|
|
|
|
/* Exported interface documented in content/llcache.h */
|
2012-04-28 17:24:09 +04:00
|
|
|
void llcache_finalise(void)
|
|
|
|
{
|
|
|
|
llcache_object *object, *next;
|
2015-05-12 12:19:38 +03:00
|
|
|
uint64_t total_bandwidth = 0; /* total bandwidth */
|
2015-01-03 18:12:53 +03:00
|
|
|
|
2020-02-23 23:52:17 +03:00
|
|
|
/* Attempt to persist anything we have left lying around */
|
|
|
|
llcache_persist(NULL);
|
|
|
|
/* Now clear the persistence callback */
|
|
|
|
guit->misc->schedule(-1, llcache_persist, NULL);
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Clean uncached objects */
|
|
|
|
for (object = llcache->uncached_objects; object != NULL; object = next) {
|
|
|
|
llcache_object_user *user, *next_user;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
next = object->next;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
for (user = object->users; user != NULL; user = next_user) {
|
|
|
|
next_user = user->next;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (user->handle != NULL)
|
|
|
|
free(user->handle);
|
|
|
|
|
|
|
|
free(user);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Fetch system has already been destroyed */
|
|
|
|
object->fetch.fetch = NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object_destroy(object);
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Clean cached objects */
|
|
|
|
for (object = llcache->cached_objects; object != NULL; object = next) {
|
|
|
|
llcache_object_user *user, *next_user;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
next = object->next;
|
2010-10-28 02:12:00 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
for (user = object->users; user != NULL; user = next_user) {
|
|
|
|
next_user = user->next;
|
2010-10-28 02:12:00 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (user->handle != NULL)
|
|
|
|
free(user->handle);
|
2010-10-28 02:12:00 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
free(user);
|
|
|
|
}
|
2010-10-28 02:12:00 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Fetch system has already been destroyed */
|
2014-02-21 15:31:39 +04:00
|
|
|
object->fetch.fetch = NULL;
|
2011-02-24 13:14:50 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object_destroy(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2014-03-22 18:54:51 +04:00
|
|
|
/* backing store finalisation */
|
|
|
|
guit->llcache->finalise();
|
|
|
|
|
2015-05-08 17:42:49 +03:00
|
|
|
if (llcache->total_elapsed > 0) {
|
|
|
|
total_bandwidth = (llcache->total_written * 1000) /
|
|
|
|
llcache->total_elapsed;
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:31:12 +03:00
|
|
|
NSLOG(llcache, INFO,
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
"Backing store wrote %"PRIu64" bytes in %"PRIu64" ms ""(average %"PRIu64" bytes/second)",
|
|
|
|
llcache->total_written,
|
|
|
|
llcache->total_elapsed,
|
|
|
|
total_bandwidth);
|
2014-12-03 23:13:10 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
free(llcache);
|
|
|
|
llcache = NULL;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
|
2014-06-03 20:00:23 +04:00
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/* Exported interface documented in content/llcache.h */
|
2021-03-26 02:31:37 +03:00
|
|
|
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)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
nserror error;
|
|
|
|
llcache_object_user *user;
|
|
|
|
llcache_object *object;
|
2018-04-22 01:54:04 +03:00
|
|
|
nsurl *hsts_url;
|
|
|
|
bool hsts_in_use;
|
|
|
|
|
|
|
|
/* Perform HSTS transform */
|
|
|
|
error = llcache_hsts_transform_url(url, &hsts_url, &hsts_in_use);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
return error;
|
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
|
|
|
|
/* Can we fetch this URL at all? */
|
2018-04-22 01:54:04 +03:00
|
|
|
if (fetch_can_fetch(hsts_url) == false) {
|
|
|
|
nsurl_unref(hsts_url);
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_NO_FETCH_HANDLER;
|
2015-06-24 12:31:13 +03:00
|
|
|
}
|
2012-04-28 17:24:09 +04:00
|
|
|
|
|
|
|
/* Create a new object user */
|
|
|
|
error = llcache_object_user_new(cb, pw, &user);
|
2015-06-24 12:31:13 +03:00
|
|
|
if (error != NSERROR_OK) {
|
2018-04-22 01:54:04 +03:00
|
|
|
nsurl_unref(hsts_url);
|
2012-04-28 17:24:09 +04:00
|
|
|
return error;
|
2015-06-24 12:31:13 +03:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Retrieve a suitable object from the cache,
|
|
|
|
* creating a new one if needed. */
|
2018-04-22 07:09:24 +03:00
|
|
|
error = llcache_object_retrieve(hsts_url, flags, referer, post, 0,
|
|
|
|
hsts_in_use, &object);
|
2012-04-28 17:24:09 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
llcache_object_user_destroy(user);
|
2018-04-22 01:54:04 +03:00
|
|
|
nsurl_unref(hsts_url);
|
2012-04-28 17:24:09 +04:00
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Add user to object */
|
|
|
|
llcache_object_add_user(object, user);
|
|
|
|
|
|
|
|
*result = user->handle;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-06-03 20:00:23 +04:00
|
|
|
/* Users exist which are now not caught up! */
|
|
|
|
llcache_users_not_caught_up();
|
|
|
|
|
2018-04-22 01:54:04 +03:00
|
|
|
nsurl_unref(hsts_url);
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
|
|
|
|
/* Exported interface documented in content/llcache.h */
|
2012-04-28 17:24:09 +04:00
|
|
|
nserror llcache_handle_change_callback(llcache_handle *handle,
|
|
|
|
llcache_handle_callback cb, void *pw)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
handle->cb = cb;
|
|
|
|
handle->pw = pw;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
|
|
|
|
/* Exported interface documented in content/llcache.h */
|
2012-04-28 17:24:09 +04:00
|
|
|
nserror llcache_handle_release(llcache_handle *handle)
|
|
|
|
{
|
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
llcache_object *object = handle->object;
|
|
|
|
llcache_object_user *user = llcache_object_find_user(handle);
|
2010-04-17 00:08:27 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
assert(user != NULL);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (user->iterator_target) {
|
2014-02-21 15:31:39 +04:00
|
|
|
/* Can't remove / delete user object if it's
|
2012-04-28 17:24:09 +04:00
|
|
|
* the target of an iterator */
|
|
|
|
user->queued_for_delete = true;
|
|
|
|
} else {
|
|
|
|
/* Remove the user from the object and destroy it */
|
|
|
|
error = llcache_object_remove_user(object, user);
|
|
|
|
if (error == NSERROR_OK) {
|
|
|
|
error = llcache_object_user_destroy(user);
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
|
|
|
|
return error;
|
2012-04-28 17:24:09 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-11-23 18:43:03 +03:00
|
|
|
/* Exported interface documented in content/llcache.h */
|
2012-04-28 17:24:09 +04:00
|
|
|
nserror llcache_handle_clone(llcache_handle *handle, llcache_handle **result)
|
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
llcache_object_user *newuser;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
error = llcache_object_user_new(handle->cb, handle->pw, &newuser);
|
|
|
|
if (error == NSERROR_OK) {
|
|
|
|
llcache_object_add_user(handle->object, newuser);
|
|
|
|
newuser->handle->state = handle->state;
|
|
|
|
*result = newuser->handle;
|
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return error;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* See llcache.h for documentation */
|
|
|
|
nserror llcache_handle_abort(llcache_handle *handle)
|
|
|
|
{
|
|
|
|
llcache_object_user *user = llcache_object_find_user(handle);
|
|
|
|
llcache_object *object = handle->object, *newobject;
|
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
bool all_alone = true;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Determine if we are the only user */
|
|
|
|
if (user->prev != NULL)
|
|
|
|
all_alone = false;
|
|
|
|
if (user->next != NULL)
|
|
|
|
all_alone = false;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
if (all_alone == false) {
|
|
|
|
/* We must snapshot this object */
|
|
|
|
error = llcache_object_snapshot(object, &newobject);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Move across to the new object */
|
|
|
|
if (user->iterator_target) {
|
|
|
|
/* User is current iterator target, clone it */
|
2014-02-21 15:31:39 +04:00
|
|
|
llcache_object_user *newuser =
|
2012-04-28 17:24:09 +04:00
|
|
|
calloc(1, sizeof(llcache_object_user));
|
|
|
|
if (newuser == NULL) {
|
|
|
|
llcache_object_destroy(newobject);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2010-04-15 00:42:03 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Move handle across to clone */
|
|
|
|
newuser->handle = user->handle;
|
|
|
|
user->handle = NULL;
|
2010-04-17 00:00:10 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Mark user as needing deletion */
|
|
|
|
user->queued_for_delete = true;
|
2011-03-03 03:40:50 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object_add_user(newobject, newuser);
|
2010-04-17 00:00:10 +04:00
|
|
|
} else {
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_add_user(newobject, user);
|
2010-04-17 00:00:10 +04:00
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Add new object to uncached list */
|
2014-02-21 15:31:39 +04:00
|
|
|
llcache_object_add_to_list(newobject,
|
2012-04-28 17:24:09 +04:00
|
|
|
&llcache->uncached_objects);
|
2010-04-17 00:00:10 +04:00
|
|
|
} else {
|
2012-04-28 17:24:09 +04:00
|
|
|
/* We're the only user, so abort any fetch in progress */
|
|
|
|
if (object->fetch.fetch != NULL) {
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Invalidate cache control data */
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
2014-02-21 15:31:39 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* See llcache.h for documentation */
|
|
|
|
nserror llcache_handle_force_stream(llcache_handle *handle)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2012-04-28 17:24:09 +04:00
|
|
|
llcache_object_user *user = llcache_object_find_user(handle);
|
|
|
|
llcache_object *object = handle->object;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Cannot stream if there are multiple users */
|
|
|
|
if (user->prev != NULL || user->next != NULL)
|
|
|
|
return NSERROR_OK;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* Forcibly uncache this object */
|
|
|
|
if (llcache_object_in_list(object, llcache->cached_objects)) {
|
2014-02-21 15:31:39 +04:00
|
|
|
llcache_object_remove_from_list(object,
|
2012-04-28 17:24:09 +04:00
|
|
|
&llcache->cached_objects);
|
|
|
|
llcache_object_add_to_list(object, &llcache->uncached_objects);
|
|
|
|
}
|
2010-04-17 00:08:27 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
object->fetch.flags |= LLCACHE_RETRIEVE_STREAM_DATA;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* See llcache.h for documentation */
|
|
|
|
nserror llcache_handle_invalidate_cache_data(llcache_handle *handle)
|
|
|
|
{
|
2018-08-12 14:50:57 +03:00
|
|
|
if ((handle->object != NULL) &&
|
|
|
|
(handle->object->fetch.fetch == NULL) &&
|
|
|
|
(handle->object->cache.no_cache == LLCACHE_VALIDATE_FRESH)) {
|
|
|
|
/* mark the cached object as requiring validation */
|
2012-04-28 17:24:09 +04:00
|
|
|
handle->object->cache.no_cache = LLCACHE_VALIDATE_ONCE;
|
|
|
|
}
|
2011-03-03 03:40:50 +03:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* See llcache.h for documentation */
|
|
|
|
nsurl *llcache_handle_get_url(const llcache_handle *handle)
|
|
|
|
{
|
|
|
|
return handle->object != NULL ? handle->object->url : NULL;
|
|
|
|
}
|
2010-04-15 00:42:03 +04:00
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
/* See llcache.h for documentation */
|
|
|
|
const uint8_t *llcache_handle_get_source_data(const llcache_handle *handle,
|
|
|
|
size_t *size)
|
|
|
|
{
|
|
|
|
*size = handle->object != NULL ? handle->object->source_len : 0;
|
|
|
|
|
|
|
|
return handle->object != NULL ? handle->object->source_data : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See llcache.h for documentation */
|
2014-02-21 15:31:39 +04:00
|
|
|
const char *llcache_handle_get_header(const llcache_handle *handle,
|
2012-04-28 17:24:09 +04:00
|
|
|
const char *key)
|
|
|
|
{
|
|
|
|
const llcache_object *object = handle->object;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (object == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* About as trivial as possible */
|
|
|
|
for (i = 0; i < object->num_headers; i++) {
|
|
|
|
if (strcasecmp(key, object->headers[i].name) == 0)
|
|
|
|
return object->headers[i].value;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-28 17:24:09 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See llcache.h for documentation */
|
2014-02-21 15:31:39 +04:00
|
|
|
bool llcache_handle_references_same_object(const llcache_handle *a,
|
2012-04-28 17:24:09 +04:00
|
|
|
const llcache_handle *b)
|
|
|
|
{
|
|
|
|
return a->object == b->object;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|