2003-06-30 16:44:03 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
2005-08-21 16:04:18 +04:00
|
|
|
* Copyright 2005 James Bursa <bursa@users.sourceforge.net>
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
2003-10-01 00:33:45 +04:00
|
|
|
/** \file
|
|
|
|
* High-level fetching, caching and conversion (implementation).
|
|
|
|
*
|
|
|
|
* The implementation checks the cache for the requested URL. If it is not
|
|
|
|
* present, a content is created and a fetch is initiated. As the status of the
|
|
|
|
* fetch changes and data is received, the content is updated appropriately.
|
|
|
|
*/
|
|
|
|
|
2005-01-13 23:29:24 +03:00
|
|
|
#define _GNU_SOURCE /* for strndup */
|
2003-02-09 15:58:15 +03:00
|
|
|
#include <assert.h>
|
2003-02-28 14:49:13 +03:00
|
|
|
#include <string.h>
|
2003-12-27 23:15:23 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <regex.h>
|
2006-02-06 03:10:09 +03:00
|
|
|
#include <time.h>
|
2004-01-05 05:10:59 +03:00
|
|
|
#include "netsurf/utils/config.h"
|
2003-06-17 23:24:21 +04:00
|
|
|
#include "netsurf/content/content.h"
|
2003-02-09 15:58:15 +03:00
|
|
|
#include "netsurf/content/fetchcache.h"
|
|
|
|
#include "netsurf/content/fetch.h"
|
|
|
|
#include "netsurf/utils/log.h"
|
2004-01-23 23:46:29 +03:00
|
|
|
#include "netsurf/utils/messages.h"
|
2006-02-06 03:10:09 +03:00
|
|
|
#include "netsurf/utils/talloc.h"
|
2004-03-02 21:02:41 +03:00
|
|
|
#include "netsurf/utils/url.h"
|
2003-02-09 15:58:15 +03:00
|
|
|
#include "netsurf/utils/utils.h"
|
|
|
|
|
|
|
|
|
2004-01-23 23:46:29 +03:00
|
|
|
static char error_page[1000];
|
2003-12-27 23:15:23 +03:00
|
|
|
static regex_t re_content_type;
|
2006-02-23 18:06:54 +03:00
|
|
|
static void fetchcache_callback(fetch_msg msg, void *p, const void *data,
|
2004-06-22 21:37:51 +04:00
|
|
|
unsigned long size);
|
|
|
|
static char *fetchcache_parse_type(const char *s, char **params[]);
|
2004-01-23 23:46:29 +03:00
|
|
|
static void fetchcache_error_page(struct content *c, const char *error);
|
2006-02-06 03:10:09 +03:00
|
|
|
static void fetchcache_cache_update(struct content *c,
|
|
|
|
const struct cache_data *data);
|
2006-02-23 18:06:54 +03:00
|
|
|
static void fetchcache_notmodified(struct content *c, const void *data);
|
2003-02-09 15:58:15 +03:00
|
|
|
|
|
|
|
|
2003-10-01 00:33:45 +04:00
|
|
|
/**
|
2004-06-11 03:55:23 +04:00
|
|
|
* Retrieve a URL or prepare to fetch, convert, and cache it.
|
2003-10-01 00:33:45 +04:00
|
|
|
*
|
|
|
|
* The caller must supply a callback function which is called when anything
|
|
|
|
* interesting happens to the content which is returned. See content.h.
|
|
|
|
*
|
2004-06-11 03:55:23 +04:00
|
|
|
* \param url address to fetch
|
|
|
|
* \param callback function to call when anything interesting happens to
|
|
|
|
* the new content
|
2005-08-21 16:04:18 +04:00
|
|
|
* \param p1 user parameter for callback (may be a pointer or integer)
|
|
|
|
* \param p2 user parameter for callback (may be a pointer or integer)
|
2004-06-11 03:55:23 +04:00
|
|
|
* \param width available space
|
|
|
|
* \param height available space
|
|
|
|
* \param no_error_pages if an error occurs, send CONTENT_MSG_ERROR instead
|
|
|
|
* of generating an error page
|
|
|
|
* \param post_urlenc url encoded post data, or 0 if none
|
|
|
|
* \param post_multipart multipart post data, or 0 if none
|
|
|
|
* \param cookies send and accept cookies
|
2005-01-03 05:09:20 +03:00
|
|
|
* \param download download, rather than render content
|
2004-06-11 03:55:23 +04:00
|
|
|
* \return a new content, or 0 on memory exhaustion
|
2004-01-26 17:16:23 +03:00
|
|
|
*
|
2004-06-11 03:55:23 +04:00
|
|
|
* On success, call fetchcache_go() to start work on the new content.
|
2003-10-01 00:33:45 +04:00
|
|
|
*/
|
|
|
|
|
2004-06-11 03:55:23 +04:00
|
|
|
struct content * fetchcache(const char *url,
|
2005-08-21 16:04:18 +04:00
|
|
|
void (*callback)(content_msg msg, struct content *c,
|
|
|
|
intptr_t p1, intptr_t p2, union content_msg_data data),
|
|
|
|
intptr_t p1, intptr_t p2,
|
2004-06-11 03:55:23 +04:00
|
|
|
int width, int height,
|
|
|
|
bool no_error_pages,
|
|
|
|
char *post_urlenc,
|
|
|
|
struct form_successful_control *post_multipart,
|
2005-01-03 05:09:20 +03:00
|
|
|
bool cookies,
|
|
|
|
bool download)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
|
|
|
struct content *c;
|
2004-06-11 00:41:26 +04:00
|
|
|
char *url1;
|
2006-02-06 03:10:09 +03:00
|
|
|
char *hash, *query;
|
|
|
|
char *etag = 0;
|
|
|
|
time_t date = 0;
|
2005-04-01 06:25:11 +04:00
|
|
|
|
2006-04-12 12:09:27 +04:00
|
|
|
if (strncasecmp(url, "file:///", 8) &&
|
|
|
|
strncasecmp(url, "file:/", 6) == 0) {
|
|
|
|
/* Manipulate file URLs into correct format */
|
|
|
|
if (strncasecmp(url, "file://", 7) == 0) {
|
2006-07-02 02:27:10 +04:00
|
|
|
/* file://path */
|
2006-04-12 12:09:27 +04:00
|
|
|
url1 = malloc(7 + strlen(url));
|
|
|
|
if (!url1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
strcpy(url1, "file://");
|
2006-07-02 02:27:10 +04:00
|
|
|
strcat(url1 + 7, url + 6);
|
2006-04-12 12:09:27 +04:00
|
|
|
} else {
|
|
|
|
/* file:/... */
|
|
|
|
url1 = malloc(7 + strlen(url));
|
|
|
|
if (!url1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
strcpy(url1, "file://");
|
|
|
|
strcat(url1 + 7, url + 5);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* simply duplicate the URL */
|
|
|
|
if ((url1 = strdup(url)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-06-11 00:41:26 +04:00
|
|
|
|
2003-07-01 02:21:33 +04:00
|
|
|
/* strip fragment identifier */
|
2004-08-14 18:30:12 +04:00
|
|
|
if ((hash = strchr(url1, '#')) != NULL)
|
2005-01-13 23:29:24 +03:00
|
|
|
*hash = 0;
|
2003-06-17 23:24:21 +04:00
|
|
|
|
2006-02-06 03:10:09 +03:00
|
|
|
/* look for query; we don't cache URLs with a query segment */
|
|
|
|
query = strchr(url1, '?');
|
|
|
|
|
2004-01-26 17:16:23 +03:00
|
|
|
LOG(("url %s", url1));
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2006-02-06 03:10:09 +03:00
|
|
|
if (!post_urlenc && !post_multipart && !download && !query) {
|
2004-08-14 18:30:12 +04:00
|
|
|
if ((c = content_get(url1)) != NULL) {
|
2006-02-06 03:10:09 +03:00
|
|
|
struct cache_data *cd = c->cache_data;
|
|
|
|
int current_age, freshness_lifetime;
|
|
|
|
|
|
|
|
/* Calculate staleness of cached content 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 +
|
|
|
|
time(0) - cd->res_time;
|
|
|
|
freshness_lifetime =
|
|
|
|
(cd->max_age != INVALID_AGE) ? cd->max_age :
|
|
|
|
(cd->expires != 0) ? cd->expires - cd->date :
|
2006-02-08 03:35:05 +03:00
|
|
|
(cd->last_modified != 0) ?
|
|
|
|
(time(0) - cd->last_modified) / 10 :
|
|
|
|
0;
|
2006-02-06 03:10:09 +03:00
|
|
|
|
|
|
|
if (freshness_lifetime > current_age ||
|
|
|
|
cd->date == 0) {
|
|
|
|
/* Ok, either a fresh content or we're
|
|
|
|
* currently fetching the selected content
|
|
|
|
* (therefore it must be fresh) */
|
|
|
|
free(url1);
|
|
|
|
if (!content_add_user(c, callback, p1, p2))
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ok. We have a cache entry, but it appears stale.
|
|
|
|
* Therefore, validate it. */
|
2006-02-08 03:35:05 +03:00
|
|
|
if (cd->last_modified)
|
|
|
|
date = cd->last_modified;
|
|
|
|
else
|
|
|
|
date = c->cache_data->date;
|
2006-02-06 03:10:09 +03:00
|
|
|
etag = c->cache_data->etag;
|
2003-10-25 20:22:11 +04:00
|
|
|
}
|
2003-04-18 01:35:02 +04:00
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2004-01-26 17:16:23 +03:00
|
|
|
c = content_create(url1);
|
2004-07-30 20:14:44 +04:00
|
|
|
free(url1);
|
|
|
|
if (!c)
|
2004-08-14 18:30:12 +04:00
|
|
|
return NULL;
|
2006-02-06 03:10:09 +03:00
|
|
|
|
|
|
|
/* Fill in cache validation fields (if present) */
|
|
|
|
if (date)
|
|
|
|
c->cache_data->date = date;
|
|
|
|
if (etag) {
|
|
|
|
c->cache_data->etag = talloc_strdup(c, etag);
|
|
|
|
if (!c->cache_data->etag)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-01-02 06:58:21 +03:00
|
|
|
if (!content_add_user(c, callback, p1, p2)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-01-20 22:08:34 +03:00
|
|
|
|
2006-02-06 03:10:09 +03:00
|
|
|
if (!post_urlenc && !post_multipart && !download && !query)
|
2004-06-21 19:09:59 +04:00
|
|
|
c->fresh = true;
|
2004-01-23 23:46:29 +03:00
|
|
|
|
2003-06-17 23:24:21 +04:00
|
|
|
c->width = width;
|
|
|
|
c->height = height;
|
2004-01-26 17:16:23 +03:00
|
|
|
c->no_error_pages = no_error_pages;
|
2005-01-03 05:09:20 +03:00
|
|
|
c->download = download;
|
2004-06-11 03:55:23 +04:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start fetching and converting a content.
|
|
|
|
*
|
2004-06-21 19:09:59 +04:00
|
|
|
* \param content content to fetch, as returned by fetchcache()
|
2004-06-11 03:55:23 +04:00
|
|
|
* \param referer referring URL, or 0
|
|
|
|
* \param callback function to call when anything interesting happens to
|
|
|
|
* the new content
|
2005-08-21 16:04:18 +04:00
|
|
|
* \param p1 user parameter for callback
|
|
|
|
* \param p2 user parameter for callback
|
2005-01-25 02:02:37 +03:00
|
|
|
* \param width available space
|
|
|
|
* \param height available space
|
2004-06-11 03:55:23 +04:00
|
|
|
* \param post_urlenc url encoded post data, or 0 if none
|
|
|
|
* \param post_multipart multipart post data, or 0 if none
|
|
|
|
* \param cookies send and accept cookies
|
|
|
|
*
|
|
|
|
* Errors will be sent back through the callback.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void fetchcache_go(struct content *content, char *referer,
|
2005-08-21 16:04:18 +04:00
|
|
|
void (*callback)(content_msg msg, struct content *c,
|
|
|
|
intptr_t p1, intptr_t p2, union content_msg_data data),
|
|
|
|
intptr_t p1, intptr_t p2,
|
2005-01-25 02:02:37 +03:00
|
|
|
int width, int height,
|
2004-06-11 03:55:23 +04:00
|
|
|
char *post_urlenc,
|
|
|
|
struct form_successful_control *post_multipart,
|
|
|
|
bool cookies)
|
|
|
|
{
|
|
|
|
char error_message[500];
|
|
|
|
union content_msg_data msg_data;
|
|
|
|
|
|
|
|
LOG(("url %s, status %s", content->url,
|
|
|
|
content_status_name[content->status]));
|
|
|
|
|
|
|
|
if (content->status == CONTENT_STATUS_TYPE_UNKNOWN && content->fetch) {
|
|
|
|
/* fetching, but not yet received any response:
|
|
|
|
* no action required */
|
|
|
|
|
2006-02-06 03:10:09 +03:00
|
|
|
} else if (content->status == CONTENT_STATUS_TYPE_UNKNOWN) {
|
|
|
|
/* brand new content: start fetch */
|
|
|
|
char **headers;
|
|
|
|
int i = 0;
|
|
|
|
char *etag = content->cache_data->etag;
|
|
|
|
time_t date = content->cache_data->date;
|
|
|
|
content->cache_data->etag = 0;
|
|
|
|
content->cache_data->date = 0;
|
|
|
|
headers = malloc(3 * sizeof(char *));
|
|
|
|
if (!headers) {
|
|
|
|
talloc_free(etag);
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
callback(CONTENT_MSG_ERROR, content, p1, p2,
|
|
|
|
msg_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (etag) {
|
|
|
|
headers[i] = malloc(15 + strlen(etag) + 1);
|
|
|
|
if (!headers[i]) {
|
|
|
|
free(headers);
|
|
|
|
talloc_free(etag);
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
callback(CONTENT_MSG_ERROR, content, p1, p2,
|
|
|
|
msg_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sprintf(headers[i++], "If-None-Match: %s", etag);
|
|
|
|
talloc_free(etag);
|
|
|
|
}
|
|
|
|
if (date) {
|
|
|
|
headers[i] = malloc(19 + 29 + 1);
|
|
|
|
if (!headers[i]) {
|
|
|
|
while (--i >= 0) {
|
|
|
|
free(headers[i]);
|
|
|
|
}
|
|
|
|
free(headers);
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
callback(CONTENT_MSG_ERROR, content, p1, p2,
|
|
|
|
msg_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sprintf(headers[i++], "If-Modified-Since: %s",
|
|
|
|
rfc1123_date(date));
|
|
|
|
}
|
|
|
|
headers[i] = 0;
|
2004-06-11 03:55:23 +04:00
|
|
|
content->fetch = fetch_start(content->url, referer,
|
|
|
|
fetchcache_callback, content,
|
|
|
|
content->no_error_pages,
|
2006-02-06 03:10:09 +03:00
|
|
|
post_urlenc, post_multipart, cookies,
|
|
|
|
headers);
|
|
|
|
for (i = 0; headers[i]; i++)
|
|
|
|
free(headers[i]);
|
|
|
|
free(headers);
|
2004-06-11 03:55:23 +04:00
|
|
|
if (!content->fetch) {
|
|
|
|
LOG(("warning: fetch_start failed"));
|
|
|
|
snprintf(error_message, sizeof error_message,
|
|
|
|
messages_get("InvalidURL"),
|
|
|
|
content->url);
|
|
|
|
if (content->no_error_pages) {
|
|
|
|
content->status = CONTENT_STATUS_ERROR;
|
|
|
|
msg_data.error = error_message;
|
|
|
|
content_broadcast(content, CONTENT_MSG_ERROR,
|
|
|
|
msg_data);
|
|
|
|
} else {
|
|
|
|
fetchcache_error_page(content, error_message);
|
|
|
|
}
|
2004-01-26 17:16:23 +03:00
|
|
|
}
|
2004-06-11 03:55:23 +04:00
|
|
|
|
|
|
|
/* in these remaining cases, we have to 'catch up' with the content's
|
|
|
|
* status, ie. send the same messages as if the content was
|
|
|
|
* gradually getting to the current status from TYPE_UNKNOWN */
|
|
|
|
} else if (content->status == CONTENT_STATUS_LOADING) {
|
|
|
|
callback(CONTENT_MSG_LOADING, content, p1, p2, msg_data);
|
|
|
|
|
|
|
|
} else if (content->status == CONTENT_STATUS_READY) {
|
|
|
|
callback(CONTENT_MSG_LOADING, content, p1, p2, msg_data);
|
2004-06-23 19:41:50 +04:00
|
|
|
if (content_find_user(content, callback, p1, p2))
|
|
|
|
callback(CONTENT_MSG_READY, content, p1, p2, msg_data);
|
2004-06-11 03:55:23 +04:00
|
|
|
|
|
|
|
} else if (content->status == CONTENT_STATUS_DONE) {
|
|
|
|
callback(CONTENT_MSG_LOADING, content, p1, p2, msg_data);
|
2005-01-25 02:02:37 +03:00
|
|
|
if (content->available_width != width)
|
|
|
|
content_reformat(content, width, height);
|
2004-06-23 19:41:50 +04:00
|
|
|
if (content_find_user(content, callback, p1, p2))
|
|
|
|
callback(CONTENT_MSG_READY, content, p1, p2, msg_data);
|
|
|
|
if (content_find_user(content, callback, p1, p2))
|
|
|
|
callback(CONTENT_MSG_DONE, content, p1, p2, msg_data);
|
2004-06-11 03:55:23 +04:00
|
|
|
|
|
|
|
} else if (content->status == CONTENT_STATUS_ERROR) {
|
|
|
|
/* shouldn't usually occur */
|
|
|
|
msg_data.error = messages_get("MiscError");
|
|
|
|
callback(CONTENT_MSG_ERROR, content, p1, p2, msg_data);
|
|
|
|
|
2003-07-16 21:38:46 +04:00
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-01 00:33:45 +04:00
|
|
|
/**
|
|
|
|
* Callback function for fetch.
|
|
|
|
*
|
|
|
|
* This is called when the status of a fetch changes.
|
|
|
|
*/
|
|
|
|
|
2006-02-23 18:06:54 +03:00
|
|
|
void fetchcache_callback(fetch_msg msg, void *p, const void *data,
|
2004-06-22 21:37:51 +04:00
|
|
|
unsigned long size)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2004-06-11 00:41:26 +04:00
|
|
|
bool res;
|
2003-06-17 23:24:21 +04:00
|
|
|
struct content *c = p;
|
2003-02-09 15:58:15 +03:00
|
|
|
content_type type;
|
2003-12-27 23:15:23 +03:00
|
|
|
char *mime_type, *url;
|
|
|
|
char **params;
|
|
|
|
unsigned int i;
|
2004-04-25 03:42:32 +04:00
|
|
|
union content_msg_data msg_data;
|
2004-08-09 20:11:58 +04:00
|
|
|
url_func_result result;
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
switch (msg) {
|
|
|
|
case FETCH_TYPE:
|
2003-08-29 00:04:35 +04:00
|
|
|
c->total_size = size;
|
2003-12-27 23:15:23 +03:00
|
|
|
mime_type = fetchcache_parse_type(data, ¶ms);
|
2005-01-02 01:27:05 +03:00
|
|
|
if (!mime_type) {
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR,
|
|
|
|
msg_data);
|
|
|
|
fetch_abort(c->fetch);
|
|
|
|
c->fetch = 0;
|
|
|
|
return;
|
|
|
|
}
|
2003-02-26 00:00:27 +03:00
|
|
|
type = content_lookup(mime_type);
|
2005-01-03 05:09:20 +03:00
|
|
|
res = content_set_type(c,
|
|
|
|
c->download ? CONTENT_OTHER : type,
|
|
|
|
mime_type, params);
|
2003-07-08 02:10:51 +04:00
|
|
|
free(mime_type);
|
2003-12-27 23:15:23 +03:00
|
|
|
for (i = 0; params[i]; i++)
|
|
|
|
free(params[i]);
|
|
|
|
free(params);
|
2004-06-21 19:09:59 +04:00
|
|
|
if (!res) {
|
2004-06-11 00:41:26 +04:00
|
|
|
fetch_abort(c->fetch);
|
2004-06-21 19:09:59 +04:00
|
|
|
c->fetch = 0;
|
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2004-07-10 06:35:31 +04:00
|
|
|
case FETCH_PROGRESS:
|
|
|
|
if (size)
|
|
|
|
content_set_status(c,
|
|
|
|
messages_get("RecPercent"),
|
|
|
|
data, (unsigned int)size);
|
|
|
|
else
|
|
|
|
content_set_status(c,
|
|
|
|
messages_get("Received"),
|
|
|
|
data);
|
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, msg_data);
|
|
|
|
break;
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
case FETCH_DATA:
|
2006-02-06 03:10:09 +03:00
|
|
|
if (!content_process_data(c, data, size)) {
|
2004-06-11 00:41:26 +04:00
|
|
|
fetch_abort(c->fetch);
|
2004-06-21 19:09:59 +04:00
|
|
|
c->fetch = 0;
|
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
case FETCH_FINISHED:
|
2006-02-06 03:10:09 +03:00
|
|
|
fetchcache_cache_update(c,
|
|
|
|
(const struct cache_data *)data);
|
2003-06-17 23:24:21 +04:00
|
|
|
c->fetch = 0;
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c, messages_get("Converting"),
|
|
|
|
c->source_size);
|
2004-04-25 03:42:32 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, msg_data);
|
2003-06-17 23:24:21 +04:00
|
|
|
content_convert(c, c->width, c->height);
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
case FETCH_ERROR:
|
2006-02-23 18:06:54 +03:00
|
|
|
LOG(("FETCH_ERROR, '%s'", (const char *)data));
|
2003-06-17 23:24:21 +04:00
|
|
|
c->fetch = 0;
|
2004-01-26 17:16:23 +03:00
|
|
|
if (c->no_error_pages) {
|
2004-06-11 00:41:26 +04:00
|
|
|
c->status = CONTENT_STATUS_ERROR;
|
2004-04-25 03:42:32 +04:00
|
|
|
msg_data.error = data;
|
2004-06-11 00:41:26 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR,
|
|
|
|
msg_data);
|
2004-01-26 17:16:23 +03:00
|
|
|
} else {
|
|
|
|
content_reset(c);
|
|
|
|
fetchcache_error_page(c, data);
|
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
break;
|
2003-04-18 01:35:02 +04:00
|
|
|
|
2003-06-26 15:41:26 +04:00
|
|
|
case FETCH_REDIRECT:
|
|
|
|
c->fetch = 0;
|
2003-07-02 00:36:28 +04:00
|
|
|
/* redirect URLs must be absolute by HTTP/1.1, but many sites send
|
|
|
|
* relative ones: treat them as relative to requested URL */
|
2004-08-09 20:11:58 +04:00
|
|
|
result = url_join(data, c->url, &url);
|
2005-04-19 01:40:29 +04:00
|
|
|
/* set the status to ERROR so that the content is
|
|
|
|
* destroyed in content_clean() */
|
|
|
|
c->status = CONTENT_STATUS_ERROR;
|
2004-08-09 20:11:58 +04:00
|
|
|
if (result == URL_FUNC_OK) {
|
2006-07-03 02:34:04 +04:00
|
|
|
bool same;
|
|
|
|
|
|
|
|
result = url_compare(c->url, url, &same);
|
|
|
|
|
2006-01-04 02:26:58 +03:00
|
|
|
/* check that we're not attempting to
|
|
|
|
* redirect to the same URL */
|
2006-07-03 02:34:04 +04:00
|
|
|
if (result != URL_FUNC_OK || same) {
|
2006-01-04 02:26:58 +03:00
|
|
|
msg_data.error =
|
|
|
|
messages_get("BadRedirect");
|
|
|
|
content_broadcast(c,
|
|
|
|
CONTENT_MSG_ERROR, msg_data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg_data.redirect = url;
|
|
|
|
content_broadcast(c,
|
|
|
|
CONTENT_MSG_REDIRECT,
|
|
|
|
msg_data);
|
|
|
|
}
|
2004-04-25 03:42:32 +04:00
|
|
|
free(url);
|
2003-12-26 03:17:55 +03:00
|
|
|
} else {
|
2004-04-25 03:42:32 +04:00
|
|
|
msg_data.error = messages_get("BadRedirect");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
2003-12-26 03:17:55 +03:00
|
|
|
}
|
2003-06-26 15:41:26 +04:00
|
|
|
break;
|
2006-02-06 03:10:09 +03:00
|
|
|
|
|
|
|
case FETCH_NOTMODIFIED:
|
|
|
|
fetchcache_notmodified(c, data);
|
|
|
|
break;
|
|
|
|
|
2004-01-05 05:10:59 +03:00
|
|
|
#ifdef WITH_AUTH
|
2003-10-25 23:20:13 +04:00
|
|
|
case FETCH_AUTH:
|
2004-06-09 00:25:04 +04:00
|
|
|
/* data -> string containing the Realm */
|
2006-02-23 18:06:54 +03:00
|
|
|
LOG(("FETCH_AUTH, '%s'", (const char *)data));
|
2004-06-09 00:25:04 +04:00
|
|
|
c->fetch = 0;
|
|
|
|
msg_data.auth_realm = data;
|
|
|
|
content_broadcast(c, CONTENT_MSG_AUTH, msg_data);
|
2004-06-23 15:40:29 +04:00
|
|
|
/* set the status to ERROR so that the content is
|
|
|
|
* destroyed in content_clean() */
|
|
|
|
c->status = CONTENT_STATUS_ERROR;
|
2004-06-09 00:25:04 +04:00
|
|
|
break;
|
2004-01-05 05:10:59 +03:00
|
|
|
#endif
|
2006-02-23 18:06:54 +03:00
|
|
|
|
|
|
|
#ifdef WITH_SSL
|
|
|
|
case FETCH_CERT_ERR:
|
|
|
|
c->fetch = 0;
|
|
|
|
/* set the status to ERROR so that the content is
|
|
|
|
* destroyed in content_clean() */
|
|
|
|
c->status = CONTENT_STATUS_ERROR;
|
|
|
|
|
|
|
|
msg_data.ssl.certs = data;
|
|
|
|
msg_data.ssl.num = size;
|
|
|
|
content_broadcast(c, CONTENT_MSG_SSL, msg_data);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-27 23:15:23 +03:00
|
|
|
/**
|
|
|
|
* Initialise the fetchcache module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void fetchcache_init(void)
|
|
|
|
{
|
|
|
|
regcomp_wrapper(&re_content_type,
|
2005-04-01 06:25:11 +04:00
|
|
|
"^([-0-9a-zA-Z_.]+/[-0-9a-zA-Z_.+]+)[ \t]*"
|
2003-12-27 23:15:23 +03:00
|
|
|
"(;[ \t]*([-0-9a-zA-Z_.]+)="
|
|
|
|
"([-0-9a-zA-Z_.]+|\"([^\"]|[\\].)*\")[ \t]*)*$",
|
|
|
|
REG_EXTENDED);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a Content-Type header.
|
|
|
|
*
|
2005-01-02 01:27:05 +03:00
|
|
|
* \param s a Content-Type header
|
|
|
|
* \param params updated to point to an array of strings, ordered attribute,
|
|
|
|
* value, attribute, ..., 0
|
|
|
|
* \return a new string containing the MIME-type, or 0 on memory exhaustion
|
2003-12-27 23:15:23 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_ATTRS 10
|
|
|
|
|
2004-06-22 21:37:51 +04:00
|
|
|
char *fetchcache_parse_type(const char *s, char **params[])
|
2003-12-27 23:15:23 +03:00
|
|
|
{
|
2005-01-02 01:27:05 +03:00
|
|
|
char *type = 0;
|
2003-12-27 23:15:23 +03:00
|
|
|
unsigned int i;
|
|
|
|
int r;
|
|
|
|
regmatch_t pmatch[2 + MAX_ATTRS * 3];
|
2005-01-02 01:27:05 +03:00
|
|
|
|
|
|
|
*params = malloc((MAX_ATTRS * 2 + 2) * sizeof (*params)[0]);
|
|
|
|
if (!*params)
|
|
|
|
goto no_memory;
|
|
|
|
for (i = 0; i != MAX_ATTRS * 2 + 2; i++)
|
|
|
|
(*params)[i] = 0;
|
2003-12-27 23:15:23 +03:00
|
|
|
|
|
|
|
r = regexec(&re_content_type, s, 2 + MAX_ATTRS * 3, pmatch, 0);
|
|
|
|
if (r) {
|
|
|
|
LOG(("failed to parse content-type '%s'", s));
|
2005-05-02 02:20:40 +04:00
|
|
|
/* The mime type must be first, so only copy up to the
|
|
|
|
* first semicolon in the string. This allows us to have
|
|
|
|
* a better attempt at handling pages sent with broken
|
|
|
|
* Content-Type headers. Obviously, any truly broken
|
|
|
|
* Content-Type headers will be unaffected by this heuristic
|
|
|
|
*/
|
|
|
|
char *semi = strchr(s, ';');
|
|
|
|
if (semi)
|
|
|
|
type = strndup(s, semi - s);
|
|
|
|
else
|
|
|
|
type = strdup(s);
|
2005-01-02 01:27:05 +03:00
|
|
|
if (!type)
|
|
|
|
goto no_memory;
|
|
|
|
return type;
|
2003-12-27 23:15:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type = strndup(s + pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
|
2005-01-02 01:27:05 +03:00
|
|
|
if (!type) {
|
|
|
|
free(*params);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-12-27 23:15:23 +03:00
|
|
|
|
|
|
|
/* parameters */
|
|
|
|
for (i = 0; i != MAX_ATTRS && pmatch[2 + 3 * i].rm_so != -1; i++) {
|
|
|
|
(*params)[2 * i] = strndup(s + pmatch[2 + 3 * i + 1].rm_so,
|
2005-01-02 01:27:05 +03:00
|
|
|
pmatch[2 + 3 * i + 1].rm_eo -
|
|
|
|
pmatch[2 + 3 * i + 1].rm_so);
|
2003-12-27 23:15:23 +03:00
|
|
|
(*params)[2 * i + 1] = strndup(s + pmatch[2 + 3 * i + 2].rm_so,
|
2005-01-02 01:27:05 +03:00
|
|
|
pmatch[2 + 3 * i + 2].rm_eo -
|
|
|
|
pmatch[2 + 3 * i + 2].rm_so);
|
|
|
|
if (!(*params)[2 * i] || !(*params)[2 * i + 1])
|
|
|
|
goto no_memory;
|
2003-12-27 23:15:23 +03:00
|
|
|
}
|
|
|
|
(*params)[2 * i] = 0;
|
|
|
|
|
|
|
|
return type;
|
2005-01-02 01:27:05 +03:00
|
|
|
|
|
|
|
no_memory:
|
|
|
|
for (i = 0; i != MAX_ATTRS * 2 + 2; i++)
|
|
|
|
free((*params)[i]);
|
|
|
|
free(*params);
|
|
|
|
free(type);
|
|
|
|
|
|
|
|
return 0;
|
2003-12-27 23:15:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-23 23:46:29 +03:00
|
|
|
/**
|
|
|
|
* Generate an error page.
|
|
|
|
*
|
|
|
|
* \param c empty content to generate the page in
|
|
|
|
* \param error message to display
|
|
|
|
*/
|
|
|
|
|
|
|
|
void fetchcache_error_page(struct content *c, const char *error)
|
|
|
|
{
|
|
|
|
const char *params[] = { 0 };
|
2004-06-09 00:25:04 +04:00
|
|
|
int length;
|
|
|
|
|
|
|
|
if ((length = snprintf(error_page, sizeof(error_page),
|
|
|
|
messages_get("ErrorPage"), error)) < 0)
|
|
|
|
length = 0;
|
2004-06-11 03:55:23 +04:00
|
|
|
if (!content_set_type(c, CONTENT_HTML, "text/html", params))
|
|
|
|
return;
|
|
|
|
if (!content_process_data(c, error_page, length))
|
|
|
|
return;
|
2004-01-23 23:46:29 +03:00
|
|
|
content_convert(c, c->width, c->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-06 03:10:09 +03:00
|
|
|
/**
|
|
|
|
* Update a content's cache info
|
|
|
|
*
|
|
|
|
* \param The content
|
|
|
|
* \param Cache data
|
|
|
|
*/
|
|
|
|
|
|
|
|
void fetchcache_cache_update(struct content *c,
|
|
|
|
const struct cache_data *data)
|
|
|
|
{
|
|
|
|
assert(c && data);
|
|
|
|
|
|
|
|
c->cache_data->req_time = data->req_time;
|
|
|
|
c->cache_data->res_time = data->res_time;
|
|
|
|
|
|
|
|
if (data->date != 0)
|
|
|
|
c->cache_data->date = data->date;
|
|
|
|
else
|
|
|
|
c->cache_data->date = time(0);
|
|
|
|
|
|
|
|
if (data->expires != 0)
|
|
|
|
c->cache_data->expires = data->expires;
|
|
|
|
|
|
|
|
if (data->age != INVALID_AGE)
|
|
|
|
c->cache_data->age = data->age;
|
|
|
|
|
|
|
|
if (data->max_age != INVALID_AGE)
|
|
|
|
c->cache_data->max_age = data->max_age;
|
|
|
|
|
|
|
|
if (data->no_cache)
|
|
|
|
c->fresh = false;
|
|
|
|
|
|
|
|
if (data->etag) {
|
|
|
|
talloc_free(c->cache_data->etag);
|
|
|
|
c->cache_data->etag = talloc_strdup(c, data->etag);
|
|
|
|
}
|
2006-02-08 03:35:05 +03:00
|
|
|
|
|
|
|
if (data->last_modified)
|
|
|
|
c->cache_data->last_modified = data->last_modified;
|
2006-02-06 03:10:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not modified callback handler
|
|
|
|
*/
|
|
|
|
|
2006-02-23 18:06:54 +03:00
|
|
|
void fetchcache_notmodified(struct content *c, const void *data)
|
2006-02-06 03:10:09 +03:00
|
|
|
{
|
|
|
|
struct content *fb;
|
|
|
|
union content_msg_data msg_data;
|
|
|
|
|
|
|
|
assert(c && data);
|
|
|
|
assert(c->status == CONTENT_STATUS_TYPE_UNKNOWN);
|
|
|
|
|
|
|
|
/* Look for cached content */
|
|
|
|
fb = content_get_ready(c->url);
|
|
|
|
|
|
|
|
if (fb) {
|
|
|
|
/* Found it */
|
|
|
|
intptr_t p1, p2;
|
|
|
|
void (*callback)(content_msg msg,
|
|
|
|
struct content *c, intptr_t p1,
|
|
|
|
intptr_t p2,
|
|
|
|
union content_msg_data data);
|
|
|
|
|
|
|
|
/* Now notify all users that we're changing content */
|
|
|
|
while (c->user_list->next) {
|
|
|
|
p1 = c->user_list->next->p1;
|
|
|
|
p2 = c->user_list->next->p2;
|
|
|
|
callback = c->user_list->next->callback;
|
|
|
|
|
|
|
|
if (!content_add_user(fb, callback, p1, p2)) {
|
|
|
|
c->type = CONTENT_UNKNOWN;
|
|
|
|
c->status = CONTENT_STATUS_ERROR;
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR,
|
|
|
|
msg_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
content_remove_user(c, callback, p1, p2);
|
|
|
|
callback(CONTENT_MSG_NEWPTR, fb, p1, p2, msg_data);
|
|
|
|
|
|
|
|
/* and catch user up with fallback's state */
|
|
|
|
if (fb->status == CONTENT_STATUS_LOADING) {
|
|
|
|
callback(CONTENT_MSG_LOADING,
|
|
|
|
fb, p1, p2, msg_data);
|
|
|
|
} else if (fb->status == CONTENT_STATUS_READY) {
|
|
|
|
callback(CONTENT_MSG_LOADING,
|
|
|
|
fb, p1, p2, msg_data);
|
|
|
|
if (content_find_user(fb, callback, p1, p2))
|
|
|
|
callback(CONTENT_MSG_READY,
|
|
|
|
fb, p1, p2, msg_data);
|
|
|
|
} else if (fb->status == CONTENT_STATUS_DONE) {
|
|
|
|
callback(CONTENT_MSG_LOADING,
|
|
|
|
fb, p1, p2, msg_data);
|
|
|
|
if (content_find_user(fb, callback, p1, p2))
|
|
|
|
callback(CONTENT_MSG_READY,
|
|
|
|
fb, p1, p2, msg_data);
|
|
|
|
if (content_find_user(fb, callback, p1, p2))
|
|
|
|
callback(CONTENT_MSG_DONE,
|
|
|
|
fb, p1, p2, msg_data);
|
|
|
|
} else if (fb->status == CONTENT_STATUS_ERROR) {
|
|
|
|
/* shouldn't usually occur */
|
|
|
|
msg_data.error = messages_get("MiscError");
|
|
|
|
callback(CONTENT_MSG_ERROR, fb, p1, p2,
|
|
|
|
msg_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mark content invalid */
|
|
|
|
c->fetch = 0;
|
|
|
|
c->status = CONTENT_STATUS_ERROR;
|
|
|
|
|
|
|
|
/* and update fallback's cache control data */
|
|
|
|
fetchcache_cache_update(fb,
|
|
|
|
(const struct cache_data *)data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* No cached content, so unconditionally refetch */
|
|
|
|
struct content_user *u;
|
|
|
|
|
|
|
|
fetch_abort(c->fetch);
|
|
|
|
c->fetch = 0;
|
|
|
|
|
|
|
|
c->cache_data->date = 0;
|
|
|
|
talloc_free(c->cache_data->etag);
|
|
|
|
c->cache_data->etag = 0;
|
|
|
|
|
|
|
|
for (u = c->user_list->next; u; u = u->next) {
|
|
|
|
fetchcache_go(c, 0, u->callback, u->p1, u->p2,
|
|
|
|
c->width, c->height, 0, 0, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
#ifdef TEST
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
void callback(fetchcache_msg msg, struct content *c, void *p, char *error)
|
|
|
|
{
|
|
|
|
switch (msg) {
|
|
|
|
case FETCHCACHE_OK:
|
|
|
|
LOG(("FETCHCACHE_OK, url '%s'", p));
|
|
|
|
break;
|
|
|
|
case FETCHCACHE_BADTYPE:
|
|
|
|
LOG(("FETCHCACHE_BADTYPE, url '%s'", p));
|
|
|
|
break;
|
|
|
|
case FETCHCACHE_ERROR:
|
|
|
|
LOG(("FETCHCACHE_ERROR, url '%s', error '%s'", p, error));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *test[] = {"http://www.google.co.uk/", "http://www.ox.ac.uk/", "blah://blah/"};
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
cache_init();
|
|
|
|
fetch_init();
|
|
|
|
|
|
|
|
for (i = 0; i != sizeof(test) / sizeof(test[0]); i++)
|
|
|
|
fetchcache(test[i], 0, callback, test[i], 800, 0);
|
|
|
|
for (i = 0; i != 5; i++) {
|
|
|
|
fetch_poll();
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
for (i = 0; i != sizeof(test) / sizeof(test[0]); i++)
|
|
|
|
fetchcache(test[i], 0, callback, test[i], 800, 0);
|
|
|
|
for (i = 0; i != 20; i++) {
|
|
|
|
fetch_poll();
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|