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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Low-level resource cache (implementation)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include "content/fetch.h"
|
|
|
|
#include "content/llcache.h"
|
2010-04-17 00:00:10 +04:00
|
|
|
#include "content/urldb.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"
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
/** Define to enable tracing of llcache operations. */
|
2011-01-06 03:31:54 +03:00
|
|
|
#undef LLCACHE_TRACE
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/** State of a low-level cache object fetch */
|
|
|
|
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;
|
|
|
|
|
|
|
|
/** Type of low-level cache object */
|
|
|
|
typedef struct llcache_object llcache_object;
|
|
|
|
|
|
|
|
/** Handle to low-level cache object */
|
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Low-level cache object user record */
|
|
|
|
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
|
|
|
|
|
|
|
bool iterator_target; /**< This is the an iterator target */
|
|
|
|
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;
|
|
|
|
|
|
|
|
/** Low-level cache object fetch context */
|
|
|
|
typedef struct {
|
|
|
|
uint32_t flags; /**< Fetch flags */
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl *referer; /**< Referring URL, or NULL if none */
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_post_data *post; /**< POST data, or NULL for GET */
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
bool tried_with_auth; /**< Whether we've tried with auth */
|
2011-03-03 03:40:50 +03:00
|
|
|
|
|
|
|
bool outstanding_query; /**< Waiting for a query response */
|
2010-03-28 16:56:39 +04:00
|
|
|
} llcache_fetch_ctx;
|
|
|
|
|
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;
|
|
|
|
|
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 */
|
|
|
|
time_t date; /**< Date: response header */
|
|
|
|
time_t expires; /**< Expires: response header */
|
|
|
|
#define INVALID_AGE -1
|
|
|
|
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;
|
|
|
|
|
|
|
|
/** Low-level cache object */
|
|
|
|
/** \todo Consider whether a list is a sane container */
|
|
|
|
struct llcache_object {
|
|
|
|
llcache_object *prev; /**< Previous in list */
|
|
|
|
llcache_object *next; /**< Next in list */
|
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl *url; /**< Post-redirect URL for object */
|
2010-03-28 20:51:47 +04:00
|
|
|
bool has_query; /**< URL has a query segment */
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/** \todo We need a generic dynamic buffer object */
|
|
|
|
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 */
|
|
|
|
|
|
|
|
llcache_object_user *users; /**< List of users */
|
|
|
|
|
|
|
|
llcache_fetch_ctx fetch; /**< Fetch context for object */
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
llcache_header *headers; /**< Fetch headers */
|
|
|
|
size_t num_headers; /**< Number of fetch headers */
|
|
|
|
};
|
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
struct llcache_s {
|
|
|
|
/** Handler for fetch-related queries */
|
|
|
|
llcache_query_callback query_cb;
|
|
|
|
/** Data for fetch-related query handler */
|
|
|
|
void *query_cb_pw;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
/** Head of the low-level cached object list */
|
|
|
|
llcache_object *cached_objects;
|
|
|
|
|
|
|
|
/** Head of the low-level uncached object list */
|
|
|
|
llcache_object *uncached_objects;
|
|
|
|
|
|
|
|
uint32_t limit;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** low level cache state */
|
|
|
|
static struct llcache_s *llcache = NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
/* Static lwc_strings */
|
|
|
|
static lwc_string *llcache_file_lwc;
|
|
|
|
static lwc_string *llcache_about_lwc;
|
|
|
|
static lwc_string *llcache_resource_lwc;
|
|
|
|
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror llcache_object_user_new(llcache_handle_callback cb, void *pw,
|
|
|
|
llcache_object_user **user);
|
|
|
|
static nserror llcache_object_user_destroy(llcache_object_user *user);
|
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
static nserror llcache_object_retrieve(nsurl *url, uint32_t flags,
|
|
|
|
nsurl *referer, const llcache_post_data *post,
|
2010-04-11 17:58:12 +04:00
|
|
|
uint32_t redirect_count, llcache_object **result);
|
2011-09-28 15:26:30 +04:00
|
|
|
static nserror llcache_object_retrieve_from_cache(nsurl *url,
|
|
|
|
uint32_t flags, nsurl *referer,
|
2010-04-11 17:58:12 +04:00
|
|
|
const llcache_post_data *post, uint32_t redirect_count,
|
|
|
|
llcache_object **result);
|
2010-03-28 16:56:39 +04:00
|
|
|
static bool llcache_object_is_fresh(const llcache_object *object);
|
|
|
|
static nserror llcache_object_cache_update(llcache_object *object);
|
2010-09-16 23:33:18 +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);
|
|
|
|
static nserror llcache_object_fetch(llcache_object *object, uint32_t flags,
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl *referer, const llcache_post_data *post,
|
2010-04-11 17:58:12 +04:00
|
|
|
uint32_t redirect_count);
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror llcache_object_refetch(llcache_object *object);
|
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
static nserror llcache_object_new(nsurl *url, llcache_object **result);
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror llcache_object_destroy(llcache_object *object);
|
|
|
|
static nserror llcache_object_add_user(llcache_object *object,
|
|
|
|
llcache_object_user *user);
|
|
|
|
static nserror llcache_object_remove_user(llcache_object *object,
|
|
|
|
llcache_object_user *user);
|
2010-12-16 00:15:49 +03:00
|
|
|
static llcache_object_user *llcache_object_find_user(
|
|
|
|
const llcache_handle *handle);
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror llcache_object_add_to_list(llcache_object *object,
|
|
|
|
llcache_object **list);
|
|
|
|
static nserror llcache_object_remove_from_list(llcache_object *object,
|
|
|
|
llcache_object **list);
|
2010-04-06 01:35:38 +04:00
|
|
|
static bool llcache_object_in_list(const llcache_object *object,
|
|
|
|
const llcache_object *list);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
static nserror llcache_object_notify_users(llcache_object *object);
|
|
|
|
|
2010-04-04 16:41:19 +04:00
|
|
|
static nserror llcache_object_snapshot(llcache_object *object,
|
|
|
|
llcache_object **snapshot);
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror llcache_post_data_clone(const llcache_post_data *orig,
|
|
|
|
llcache_post_data **clone);
|
|
|
|
|
|
|
|
static nserror llcache_query_handle_response(bool proceed, void *cbpw);
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
static void llcache_fetch_callback(const fetch_msg *msg, void *p);
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror llcache_fetch_redirect(llcache_object *object,
|
|
|
|
const char *target, llcache_object **replacement);
|
|
|
|
static nserror llcache_fetch_notmodified(llcache_object *object,
|
|
|
|
llcache_object **replacement);
|
2011-11-09 01:51:42 +04:00
|
|
|
static nserror llcache_fetch_split_header(const uint8_t *data, size_t len,
|
2010-03-28 16:56:39 +04:00
|
|
|
char **name, char **value);
|
|
|
|
static nserror llcache_fetch_parse_header(llcache_object *object,
|
2011-11-09 01:51:42 +04:00
|
|
|
const uint8_t *data, size_t len, char **name, char **value);
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror llcache_fetch_process_header(llcache_object *object,
|
2011-11-09 01:51:42 +04:00
|
|
|
const uint8_t *data, size_t len);
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror llcache_fetch_process_data(llcache_object *object,
|
|
|
|
const uint8_t *data, size_t len);
|
|
|
|
static nserror llcache_fetch_auth(llcache_object *object,
|
|
|
|
const char *realm);
|
|
|
|
static nserror llcache_fetch_cert_error(llcache_object *object,
|
|
|
|
const struct ssl_cert_info *certs, size_t num);
|
|
|
|
|
2010-10-28 02:12:00 +04:00
|
|
|
/* Destroy headers */
|
|
|
|
static inline void llcache_destroy_headers(llcache_object *object)
|
|
|
|
{
|
|
|
|
while (object->num_headers > 0) {
|
|
|
|
object->num_headers--;
|
|
|
|
|
|
|
|
free(object->headers[object->num_headers].name);
|
|
|
|
free(object->headers[object->num_headers].value);
|
|
|
|
}
|
|
|
|
free(object->headers);
|
|
|
|
object->headers = NULL;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-09-16 23:47:15 +04:00
|
|
|
/* Invalidate cache control data */
|
|
|
|
static inline void llcache_invalidate_cache_control_data(llcache_object *object)
|
|
|
|
{
|
|
|
|
free(object->cache.etag);
|
|
|
|
memset(&(object->cache), 0, sizeof(llcache_cache_control));
|
2011-02-24 13:14:50 +03:00
|
|
|
|
|
|
|
object->cache.age = INVALID_AGE;
|
|
|
|
object->cache.max_age = INVALID_AGE;
|
2010-09-16 23:47:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/******************************************************************************
|
2010-03-28 20:00:54 +04:00
|
|
|
* Public API *
|
2010-03-28 16:56:39 +04:00
|
|
|
******************************************************************************/
|
|
|
|
|
2010-03-29 03:25:21 +04:00
|
|
|
/* See llcache.h for documentation */
|
2011-09-10 04:55:39 +04:00
|
|
|
nserror
|
|
|
|
llcache_initialise(llcache_query_callback cb, void *pw, uint32_t llcache_limit)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2011-09-10 04:55:39 +04:00
|
|
|
llcache = calloc(1, sizeof(struct llcache_s));
|
|
|
|
if (llcache == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
llcache->query_cb = cb;
|
|
|
|
llcache->query_cb_pw = pw;
|
|
|
|
llcache->limit = llcache_limit;
|
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
/* Create static scheme strings */
|
|
|
|
if (lwc_intern_string("file", SLEN("file"),
|
|
|
|
&llcache_file_lwc) != lwc_error_ok)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
if (lwc_intern_string("about", SLEN("about"),
|
|
|
|
&llcache_about_lwc) != lwc_error_ok)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
if (lwc_intern_string("resource", SLEN("resource"),
|
|
|
|
&llcache_resource_lwc) != lwc_error_ok)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
LOG(("llcache initialised with a limit of %d bytes", llcache_limit));
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-27 10:48:09 +04:00
|
|
|
/* See llcache.h for documentation */
|
|
|
|
void llcache_finalise(void)
|
|
|
|
{
|
|
|
|
llcache_object *object, *next;
|
|
|
|
|
|
|
|
/* Clean uncached objects */
|
2011-09-10 04:55:39 +04:00
|
|
|
for (object = llcache->uncached_objects; object != NULL; object = next) {
|
2010-04-27 10:48:09 +04:00
|
|
|
llcache_object_user *user, *next_user;
|
|
|
|
|
|
|
|
next = object->next;
|
|
|
|
|
|
|
|
for (user = object->users; user != NULL; user = next_user) {
|
|
|
|
next_user = user->next;
|
|
|
|
|
2010-12-16 00:15:49 +03:00
|
|
|
if (user->handle != NULL)
|
|
|
|
free(user->handle);
|
|
|
|
|
2010-04-27 10:48:09 +04:00
|
|
|
free(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fetch system has already been destroyed */
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
|
|
|
llcache_object_destroy(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean cached objects */
|
2011-09-10 04:55:39 +04:00
|
|
|
for (object = llcache->cached_objects; object != NULL; object = next) {
|
2010-04-27 10:48:09 +04:00
|
|
|
llcache_object_user *user, *next_user;
|
|
|
|
|
|
|
|
next = object->next;
|
|
|
|
|
|
|
|
for (user = object->users; user != NULL; user = next_user) {
|
|
|
|
next_user = user->next;
|
|
|
|
|
2010-12-16 00:15:49 +03:00
|
|
|
if (user->handle != NULL)
|
|
|
|
free(user->handle);
|
|
|
|
|
2010-04-27 10:48:09 +04:00
|
|
|
free(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fetch system has already been destroyed */
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
|
|
|
llcache_object_destroy(object);
|
|
|
|
}
|
2011-09-10 04:55:39 +04:00
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
/* Unref static scheme lwc strings */
|
|
|
|
lwc_string_unref(llcache_file_lwc);
|
|
|
|
lwc_string_unref(llcache_about_lwc);
|
|
|
|
lwc_string_unref(llcache_resource_lwc);
|
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
free(llcache);
|
|
|
|
llcache = NULL;
|
2010-04-27 10:48:09 +04:00
|
|
|
}
|
|
|
|
|
2010-03-29 03:25:21 +04:00
|
|
|
/* See llcache.h for documentation */
|
2010-03-28 16:56:39 +04:00
|
|
|
nserror llcache_poll(void)
|
|
|
|
{
|
|
|
|
llcache_object *object;
|
|
|
|
|
2010-03-28 20:00:54 +04:00
|
|
|
fetch_poll();
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Catch new users up with state of objects */
|
2011-09-10 04:55:39 +04:00
|
|
|
for (object = llcache->cached_objects; object != NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
object = object->next) {
|
|
|
|
llcache_object_notify_users(object);
|
|
|
|
}
|
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
for (object = llcache->uncached_objects; object != NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
object = object->next) {
|
|
|
|
llcache_object_notify_users(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-29 03:25:21 +04:00
|
|
|
/* See llcache.h for documentation */
|
2011-09-28 15:26:30 +04:00
|
|
|
nserror llcache_handle_retrieve(nsurl *url, uint32_t flags,
|
|
|
|
nsurl *referer, const llcache_post_data *post,
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_handle_callback cb, void *pw,
|
|
|
|
llcache_handle **result)
|
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
llcache_object_user *user;
|
|
|
|
llcache_object *object;
|
|
|
|
|
|
|
|
/* Can we fetch this URL at all? */
|
2011-09-28 15:26:30 +04:00
|
|
|
if (fetch_can_fetch(url) == false)
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_NO_FETCH_HANDLER;
|
|
|
|
|
|
|
|
/* Create a new object user */
|
|
|
|
error = llcache_object_user_new(cb, pw, &user);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
/* Retrieve a suitable object from the cache,
|
|
|
|
* creating a new one if needed. */
|
2010-04-11 17:58:12 +04:00
|
|
|
error = llcache_object_retrieve(url, flags, referer, post, 0, &object);
|
2010-03-28 16:56:39 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
llcache_object_user_destroy(user);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add user to object */
|
|
|
|
llcache_object_add_user(object, user);
|
|
|
|
|
2010-12-16 00:15:49 +03:00
|
|
|
*result = user->handle;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-29 03:25:21 +04:00
|
|
|
/* See llcache.h for documentation */
|
2010-03-28 16:56:39 +04:00
|
|
|
nserror llcache_handle_change_callback(llcache_handle *handle,
|
|
|
|
llcache_handle_callback cb, void *pw)
|
|
|
|
{
|
|
|
|
handle->cb = cb;
|
|
|
|
handle->pw = pw;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-29 03:25:21 +04:00
|
|
|
/* See llcache.h for documentation */
|
2010-03-28 16:56:39 +04:00
|
|
|
nserror llcache_handle_release(llcache_handle *handle)
|
|
|
|
{
|
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
llcache_object *object = handle->object;
|
2010-12-16 00:15:49 +03:00
|
|
|
llcache_object_user *user = llcache_object_find_user(handle);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-12-16 00:15:49 +03:00
|
|
|
assert(user != NULL);
|
|
|
|
|
|
|
|
if (user->iterator_target) {
|
|
|
|
/* Can't remove / delete user object if it's
|
|
|
|
* 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) {
|
2010-03-28 16:56:39 +04:00
|
|
|
error = llcache_object_user_destroy(user);
|
2010-12-16 00:15:49 +03:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2010-04-04 16:41:19 +04:00
|
|
|
/* See llcache.h for documentation */
|
|
|
|
nserror llcache_handle_clone(llcache_handle *handle, llcache_handle **result)
|
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
llcache_object_user *newuser;
|
|
|
|
|
|
|
|
error = llcache_object_user_new(handle->cb, handle->pw, &newuser);
|
|
|
|
if (error == NSERROR_OK) {
|
|
|
|
llcache_object_add_user(handle->object, newuser);
|
2010-12-16 00:15:49 +03:00
|
|
|
newuser->handle->state = handle->state;
|
|
|
|
*result = newuser->handle;
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See llcache.h for documentation */
|
|
|
|
nserror llcache_handle_abort(llcache_handle *handle)
|
|
|
|
{
|
2010-12-16 00:15:49 +03:00
|
|
|
llcache_object_user *user = llcache_object_find_user(handle);
|
2010-04-04 16:41:19 +04:00
|
|
|
llcache_object *object = handle->object, *newobject;
|
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
bool all_alone = true;
|
|
|
|
|
|
|
|
/* Determine if we are the only user */
|
|
|
|
if (user->prev != NULL)
|
|
|
|
all_alone = false;
|
|
|
|
if (user->next != NULL)
|
|
|
|
all_alone = false;
|
|
|
|
|
|
|
|
if (all_alone == false) {
|
|
|
|
/* We must snapshot this object */
|
|
|
|
error = llcache_object_snapshot(object, &newobject);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2010-04-04 16:41:19 +04:00
|
|
|
/* Move across to the new object */
|
2010-12-16 00:15:49 +03:00
|
|
|
if (user->iterator_target) {
|
|
|
|
/* User is current iterator target, clone it */
|
|
|
|
llcache_object_user *newuser =
|
|
|
|
calloc(1, sizeof(llcache_object_user));
|
|
|
|
if (newuser == NULL) {
|
|
|
|
llcache_object_destroy(newobject);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move handle across to clone */
|
|
|
|
newuser->handle = user->handle;
|
|
|
|
user->handle = NULL;
|
|
|
|
|
|
|
|
/* Mark user as needing deletion */
|
|
|
|
user->queued_for_delete = true;
|
|
|
|
|
|
|
|
llcache_object_add_user(newobject, newuser);
|
|
|
|
} else {
|
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_add_user(newobject, user);
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
|
|
|
|
/* Add new object to uncached list */
|
2010-04-08 11:08:16 +04:00
|
|
|
llcache_object_add_to_list(newobject,
|
2011-09-10 04:55:39 +04:00
|
|
|
&llcache->uncached_objects);
|
2010-04-04 16:41:19 +04:00
|
|
|
} else {
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
|
|
|
/* Invalidate cache control data */
|
2010-09-16 23:47:15 +04:00
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2010-04-06 01:35:38 +04:00
|
|
|
/* See llcache.h for documentation */
|
|
|
|
nserror llcache_handle_force_stream(llcache_handle *handle)
|
|
|
|
{
|
2010-12-16 00:15:49 +03:00
|
|
|
llcache_object_user *user = llcache_object_find_user(handle);
|
2010-04-06 01:35:38 +04:00
|
|
|
llcache_object *object = handle->object;
|
|
|
|
|
|
|
|
/* Cannot stream if there are multiple users */
|
|
|
|
if (user->prev != NULL || user->next != NULL)
|
|
|
|
return NSERROR_OK;
|
|
|
|
|
|
|
|
/* Forcibly uncache this object */
|
2011-09-10 04:55:39 +04:00
|
|
|
if (llcache_object_in_list(object, llcache->cached_objects)) {
|
2010-04-06 01:35:38 +04:00
|
|
|
llcache_object_remove_from_list(object,
|
2011-09-10 04:55:39 +04:00
|
|
|
&llcache->cached_objects);
|
|
|
|
llcache_object_add_to_list(object, &llcache->uncached_objects);
|
2010-04-06 01:35:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
object->fetch.flags |= LLCACHE_RETRIEVE_STREAM_DATA;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-17 00:42:10 +04:00
|
|
|
/* See llcache.h for documentation */
|
|
|
|
nserror llcache_handle_invalidate_cache_data(llcache_handle *handle)
|
|
|
|
{
|
2011-02-25 00:22:24 +03:00
|
|
|
if (handle->object != NULL && handle->object->fetch.fetch == NULL &&
|
|
|
|
handle->object->cache.no_cache ==
|
|
|
|
LLCACHE_VALIDATE_FRESH) {
|
|
|
|
handle->object->cache.no_cache = LLCACHE_VALIDATE_ONCE;
|
2010-04-17 00:42:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-29 03:25:21 +04:00
|
|
|
/* See llcache.h for documentation */
|
2011-09-28 18:21:35 +04:00
|
|
|
nsurl *llcache_handle_get_url(const llcache_handle *handle)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
2011-09-28 18:21:35 +04:00
|
|
|
return handle->object != NULL ? handle->object->url : NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
2010-03-29 03:25:21 +04:00
|
|
|
/* See llcache.h for documentation */
|
2010-03-28 16:56:39 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-03-29 03:25:21 +04:00
|
|
|
/* See llcache.h for documentation */
|
2010-03-28 16:56:39 +04:00
|
|
|
const char *llcache_handle_get_header(const llcache_handle *handle,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-29 03:25:21 +04:00
|
|
|
/* See llcache.h for documentation */
|
2010-03-28 16:56:39 +04:00
|
|
|
bool llcache_handle_references_same_object(const llcache_handle *a,
|
|
|
|
const llcache_handle *b)
|
|
|
|
{
|
|
|
|
return a->object == b->object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2010-03-28 20:00:54 +04:00
|
|
|
* Low-level cache internals *
|
2010-03-28 16:56:39 +04:00
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new object user
|
|
|
|
*
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param cb Callback routine
|
|
|
|
* \param pw Private data for callback
|
|
|
|
* \param user Pointer to location to receive result
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_object_user_new(llcache_handle_callback cb, void *pw,
|
|
|
|
llcache_object_user **user)
|
|
|
|
{
|
2010-12-16 00:15:49 +03:00
|
|
|
llcache_handle *h;
|
|
|
|
llcache_object_user *u;
|
|
|
|
|
|
|
|
h = calloc(1, sizeof(llcache_handle));
|
|
|
|
if (h == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
u = calloc(1, sizeof(llcache_object_user));
|
|
|
|
if (u == NULL) {
|
|
|
|
free(h);
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_NOMEM;
|
2010-12-16 00:15:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
h->cb = cb;
|
|
|
|
h->pw = pw;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-12-16 00:15:49 +03:00
|
|
|
u->handle = h;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
2010-12-16 00:15:49 +03:00
|
|
|
LOG(("Created user %p (%p, %p, %p)", u, h, (void *) cb, pw));
|
2010-03-29 10:27:37 +04:00
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
*user = u;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy an object user
|
|
|
|
*
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param user User to destroy
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*
|
|
|
|
* \pre User is not attached to an object
|
|
|
|
*/
|
|
|
|
nserror llcache_object_user_destroy(llcache_object_user *user)
|
|
|
|
{
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Destroyed user %p", user));
|
|
|
|
#endif
|
2010-12-13 11:25:54 +03:00
|
|
|
|
|
|
|
assert(user->next == NULL);
|
|
|
|
assert(user->prev == NULL);
|
|
|
|
|
2010-12-16 00:15:49 +03:00
|
|
|
if (user->handle != NULL)
|
|
|
|
free(user->handle);
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
free(user);
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-16 00:15:49 +03:00
|
|
|
/**
|
|
|
|
* Iterate the users of an object, calling their callbacks.
|
2010-04-14 14:56:44 +04:00
|
|
|
*
|
|
|
|
* \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)
|
|
|
|
{
|
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
llcache_object_user *user, *next_user;
|
|
|
|
|
|
|
|
user = object->users;
|
|
|
|
while (user != NULL) {
|
2010-12-16 00:15:49 +03:00
|
|
|
user->iterator_target = true;
|
|
|
|
|
|
|
|
error = user->handle->cb(user->handle, event,
|
|
|
|
user->handle->pw);
|
|
|
|
|
|
|
|
next_user = user->next;
|
|
|
|
|
|
|
|
user->iterator_target = false;
|
|
|
|
|
|
|
|
if (user->queued_for_delete) {
|
|
|
|
llcache_object_remove_user(object, user);
|
|
|
|
llcache_object_user_destroy(user);
|
|
|
|
}
|
|
|
|
|
2010-12-16 01:59:19 +03:00
|
|
|
if (error != NSERROR_OK)
|
|
|
|
break;
|
|
|
|
|
2010-04-14 14:56:44 +04:00
|
|
|
user = next_user;
|
|
|
|
}
|
2010-12-16 00:15:49 +03:00
|
|
|
|
2010-04-14 14:56:44 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Retrieve an object from the cache, fetching it if necessary.
|
|
|
|
*
|
2010-04-14 14:56:44 +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
|
2010-04-11 17:58:12 +04:00
|
|
|
* \param redirect_count Number of redirects followed so far
|
2010-04-14 14:56:44 +04:00
|
|
|
* \param result Pointer to location to recieve retrieved object
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2011-09-28 15:26:30 +04:00
|
|
|
nserror llcache_object_retrieve(nsurl *url, uint32_t flags,
|
|
|
|
nsurl *referer, const llcache_post_data *post,
|
2010-04-11 17:58:12 +04:00
|
|
|
uint32_t redirect_count, llcache_object **result)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
llcache_object *obj;
|
|
|
|
bool has_query;
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl *defragmented_url;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Retrieve %s (%x, %s, %p)", url, flags, referer, post));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Caching Rules:
|
|
|
|
*
|
|
|
|
* 1) Forced fetches are never cached
|
2010-03-28 20:51:47 +04:00
|
|
|
* 2) POST requests are never cached
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Look for a query segment */
|
2011-11-27 22:32:06 +04:00
|
|
|
has_query = nsurl_has_component(url, NSURL_QUERY);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-10-05 22:05:41 +04:00
|
|
|
/* Get rid of any url fragment */
|
2011-11-27 22:32:06 +04:00
|
|
|
if (nsurl_has_component(url, NSURL_FRAGMENT)) {
|
2011-10-05 22:05:41 +04:00
|
|
|
error = nsurl_defragment(url, &defragmented_url);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
} else {
|
|
|
|
defragmented_url = nsurl_ref(url);
|
|
|
|
}
|
2010-04-14 18:53:21 +04:00
|
|
|
|
2010-03-28 20:51:47 +04:00
|
|
|
if (flags & LLCACHE_RETRIEVE_FORCE_FETCH || post != NULL) {
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Create new object */
|
2010-04-14 18:53:21 +04:00
|
|
|
error = llcache_object_new(defragmented_url, &obj);
|
|
|
|
if (error != NSERROR_OK) {
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl_unref(defragmented_url);
|
2010-03-28 16:56:39 +04:00
|
|
|
return error;
|
2010-04-14 18:53:21 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/* Attempt to kick-off fetch */
|
2010-04-11 17:58:12 +04:00
|
|
|
error = llcache_object_fetch(obj, flags, referer, post,
|
|
|
|
redirect_count);
|
2010-03-28 16:56:39 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
llcache_object_destroy(obj);
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl_unref(defragmented_url);
|
2010-03-28 16:56:39 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new object to uncached list */
|
2011-09-10 04:55:39 +04:00
|
|
|
llcache_object_add_to_list(obj, &llcache->uncached_objects);
|
2010-03-28 16:56:39 +04:00
|
|
|
} else {
|
2011-10-05 22:05:41 +04:00
|
|
|
error = llcache_object_retrieve_from_cache(defragmented_url,
|
|
|
|
flags, referer, post, redirect_count, &obj);
|
2010-04-14 18:53:21 +04:00
|
|
|
if (error != NSERROR_OK) {
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl_unref(defragmented_url);
|
2010-03-28 16:56:39 +04:00
|
|
|
return error;
|
2010-04-14 18:53:21 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/* Returned object is already in the cached list */
|
|
|
|
}
|
2010-03-28 20:51:47 +04:00
|
|
|
|
|
|
|
obj->has_query = has_query;
|
2010-03-29 10:27:37 +04:00
|
|
|
|
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Retrieved %p", obj));
|
|
|
|
#endif
|
2010-03-28 20:51:47 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
*result = obj;
|
2010-04-14 18:53:21 +04:00
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl_unref(defragmented_url);
|
2010-04-14 18:53:21 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a potentially cached object
|
|
|
|
*
|
2010-04-14 14:56:44 +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
|
2010-04-11 17:58:12 +04:00
|
|
|
* \param redirect_count Number of redirects followed so far
|
2010-04-14 14:56:44 +04:00
|
|
|
* \param result Pointer to location to recieve retrieved object
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2011-09-28 15:26:30 +04:00
|
|
|
nserror llcache_object_retrieve_from_cache(nsurl *url, uint32_t flags,
|
|
|
|
nsurl *referer, const llcache_post_data *post,
|
2010-04-11 17:58:12 +04:00
|
|
|
uint32_t redirect_count, llcache_object **result)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
llcache_object *obj, *newest = NULL;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Searching cache for %s (%x %s %p)", url, flags, referer, post));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Search for the most recently fetched matching object */
|
2011-09-10 04:55:39 +04:00
|
|
|
for (obj = llcache->cached_objects; obj != NULL; obj = obj->next) {
|
2010-08-29 21:41:54 +04:00
|
|
|
|
2011-02-24 23:51:35 +03:00
|
|
|
if ((newest == NULL ||
|
|
|
|
obj->cache.req_time > newest->cache.req_time) &&
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl_compare(obj->url, url,
|
|
|
|
NSURL_COMPLETE) == true) {
|
2010-03-28 16:56:39 +04:00
|
|
|
newest = obj;
|
2010-08-29 21:41:54 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newest != NULL && llcache_object_is_fresh(newest)) {
|
|
|
|
/* Found a suitable object, and it's still fresh, so use it */
|
|
|
|
obj = newest;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Found fresh %p", obj));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* The client needs to catch up with the object's state.
|
|
|
|
* This will occur the next time that llcache_poll is called.
|
|
|
|
*/
|
|
|
|
} else if (newest != NULL) {
|
|
|
|
/* Found a candidate object but it needs freshness validation */
|
2010-03-29 10:27:37 +04:00
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Create a new object */
|
|
|
|
error = llcache_object_new(url, &obj);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Found candidate %p (%p)", obj, newest));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +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 */
|
2010-04-11 17:58:12 +04:00
|
|
|
error = llcache_object_fetch(obj, flags, referer, post,
|
|
|
|
redirect_count);
|
2010-03-28 16:56:39 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
newest->candidate_count--;
|
|
|
|
llcache_object_destroy(obj);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new object to cache */
|
2011-09-10 04:55:39 +04:00
|
|
|
llcache_object_add_to_list(obj, &llcache->cached_objects);
|
2010-03-28 16:56:39 +04:00
|
|
|
} else {
|
|
|
|
/* No object found; create a new one */
|
|
|
|
/* Create new object */
|
|
|
|
error = llcache_object_new(url, &obj);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Not found %p", obj));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Attempt to kick-off fetch */
|
2010-04-11 17:58:12 +04:00
|
|
|
error = llcache_object_fetch(obj, flags, referer, post,
|
|
|
|
redirect_count);
|
2010-03-28 16:56:39 +04:00
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
llcache_object_destroy(obj);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new object to cache */
|
2011-09-10 04:55:39 +04:00
|
|
|
llcache_object_add_to_list(obj, &llcache->cached_objects);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*result = obj;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if an object is still fresh
|
|
|
|
*
|
|
|
|
* \param object Object to consider
|
|
|
|
* \return True if object is still fresh, false otherwise
|
|
|
|
*/
|
|
|
|
bool llcache_object_is_fresh(const llcache_object *object)
|
|
|
|
{
|
|
|
|
const llcache_cache_control *cd = &object->cache;
|
|
|
|
int current_age, freshness_lifetime;
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* Determine freshness lifetime of this object */
|
|
|
|
if (cd->max_age != INVALID_AGE)
|
|
|
|
freshness_lifetime = cd->max_age;
|
|
|
|
else if (cd->expires != 0)
|
|
|
|
freshness_lifetime = cd->expires - cd->date;
|
|
|
|
else if (cd->last_modified != 0)
|
|
|
|
freshness_lifetime = (now - cd->last_modified) / 10;
|
|
|
|
else
|
|
|
|
freshness_lifetime = 0;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("%p: (%d > %d || %d != %d)", object,
|
|
|
|
freshness_lifetime, current_age,
|
|
|
|
object->fetch.state, LLCACHE_FETCH_COMPLETE));
|
|
|
|
#endif
|
|
|
|
|
2010-09-16 22:58:32 +04:00
|
|
|
/* The object is fresh if:
|
|
|
|
*
|
|
|
|
* it was not forbidden from being returned from the cache
|
|
|
|
* unvalidated (i.e. the response contained a no-cache directive)
|
|
|
|
*
|
|
|
|
* and:
|
|
|
|
*
|
|
|
|
* its current age is within the freshness lifetime
|
|
|
|
* or if we're still fetching the object
|
|
|
|
*/
|
2011-02-25 00:22:24 +03:00
|
|
|
return (cd->no_cache == LLCACHE_VALIDATE_FRESH &&
|
|
|
|
(freshness_lifetime > current_age ||
|
2010-09-16 22:58:32 +04:00
|
|
|
object->fetch.state != LLCACHE_FETCH_COMPLETE));
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an object's cache state
|
|
|
|
*
|
|
|
|
* \param object Object to update cache for
|
|
|
|
* \return NSERROR_OK.
|
|
|
|
*/
|
|
|
|
nserror llcache_object_cache_update(llcache_object *object)
|
|
|
|
{
|
|
|
|
if (object->cache.date == 0)
|
|
|
|
object->cache.date = time(NULL);
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone an object's cache data
|
|
|
|
*
|
|
|
|
* \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
|
2010-09-16 23:33:18 +04: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
|
|
|
*/
|
2010-09-16 23:33:18 +04:00
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (source->cache.last_modified != 0)
|
|
|
|
destination->cache.last_modified = source->cache.last_modified;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Kick-off a fetch for an object
|
|
|
|
*
|
2010-04-14 14:56:44 +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
|
2010-04-11 17:58:12 +04:00
|
|
|
* \param redirect_count Number of redirects followed so far
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*
|
|
|
|
* \pre object::url must contain the URL to fetch
|
|
|
|
* \pre If there is a freshness validation candidate,
|
2010-03-28 20:00:54 +04:00
|
|
|
* object::candidate and object::cache must be filled in
|
2010-03-28 16:56:39 +04:00
|
|
|
* \pre There must not be a fetch in progress for \a object
|
|
|
|
*/
|
|
|
|
nserror llcache_object_fetch(llcache_object *object, uint32_t flags,
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl *referer, const llcache_post_data *post,
|
2010-04-11 17:58:12 +04:00
|
|
|
uint32_t redirect_count)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
nserror error;
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl *referer_clone = NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_post_data *post_clone = NULL;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Starting fetch for %p", object));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
if (post != NULL) {
|
|
|
|
error = llcache_post_data_clone(post, &post_clone);
|
2011-09-28 15:26:30 +04:00
|
|
|
if (error != NSERROR_OK)
|
2010-03-28 16:56:39 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
if (referer != NULL)
|
|
|
|
referer_clone = nsurl_ref(referer);
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
object->fetch.flags = flags;
|
|
|
|
object->fetch.referer = referer_clone;
|
|
|
|
object->fetch.post = post_clone;
|
2010-04-11 17:58:12 +04:00
|
|
|
object->fetch.redirect_count = redirect_count;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
return llcache_object_refetch(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (Re)fetch an object
|
|
|
|
*
|
|
|
|
* \param object Object to refetch
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*
|
|
|
|
* \pre The fetch parameters in object->fetch must be populated
|
|
|
|
*/
|
|
|
|
nserror llcache_object_refetch(llcache_object *object)
|
|
|
|
{
|
|
|
|
const char *urlenc = NULL;
|
|
|
|
struct fetch_multipart_data *multipart = NULL;
|
|
|
|
char **headers = NULL;
|
|
|
|
int header_idx = 0;
|
|
|
|
|
|
|
|
if (object->fetch.post != NULL) {
|
|
|
|
if (object->fetch.post->type == LLCACHE_POST_URL_ENCODED)
|
|
|
|
urlenc = object->fetch.post->data.urlenc;
|
|
|
|
else
|
|
|
|
multipart = object->fetch.post->data.multipart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate cache-control headers */
|
|
|
|
headers = malloc(3 * sizeof(char *));
|
|
|
|
if (headers == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
if (object->cache.etag != NULL) {
|
|
|
|
const size_t len = SLEN("If-None-Match: ") +
|
|
|
|
strlen(object->cache.etag) + 1;
|
|
|
|
|
|
|
|
headers[header_idx] = malloc(len);
|
|
|
|
if (headers[header_idx] == NULL) {
|
|
|
|
free(headers);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(headers[header_idx], len, "If-None-Match: %s",
|
|
|
|
object->cache.etag);
|
|
|
|
|
|
|
|
header_idx++;
|
|
|
|
}
|
|
|
|
if (object->cache.date != 0) {
|
|
|
|
/* Maximum length of an RFC 1123 date is 29 bytes */
|
|
|
|
const size_t len = SLEN("If-Modified-Since: ") + 29 + 1;
|
|
|
|
|
|
|
|
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",
|
|
|
|
rfc1123_date(object->cache.date));
|
|
|
|
|
|
|
|
header_idx++;
|
|
|
|
}
|
|
|
|
headers[header_idx] = NULL;
|
|
|
|
|
|
|
|
/* Reset cache control data */
|
2011-02-24 13:14:50 +03:00
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
object->cache.req_time = time(NULL);
|
|
|
|
|
2010-04-17 00:08:27 +04:00
|
|
|
/* Reset fetch state */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_INIT;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Refetching %p", object));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Kick off fetch */
|
2011-09-28 15:26:30 +04:00
|
|
|
object->fetch.fetch = fetch_start(object->url, object->fetch.referer,
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_fetch_callback, object,
|
|
|
|
object->fetch.flags & LLCACHE_RETRIEVE_NO_ERROR_PAGES,
|
|
|
|
urlenc, multipart,
|
|
|
|
object->fetch.flags & LLCACHE_RETRIEVE_VERIFIABLE,
|
2010-04-10 21:13:53 +04:00
|
|
|
(const char **) headers);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/* Clean up cache-control headers */
|
|
|
|
while (--header_idx >= 0)
|
|
|
|
free(headers[header_idx]);
|
|
|
|
free(headers);
|
|
|
|
|
|
|
|
/* Did we succeed in creating a fetch? */
|
|
|
|
if (object->fetch.fetch == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new low-level cache object
|
|
|
|
*
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param url URL of object to create
|
2010-03-28 16:56:39 +04:00
|
|
|
* \param result Pointer to location to receive result
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2011-09-28 15:26:30 +04:00
|
|
|
nserror llcache_object_new(nsurl *url, llcache_object **result)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
llcache_object *obj = calloc(1, sizeof(llcache_object));
|
|
|
|
if (obj == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
2011-09-28 15:26:30 +04:00
|
|
|
LOG(("Created object %p (%s)", obj, nsurl_access(url)));
|
2010-03-29 10:27:37 +04:00
|
|
|
#endif
|
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
obj->url = nsurl_ref(url);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
*result = obj;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a low-level cache object
|
|
|
|
*
|
|
|
|
* \param object Object to destroy
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*
|
|
|
|
* \pre Object is detached from cache list
|
|
|
|
* \pre Object has no users
|
|
|
|
* \pre Object is not a candidate (i.e. object::candidate_count == 0)
|
|
|
|
*/
|
|
|
|
nserror llcache_object_destroy(llcache_object *object)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Destroying object %p", object));
|
|
|
|
#endif
|
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl_unref(object->url);
|
2010-03-28 16:56:39 +04:00
|
|
|
free(object->source_data);
|
|
|
|
|
|
|
|
if (object->fetch.fetch != NULL) {
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
if (object->fetch.referer != NULL)
|
|
|
|
nsurl_unref(object->fetch.referer);
|
2010-03-28 16:56:39 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(object->fetch.post);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(object->cache.etag);
|
|
|
|
|
|
|
|
for (i = 0; i < object->num_headers; i++) {
|
|
|
|
free(object->headers[i].name);
|
|
|
|
free(object->headers[i].value);
|
|
|
|
}
|
|
|
|
free(object->headers);
|
|
|
|
|
|
|
|
free(object);
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a user to a low-level cache object
|
|
|
|
*
|
|
|
|
* \param object Object to add user to
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param user User to add
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK.
|
|
|
|
*/
|
|
|
|
nserror llcache_object_add_user(llcache_object *object,
|
|
|
|
llcache_object_user *user)
|
|
|
|
{
|
2010-04-09 04:06:36 +04:00
|
|
|
assert(user->next == NULL);
|
|
|
|
assert(user->prev == NULL);
|
|
|
|
|
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;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Adding user %p to %p", user, object));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a user from a low-level cache object
|
|
|
|
*
|
|
|
|
* \param object Object to remove user from
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param user User to remove
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK.
|
|
|
|
*/
|
|
|
|
nserror llcache_object_remove_user(llcache_object *object,
|
|
|
|
llcache_object_user *user)
|
|
|
|
{
|
2010-12-29 23:42:00 +03:00
|
|
|
assert(user != NULL);
|
|
|
|
assert(object != NULL);
|
2010-04-09 04:06:36 +04:00
|
|
|
assert(object->users != NULL);
|
2010-12-16 03:18:41 +03:00
|
|
|
assert(user->handle == NULL || user->handle->object == object);
|
2010-12-13 11:12:31 +03:00
|
|
|
assert((user->prev != NULL) || (object->users == user));
|
2010-04-08 17:12:30 +04:00
|
|
|
|
2010-03-28 16:56:39 +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;
|
2010-12-13 11:34:01 +03:00
|
|
|
|
|
|
|
user->next = user->prev = NULL;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Removing user %p from %p", user, object));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-16 00:15:49 +03:00
|
|
|
/**
|
|
|
|
* Find a user of a low-level cache object
|
|
|
|
*
|
|
|
|
* \param handle External cache handle to search for
|
|
|
|
* \return Pointer to corresponding user, or NULL if not found
|
|
|
|
*/
|
|
|
|
llcache_object_user *llcache_object_find_user(const llcache_handle *handle)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Add a low-level cache object to a cache list
|
|
|
|
*
|
|
|
|
* \param object Object to add
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param list List to add to
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK
|
|
|
|
*/
|
|
|
|
nserror llcache_object_add_to_list(llcache_object *object,
|
|
|
|
llcache_object **list)
|
|
|
|
{
|
|
|
|
object->prev = NULL;
|
|
|
|
object->next = *list;
|
|
|
|
|
|
|
|
if (*list != NULL)
|
|
|
|
(*list)->prev = object;
|
|
|
|
*list = object;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a low-level cache object from a cache list
|
|
|
|
*
|
|
|
|
* \param object Object to remove
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param list List to remove from
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK
|
|
|
|
*/
|
|
|
|
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)
|
2010-03-29 10:29:25 +04:00
|
|
|
object->next->prev = object->prev;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-06 01:35:38 +04:00
|
|
|
/**
|
|
|
|
* Determine if a low-level cache object resides in a given list
|
|
|
|
*
|
|
|
|
* \param object Object to search for
|
2010-04-09 04:06:36 +04:00
|
|
|
* \param list List to search in
|
2010-04-06 01:35:38 +04:00
|
|
|
* \return True if object resides in list, false otherwise
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Notify users of an object's current state
|
|
|
|
*
|
|
|
|
* \param object Object to notify users about
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_object_notify_users(llcache_object *object)
|
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
llcache_object_user *user, *next_user;
|
|
|
|
llcache_event event;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
2010-08-11 01:46:45 +04:00
|
|
|
bool emitted_notify = false;
|
2010-03-29 10:27:37 +04:00
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* State transitions and event emission for users.
|
|
|
|
* Rows: user state. Cols: object state.
|
|
|
|
*
|
|
|
|
* User\Obj INIT HEADERS DATA COMPLETE
|
|
|
|
* 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
|
2010-03-28 20:00:54 +04:00
|
|
|
* DONE : on transition from DATA -> COMPLETE state
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (user = object->users; user != NULL; user = next_user) {
|
|
|
|
/* Emit necessary events to bring the user up-to-date */
|
2010-12-16 00:15:49 +03:00
|
|
|
llcache_handle *handle = user->handle;
|
2010-04-04 16:41:19 +04:00
|
|
|
const llcache_fetch_state objstate = object->fetch.state;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-12-13 02:37:21 +03:00
|
|
|
/* Flag that this user is the current iteration target
|
|
|
|
* in case the client attempts to destroy it underneath us */
|
2010-03-28 16:56:39 +04:00
|
|
|
user->iterator_target = true;
|
2010-12-13 02:37:21 +03:00
|
|
|
|
|
|
|
/* A note on the computation of next_user:
|
|
|
|
*
|
|
|
|
* Within this loop, we may make a number of calls to
|
|
|
|
* 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.
|
|
|
|
* However, no such protection exists if the client
|
|
|
|
* 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
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
2010-08-11 01:46:45 +04:00
|
|
|
if (handle->state != objstate) {
|
|
|
|
if (emitted_notify == false) {
|
|
|
|
LOG(("Notifying users of %p", object));
|
|
|
|
emitted_notify = true;
|
|
|
|
}
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
LOG(("User %p state: %d Object state: %d",
|
2010-04-04 16:41:19 +04:00
|
|
|
user, handle->state, objstate));
|
2010-08-11 01:46:45 +04:00
|
|
|
}
|
2010-03-29 10:27:37 +04:00
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* User: INIT, Obj: HEADERS, DATA, COMPLETE => User->HEADERS */
|
2010-04-04 16:41:19 +04:00
|
|
|
if (handle->state == LLCACHE_FETCH_INIT &&
|
2010-03-28 16:56:39 +04:00
|
|
|
objstate > LLCACHE_FETCH_INIT) {
|
2010-04-04 16:41:19 +04:00
|
|
|
handle->state = LLCACHE_FETCH_HEADERS;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* User: HEADERS, Obj: DATA, COMPLETE => User->DATA */
|
2010-04-04 16:41:19 +04:00
|
|
|
if (handle->state == LLCACHE_FETCH_HEADERS &&
|
2010-03-28 16:56:39 +04:00
|
|
|
objstate > LLCACHE_FETCH_HEADERS) {
|
2010-04-04 16:41:19 +04:00
|
|
|
handle->state = LLCACHE_FETCH_DATA;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Emit HAD_HEADERS event */
|
|
|
|
event.type = LLCACHE_EVENT_HAD_HEADERS;
|
|
|
|
|
|
|
|
error = handle->cb(handle, &event, handle->pw);
|
|
|
|
|
|
|
|
if (user->queued_for_delete) {
|
2010-12-13 02:37:21 +03:00
|
|
|
next_user = user->next;
|
2010-12-16 00:15:49 +03:00
|
|
|
llcache_object_remove_user(object, user);
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object_user_destroy(user);
|
2010-12-16 01:59:19 +03:00
|
|
|
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
2011-09-08 12:26:16 +04:00
|
|
|
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;
|
2010-03-28 16:56:39 +04:00
|
|
|
continue;
|
2010-12-16 01:59:19 +03:00
|
|
|
} else if (error != NSERROR_OK) {
|
|
|
|
user->iterator_target = false;
|
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* User: DATA, Obj: DATA, COMPLETE, more source available */
|
2010-04-04 16:41:19 +04:00
|
|
|
if (handle->state == LLCACHE_FETCH_DATA &&
|
2010-03-28 16:56:39 +04:00
|
|
|
objstate >= LLCACHE_FETCH_DATA &&
|
|
|
|
object->source_len > handle->bytes) {
|
2011-09-04 10:28:09 +04:00
|
|
|
size_t orig_handle_read;
|
|
|
|
|
2010-04-06 01:35:38 +04:00
|
|
|
/* Construct HAD_DATA event */
|
|
|
|
event.type = LLCACHE_EVENT_HAD_DATA;
|
|
|
|
event.data.data.buf =
|
|
|
|
object->source_data + handle->bytes;
|
|
|
|
event.data.data.len =
|
|
|
|
object->source_len - handle->bytes;
|
2010-04-04 16:41:19 +04:00
|
|
|
|
|
|
|
/* Update record of last byte emitted */
|
2010-04-06 01:35:38 +04:00
|
|
|
if (object->fetch.flags &
|
|
|
|
LLCACHE_RETRIEVE_STREAM_DATA) {
|
|
|
|
/* Streaming, so reset to zero to
|
2011-09-04 10:28:09 +04:00
|
|
|
* minimise amount of cached source data.
|
|
|
|
* Additionally, we don't support replay
|
|
|
|
* when streaming. */
|
|
|
|
orig_handle_read = 0;
|
2010-04-06 01:35:38 +04:00
|
|
|
handle->bytes = object->source_len = 0;
|
|
|
|
} else {
|
2011-09-04 10:28:09 +04:00
|
|
|
orig_handle_read = handle->bytes;
|
2010-04-06 01:35:38 +04:00
|
|
|
handle->bytes = object->source_len;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-04-06 01:35:38 +04:00
|
|
|
/* Emit event */
|
2010-03-28 16:56:39 +04:00
|
|
|
error = handle->cb(handle, &event, handle->pw);
|
|
|
|
if (user->queued_for_delete) {
|
2010-12-13 02:37:21 +03:00
|
|
|
next_user = user->next;
|
2010-12-16 00:15:49 +03:00
|
|
|
llcache_object_remove_user(object, user);
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object_user_destroy(user);
|
2010-12-16 01:59:19 +03:00
|
|
|
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
2011-09-04 10:28:09 +04:00
|
|
|
continue;
|
|
|
|
} else if (error == NSERROR_NEED_DATA) {
|
|
|
|
/* User requested replay */
|
|
|
|
handle->bytes = orig_handle_read;
|
|
|
|
|
|
|
|
/* Continue with the next user -- we'll
|
|
|
|
* reemit the data next time round */
|
|
|
|
user->iterator_target = false;
|
|
|
|
next_user = user->next;
|
2010-03-28 16:56:39 +04:00
|
|
|
continue;
|
2010-12-16 01:59:19 +03:00
|
|
|
} else if (error != NSERROR_OK) {
|
|
|
|
user->iterator_target = false;
|
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* User: DATA, Obj: COMPLETE => User->COMPLETE */
|
2010-04-04 16:41:19 +04:00
|
|
|
if (handle->state == LLCACHE_FETCH_DATA &&
|
2010-03-28 16:56:39 +04:00
|
|
|
objstate > LLCACHE_FETCH_DATA) {
|
2010-04-04 16:41:19 +04:00
|
|
|
handle->state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Emit DONE event */
|
|
|
|
event.type = LLCACHE_EVENT_DONE;
|
|
|
|
|
|
|
|
error = handle->cb(handle, &event, handle->pw);
|
|
|
|
if (user->queued_for_delete) {
|
2010-12-13 02:37:21 +03:00
|
|
|
next_user = user->next;
|
2010-12-16 00:15:49 +03:00
|
|
|
llcache_object_remove_user(object, user);
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object_user_destroy(user);
|
2010-12-16 01:59:19 +03:00
|
|
|
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
2011-09-08 12:26:16 +04:00
|
|
|
continue;
|
|
|
|
} else if (error == NSERROR_NEED_DATA) {
|
|
|
|
/* User requested replay */
|
|
|
|
handle->state = LLCACHE_FETCH_DATA;
|
|
|
|
|
|
|
|
/* Continue with the next user -- we'll
|
|
|
|
* reemit the event next time round */
|
|
|
|
user->iterator_target = false;
|
|
|
|
next_user = user->next;
|
2010-03-28 16:56:39 +04:00
|
|
|
continue;
|
2010-12-16 01:59:19 +03:00
|
|
|
} else if (error != NSERROR_OK) {
|
|
|
|
user->iterator_target = false;
|
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No longer the target of an iterator */
|
|
|
|
user->iterator_target = false;
|
2010-12-13 02:37:21 +03:00
|
|
|
|
|
|
|
next_user = user->next;
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-04 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* Make a snapshot of the current state of an llcache_object.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
nserror llcache_object_snapshot(llcache_object *object,
|
|
|
|
llcache_object **snapshot)
|
|
|
|
{
|
|
|
|
llcache_object *newobj;
|
|
|
|
nserror error;
|
|
|
|
|
|
|
|
error = llcache_object_new(object->url, &newobj);
|
|
|
|
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
newobj->has_query = object->has_query;
|
|
|
|
|
|
|
|
newobj->source_alloc = newobj->source_len = object->source_len;
|
|
|
|
|
|
|
|
if (object->source_len > 0) {
|
|
|
|
newobj->source_data = malloc(newobj->source_alloc);
|
|
|
|
if (newobj->source_data == NULL) {
|
|
|
|
llcache_object_destroy(newobj);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2010-04-11 18:39:26 +04:00
|
|
|
memcpy(newobj->source_data, object->source_data,
|
|
|
|
newobj->source_len);
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (object->num_headers > 0) {
|
2010-04-11 18:39:26 +04:00
|
|
|
newobj->headers = calloc(sizeof(llcache_header),
|
|
|
|
object->num_headers);
|
2010-04-04 16:41:19 +04:00
|
|
|
if (newobj->headers == NULL) {
|
|
|
|
llcache_object_destroy(newobj);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
while (newobj->num_headers < object->num_headers) {
|
2010-04-11 18:39:26 +04:00
|
|
|
llcache_header *nh =
|
|
|
|
&(newobj->headers[newobj->num_headers]);
|
|
|
|
llcache_header *oh =
|
|
|
|
&(object->headers[newobj->num_headers]);
|
2010-04-04 16:41:19 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newobj->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
|
|
|
*snapshot = newobj;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/**
|
|
|
|
* Attempt to clean the cache
|
|
|
|
*/
|
2011-03-13 21:26:46 +03:00
|
|
|
void llcache_clean(void)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
llcache_object *object, *next;
|
2010-04-11 18:32:39 +04:00
|
|
|
uint32_t llcache_size = 0;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Attempting cache clean"));
|
|
|
|
#endif
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Candidates for cleaning are (in order of priority):
|
|
|
|
*
|
|
|
|
* 1) Uncacheable objects with no users
|
|
|
|
* 2) Stale cacheable objects with no users or pending fetches
|
|
|
|
* 3) Fresh cacheable objects with no users or pending fetches
|
|
|
|
*/
|
|
|
|
|
2010-03-29 10:28:46 +04:00
|
|
|
/* 1) Uncacheable objects with no users or fetches */
|
2011-09-10 04:55:39 +04:00
|
|
|
for (object = llcache->uncached_objects; object != NULL; object = next) {
|
2010-03-28 16:56:39 +04:00
|
|
|
next = object->next;
|
|
|
|
|
|
|
|
/* The candidate count of uncacheable objects is always 0 */
|
2010-03-29 10:28:46 +04:00
|
|
|
if (object->users == NULL && object->candidate_count == 0 &&
|
2011-03-03 03:40:50 +03:00
|
|
|
object->fetch.fetch == NULL &&
|
|
|
|
object->fetch.outstanding_query == false) {
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Found victim %p", object));
|
|
|
|
#endif
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object_remove_from_list(object,
|
2011-09-10 04:55:39 +04:00
|
|
|
&llcache->uncached_objects);
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object_destroy(object);
|
2010-04-11 18:32:39 +04:00
|
|
|
} else {
|
|
|
|
llcache_size += object->source_len + sizeof(*object);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 2) Stale cacheable objects with no users or pending fetches */
|
2011-09-10 04:55:39 +04:00
|
|
|
for (object = llcache->cached_objects; object != NULL; object = next) {
|
2010-03-28 16:56:39 +04:00
|
|
|
next = object->next;
|
|
|
|
|
|
|
|
if (object->users == NULL && object->candidate_count == 0 &&
|
2010-03-29 10:28:46 +04:00
|
|
|
llcache_object_is_fresh(object) == false &&
|
2011-03-03 03:40:50 +03:00
|
|
|
object->fetch.fetch == NULL &&
|
|
|
|
object->fetch.outstanding_query == false) {
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Found victim %p", object));
|
|
|
|
#endif
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object_remove_from_list(object,
|
2011-09-10 04:55:39 +04:00
|
|
|
&llcache->cached_objects);
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_object_destroy(object);
|
2010-04-11 18:32:39 +04:00
|
|
|
} else {
|
|
|
|
llcache_size += object->source_len + sizeof(*object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
if (llcache->limit < llcache_size) {
|
2010-04-11 18:32:39 +04:00
|
|
|
/* 3) Fresh cacheable objects with
|
|
|
|
* no users or pending fetches */
|
2011-09-10 04:55:39 +04:00
|
|
|
for (object = llcache->cached_objects; object != NULL;
|
2010-04-11 18:32:39 +04:00
|
|
|
object = next) {
|
|
|
|
next = object->next;
|
|
|
|
|
|
|
|
if (object->users == NULL &&
|
|
|
|
object->candidate_count == 0 &&
|
2011-03-03 03:40:50 +03:00
|
|
|
object->fetch.fetch == NULL &&
|
|
|
|
object->fetch.outstanding_query ==
|
|
|
|
false) {
|
2010-04-11 18:32:39 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Found victim %p", object));
|
|
|
|
#endif
|
|
|
|
llcache_size -=
|
|
|
|
object->source_len + sizeof(*object);
|
|
|
|
|
|
|
|
llcache_object_remove_from_list(object,
|
2011-09-10 04:55:39 +04:00
|
|
|
&llcache->cached_objects);
|
2010-04-11 18:32:39 +04:00
|
|
|
llcache_object_destroy(object);
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-11 18:32:39 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
|
|
|
LOG(("Size: %u", llcache_size));
|
|
|
|
#endif
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone a POST data object
|
|
|
|
*
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param orig Object to clone
|
|
|
|
* \param clone Pointer to location to receive clone
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_post_data_clone(const llcache_post_data *orig,
|
|
|
|
llcache_post_data **clone)
|
|
|
|
{
|
|
|
|
llcache_post_data *post_clone;
|
|
|
|
|
|
|
|
post_clone = calloc(1, sizeof(llcache_post_data));
|
|
|
|
if (post_clone == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
post_clone->type = orig->type;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
post_clone->data.multipart = fetch_multipart_data_clone(
|
|
|
|
orig->data.multipart);
|
|
|
|
if (post_clone->data.multipart == NULL) {
|
|
|
|
free(post_clone);
|
|
|
|
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*clone = post_clone;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a query response
|
|
|
|
*
|
|
|
|
* \param proceed Whether to proceed with fetch
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param cbpw Our context for query
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_query_handle_response(bool proceed, void *cbpw)
|
|
|
|
{
|
|
|
|
llcache_event event;
|
|
|
|
llcache_object *object = cbpw;
|
|
|
|
|
2011-03-03 03:40:50 +03:00
|
|
|
object->fetch.outstanding_query = false;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Refetch, using existing fetch parameters, if client allows us to */
|
|
|
|
if (proceed)
|
|
|
|
return llcache_object_refetch(object);
|
|
|
|
|
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
|
|
|
/* Mark it complete */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Inform client(s) that object fetch failed */
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
|
|
|
/** \todo More appropriate error message */
|
|
|
|
event.data.msg = messages_get("FetchFailed");
|
2010-04-14 14:56:44 +04:00
|
|
|
|
|
|
|
return llcache_send_event_to_users(object, &event);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for fetch events
|
|
|
|
*
|
2011-11-09 01:51:42 +04:00
|
|
|
* \param msg Fetch event
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param p Our private data
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2011-11-09 01:51:42 +04:00
|
|
|
void llcache_fetch_callback(const fetch_msg *msg, void *p)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
llcache_object *object = p;
|
|
|
|
llcache_event event;
|
|
|
|
|
2010-03-29 10:27:37 +04:00
|
|
|
#ifdef LLCACHE_TRACE
|
2011-11-09 01:51:42 +04:00
|
|
|
LOG(("Fetch event %d for %p", msg->type, object));
|
2010-03-29 10:27:37 +04:00
|
|
|
#endif
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
switch (msg->type) {
|
2010-09-17 02:46:40 +04:00
|
|
|
case FETCH_HEADER:
|
|
|
|
/* Received a fetch header */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_HEADERS;
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
error = llcache_fetch_process_header(object,
|
|
|
|
msg->data.header_or_data.buf,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
error = llcache_fetch_redirect(object,
|
|
|
|
msg->data.redirect, &object);
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
|
|
|
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 */
|
2010-09-15 23:33:18 +04:00
|
|
|
if (object->fetch.state != LLCACHE_FETCH_DATA) {
|
|
|
|
/* On entry into this state, check if we need to
|
|
|
|
* 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.
|
2010-03-28 20:51:47 +04:00
|
|
|
*/
|
2010-09-15 23:33:18 +04:00
|
|
|
long http_code = fetch_http_code(object->fetch.fetch);
|
|
|
|
|
2011-12-09 02:44:19 +04:00
|
|
|
if ((http_code != 200 && http_code != 203) ||
|
2010-09-15 23:33:18 +04:00
|
|
|
(object->has_query &&
|
|
|
|
(object->cache.max_age == INVALID_AGE &&
|
|
|
|
object->cache.expires == 0))) {
|
|
|
|
/* Invalidate cache control data */
|
2010-09-16 23:47:15 +04:00
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-09-15 23:33:18 +04:00
|
|
|
}
|
2010-09-17 02:46:40 +04:00
|
|
|
|
|
|
|
/* Release candidate, if any */
|
|
|
|
if (object->candidate != NULL) {
|
|
|
|
object->candidate->candidate_count--;
|
|
|
|
object->candidate = NULL;
|
|
|
|
}
|
2010-03-28 20:51:47 +04:00
|
|
|
}
|
2010-09-15 23:33:18 +04:00
|
|
|
|
|
|
|
object->fetch.state = LLCACHE_FETCH_DATA;
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
error = llcache_fetch_process_data(object,
|
|
|
|
msg->data.header_or_data.buf,
|
|
|
|
msg->data.header_or_data.len);
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
|
|
|
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 */
|
|
|
|
temp = realloc(object->source_data,
|
|
|
|
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);
|
2010-04-12 01:26:20 +04:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Out-of-band information */
|
|
|
|
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;
|
2011-11-09 01:51:42 +04:00
|
|
|
event.data.msg = msg->data.error;
|
2010-04-14 14:56:44 +04:00
|
|
|
|
|
|
|
error = llcache_send_event_to_users(object, &event);
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
|
|
|
case FETCH_PROGRESS:
|
|
|
|
/* Progress update */
|
|
|
|
event.type = LLCACHE_EVENT_PROGRESS;
|
2011-11-09 01:51:42 +04:00
|
|
|
event.data.msg = msg->data.progress;
|
|
|
|
|
2010-04-14 14:56:44 +04:00
|
|
|
error = llcache_send_event_to_users(object, &event);
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
error = llcache_fetch_cert_error(object,
|
|
|
|
msg->data.cert_err.certs,
|
|
|
|
msg->data.cert_err.num_certs);
|
2010-03-28 16:56:39 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deal with any errors reported by event handlers */
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
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
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle FETCH_REDIRECT event
|
|
|
|
*
|
|
|
|
* \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
|
|
|
|
*/
|
|
|
|
nserror llcache_fetch_redirect(llcache_object *object, const char *target,
|
|
|
|
llcache_object **replacement)
|
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
llcache_object *dest;
|
|
|
|
llcache_object_user *user, *next;
|
|
|
|
const llcache_post_data *post = object->fetch.post;
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl *url;
|
|
|
|
lwc_string *scheme;
|
|
|
|
lwc_string *object_scheme;
|
|
|
|
bool match;
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Extract HTTP response code from the fetch object */
|
|
|
|
long http_code = fetch_http_code(object->fetch.fetch);
|
|
|
|
|
|
|
|
/* Abort fetch for this object */
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
2010-03-28 20:28:28 +04:00
|
|
|
|
|
|
|
/* Invalidate the cache control data */
|
2010-09-16 23:47:15 +04:00
|
|
|
llcache_invalidate_cache_control_data(object);
|
|
|
|
|
2010-03-28 20:28:28 +04:00
|
|
|
/* And mark it complete */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
2010-04-11 17:58:12 +04:00
|
|
|
/* Forcibly stop redirecting if we've followed too many redirects */
|
|
|
|
#define REDIRECT_LIMIT 10
|
|
|
|
if (object->fetch.redirect_count > REDIRECT_LIMIT) {
|
|
|
|
llcache_event event;
|
|
|
|
|
|
|
|
LOG(("Too many nested redirects"));
|
|
|
|
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
|
|
|
event.data.msg = messages_get("BadRedirect");
|
2010-04-14 14:56:44 +04:00
|
|
|
|
2011-05-17 01:47:36 +04:00
|
|
|
return llcache_send_event_to_users(object, &event);
|
2010-04-11 17:58:12 +04:00
|
|
|
}
|
|
|
|
#undef REDIRECT_LIMIT
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/* Make target absolute */
|
2011-09-28 15:26:30 +04:00
|
|
|
error = nsurl_join(object->url, target, &url);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-02-24 02:05:36 +03: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. */
|
2011-09-28 15:26:30 +04:00
|
|
|
object_scheme = nsurl_get_component(object->url, NSURL_SCHEME);
|
|
|
|
scheme = nsurl_get_component(url, NSURL_SCHEME);
|
2011-02-24 01:27:55 +03:00
|
|
|
|
2011-02-24 02:05:36 +03:00
|
|
|
/* resource: and about: are allowed to redirect anywhere */
|
2011-09-28 15:26:30 +04:00
|
|
|
if ((lwc_string_isequal(object_scheme, llcache_resource_lwc,
|
|
|
|
&match) == lwc_error_ok && match == false) &&
|
|
|
|
(lwc_string_isequal(object_scheme, llcache_about_lwc,
|
|
|
|
&match) == lwc_error_ok && match == false)) {
|
2011-02-24 01:27:55 +03:00
|
|
|
/* file, about and resource are not valid redirect targets */
|
2011-09-28 15:26:30 +04:00
|
|
|
if ((lwc_string_isequal(object_scheme, llcache_file_lwc,
|
|
|
|
&match) == lwc_error_ok && match == true) ||
|
|
|
|
(lwc_string_isequal(object_scheme, llcache_about_lwc,
|
|
|
|
&match) == lwc_error_ok && match == true) ||
|
|
|
|
(lwc_string_isequal(object_scheme, llcache_resource_lwc,
|
|
|
|
&match) == lwc_error_ok && match == true)) {
|
|
|
|
lwc_string_unref(object_scheme);
|
|
|
|
lwc_string_unref(scheme);
|
|
|
|
nsurl_unref(url);
|
2011-02-24 01:27:55 +03:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-04-11 18:06:02 +04:00
|
|
|
}
|
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
lwc_string_unref(scheme);
|
|
|
|
lwc_string_unref(object_scheme);
|
2011-09-27 15:07:32 +04:00
|
|
|
|
2010-04-11 18:01:11 +04:00
|
|
|
/* Bail out if we've no way of handling this URL */
|
2011-09-28 15:26:30 +04:00
|
|
|
if (fetch_can_fetch(url) == false) {
|
|
|
|
nsurl_unref(url);
|
2010-04-11 18:01:11 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
if (http_code == 301 || http_code == 302 || http_code == 303) {
|
|
|
|
/* 301, 302, 303 redirects are all unconditional GET requests */
|
|
|
|
post = NULL;
|
2010-04-11 18:11:33 +04:00
|
|
|
} else if (http_code != 307 || post != NULL) {
|
|
|
|
/** \todo 300, 305, 307 with POST */
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl_unref(url);
|
2010-03-28 16:56:39 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempt to fetch target URL */
|
|
|
|
error = llcache_object_retrieve(url, object->fetch.flags,
|
2010-04-11 17:58:12 +04:00
|
|
|
object->fetch.referer, post,
|
|
|
|
object->fetch.redirect_count + 1, &dest);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/* No longer require url */
|
2011-09-28 15:26:30 +04:00
|
|
|
nsurl_unref(url);
|
2010-03-28 16:56:39 +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;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle FETCH_NOTMODIFIED event
|
|
|
|
*
|
|
|
|
* \param object Object to process
|
|
|
|
* \param replacement Pointer to location to receive replacement object
|
|
|
|
* \return NSERROR_OK.
|
|
|
|
*/
|
|
|
|
nserror llcache_fetch_notmodified(llcache_object *object,
|
|
|
|
llcache_object **replacement)
|
|
|
|
{
|
2011-02-17 20:50:14 +03:00
|
|
|
/* 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;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-02-17 20:50:14 +03:00
|
|
|
/* Move user(s) to candidate content */
|
|
|
|
for (user = object->users; user != NULL; user = next) {
|
|
|
|
next = user->next;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-02-17 20:50:14 +03:00
|
|
|
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--;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-02-17 20:50:14 +03:00
|
|
|
/* Clone our cache control data into the candidate */
|
|
|
|
llcache_object_clone_cache_data(object, object->candidate,
|
|
|
|
false);
|
|
|
|
/* Bring candidate's cache data up to date */
|
|
|
|
llcache_object_cache_update(object->candidate);
|
2011-02-25 00:22:24 +03:00
|
|
|
/* Revert no-cache to normal, if required */
|
|
|
|
if (object->candidate->cache.no_cache ==
|
|
|
|
LLCACHE_VALIDATE_ONCE) {
|
|
|
|
object->candidate->cache.no_cache =
|
|
|
|
LLCACHE_VALIDATE_FRESH;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-02-17 20:50:14 +03:00
|
|
|
/* Candidate is now our object */
|
|
|
|
*replacement = object->candidate;
|
|
|
|
object->candidate = NULL;
|
|
|
|
} else {
|
|
|
|
/* There was no candidate: retain object */
|
|
|
|
*replacement = object;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/* Ensure fetch has stopped */
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
2010-04-15 00:42:03 +04:00
|
|
|
/* Invalidate our 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
|
|
|
/* Mark it complete */
|
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
2010-04-08 16:24:29 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Split a fetch header into name and value
|
|
|
|
*
|
2010-03-28 20:00:54 +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
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2011-11-09 01:51:42 +04:00
|
|
|
nserror llcache_fetch_split_header(const uint8_t *data, size_t len,
|
|
|
|
char **name, char **value)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
char *n, *v;
|
2011-11-09 01:51:42 +04:00
|
|
|
const uint8_t *colon;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
/* Find colon */
|
2011-11-09 01:51:42 +04:00
|
|
|
colon = (const uint8_t *) strchr((const char *) data, ':');
|
2010-03-28 16:56:39 +04:00
|
|
|
if (colon == NULL) {
|
|
|
|
/* Failed, assume a key with no value */
|
2011-11-09 01:51:42 +04:00
|
|
|
n = strdup((const char *) data);
|
2010-03-28 16:56:39 +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 leading whitespace from name */
|
|
|
|
while (data[0] == ' ' || data[0] == '\t' ||
|
|
|
|
data[0] == '\r' || data[0] == '\n') {
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Strip trailing whitespace from name */
|
|
|
|
while (colon > data && (colon[-1] == ' ' ||
|
|
|
|
colon[-1] == '\t' || colon[-1] == '\r' ||
|
|
|
|
colon[-1] == '\n'))
|
|
|
|
colon--;
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
n = strndup((const char *) data, colon - data);
|
2010-03-28 16:56:39 +04:00
|
|
|
if (n == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
/* Find colon again */
|
|
|
|
while (*colon != ':') {
|
|
|
|
colon++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip over colon and any subsequent whitespace */
|
|
|
|
do {
|
|
|
|
colon++;
|
|
|
|
} while (*colon == ' ' || *colon == '\t' ||
|
|
|
|
*colon == '\r' || *colon == '\n');
|
|
|
|
|
|
|
|
/* Strip trailing whitespace from value */
|
|
|
|
while (len > 0 && (data[len - 1] == ' ' ||
|
|
|
|
data[len - 1] == '\t' ||
|
|
|
|
data[len - 1] == '\r' ||
|
|
|
|
data[len - 1] == '\n')) {
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
2011-11-09 01:51:42 +04:00
|
|
|
v = strndup((const char *) colon, len - (colon - data));
|
2010-03-28 16:56:39 +04:00
|
|
|
if (v == NULL) {
|
|
|
|
free(n);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*name = n;
|
|
|
|
*value = v;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a fetch header
|
|
|
|
*
|
|
|
|
* \param object Object to parse header for
|
2010-03-28 20:00:54 +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
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
2011-02-24 13:14:50 +03:00
|
|
|
*
|
|
|
|
* \note This function also has the side-effect of updating
|
2011-03-13 21:26:46 +03:00
|
|
|
* the cache control data for the object if an interesting
|
|
|
|
* header is encountered
|
2010-03-28 16:56:39 +04:00
|
|
|
*/
|
2011-11-09 01:51:42 +04:00
|
|
|
nserror llcache_fetch_parse_header(llcache_object *object,
|
|
|
|
const uint8_t *data, size_t len, char **name, char **value)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
|
|
|
|
/* Set fetch response time if not already set */
|
|
|
|
if (object->cache.res_time == 0)
|
|
|
|
object->cache.res_time = time(NULL);
|
|
|
|
|
|
|
|
/* Decompose header into name-value pair */
|
|
|
|
error = llcache_fetch_split_header(data, len, name, value);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
/* Parse cache headers to populate cache control data */
|
|
|
|
#define SKIP_ST(p) while (*p != '\0' && (*p == ' ' || *p == '\t')) p++
|
|
|
|
|
|
|
|
if (5 < len && strcasecmp(*name, "Date") == 0) {
|
|
|
|
/* extract Date header */
|
|
|
|
object->cache.date = curl_getdate(*value, NULL);
|
|
|
|
} else if (4 < len && strcasecmp(*name, "Age") == 0) {
|
|
|
|
/* extract Age header */
|
|
|
|
if ('0' <= **value && **value <= '9')
|
|
|
|
object->cache.age = atoi(*value);
|
|
|
|
} else if (8 < len && strcasecmp(*name, "Expires") == 0) {
|
|
|
|
/* extract Expires header */
|
|
|
|
object->cache.expires = curl_getdate(*value, NULL);
|
|
|
|
} else if (14 < len && strcasecmp(*name, "Cache-Control") == 0) {
|
|
|
|
/* extract and parse Cache-Control header */
|
|
|
|
const char *start = *value;
|
|
|
|
const char *comma = *value;
|
|
|
|
|
|
|
|
while (*comma != '\0') {
|
|
|
|
while (*comma != '\0' && *comma != ',')
|
|
|
|
comma++;
|
|
|
|
|
|
|
|
if (8 < comma - start && (strncasecmp(start,
|
|
|
|
"no-cache", 8) == 0 ||
|
|
|
|
strncasecmp(start, "no-store", 8) == 0))
|
|
|
|
/* When we get a disk cache we should
|
|
|
|
* distinguish between these two */
|
2011-02-25 00:22:24 +03:00
|
|
|
object->cache.no_cache = LLCACHE_VALIDATE_ALWAYS;
|
2010-03-28 16:56:39 +04:00
|
|
|
else if (7 < comma - start &&
|
|
|
|
strncasecmp(start, "max-age", 7) == 0) {
|
|
|
|
/* Find '=' */
|
|
|
|
while (start < comma && *start != '=')
|
|
|
|
start++;
|
|
|
|
|
|
|
|
/* Skip over it */
|
|
|
|
start++;
|
|
|
|
|
|
|
|
/* Skip whitespace */
|
|
|
|
SKIP_ST(start);
|
|
|
|
|
|
|
|
if (start < comma)
|
|
|
|
object->cache.max_age = atoi(start);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*comma != '\0') {
|
|
|
|
/* Skip past comma */
|
|
|
|
comma++;
|
|
|
|
/* Skip whitespace */
|
|
|
|
SKIP_ST(comma);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set start for next token */
|
|
|
|
start = comma;
|
|
|
|
}
|
|
|
|
} else if (5 < len && strcasecmp(*name, "ETag") == 0) {
|
|
|
|
/* extract ETag header */
|
|
|
|
free(object->cache.etag);
|
|
|
|
object->cache.etag = strdup(*value);
|
|
|
|
if (object->cache.etag == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
} else if (14 < len && strcasecmp(*name, "Last-Modified") == 0) {
|
|
|
|
/* extract Last-Modified header */
|
|
|
|
object->cache.last_modified = curl_getdate(*value, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SKIP_ST
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a fetch header
|
|
|
|
*
|
|
|
|
* \param object Object being fetched
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param data Header string
|
|
|
|
* \param len Byte length of header
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
2011-11-09 01:51:42 +04:00
|
|
|
nserror llcache_fetch_process_header(llcache_object *object,
|
|
|
|
const uint8_t *data, size_t len)
|
2010-03-28 16:56:39 +04:00
|
|
|
{
|
|
|
|
nserror error;
|
|
|
|
char *name, *value;
|
|
|
|
llcache_header *temp;
|
|
|
|
|
2010-10-28 02:12:00 +04:00
|
|
|
/* 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)
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* that we might have computed, and start again.
|
|
|
|
*/
|
2011-02-24 13:14:50 +03:00
|
|
|
/** \todo Properly parse the response line */
|
2011-11-09 01:51:42 +04:00
|
|
|
if (strncmp((const char *) data, "HTTP/", SLEN("HTTP/")) == 0) {
|
2010-10-28 02:12:00 +04:00
|
|
|
time_t req_time = object->cache.req_time;
|
|
|
|
|
|
|
|
llcache_invalidate_cache_control_data(object);
|
|
|
|
|
|
|
|
/* Restore request time, so we compute object's age correctly */
|
|
|
|
object->cache.req_time = req_time;
|
|
|
|
|
|
|
|
llcache_destroy_headers(object);
|
|
|
|
}
|
|
|
|
|
2011-02-24 13:14:50 +03:00
|
|
|
error = llcache_fetch_parse_header(object, data, len, &name, &value);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
return error;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Append header data to the object's headers array */
|
|
|
|
temp = realloc(object->headers, (object->num_headers + 1) *
|
|
|
|
sizeof(llcache_header));
|
|
|
|
if (temp == NULL) {
|
|
|
|
free(name);
|
|
|
|
free(value);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
object->headers = temp;
|
|
|
|
|
|
|
|
object->headers[object->num_headers].name = name;
|
|
|
|
object->headers[object->num_headers].value = value;
|
|
|
|
|
|
|
|
object->num_headers++;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a chunk of fetched data
|
|
|
|
*
|
|
|
|
* \param object Object being fetched
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param data Data to process
|
|
|
|
* \param len Byte length of data
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise.
|
|
|
|
*/
|
|
|
|
nserror llcache_fetch_process_data(llcache_object *object, const uint8_t *data,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
object->source_data = temp;
|
|
|
|
object->source_alloc = new_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append this data chunk to source buffer */
|
|
|
|
memcpy(object->source_data + object->source_len, data, len);
|
|
|
|
object->source_len += len;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an authentication request
|
|
|
|
*
|
|
|
|
* \param object Object being fetched
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param realm Authentication realm
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise.
|
|
|
|
*/
|
|
|
|
nserror llcache_fetch_auth(llcache_object *object, const char *realm)
|
|
|
|
{
|
2010-04-17 00:00:10 +04:00
|
|
|
const char *auth;
|
2010-03-28 16:56:39 +04:00
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
|
|
|
|
/* Abort fetch for this object */
|
|
|
|
fetch_abort(object->fetch.fetch);
|
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
2010-04-17 00:08:27 +04:00
|
|
|
/* Invalidate cache-control data */
|
2010-09-16 23:47:15 +04:00
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-04-17 00:08:27 +04:00
|
|
|
|
2010-04-17 00:00:10 +04:00
|
|
|
/* Destroy headers */
|
2010-10-28 02:12:00 +04:00
|
|
|
llcache_destroy_headers(object);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-04-17 00:00:10 +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)
|
2011-09-28 15:26:30 +04:00
|
|
|
realm = nsurl_access(object->url);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-09-28 15:26:30 +04:00
|
|
|
auth = urldb_get_auth_details(nsurl_access(object->url), realm);
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2010-04-17 00:00:10 +04:00
|
|
|
if (auth == NULL || object->fetch.tried_with_auth == true) {
|
|
|
|
/* No authentication details, or tried what we had, so ask */
|
|
|
|
object->fetch.tried_with_auth = false;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
if (llcache->query_cb != NULL) {
|
2010-04-17 00:00:10 +04:00
|
|
|
llcache_query query;
|
2010-04-15 00:42:03 +04:00
|
|
|
|
2010-04-17 00:00:10 +04:00
|
|
|
/* Emit query for authentication details */
|
|
|
|
query.type = LLCACHE_QUERY_AUTH;
|
|
|
|
query.url = object->url;
|
|
|
|
query.data.auth.realm = realm;
|
|
|
|
|
2011-03-03 03:40:50 +03:00
|
|
|
object->fetch.outstanding_query = true;
|
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
error = llcache->query_cb(&query, llcache->query_cb_pw,
|
2010-04-17 00:00:10 +04:00
|
|
|
llcache_query_handle_response, object);
|
|
|
|
} else {
|
|
|
|
llcache_event event;
|
|
|
|
|
2010-04-17 00:08:27 +04:00
|
|
|
/* Mark object complete */
|
2010-04-17 00:00:10 +04:00
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
|
|
|
/* Inform client(s) that object fetch failed */
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
|
|
|
/** \todo More appropriate error message */
|
|
|
|
event.data.msg = messages_get("FetchFailed");
|
2010-04-14 14:56:44 +04:00
|
|
|
|
2010-04-17 00:00:10 +04:00
|
|
|
error = llcache_send_event_to_users(object, &event);
|
|
|
|
}
|
|
|
|
} 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-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a TLS certificate verification failure
|
|
|
|
*
|
|
|
|
* \param object Object being fetched
|
2010-03-28 20:00:54 +04:00
|
|
|
* \param certs Certificate chain
|
|
|
|
* \param num Number of certificates in chain
|
2010-03-28 16:56:39 +04:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
|
|
|
*/
|
|
|
|
nserror llcache_fetch_cert_error(llcache_object *object,
|
|
|
|
const struct ssl_cert_info *certs, size_t num)
|
|
|
|
{
|
|
|
|
nserror error = NSERROR_OK;
|
|
|
|
|
2010-04-17 03:15:22 +04:00
|
|
|
/* Fetch has been stopped, and destroyed. Invalidate object's pointer */
|
2010-03-28 16:56:39 +04:00
|
|
|
object->fetch.fetch = NULL;
|
|
|
|
|
2010-04-17 00:08:27 +04:00
|
|
|
/* Invalidate cache-control data */
|
2010-09-16 23:47:15 +04:00
|
|
|
llcache_invalidate_cache_control_data(object);
|
2010-04-17 00:08:27 +04:00
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
if (llcache->query_cb != NULL) {
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_query query;
|
|
|
|
|
|
|
|
/* Emit query for TLS */
|
|
|
|
query.type = LLCACHE_QUERY_SSL;
|
|
|
|
query.url = object->url;
|
|
|
|
query.data.ssl.certs = certs;
|
|
|
|
query.data.ssl.num = num;
|
|
|
|
|
2011-03-03 03:40:50 +03:00
|
|
|
object->fetch.outstanding_query = true;
|
|
|
|
|
2011-09-10 04:55:39 +04:00
|
|
|
error = llcache->query_cb(&query, llcache->query_cb_pw,
|
2010-03-28 16:56:39 +04:00
|
|
|
llcache_query_handle_response, object);
|
|
|
|
} else {
|
|
|
|
llcache_event event;
|
|
|
|
|
2010-04-17 00:08:27 +04:00
|
|
|
/* Mark object complete */
|
2010-04-15 00:42:03 +04:00
|
|
|
object->fetch.state = LLCACHE_FETCH_COMPLETE;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
/* Inform client(s) that object fetch failed */
|
|
|
|
event.type = LLCACHE_EVENT_ERROR;
|
|
|
|
/** \todo More appropriate error message */
|
|
|
|
event.data.msg = messages_get("FetchFailed");
|
2010-04-14 14:56:44 +04:00
|
|
|
|
|
|
|
error = llcache_send_event_to_users(object, &event);
|
2010-03-28 16:56:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|