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
|
2004-03-11 05:19:14 +03:00
|
|
|
* Copyright 2004 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>
|
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"
|
2005-02-03 16:18:22 +03:00
|
|
|
#include "netsurf/content/url_store.h"
|
2003-02-09 15:58:15 +03:00
|
|
|
#include "netsurf/utils/log.h"
|
2004-01-23 23:46:29 +03:00
|
|
|
#include "netsurf/utils/messages.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;
|
2004-06-22 21:37:51 +04:00
|
|
|
static void fetchcache_callback(fetch_msg msg, void *p, const char *data,
|
|
|
|
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);
|
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
|
|
|
|
* \param p1 user parameter for callback
|
|
|
|
* \param p2 user parameter for callback
|
|
|
|
* \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,
|
2003-06-17 23:24:21 +04:00
|
|
|
void (*callback)(content_msg msg, struct content *c, void *p1,
|
2004-04-25 03:42:32 +04:00
|
|
|
void *p2, union content_msg_data data),
|
2004-06-11 03:55:23 +04:00
|
|
|
void *p1, void *p2,
|
|
|
|
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;
|
|
|
|
char *hash;
|
2005-04-01 06:25:11 +04:00
|
|
|
|
2004-08-14 18:30:12 +04:00
|
|
|
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
|
|
|
|
2004-01-26 17:16:23 +03:00
|
|
|
LOG(("url %s", url1));
|
2003-02-09 15:58:15 +03:00
|
|
|
|
2005-01-03 19:09:11 +03:00
|
|
|
if (!post_urlenc && !post_multipart && !download) {
|
2004-08-14 18:30:12 +04:00
|
|
|
if ((c = content_get(url1)) != NULL) {
|
2004-01-26 17:16:23 +03:00
|
|
|
free(url1);
|
2005-01-02 06:58:21 +03:00
|
|
|
if (!content_add_user(c, callback, p1, p2))
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return c;
|
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;
|
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
|
|
|
|
2005-01-03 19:09:11 +03:00
|
|
|
if (!post_urlenc && !post_multipart && !download)
|
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
|
|
|
|
* \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,
|
|
|
|
void (*callback)(content_msg msg, struct content *c, void *p1,
|
|
|
|
void *p2, union content_msg_data data),
|
|
|
|
void *p1, void *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 */
|
|
|
|
|
|
|
|
} else if (content->status == CONTENT_STATUS_TYPE_UNKNOWN) {
|
|
|
|
/* brand new content: start fetch */
|
|
|
|
content->fetch = fetch_start(content->url, referer,
|
|
|
|
fetchcache_callback, content,
|
|
|
|
content->no_error_pages,
|
|
|
|
post_urlenc, post_multipart, cookies);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2004-06-22 21:37:51 +04:00
|
|
|
void fetchcache_callback(fetch_msg msg, void *p, const char *data,
|
|
|
|
unsigned long size)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
2005-02-03 16:18:22 +03:00
|
|
|
struct url_content *url_content;
|
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
|
|
|
LOG(("FETCH_TYPE, type %u", c->download ?
|
|
|
|
CONTENT_OTHER : type));
|
|
|
|
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:
|
|
|
|
LOG(("FETCH_DATA"));
|
2004-07-10 06:35:31 +04:00
|
|
|
/* if (c->total_size)
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c,
|
2004-02-27 20:45:19 +03:00
|
|
|
messages_get("RecPercent"),
|
2004-04-25 15:40:05 +04:00
|
|
|
human_friendly_bytesize(c->source_size + size),
|
|
|
|
human_friendly_bytesize(c->total_size),
|
2004-06-09 00:25:04 +04:00
|
|
|
(unsigned int) ((c->source_size + size) * 100 / c->total_size));
|
2003-08-29 00:04:35 +04:00
|
|
|
else
|
2004-06-05 19:03:59 +04:00
|
|
|
content_set_status(c,
|
2004-02-27 20:45:19 +03:00
|
|
|
messages_get("Received"),
|
2004-04-25 15:40:05 +04:00
|
|
|
human_friendly_bytesize(c->source_size + size));
|
2004-04-25 03:42:32 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_STATUS, msg_data);
|
2004-07-10 06:35:31 +04: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:
|
2005-02-03 16:18:22 +03:00
|
|
|
url_content = url_store_find(c->url);
|
|
|
|
if (url_content)
|
|
|
|
url_content->requests++;
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
LOG(("FETCH_FINISHED"));
|
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:
|
|
|
|
LOG(("FETCH_ERROR, '%s'", 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:
|
|
|
|
LOG(("FETCH_REDIRECT, '%s'", data));
|
|
|
|
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);
|
|
|
|
if (result == URL_FUNC_OK) {
|
2004-04-25 03:42:32 +04:00
|
|
|
msg_data.redirect = url;
|
|
|
|
content_broadcast(c, CONTENT_MSG_REDIRECT, msg_data);
|
|
|
|
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
|
|
|
}
|
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;
|
2003-06-26 15:41:26 +04:00
|
|
|
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 */
|
|
|
|
LOG(("FETCH_AUTH, '%s'", data));
|
|
|
|
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
|
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-01-02 01:27:05 +03:00
|
|
|
type = strdup(s);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|